From 498c1dca209f37c70e8b73b2e2656a44b8c5780f Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Sun, 7 Jun 2009 21:39:23 +0000 Subject: [PATCH] Issue 58 reverted as path style bucket access is deprecated git-svn-id: http://jclouds.googlecode.com/svn/trunk@1087 3d8758e0-26b5-11de-8745-db77d3ebf521 --- .../jclouds/aws/s3/commands/BucketExists.java | 67 ++++++++------- .../jclouds/aws/s3/commands/CopyObject.java | 42 +++++----- .../jclouds/aws/s3/commands/DeleteBucket.java | 81 +++++++++--------- .../jclouds/aws/s3/commands/DeleteObject.java | 16 ++-- .../aws/s3/commands/GetAccessControlList.java | 25 ++++-- .../jclouds/aws/s3/commands/GetObject.java | 9 +- .../jclouds/aws/s3/commands/HeadObject.java | 82 ++++++++++--------- .../jclouds/aws/s3/commands/ListBucket.java | 9 +- .../aws/s3/commands/ListOwnedBuckets.java | 16 ++-- .../jclouds/aws/s3/commands/PutBucket.java | 39 ++++----- .../jclouds/aws/s3/commands/PutObject.java | 72 ++++++++-------- .../aws/s3/commands/S3CommandFactory.java | 14 ++-- .../aws/s3/commands/S3FutureCommand.java | 14 +++- .../s3/filters/RequestAuthorizeSignature.java | 13 ++- ...seObjectFromHeadersAndHttpContentTest.java | 12 +-- 15 files changed, 280 insertions(+), 231 deletions(-) diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/BucketExists.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/BucketExists.java index a760f3b41a..1f29258bb8 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/BucketExists.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/BucketExists.java @@ -35,6 +35,7 @@ import org.jclouds.http.commands.callables.ReturnTrueIf2xx; import com.google.common.annotations.VisibleForTesting; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; +import com.google.inject.name.Named; /** * Issues a HEAD command to determine if the bucket exists or not. @@ -44,38 +45,42 @@ import com.google.inject.assistedinject.Assisted; */ public class BucketExists extends S3FutureCommand { - @Inject - public BucketExists(ReturnTrueIf2xx callable, @Assisted String s3Bucket) { - super("HEAD", "/" + maxResults(0).buildQueryString(), callable, s3Bucket); - } + @Inject + public BucketExists(@Named("jclouds.http.address") String amazonHost, + ReturnTrueIf2xx callable, @Assisted String s3Bucket) { + super("HEAD", "/" + maxResults(0).buildQueryString(), callable, + amazonHost, s3Bucket); + } - @Override - public Boolean get() throws InterruptedException, ExecutionException { - try { - return super.get(); - } catch (ExecutionException e) { - return attemptNotFound(e); - } - } + @Override + public Boolean get() throws InterruptedException, ExecutionException { + try { + return super.get(); + } catch (ExecutionException e) { + return attemptNotFound(e); + } + } - @VisibleForTesting - Boolean attemptNotFound(ExecutionException e) throws ExecutionException { - if (e.getCause() != null && e.getCause() instanceof HttpResponseException) { - HttpResponseException responseException = (HttpResponseException) e.getCause(); - if (responseException.getResponse().getStatusCode() == 404) { - return false; - } - } - throw e; - } + @VisibleForTesting + Boolean attemptNotFound(ExecutionException e) throws ExecutionException { + if (e.getCause() != null + && e.getCause() instanceof HttpResponseException) { + HttpResponseException responseException = (HttpResponseException) e + .getCause(); + if (responseException.getResponse().getStatusCode() == 404) { + return false; + } + } + throw e; + } - @Override - public Boolean get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, - TimeoutException { - try { - return super.get(l, timeUnit); - } catch (ExecutionException e) { - return attemptNotFound(e); - } - } + @Override + public Boolean get(long l, TimeUnit timeUnit) throws InterruptedException, + ExecutionException, TimeoutException { + try { + return super.get(l, timeUnit); + } catch (ExecutionException e) { + return attemptNotFound(e); + } + } } \ No newline at end of file diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/CopyObject.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/CopyObject.java index 48e1fcd693..d3356d6bd2 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/CopyObject.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/CopyObject.java @@ -33,19 +33,20 @@ import org.jclouds.util.Utils; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; +import com.google.inject.name.Named; /** - * The copy operation creates a copy of an object that is already storedin Amazon S3. + * The copy operation creates a copy of an object that is already storedin + * Amazon S3. *

* When copying an object, you can preserve all metadata (default) or - * {@link CopyObjectOptions#overrideMetadataWith(com.google.common.collect.Multimap) specify new - * metadata}. However, the ACL is not preserved and is set to private for the user making the - * request. To override the default ACL setting, - * {@link CopyObjectOptions#overrideAcl(org.jclouds.aws.s3.domain.acl.CannedAccessPolicy) specify a - * new ACL} when generating a copy request. + * {@link CopyObjectOptions#overrideMetadataWith(com.google.common.collect.Multimap) + * specify new metadata}. However, the ACL is not preserved and is set to + * private for the user making the request. To override the default ACL setting, + * {@link CopyObjectOptions#overrideAcl(org.jclouds.aws.s3.domain.acl.CannedAccessPolicy) + * specify a new ACL} when generating a copy request. * - * @see * @see CopyObjectOptions * @see org.jclouds.aws.s3.domain.acl.CannedAccessPolicy @@ -55,20 +56,23 @@ import com.google.inject.assistedinject.Assisted; public class CopyObject extends S3FutureCommand { @Inject - public CopyObject(ParseSax callable, - @Assisted("sourceBucket") String sourceBucket, - @Assisted("sourceObject") String sourceObject, - @Assisted("destinationBucket") String destinationBucket, - @Assisted("destinationObject") String destinationObject, - @Assisted CopyObjectOptions options) { - super("PUT", "/" + checkNotNull(destinationObject, "destinationObject"), callable, - destinationBucket); + public CopyObject(@Named("jclouds.http.address") String amazonHost, + ParseSax callable, @Assisted("sourceBucket") String sourceBucket, + @Assisted("sourceObject") String sourceObject, + @Assisted("destinationBucket") String destinationBucket, + @Assisted("destinationObject") String destinationObject, + @Assisted CopyObjectOptions options) + { + super("PUT", + "/" + checkNotNull(destinationObject, "destinationObject"), + callable, amazonHost, destinationBucket); CopyObjectHandler handler = (CopyObjectHandler) callable.getHandler(); handler.setKey(destinationObject); getRequest().getHeaders().put( - "x-amz-copy-source", - String.format("/%1$s/%2$s", checkNotNull(sourceBucket, "sourceBucket"), Utils - .encodeUriPath(checkNotNull(sourceObject, "sourceObject")))); + "x-amz-copy-source", + String.format("/%1$s/%2$s", + checkNotNull(sourceBucket, "sourceBucket"), + Utils.encodeUriPath(checkNotNull(sourceObject, "sourceObject")))); getRequest().getHeaders().putAll(options.buildRequestHeaders()); } } \ No newline at end of file diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/DeleteBucket.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/DeleteBucket.java index a8f241ec25..e067b6163c 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/DeleteBucket.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/DeleteBucket.java @@ -34,54 +34,59 @@ import org.jclouds.http.commands.callables.ReturnTrueIf2xx; import com.google.common.annotations.VisibleForTesting; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; +import com.google.inject.name.Named; /** - * The DELETE request operation deletes the bucket named in the URI. All objects in the bucket must - * be deleted before the bucket itself can be deleted. + * The DELETE request operation deletes the bucket named in the URI. All objects + * in the bucket must be deleted before the bucket itself can be deleted. *

- * Only the owner of a bucket can delete it, regardless of the bucket's access control policy. + * Only the owner of a bucket can delete it, regardless of the bucket's access + * control policy. * - * @see * @author Adrian Cole */ public class DeleteBucket extends S3FutureCommand { - @Inject - public DeleteBucket(ReturnTrueIf2xx callable, @Assisted String s3Bucket) { - super("DELETE", "/", callable, s3Bucket); - } + @Inject + public DeleteBucket(@Named("jclouds.http.address") String amazonHost, + ReturnTrueIf2xx callable, @Assisted String s3Bucket) { + super("DELETE", "/", callable, amazonHost, s3Bucket); + } - @Override - public Boolean get() throws InterruptedException, ExecutionException { - try { - return super.get(); - } catch (ExecutionException e) { - return attemptNotFound(e); - } - } + @Override + public Boolean get() throws InterruptedException, ExecutionException { + try { + return super.get(); + } catch (ExecutionException e) { + return attemptNotFound(e); + } + } - @VisibleForTesting - Boolean attemptNotFound(ExecutionException e) throws ExecutionException { - if (e.getCause() != null && e.getCause() instanceof HttpResponseException) { - AWSResponseException responseException = (AWSResponseException) e.getCause(); - if (responseException.getResponse().getStatusCode() == 404) { - return true; - } else if ("BucketNotEmpty".equals(responseException.getError().getCode())) { - return false; - } - } - throw e; - } + @VisibleForTesting + Boolean attemptNotFound(ExecutionException e) throws ExecutionException { + if (e.getCause() != null + && e.getCause() instanceof HttpResponseException) { + AWSResponseException responseException = (AWSResponseException) e + .getCause(); + if (responseException.getResponse().getStatusCode() == 404) { + return true; + } else if ("BucketNotEmpty".equals(responseException.getError() + .getCode())) { + return false; + } + } + throw e; + } - @Override - public Boolean get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, - TimeoutException { - try { - return super.get(l, timeUnit); - } catch (ExecutionException e) { - return attemptNotFound(e); - } - } + @Override + public Boolean get(long l, TimeUnit timeUnit) throws InterruptedException, + ExecutionException, TimeoutException { + try { + return super.get(l, timeUnit); + } catch (ExecutionException e) { + return attemptNotFound(e); + } + } } \ No newline at end of file diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/DeleteObject.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/DeleteObject.java index 6526440333..163f3b0c7a 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/DeleteObject.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/DeleteObject.java @@ -29,10 +29,11 @@ import org.jclouds.http.commands.callables.ReturnTrueIf2xx; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; +import com.google.inject.name.Named; /** - * The DELETE request operation removes the specified object from Amazon S3. Once deleted, there is - * no method to restore or undelete an object. + * The DELETE request operation removes the specified object from Amazon S3. + * Once deleted, there is no method to restore or undelete an object. * * @see @@ -40,9 +41,10 @@ import com.google.inject.assistedinject.Assisted; */ public class DeleteObject extends S3FutureCommand { - @Inject - public DeleteObject(ReturnTrueIf2xx callable, @Assisted("bucketName") String bucket, - @Assisted("key") String key) { - super("DELETE", "/" + checkNotNull(key), callable, bucket); - } + @Inject + public DeleteObject(@Named("jclouds.http.address") String amazonHost, + ReturnTrueIf2xx callable, @Assisted("bucketName") String bucket, + @Assisted("key") String key) { + super("DELETE", "/" + checkNotNull(key), callable, amazonHost, bucket); + } } \ No newline at end of file diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/GetAccessControlList.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/GetAccessControlList.java index 4d67cdce77..d127d5dd2d 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/GetAccessControlList.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/GetAccessControlList.java @@ -35,6 +35,7 @@ import org.jclouds.http.commands.callables.xml.ParseSax; import com.google.common.annotations.VisibleForTesting; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; +import com.google.inject.name.Named; /** * A GET request operation directed at an object or bucket URI with the "acl" parameter retrieves @@ -48,15 +49,20 @@ import com.google.inject.assistedinject.Assisted; public class GetAccessControlList extends S3FutureCommand { @Inject - public GetAccessControlList(ParseSax accessControlListParser, - @Assisted("bucketName") String bucket) { - super("GET", "/?acl", accessControlListParser, bucket); + public GetAccessControlList(@Named("jclouds.http.address") String amazonHost, + ParseSax accessControlListParser, + @Assisted("bucketName") String bucket) + { + super("GET", "/?acl", accessControlListParser, amazonHost, bucket); } @Inject - public GetAccessControlList(ParseSax accessControlListParser, - @Assisted("bucketName") String bucket, @Assisted("objectKey") String objectKey) { - super("GET", "/" + objectKey + "?acl", accessControlListParser, bucket); + public GetAccessControlList(@Named("jclouds.http.address") String amazonHost, + ParseSax accessControlListParser, + @Assisted("bucketName") String bucket, + @Assisted("objectKey") String objectKey) + { + super("GET", "/" + objectKey + "?acl", accessControlListParser, amazonHost, bucket); } @Override @@ -73,7 +79,8 @@ public class GetAccessControlList extends S3FutureCommand { if (e.getCause() != null && e.getCause() instanceof HttpResponseException) { AWSResponseException responseException = (AWSResponseException) e.getCause(); if ("NoSuchBucket".equals(responseException.getError().getCode()) - || "NoSuchObject".equals(responseException.getError().getCode())) { + || "NoSuchObject".equals(responseException.getError().getCode())) + { return AccessControlList.NOT_FOUND; } } @@ -81,8 +88,8 @@ public class GetAccessControlList extends S3FutureCommand { } @Override - public AccessControlList get(long l, TimeUnit timeUnit) throws InterruptedException, - ExecutionException, TimeoutException { + public AccessControlList get(long l, TimeUnit timeUnit) throws InterruptedException, ExecutionException, + TimeoutException { try { return super.get(l, timeUnit); } catch (ExecutionException e) { diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/GetObject.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/GetObject.java index f8bb04069d..e325cc38ba 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/GetObject.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/GetObject.java @@ -37,6 +37,7 @@ import org.jclouds.aws.s3.domain.S3Object; import com.google.common.annotations.VisibleForTesting; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; +import com.google.inject.name.Named; /** * Retrieves the S3Object associated with the Key or {@link S3Object#NOT_FOUND} if not available; @@ -64,10 +65,10 @@ import com.google.inject.assistedinject.Assisted; public class GetObject extends S3FutureCommand { @Inject - public GetObject(ParseObjectFromHeadersAndHttpContent callable, - @Assisted("bucketName") String s3Bucket, @Assisted("key") String key, - @Assisted GetObjectOptions options) { - super("GET", "/" + checkNotNull(key), callable, s3Bucket); + public GetObject(@Named("jclouds.http.address") String amazonHost, + ParseObjectFromHeadersAndHttpContent callable, @Assisted("bucketName") String s3Bucket, + @Assisted("key") String key, @Assisted GetObjectOptions options) { + super("GET", "/" + checkNotNull(key), callable, amazonHost, s3Bucket); this.getRequest().getHeaders().putAll(options.buildRequestHeaders()); callable.setKey(key); } diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/HeadObject.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/HeadObject.java index 589ca00cdb..680a6c8b1a 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/HeadObject.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/HeadObject.java @@ -36,59 +36,65 @@ import org.jclouds.http.HttpResponseException; import com.google.common.annotations.VisibleForTesting; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; +import com.google.inject.name.Named; /** * Retrieves the metadata associated with the Key or * {@link org.jclouds.aws.s3.domain.S3Object.Metadata#NOT_FOUND} if not available. * *

- * The HEAD operation is used to retrieve information about a specific object or object size, - * without actually fetching the object itself. This is useful if you're only interested in the - * object metadata, and don't want to waste bandwidth on the object data. + * The HEAD operation is used to retrieve information about a specific object or + * object size, without actually fetching the object itself. This is useful if + * you're only interested in the object metadata, and don't want to waste + * bandwidth on the object data. * * @see GetObject - * @see * @author Adrian Cole * */ public class HeadObject extends S3FutureCommand { - @Inject - public HeadObject(ParseMetadataFromHeaders callable, @Assisted("bucketName") String bucket, - @Assisted("key") String key) { - super("HEAD", "/" + checkNotNull(key), callable, bucket); - callable.setKey(key); - } + @Inject + public HeadObject(@Named("jclouds.http.address") String amazonHost, + ParseMetadataFromHeaders callable, + @Assisted("bucketName") String bucket, @Assisted("key") String key) { + super("HEAD", "/" + checkNotNull(key), callable, amazonHost, bucket); + callable.setKey(key); + } - @Override - public S3Object.Metadata get() throws InterruptedException, ExecutionException { - try { - return super.get(); - } catch (ExecutionException e) { - return attemptNotFound(e); - } - } + @Override + public S3Object.Metadata get() throws InterruptedException, + ExecutionException { + try { + return super.get(); + } catch (ExecutionException e) { + return attemptNotFound(e); + } + } - @VisibleForTesting - S3Object.Metadata attemptNotFound(ExecutionException e) throws ExecutionException { - if (e.getCause() != null && e.getCause() instanceof HttpResponseException) { - HttpResponseException responseException = (HttpResponseException) e.getCause(); - if (responseException.getResponse().getStatusCode() == 404) { - return S3Object.Metadata.NOT_FOUND; - } - } - throw e; - } + @VisibleForTesting + S3Object.Metadata attemptNotFound(ExecutionException e) + throws ExecutionException { + if (e.getCause() != null + && e.getCause() instanceof HttpResponseException) { + HttpResponseException responseException = (HttpResponseException) e + .getCause(); + if (responseException.getResponse().getStatusCode() == 404) { + return S3Object.Metadata.NOT_FOUND; + } + } + throw e; + } - @Override - public S3Object.Metadata get(long l, TimeUnit timeUnit) throws InterruptedException, - ExecutionException, TimeoutException { - try { - return super.get(l, timeUnit); - } catch (ExecutionException e) { - return attemptNotFound(e); - } - } + @Override + public S3Object.Metadata get(long l, TimeUnit timeUnit) + throws InterruptedException, ExecutionException, TimeoutException { + try { + return super.get(l, timeUnit); + } catch (ExecutionException e) { + return attemptNotFound(e); + } + } } \ No newline at end of file diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/ListBucket.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/ListBucket.java index 338b76b4df..8e3a475d09 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/ListBucket.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/ListBucket.java @@ -31,11 +31,13 @@ import org.jclouds.aws.AWSResponseException; import org.jclouds.aws.s3.commands.options.ListBucketOptions; import org.jclouds.aws.s3.domain.S3Bucket; import org.jclouds.aws.s3.xml.ListBucketHandler; +import org.jclouds.http.HttpResponseException; import org.jclouds.http.commands.callables.xml.ParseSax; import com.google.common.annotations.VisibleForTesting; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; +import com.google.inject.name.Named; /** * A GET request operation using a bucket URI lists information about the objects in the bucket. @@ -54,9 +56,10 @@ import com.google.inject.assistedinject.Assisted; public class ListBucket extends S3FutureCommand { @Inject - public ListBucket(ParseSax bucketParser, @Assisted String bucket, + public ListBucket(@Named("jclouds.http.address") String amazonHost, + ParseSax bucketParser, @Assisted String bucket, @Assisted ListBucketOptions options) { - super("GET", "/" + options.buildQueryString(), bucketParser, bucket); + super("GET", "/" + options.buildQueryString(), bucketParser, amazonHost, bucket); ListBucketHandler handler = (ListBucketHandler) bucketParser.getHandler(); handler.setBucketName(bucket); } @@ -72,7 +75,7 @@ public class ListBucket extends S3FutureCommand { @VisibleForTesting S3Bucket attemptNotFound(ExecutionException e) throws ExecutionException { - if (e.getCause() != null && e.getCause() instanceof AWSResponseException) { + if (e.getCause() != null && e.getCause() instanceof HttpResponseException) { AWSResponseException responseException = (AWSResponseException) e.getCause(); if ("NoSuchBucket".equals(responseException.getError().getCode())) { return S3Bucket.NOT_FOUND; diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/ListOwnedBuckets.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/ListOwnedBuckets.java index cba24304d4..ad8c3e9bd0 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/ListOwnedBuckets.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/ListOwnedBuckets.java @@ -29,21 +29,23 @@ import org.jclouds.aws.s3.domain.S3Bucket; import org.jclouds.http.commands.callables.xml.ParseSax; import com.google.inject.Inject; +import com.google.inject.name.Named; /** - * Returns a list of all of the buckets owned by the authenticated sender of the request. + * Returns a list of all of the buckets owned by the authenticated sender of the + * request. * - * @see * @author Adrian Cole * */ public class ListOwnedBuckets extends S3FutureCommand> { - @Inject - public ListOwnedBuckets(ParseSax> callable) { - super("GET", "/", callable); - } + @Inject + public ListOwnedBuckets(@Named("jclouds.http.address") String amazonHost, + ParseSax> callable) { + super("GET", "/", callable, amazonHost); + } } \ No newline at end of file diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/PutBucket.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/PutBucket.java index d70c75c853..dab4d5691b 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/PutBucket.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/PutBucket.java @@ -30,35 +30,36 @@ import org.jclouds.http.commands.callables.ReturnTrueIf2xx; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; +import com.google.inject.name.Named; /** * Create and name your own bucket in which to store your objects. *

- * The PUT request operation with a bucket URI creates a new bucket. Depending on your latency and - * legal requirements, you can specify a location constraint that will affect where your data - * physically resides. You can currently specify a Europe (EU) location constraint via - * {@link PutBucketOptions}. + * The PUT request operation with a bucket URI creates a new bucket. Depending + * on your latency and legal requirements, you can specify a location constraint + * that will affect where your data physically resides. You can currently + * specify a Europe (EU) location constraint via {@link PutBucketOptions}. * * @see PutBucketOptions - * @see * @author Adrian Cole * */ public class PutBucket extends S3FutureCommand { - @Inject - public PutBucket(ReturnTrueIf2xx callable, @Assisted String bucketName, - @Assisted PutBucketOptions options) { - super("PUT", "/", callable, S3Utils.validateBucketName(bucketName)); - getRequest().getHeaders().putAll(options.buildRequestHeaders()); - String payload = options.buildPayload(); - if (payload != null) { - getRequest().setPayload(payload); - getRequest().getHeaders().put(HttpHeaders.CONTENT_LENGTH, payload.getBytes().length + ""); - } else { - getRequest().getHeaders().put(HttpHeaders.CONTENT_LENGTH, "0"); - } - } + @Inject + public PutBucket(@Named("jclouds.http.address") String amazonHost, + ReturnTrueIf2xx callable, @Assisted String bucketName, + @Assisted PutBucketOptions options) { + super("PUT", "/", callable, amazonHost, S3Utils + .validateBucketName(bucketName)); + getRequest().getHeaders().putAll(options.buildRequestHeaders()); + String payload = options.buildPayload(); + if (payload != null) { + getRequest().setPayload(payload); + getRequest().getHeaders().put(HttpHeaders.CONTENT_LENGTH, + payload.getBytes().length + ""); + } + } } \ No newline at end of file diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/PutObject.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/PutObject.java index e49cf1a36f..371f3db00f 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/PutObject.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/PutObject.java @@ -34,6 +34,7 @@ import org.jclouds.http.HttpHeaders; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; +import com.google.inject.name.Named; /** * Store data by creating or overwriting an object. @@ -42,53 +43,56 @@ import com.google.inject.assistedinject.Assisted; * This returns a byte[] of the md5 hash of what Amazon S3 received *

*

- * This command allows you to specify {@link PutObjectOptions} to control delivery of content. + * This command allows you to specify {@link PutObjectOptions} to control + * delivery of content. * * * @see PutObjectOptions - * @see * @author Adrian Cole */ public class PutObject extends S3FutureCommand { - @Inject - public PutObject(ParseMd5FromETagHeader callable, @Assisted String s3Bucket, - @Assisted S3Object object, @Assisted PutObjectOptions options) { - super("PUT", "/" + checkNotNull(object.getKey()), callable, s3Bucket); - checkArgument(object.getMetadata().getSize() >= 0, "size must be set"); + @Inject + public PutObject(@Named("jclouds.http.address") String amazonHost, + ParseMd5FromETagHeader callable, @Assisted String s3Bucket, + @Assisted S3Object object, @Assisted PutObjectOptions options) { + super("PUT", "/" + checkNotNull(object.getKey()), callable, amazonHost, + s3Bucket); + checkArgument(object.getMetadata().getSize() >= 0, "size must be set"); - getRequest().setPayload(checkNotNull(object.getData(), "object.getContent()")); + getRequest().setPayload( + checkNotNull(object.getData(), "object.getContent()")); - getRequest().getHeaders() - .put( - HttpHeaders.CONTENT_TYPE, - checkNotNull(object.getMetadata().getContentType(), - "object.metadata.contentType()")); + getRequest().getHeaders().put( + HttpHeaders.CONTENT_TYPE, + checkNotNull(object.getMetadata().getContentType(), + "object.metadata.contentType()")); - getRequest().getHeaders() - .put(HttpHeaders.CONTENT_LENGTH, object.getMetadata().getSize() + ""); + getRequest().getHeaders().put(HttpHeaders.CONTENT_LENGTH, + object.getMetadata().getSize() + ""); - if (object.getMetadata().getCacheControl() != null) { - getRequest().getHeaders().put(HttpHeaders.CACHE_CONTROL, - object.getMetadata().getCacheControl()); - } - if (object.getMetadata().getContentDisposition() != null) { - getRequest().getHeaders().put(HttpHeaders.CONTENT_DISPOSITION, - object.getMetadata().getContentDisposition()); - } - if (object.getMetadata().getContentEncoding() != null) { - getRequest().getHeaders().put(HttpHeaders.CONTENT_ENCODING, - object.getMetadata().getContentEncoding()); - } + if (object.getMetadata().getCacheControl() != null) { + getRequest().getHeaders().put(HttpHeaders.CACHE_CONTROL, + object.getMetadata().getCacheControl()); + } + if (object.getMetadata().getContentDisposition() != null) { + getRequest().getHeaders().put(HttpHeaders.CONTENT_DISPOSITION, + object.getMetadata().getContentDisposition()); + } + if (object.getMetadata().getContentEncoding() != null) { + getRequest().getHeaders().put(HttpHeaders.CONTENT_ENCODING, + object.getMetadata().getContentEncoding()); + } - if (object.getMetadata().getMd5() != null) - getRequest().getHeaders().put(HttpHeaders.CONTENT_MD5, - S3Utils.toBase64String(object.getMetadata().getMd5())); + if (object.getMetadata().getMd5() != null) + getRequest().getHeaders().put(HttpHeaders.CONTENT_MD5, + S3Utils.toBase64String(object.getMetadata().getMd5())); - getRequest().getHeaders().putAll(object.getMetadata().getUserMetadata()); - getRequest().getHeaders().putAll(options.buildRequestHeaders()); + getRequest().getHeaders() + .putAll(object.getMetadata().getUserMetadata()); + getRequest().getHeaders().putAll(options.buildRequestHeaders()); - } + } } \ No newline at end of file diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/S3CommandFactory.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/S3CommandFactory.java index d9fead7ab9..941fb0e421 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/S3CommandFactory.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/S3CommandFactory.java @@ -33,6 +33,7 @@ import org.jclouds.aws.s3.xml.S3ParserFactory; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; +import com.google.inject.name.Named; /** * Assembles the command objects for S3. @@ -121,29 +122,32 @@ public class S3CommandFactory { return headMetadataFactory.create(bucket, key); } + @Inject + @Named("jclouds.http.address") + String amazonHost; public ListOwnedBuckets createGetMetadataForOwnedBuckets() { - return new ListOwnedBuckets(parserFactory.createListBucketsParser()); + return new ListOwnedBuckets(amazonHost, parserFactory.createListBucketsParser()); } public ListBucket createListBucket(String bucket, ListBucketOptions options) { - return new ListBucket(parserFactory.createListBucketParser(), bucket, options); + return new ListBucket(amazonHost, parserFactory.createListBucketParser(), bucket, options); } public CopyObject createCopyObject(String sourceBucket, String sourceObject, String destinationBucket, String destinationObject, CopyObjectOptions options) { - return new CopyObject(parserFactory.createCopyObjectParser(), sourceBucket, + return new CopyObject(amazonHost, parserFactory.createCopyObjectParser(), sourceBucket, sourceObject, destinationBucket, destinationObject, options); } public GetAccessControlList createGetBucketACL(String bucket) { return new GetAccessControlList( - parserFactory.createAccessControlListParser(), bucket); + amazonHost, parserFactory.createAccessControlListParser(), bucket); } public GetAccessControlList createGetObjectACL(String bucket, String objectKey) { return new GetAccessControlList( - parserFactory.createAccessControlListParser(), bucket, objectKey); + amazonHost, parserFactory.createAccessControlListParser(), bucket, objectKey); } } \ No newline at end of file diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/S3FutureCommand.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/S3FutureCommand.java index da84ef24aa..e8b225e1fa 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/S3FutureCommand.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/commands/S3FutureCommand.java @@ -37,13 +37,19 @@ import org.jclouds.http.HttpFutureCommand; public class S3FutureCommand extends HttpFutureCommand { public S3FutureCommand(String method, String uri, ResponseCallable responseCallable, - String bucketName) { - super(method, String.format("/%1$s%2$s", checkNotNull(bucketName, "bucketName"), - checkNotNull(uri, "uri")), responseCallable); + String amazonHost, String bucketName) { + super(method, uri, responseCallable); + addHostHeader(checkNotNull(amazonHost, "amazonHost"), checkNotNull(bucketName, "bucketName")); } - public S3FutureCommand(String method, String uri, ResponseCallable responseCallable) { + public S3FutureCommand(String method, String uri, ResponseCallable responseCallable, + String amazonHost) { super(method, uri, responseCallable); + addHostHeader(checkNotNull(amazonHost, "amazonHost")); + } + + protected void addHostHeader(String amazonHost, String bucketName) { + addHostHeader(checkNotNull(bucketName) + "." + amazonHost); } } \ No newline at end of file diff --git a/aws/s3/core/src/main/java/org/jclouds/aws/s3/filters/RequestAuthorizeSignature.java b/aws/s3/core/src/main/java/org/jclouds/aws/s3/filters/RequestAuthorizeSignature.java index d9f820c61d..c81cf513cd 100644 --- a/aws/s3/core/src/main/java/org/jclouds/aws/s3/filters/RequestAuthorizeSignature.java +++ b/aws/s3/core/src/main/java/org/jclouds/aws/s3/filters/RequestAuthorizeSignature.java @@ -158,10 +158,9 @@ public class RequestAuthorizeSignature implements HttpRequestFilter { } private static void appendBucketName(HttpRequest request, StringBuilder toSign) { - String hostHeader = request.getFirstHeaderOrNull(HttpHeaders.HOST); - if (hostHeader != null && hostHeader.endsWith(".s3.amazonaws.com")) { + String hostHeader = request.getHeaders().get(HttpHeaders.HOST).iterator().next(); + if (hostHeader.endsWith(".s3.amazonaws.com")) toSign.append("/").append(hostHeader.substring(0, hostHeader.length() - 17)); - } } private static void appendUriPath(HttpRequest request, StringBuilder toSign) { @@ -174,21 +173,21 @@ public class RequestAuthorizeSignature implements HttpRequestFilter { } else { toSign.append(request.getUri()); } - + // ...however, there are a few exceptions that must be included in the signed URI. if (paramsString != null) { StringBuilder paramsToSign = new StringBuilder("?"); - + String[] params = paramsString.split("&"); for (String param : params) { String[] paramNameAndValue = param.split("="); - + if ("acl".equals(paramNameAndValue[0])) { paramsToSign.append("acl"); } // TODO: Other special cases not yet handled: torrent, logging, location, requestPayment } - + if (paramsToSign.length() > 1) { toSign.append(paramsToSign); } diff --git a/aws/s3/core/src/test/java/org/jclouds/aws/s3/commands/callables/ParseObjectFromHeadersAndHttpContentTest.java b/aws/s3/core/src/test/java/org/jclouds/aws/s3/commands/callables/ParseObjectFromHeadersAndHttpContentTest.java index 8eb1f77d03..dd6e2a0f96 100644 --- a/aws/s3/core/src/test/java/org/jclouds/aws/s3/commands/callables/ParseObjectFromHeadersAndHttpContentTest.java +++ b/aws/s3/core/src/test/java/org/jclouds/aws/s3/commands/callables/ParseObjectFromHeadersAndHttpContentTest.java @@ -41,13 +41,12 @@ import org.testng.annotations.Test; */ @Test(groups = "unit", testName = "s3.ParseObjectFromHeadersAndHttpContentTest") public class ParseObjectFromHeadersAndHttpContentTest { - ParseObjectFromHeadersAndHttpContent callable; - ParseMetadataFromHeaders metadataParser; @Test(expectedExceptions = IllegalStateException.class) public void testCall() throws HttpException { - metadataParser = createMock(ParseMetadataFromHeaders.class); - callable = new ParseObjectFromHeadersAndHttpContent(metadataParser); + ParseMetadataFromHeaders metadataParser = createMock(ParseMetadataFromHeaders.class); + ParseObjectFromHeadersAndHttpContent callable = new ParseObjectFromHeadersAndHttpContent( + metadataParser); HttpResponse response = createMock(HttpResponse.class); expect(response.getStatusCode()).andReturn(409).atLeastOnce(); expect(response.getContent()).andReturn(null); @@ -58,8 +57,9 @@ public class ParseObjectFromHeadersAndHttpContentTest { @Test public void testParseContentLengthWhenContentRangeSet() throws HttpException { - metadataParser = createMock(ParseMetadataFromHeaders.class); - callable = new ParseObjectFromHeadersAndHttpContent(metadataParser); + ParseMetadataFromHeaders metadataParser = createMock(ParseMetadataFromHeaders.class); + ParseObjectFromHeadersAndHttpContent callable = new ParseObjectFromHeadersAndHttpContent( + metadataParser); HttpResponse response = createMock(HttpResponse.class); metadataParser.setResponse(response); Metadata meta = createMock(Metadata.class);