1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.files
;
5 import com
.google
.common
.base
.Preconditions
;
8 import java
.util
.TreeMap
;
11 * Container class for holding options for creating Google Storage files.
13 * @see <a href="https://cloud.google.com/storage/docs/">Google Storage
17 public class GSFileOptions
{
18 public static final String GS_USER_METADATA_PREFIX
= "x-goog-meta-";
20 final String fileName
;
21 final String mimeType
;
23 final String cacheControl
;
24 final String contentEncoding
;
25 final String contentDisposition
;
26 final Map
<String
, String
> userMetadata
= new TreeMap
<String
, String
>();
28 private GSFileOptions(String bucket
,
33 String contentEncoding
,
34 String contentDisposition
,
35 Map
<String
, String
> userMetadata
) {
36 Preconditions
.checkArgument(bucket
!= null && !bucket
.isEmpty(), "Must provide bucket");
37 Preconditions
.checkArgument(key
!= null && !key
.isEmpty(), "Must provide key");
38 this.fileName
= FileServiceImpl
.GS_FILESYSTEM_PREFIX
+ bucket
+ "/" + key
;
39 this.mimeType
= (mimeType
== null || mimeType
.trim().isEmpty())
40 ? FileServiceImpl
.GS_DEFAULT_MIME_TYPE
: mimeType
;
42 this.cacheControl
= cacheControl
;
43 this.contentEncoding
= contentEncoding
;
44 this.contentDisposition
= contentDisposition
;
45 if (userMetadata
!= null) {
46 this.userMetadata
.putAll(userMetadata
);
51 * A builder of GSFileOptions.
53 public static class GSFileOptionsBuilder
{
59 String contentEncoding
;
60 String contentDisposition
;
61 Map
<String
, String
> userMetadata
= new TreeMap
<String
, String
>();
64 * Sets the name of the bucket. Required.
65 * @param bucket name of the Google Storage bucket
66 * @return this for chaining.
68 public GSFileOptionsBuilder
setBucket(String bucket
) {
69 this.bucket
= Preconditions
.checkNotNull(bucket
);
74 * Sets the key of the object. Required.
75 * @param key of the Google Storage object
76 * @return this for chaining.
78 public GSFileOptionsBuilder
setKey(String key
) {
79 this.key
= Preconditions
.checkNotNull(key
);
84 * Sets the mime type of the object. If not set, default Google Storage
85 * mime type is used when served out of Google Storage.
86 * See <a href="https://cloud.google.com/storage/docs/reference-headers#contenttype">
88 * @param mimeType of the Google Storage object.
89 * @return this for chaining.
91 public GSFileOptionsBuilder
setMimeType(String mimeType
) {
92 this.mimeType
= Preconditions
.checkNotNull(mimeType
);
97 * Sets the acl of the object. If not set, defaults to none (ie, bucket default).
98 * See <a href="https://cloud.google.com/storage/docs/access-control">Access Control</a>.
99 * @param acl to use for the Google Storage object.
100 * @return this for chaining.
102 public GSFileOptionsBuilder
setAcl(String acl
) {
103 this.acl
= Preconditions
.checkNotNull(acl
);
108 * Sets the cache control for the object. If not set, default value is used.
109 * See <a href="https://cloud.google.com/storage/docs/reference-headers#cachecontrol">
111 * @param cacheControl to use for the Google Storage object.
112 * @return this for chaining.
114 public GSFileOptionsBuilder
setCacheControl(String cacheControl
) {
115 this.cacheControl
= Preconditions
.checkNotNull(cacheControl
);
120 * Sets the content encoding for the object. If not set, default value is used.
121 * See <a href="https://cloud.google.com/storage/docs/reference-headers#contentencoding">
122 * Content-Encoding</a>.
123 * @param contentEncoding to use for the Google Storage object.
124 * @return this for chaining.
126 public GSFileOptionsBuilder
setContentEncoding(String contentEncoding
) {
127 this.contentEncoding
= Preconditions
.checkNotNull(contentEncoding
);
132 * Sets the content disposition for the object. If not set, default value is used.
133 * See <a href="https://cloud.google.com/storage/docs/reference-headers#contentdisposition">
134 * Content-Disposition</a>.
135 * @param contentDisposition to use for the Google Storage object.
136 * @return this for chaining.
138 public GSFileOptionsBuilder
setContentDisposition(String contentDisposition
) {
139 this.contentDisposition
= Preconditions
.checkNotNull(contentDisposition
);
144 * Adds user specific metadata that will be added to object headers when
145 * served through Google Storage:
146 * Each entry will be prefixed with <a
147 * href="https://cloud.google.com/storage/docs/reference-headers#xgoogmeta">
148 * x-goog-meta-</a> when serving out.
149 * For example, if you add a {@literal 'foo'->'bar'} entry to userMetadata map,
150 * it will be served out as a header:
151 * x-goog-meta-foo: bar
154 * @return this for chaining.
156 public GSFileOptionsBuilder
addUserMetadata(String key
, String value
) {
157 Preconditions
.checkArgument(key
!= null && !key
.isEmpty());
158 Preconditions
.checkArgument(value
!= null && !value
.isEmpty());
159 userMetadata
.put(key
, value
);
163 public GSFileOptions
build() {
164 return new GSFileOptions(bucket
,