added getoptions to blob request signer

This commit is contained in:
Adrian Cole 2011-04-01 21:38:53 -07:00
parent c50fddfc7a
commit 82d84b1ca1
8 changed files with 99 additions and 22 deletions

View File

@ -32,6 +32,7 @@ import org.jclouds.atmos.blobstore.functions.BlobToObject;
import org.jclouds.atmos.domain.AtmosObject; import org.jclouds.atmos.domain.AtmosObject;
import org.jclouds.blobstore.BlobRequestSigner; import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.functions.BlobToHttpGetOptions;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.options.GetOptions; import org.jclouds.http.options.GetOptions;
import org.jclouds.rest.internal.RestAnnotationProcessor; import org.jclouds.rest.internal.RestAnnotationProcessor;
@ -44,19 +45,21 @@ import org.jclouds.rest.internal.RestAnnotationProcessor;
public class AtmosBlobRequestSigner implements BlobRequestSigner { public class AtmosBlobRequestSigner implements BlobRequestSigner {
private final RestAnnotationProcessor<AtmosAsyncClient> processor; private final RestAnnotationProcessor<AtmosAsyncClient> processor;
private final BlobToObject blobToObject; private final BlobToObject blobToObject;
private final BlobToHttpGetOptions blob2ObjectGetOptions;
private final Method getMethod; private final Method getMethod;
private final Method deleteMethod; private final Method deleteMethod;
private final Method createMethod; private final Method createMethod;
@Inject @Inject
public AtmosBlobRequestSigner(RestAnnotationProcessor<AtmosAsyncClient> processor, BlobToObject blobToObject) public AtmosBlobRequestSigner(RestAnnotationProcessor<AtmosAsyncClient> processor, BlobToObject blobToObject,
throws SecurityException, NoSuchMethodException { BlobToHttpGetOptions blob2ObjectGetOptions) throws SecurityException, NoSuchMethodException {
this.processor = checkNotNull(processor, "processor"); this.processor = checkNotNull(processor, "processor");
this.blobToObject = checkNotNull(blobToObject, "blobToObject"); this.blobToObject = checkNotNull(blobToObject, "blobToObject");
this.blob2ObjectGetOptions = checkNotNull(blob2ObjectGetOptions, "blob2ObjectGetOptions");
this.getMethod = AtmosAsyncClient.class.getMethod("readFile", String.class, GetOptions[].class); this.getMethod = AtmosAsyncClient.class.getMethod("readFile", String.class, GetOptions[].class);
this.deleteMethod = AtmosAsyncClient.class.getMethod("deletePath", String.class); this.deleteMethod = AtmosAsyncClient.class.getMethod("deletePath", String.class);
this.createMethod = AtmosAsyncClient.class.getMethod("createFile", String.class, AtmosObject.class); this.createMethod = AtmosAsyncClient.class.getMethod("createFile", String.class, AtmosObject.class);
} }
@Override @Override
@ -78,4 +81,10 @@ public class AtmosBlobRequestSigner implements BlobRequestSigner {
return checkNotNull(container, "container") + "/" + checkNotNull(name, "name"); return checkNotNull(container, "container") + "/" + checkNotNull(name, "name");
} }
@Override
public HttpRequest signGetBlob(String container, String name, org.jclouds.blobstore.options.GetOptions options) {
return cleanRequest(processor.createRequest(getMethod, getPath(container, name), blob2ObjectGetOptions
.apply(options)));
}
} }

View File

@ -27,15 +27,16 @@ import java.lang.reflect.Method;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.functions.BlobToHttpGetOptions;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.options.GetOptions;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.s3.S3AsyncClient; import org.jclouds.s3.S3AsyncClient;
import org.jclouds.s3.blobstore.functions.BlobToObject; import org.jclouds.s3.blobstore.functions.BlobToObject;
import org.jclouds.s3.domain.S3Object; import org.jclouds.s3.domain.S3Object;
import org.jclouds.s3.options.PutObjectOptions; import org.jclouds.s3.options.PutObjectOptions;
import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.options.GetOptions;
import org.jclouds.rest.internal.RestAnnotationProcessor;
/** /**
* *
@ -45,15 +46,18 @@ import org.jclouds.rest.internal.RestAnnotationProcessor;
public class S3BlobRequestSigner implements BlobRequestSigner { public class S3BlobRequestSigner implements BlobRequestSigner {
private final RestAnnotationProcessor<S3AsyncClient> processor; private final RestAnnotationProcessor<S3AsyncClient> processor;
private final BlobToObject blobToObject; private final BlobToObject blobToObject;
private final BlobToHttpGetOptions blob2HttpGetOptions;
private final Method getMethod; private final Method getMethod;
private final Method deleteMethod; private final Method deleteMethod;
private final Method createMethod; private final Method createMethod;
@Inject @Inject
public S3BlobRequestSigner(RestAnnotationProcessor<S3AsyncClient> processor, BlobToObject blobToObject) public S3BlobRequestSigner(RestAnnotationProcessor<S3AsyncClient> processor, BlobToObject blobToObject,
throws SecurityException, NoSuchMethodException { BlobToHttpGetOptions blob2HttpGetOptions) throws SecurityException, NoSuchMethodException {
this.processor = checkNotNull(processor, "processor"); this.processor = checkNotNull(processor, "processor");
this.blobToObject = checkNotNull(blobToObject, "blobToObject"); this.blobToObject = checkNotNull(blobToObject, "blobToObject");
this.blob2HttpGetOptions = checkNotNull(blob2HttpGetOptions, "blob2HttpGetOptions");
this.getMethod = S3AsyncClient.class.getMethod("getObject", String.class, String.class, GetOptions[].class); this.getMethod = S3AsyncClient.class.getMethod("getObject", String.class, String.class, GetOptions[].class);
this.deleteMethod = S3AsyncClient.class.getMethod("deleteObject", String.class, String.class); this.deleteMethod = S3AsyncClient.class.getMethod("deleteObject", String.class, String.class);
this.createMethod = S3AsyncClient.class.getMethod("putObject", String.class, S3Object.class, this.createMethod = S3AsyncClient.class.getMethod("putObject", String.class, S3Object.class,
@ -76,4 +80,8 @@ public class S3BlobRequestSigner implements BlobRequestSigner {
return cleanRequest(processor.createRequest(deleteMethod, container, name)); return cleanRequest(processor.createRequest(deleteMethod, container, name));
} }
@Override
public HttpRequest signGetBlob(String container, String name, org.jclouds.blobstore.options.GetOptions options) {
return cleanRequest(processor.createRequest(getMethod, container, name, blob2HttpGetOptions.apply(options)));
}
} }

View File

@ -29,6 +29,7 @@ import javax.inject.Singleton;
import org.jclouds.blobstore.BlobRequestSigner; import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.functions.BlobToHttpGetOptions;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.options.GetOptions; import org.jclouds.http.options.GetOptions;
import org.jclouds.openstack.swift.CommonSwiftAsyncClient; import org.jclouds.openstack.swift.CommonSwiftAsyncClient;
@ -44,16 +45,20 @@ import org.jclouds.rest.internal.RestAnnotationProcessor;
public class SwiftBlobRequestSigner implements BlobRequestSigner { public class SwiftBlobRequestSigner implements BlobRequestSigner {
private final RestAnnotationProcessor<CommonSwiftAsyncClient> processor; private final RestAnnotationProcessor<CommonSwiftAsyncClient> processor;
private final BlobToObject blobToObject; private final BlobToObject blobToObject;
private final BlobToHttpGetOptions blob2HttpGetOptions;
private final Method getMethod; private final Method getMethod;
private final Method deleteMethod; private final Method deleteMethod;
private final Method createMethod; private final Method createMethod;
@Inject @Inject
public SwiftBlobRequestSigner(RestAnnotationProcessor<CommonSwiftAsyncClient> processor, BlobToObject blobToObject) public SwiftBlobRequestSigner(RestAnnotationProcessor<CommonSwiftAsyncClient> processor, BlobToObject blobToObject,
throws SecurityException, NoSuchMethodException { BlobToHttpGetOptions blob2HttpGetOptions) throws SecurityException, NoSuchMethodException {
this.processor = checkNotNull(processor, "processor"); this.processor = checkNotNull(processor, "processor");
this.blobToObject = checkNotNull(blobToObject, "blobToObject"); this.blobToObject = checkNotNull(blobToObject, "blobToObject");
this.getMethod = CommonSwiftAsyncClient.class.getMethod("getObject", String.class, String.class, GetOptions[].class); this.blob2HttpGetOptions = checkNotNull(blob2HttpGetOptions, "blob2HttpGetOptions");
this.getMethod = CommonSwiftAsyncClient.class.getMethod("getObject", String.class, String.class,
GetOptions[].class);
this.deleteMethod = CommonSwiftAsyncClient.class.getMethod("removeObject", String.class, String.class); this.deleteMethod = CommonSwiftAsyncClient.class.getMethod("removeObject", String.class, String.class);
this.createMethod = CommonSwiftAsyncClient.class.getMethod("putObject", String.class, SwiftObject.class); this.createMethod = CommonSwiftAsyncClient.class.getMethod("putObject", String.class, SwiftObject.class);
@ -74,4 +79,8 @@ public class SwiftBlobRequestSigner implements BlobRequestSigner {
return cleanRequest(processor.createRequest(deleteMethod, container, name)); return cleanRequest(processor.createRequest(deleteMethod, container, name));
} }
@Override
public HttpRequest signGetBlob(String container, String name, org.jclouds.blobstore.options.GetOptions options) {
return cleanRequest(processor.createRequest(getMethod, container, name, blob2HttpGetOptions.apply(options)));
}
} }

View File

@ -22,6 +22,7 @@ package org.jclouds.blobstore;
import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.BlobBuilder; import org.jclouds.blobstore.domain.BlobBuilder;
import org.jclouds.blobstore.internal.RequestSigningUnsupported; import org.jclouds.blobstore.internal.RequestSigningUnsupported;
import org.jclouds.blobstore.options.GetOptions;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import com.google.inject.ImplementedBy; import com.google.inject.ImplementedBy;
@ -47,6 +48,12 @@ public interface BlobRequestSigner {
*/ */
HttpRequest signGetBlob(String container, String name); HttpRequest signGetBlob(String container, String name);
/**
* @param options
* @see #signGetBlob(String, String)
*/
HttpRequest signGetBlob(String container, String name, GetOptions options);
/** /**
* gets a signed request, including headers as necessary, to delete a blob from an external * gets a signed request, including headers as necessary, to delete a blob from an external
* client. * client.

View File

@ -27,6 +27,8 @@ import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.functions.BlobToHttpGetOptions;
import org.jclouds.blobstore.options.GetOptions;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpUtils; import org.jclouds.http.HttpUtils;
import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.http.filters.BasicAuthentication;
@ -39,10 +41,12 @@ import org.jclouds.http.filters.BasicAuthentication;
public class TransientBlobRequestSigner implements BlobRequestSigner { public class TransientBlobRequestSigner implements BlobRequestSigner {
private final BasicAuthentication basicAuth; private final BasicAuthentication basicAuth;
private final BlobToHttpGetOptions blob2HttpGetOptions;
@Inject @Inject
public TransientBlobRequestSigner(BasicAuthentication basicAuth) { public TransientBlobRequestSigner(BasicAuthentication basicAuth, BlobToHttpGetOptions blob2HttpGetOptions) {
this.basicAuth = checkNotNull(basicAuth, "basicAuth"); this.basicAuth = checkNotNull(basicAuth, "basicAuth");
this.blob2HttpGetOptions = checkNotNull(blob2HttpGetOptions, "blob2HttpGetOptions");
} }
@Override @Override
@ -53,17 +57,25 @@ public class TransientBlobRequestSigner implements BlobRequestSigner {
@Override @Override
public HttpRequest signPutBlob(String container, Blob blob) { public HttpRequest signPutBlob(String container, Blob blob) {
HttpRequest request = HttpRequest.builder().method("PUT") HttpRequest request = HttpRequest.builder().method("PUT").endpoint(
.endpoint(URI.create(String.format("http://localhost/%s/%s", container, blob.getMetadata().getName()))) URI.create(String.format("http://localhost/%s/%s", container, blob.getMetadata().getName()))).payload(
.payload(blob.getPayload()) blob.getPayload()).headers(
.headers(HttpUtils.getContentHeadersFromMetadata(blob.getMetadata().getContentMetadata())).build(); HttpUtils.getContentHeadersFromMetadata(blob.getMetadata().getContentMetadata())).build();
return basicAuth.filter(request); return basicAuth.filter(request);
} }
@Override @Override
public HttpRequest signRemoveBlob(String container, String name) { public HttpRequest signRemoveBlob(String container, String name) {
HttpRequest request = new HttpRequest("DELETE", URI.create(String.format("http://localhost/%s/%s", container, HttpRequest request = new HttpRequest("DELETE", URI.create(String.format("http://localhost/%s/%s", container,
name))); name)));
return basicAuth.filter(request);
}
@Override
public HttpRequest signGetBlob(String container, String name, GetOptions options) {
HttpRequest request = HttpRequest.builder().method("GET").endpoint(
URI.create(String.format("http://localhost/%s/%s", container, name))).headers(
blob2HttpGetOptions.apply(options).buildRequestHeaders()).build();
return basicAuth.filter(request); return basicAuth.filter(request);
} }

View File

@ -23,6 +23,7 @@ import javax.inject.Singleton;
import org.jclouds.blobstore.BlobRequestSigner; import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.options.GetOptions;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
/** /**
@ -47,4 +48,9 @@ public class RequestSigningUnsupported implements BlobRequestSigner {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public HttpRequest signGetBlob(String container, String name, GetOptions options) {
throw new UnsupportedOperationException();
}
} }

View File

@ -19,6 +19,7 @@
package org.jclouds.blobstore.integration.internal; package org.jclouds.blobstore.integration.internal;
import static org.jclouds.blobstore.options.GetOptions.Builder.range;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob;
@ -73,6 +74,24 @@ public class BaseBlobSignerLiveTest extends BaseBlobStoreIntegrationTest {
} }
} }
@Test
public void testSignGetUrlOptions() throws Exception {
String name = "hello";
String text = "fooooooooooooooooooooooo";
Blob blob = context.getBlobStore().blobBuilder(name).payload(text).contentType("text/plain").build();
String container = getContainerName();
try {
context.getBlobStore().putBlob(container, blob);
assertConsistencyAwareContainerSize(container, 1);
HttpRequest request = context.getSigner().signGetBlob(container, name, range(0, 1));
assertEquals(request.getFilters().size(), 0);
assertEquals(Strings2.toStringAndClose(context.utils().http().invoke(request).getPayload().getInput()), "fo");
} finally {
returnContainer(container);
}
}
@Test @Test
public void testSignPutUrl() throws Exception { public void testSignPutUrl() throws Exception {
String name = "hello"; String name = "hello";

View File

@ -32,6 +32,7 @@ import org.jclouds.azureblob.blobstore.functions.BlobToAzureBlob;
import org.jclouds.azureblob.domain.AzureBlob; import org.jclouds.azureblob.domain.AzureBlob;
import org.jclouds.blobstore.BlobRequestSigner; import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.functions.BlobToHttpGetOptions;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.options.GetOptions; import org.jclouds.http.options.GetOptions;
import org.jclouds.rest.internal.RestAnnotationProcessor; import org.jclouds.rest.internal.RestAnnotationProcessor;
@ -44,19 +45,21 @@ import org.jclouds.rest.internal.RestAnnotationProcessor;
public class AzureBlobRequestSigner implements BlobRequestSigner { public class AzureBlobRequestSigner implements BlobRequestSigner {
private final RestAnnotationProcessor<AzureBlobAsyncClient> processor; private final RestAnnotationProcessor<AzureBlobAsyncClient> processor;
private final BlobToAzureBlob blobToBlob; private final BlobToAzureBlob blobToBlob;
private final BlobToHttpGetOptions blob2HttpGetOptions;
private final Method getMethod; private final Method getMethod;
private final Method deleteMethod; private final Method deleteMethod;
private final Method createMethod; private final Method createMethod;
@Inject @Inject
public AzureBlobRequestSigner(RestAnnotationProcessor<AzureBlobAsyncClient> processor, BlobToAzureBlob blobToBlob) public AzureBlobRequestSigner(RestAnnotationProcessor<AzureBlobAsyncClient> processor, BlobToAzureBlob blobToBlob,
throws SecurityException, NoSuchMethodException { BlobToHttpGetOptions blob2HttpGetOptions) throws SecurityException, NoSuchMethodException {
this.processor = checkNotNull(processor, "processor"); this.processor = checkNotNull(processor, "processor");
this.blobToBlob = checkNotNull(blobToBlob, "blobToBlob"); this.blobToBlob = checkNotNull(blobToBlob, "blobToBlob");
this.blob2HttpGetOptions = checkNotNull(blob2HttpGetOptions, "blob2HttpGetOptions");
this.getMethod = AzureBlobAsyncClient.class.getMethod("getBlob", String.class, String.class, GetOptions[].class); this.getMethod = AzureBlobAsyncClient.class.getMethod("getBlob", String.class, String.class, GetOptions[].class);
this.deleteMethod = AzureBlobAsyncClient.class.getMethod("deleteBlob", String.class, String.class); this.deleteMethod = AzureBlobAsyncClient.class.getMethod("deleteBlob", String.class, String.class);
this.createMethod = AzureBlobAsyncClient.class.getMethod("putBlob", String.class, AzureBlob.class); this.createMethod = AzureBlobAsyncClient.class.getMethod("putBlob", String.class, AzureBlob.class);
} }
@Override @Override
@ -74,4 +77,8 @@ public class AzureBlobRequestSigner implements BlobRequestSigner {
return cleanRequest(processor.createRequest(deleteMethod, container, name)); return cleanRequest(processor.createRequest(deleteMethod, container, name));
} }
@Override
public HttpRequest signGetBlob(String container, String name, org.jclouds.blobstore.options.GetOptions options) {
return cleanRequest(processor.createRequest(getMethod, container, name, blob2HttpGetOptions.apply(options)));
}
} }