View Javadoc

1   /**
2    *
3    * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4    *
5    * ====================================================================
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   * ====================================================================
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  import static org.jclouds.s3.reference.S3Headers.CANNED_ACL;
25  import static org.jclouds.s3.reference.S3Headers.DEFAULT_AMAZON_HEADERTAG;
26  
27  import java.util.Map.Entry;
28  
29  import javax.inject.Inject;
30  import javax.inject.Named;
31  
32  import org.jclouds.http.options.BaseHttpRequestOptions;
33  import org.jclouds.s3.domain.CannedAccessPolicy;
34  
35  import com.google.common.collect.ImmutableMultimap;
36  import com.google.common.collect.Multimap;
37  
38  /**
39   * Contains options supported in the REST API for the PUT object operation.
40   * <p/>
41   * <h2>
42   * Usage</h2> The recommended way to instantiate a PutObjectOptions object is to statically import
43   * PutObjectOptions.Builder.* and invoke a static creation method followed by an instance mutator
44   * (if needed):
45   * <p/>
46   * <code>
47   * import static org.jclouds.s3.commands.options.PutObjectOptions.Builder.*
48   * import org.jclouds.s3.S3Client;
49   * 
50   * S3Client connection = // get connection
51   * Future<Boolean> publicly readable = connection.putObject("bucketName",new S3Object("key","value"), withAcl(CannedAccessPolicy.PUBLIC_READ));
52   * <code>
53   * 
54   * @see <a
55   *      href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?RESTObjectPUT.html?"
56   *      />
57   * 
58   * @author Adrian Cole
59   * 
60   */
61  public class PutObjectOptions extends BaseHttpRequestOptions {
62     public static final PutObjectOptions NONE = new PutObjectOptions();
63  
64     private CannedAccessPolicy acl = CannedAccessPolicy.PRIVATE;
65  
66     private String headerTag;
67  
68     @Inject
69     public void setHeaderTag(@Named(PROPERTY_HEADER_TAG) String headerTag) {
70        this.headerTag = headerTag;
71     }
72  
73     @Override
74     public Multimap<String, String> buildRequestHeaders() {
75        checkState(headerTag != null, "headerTag should have been injected!");
76        ImmutableMultimap.Builder<String, String> returnVal = ImmutableMultimap.<String, String> builder();
77        for (Entry<String, String> entry : headers.entries()) {
78           returnVal.put(entry.getKey().replace(DEFAULT_AMAZON_HEADERTAG, headerTag), entry.getValue());
79        }
80        return returnVal.build();
81     }
82  
83     /**
84      * Override the default ACL (private) with the specified one.
85      * 
86      * @see CannedAccessPolicy
87      */
88     public PutObjectOptions withAcl(CannedAccessPolicy acl) {
89        this.acl = checkNotNull(acl, "acl");
90        if (!acl.equals(CannedAccessPolicy.PRIVATE))
91           this.replaceHeader(CANNED_ACL, acl.toString());
92        return this;
93     }
94  
95     /**
96      * @see PutObjectOptions#withAcl(CannedAccessPolicy)
97      */
98     public CannedAccessPolicy getAcl() {
99        return acl;
100    }
101 
102    public static class Builder {
103 
104       /**
105        * @see PutObjectOptions#withAcl(CannedAccessPolicy)
106        */
107       public static PutObjectOptions withAcl(CannedAccessPolicy acl) {
108          PutObjectOptions options = new PutObjectOptions();
109          return options.withAcl(acl);
110       }
111    }
112 }