Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / files / GSFileOptions.java
blob4d01cda3dda9355519b5c53fe35ef935e16f503e
1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com.google.appengine.api.files;
5 import com.google.common.base.Preconditions;
7 import java.util.Map;
8 import java.util.TreeMap;
10 /**
11 * Container class for holding options for creating Google Storage files.
13 * @see <a href="http://code.google.com/apis/storage/">Google Storage
14 * API</a>.
16 public class GSFileOptions {
17 final String fileName;
18 final String mimeType;
19 final String acl;
20 final String cacheControl;
21 final String contentEncoding;
22 final String contentDisposition;
23 final Map<String, String> userMetadata = new TreeMap<String, String>();
25 private GSFileOptions(String bucket,
26 String key,
27 String mimeType,
28 String acl,
29 String cacheControl,
30 String contentEncoding,
31 String contentDisposition,
32 Map<String, String> userMetadata) {
33 Preconditions.checkArgument(bucket != null && !bucket.isEmpty(), "Must provide bucket");
34 Preconditions.checkArgument(key != null && !key.isEmpty(), "Must provide key");
35 this.fileName = FileServiceImpl.GS_FILESYSTEM_PREFIX + bucket + "/" + key;
36 this.mimeType = (mimeType == null || mimeType.trim().isEmpty())
37 ? FileServiceImpl.GS_DEFAULT_MIME_TYPE : mimeType;
38 this.acl = acl;
39 this.cacheControl = cacheControl;
40 this.contentEncoding = contentEncoding;
41 this.contentDisposition = contentDisposition;
42 if (userMetadata != null) {
43 this.userMetadata.putAll(userMetadata);
47 /**
48 * A builder of GSFileOptions.
50 public static class GSFileOptionsBuilder {
51 String bucket;
52 String key;
53 String mimeType;
54 String acl;
55 String cacheControl;
56 String contentEncoding;
57 String contentDisposition;
58 Map<String, String> userMetadata = new TreeMap<String, String>();
60 /**
61 * Sets the name of the bucket. Required.
62 * @param bucket name of the Google Storage bucket
63 * @return this for chaining.
65 public GSFileOptionsBuilder setBucket(String bucket) {
66 this.bucket = Preconditions.checkNotNull(bucket);
67 return this;
70 /**
71 * Sets the key of the object. Required.
72 * @param key of the Google Storage object
73 * @return this for chaining.
75 public GSFileOptionsBuilder setKey(String key) {
76 this.key = Preconditions.checkNotNull(key);
77 return this;
80 /**
81 * Sets the mime type of the object. If not set, default Google Storage
82 * mime type is used when served out of Google Storage.
83 * {@link "http://code.google.com/apis/storage/docs/reference-headers.html#contenttype"}
84 * @param mimeType of the Google Storage object.
85 * @return this for chaining.
87 public GSFileOptionsBuilder setMimeType(String mimeType) {
88 this.mimeType = Preconditions.checkNotNull(mimeType);
89 return this;
92 /**
93 * Sets the acl of the object. If not set, defaults to none (ie, bucket default).
94 * {@link "http://code.google.com/apis/storage/docs/accesscontrol.html"}
95 * @param acl to use for the Google Storage object.
96 * @return this for chaining.
98 public GSFileOptionsBuilder setAcl(String acl) {
99 this.acl = Preconditions.checkNotNull(acl);
100 return this;
104 * Sets the cache control for the object. If not set, default value is used.
105 * {@link "http://code.google.com/apis/storage/docs/reference-headers.html#cachecontrol"}
106 * @param cacheControl to use for the Google Storage object.
107 * @return this for chaining.
109 public GSFileOptionsBuilder setCacheControl(String cacheControl) {
110 this.cacheControl = Preconditions.checkNotNull(cacheControl);
111 return this;
115 * Sets the content encoding for the object. If not set, default value is used.
116 * {@link "http://code.google.com/apis/storage/docs/reference-headers.html#contentencoding"}
117 * @param contentEncoding to use for the Google Storage object.
118 * @return this for chaining.
120 public GSFileOptionsBuilder setContentEncoding(String contentEncoding) {
121 this.contentEncoding = Preconditions.checkNotNull(contentEncoding);
122 return this;
126 * Sets the content disposition for the object. If not set, default value is used.
127 * {@link "http://code.google.com/apis/storage/docs/reference-headers.html#contentdisposition"}
128 * @param contentDisposition to use for the Google Storage object.
129 * @return this for chaining.
131 public GSFileOptionsBuilder setContentDisposition(String contentDisposition) {
132 this.contentDisposition = Preconditions.checkNotNull(contentDisposition);
133 return this;
137 * Adds user specific metadata that will be added to object headers when
138 * served through Google Storage:
139 * {@link "http://code.google.com/apis/storage/docs/reference-headers.html#xgoogmeta"}
140 * Each entry will be prefixed with x-goog-meta- when serving out.
141 * For example, if you add 'foo'->'bar' entry to userMetadata map,
142 * it will be served out as a header:
143 * x-goog-meta-foo: bar
144 * @param key
145 * @param value
146 * @return this for chaining.
148 public GSFileOptionsBuilder addUserMetadata(String key, String value) {
149 Preconditions.checkArgument(key != null && !key.isEmpty());
150 Preconditions.checkArgument(value != null && !value.isEmpty());
151 userMetadata.put(key, value);
152 return this;
155 public GSFileOptions build() {
156 return new GSFileOptions(bucket,
157 key,
158 mimeType,
159 acl,
160 cacheControl,
161 contentEncoding,
162 contentDisposition,
163 userMetadata);