| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.s3.options; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Preconditions.checkState; |
| 23 | import static org.jclouds.aws.reference.AWSConstants.PROPERTY_HEADER_TAG; |
| 24 | |
| 25 | import java.util.Map.Entry; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Named; |
| 29 | |
| 30 | import org.jclouds.s3.domain.CannedAccessPolicy; |
| 31 | import org.jclouds.s3.reference.S3Headers; |
| 32 | import org.jclouds.http.options.BaseHttpRequestOptions; |
| 33 | |
| 34 | import com.google.common.collect.LinkedHashMultimap; |
| 35 | import com.google.common.collect.Multimap; |
| 36 | |
| 37 | /** |
| 38 | * Contains options supported in the REST API for the PUT bucket operation. <h2> |
| 39 | * Usage</h2> The recommended way to instantiate a PutBucketOptions object is to statically import |
| 40 | * PutBucketOptions.Builder.* and invoke a static creation method followed by an instance mutator |
| 41 | * (if needed): |
| 42 | * <p/> |
| 43 | * <code> |
| 44 | * import static org.jclouds.s3.commands.options.PutBucketOptions.Builder.* |
| 45 | * import static org.jclouds.s3.domain.S3Bucket.Metadata.LocationConstraint.*; |
| 46 | * import org.jclouds.s3.S3Client; |
| 47 | * <p/> |
| 48 | * S3Client connection = // get connection |
| 49 | * Future<Boolean> createdInEu = connection.putBucketIfNotExists("bucketName",createIn(EU)); |
| 50 | * <code> |
| 51 | * |
| 52 | * @author Adrian Cole |
| 53 | * @see <a |
| 54 | * href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTBucketPUT.html?" |
| 55 | * /> |
| 56 | */ |
| 57 | public class PutBucketOptions extends BaseHttpRequestOptions { |
| 58 | private CannedAccessPolicy acl = CannedAccessPolicy.PRIVATE; |
| 59 | |
| 60 | private String headerTag; |
| 61 | |
| 62 | @Inject |
| 63 | public void setHeaderTag(@Named(PROPERTY_HEADER_TAG) String headerTag) { |
| 64 | this.headerTag = headerTag; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public Multimap<String, String> buildRequestHeaders() { |
| 69 | checkState(headerTag != null, "headerTag should have been injected!"); |
| 70 | Multimap<String, String> returnVal = LinkedHashMultimap.create(); |
| 71 | for (Entry<String, String> entry : headers.entries()) { |
| 72 | returnVal.put(entry.getKey().replace("aws", headerTag), entry.getValue()); |
| 73 | } |
| 74 | return returnVal; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Override the default ACL (private) with the specified one. |
| 79 | * |
| 80 | * @see CannedAccessPolicy |
| 81 | */ |
| 82 | public PutBucketOptions withBucketAcl(CannedAccessPolicy acl) { |
| 83 | this.acl = checkNotNull(acl, "acl"); |
| 84 | if (!acl.equals(CannedAccessPolicy.PRIVATE)) |
| 85 | this.replaceHeader(S3Headers.CANNED_ACL, acl.toString()); |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @see PutBucketOptions#withBucketAcl |
| 91 | */ |
| 92 | public CannedAccessPolicy getAcl() { |
| 93 | return acl; |
| 94 | } |
| 95 | |
| 96 | public static class Builder { |
| 97 | /** |
| 98 | * @see PutBucketOptions#withBucketAcl |
| 99 | */ |
| 100 | public static PutBucketOptions withBucketAcl(CannedAccessPolicy acl) { |
| 101 | PutBucketOptions options = new PutBucketOptions(); |
| 102 | return options.withBucketAcl(acl); |
| 103 | } |
| 104 | } |
| 105 | } |