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 * {@link "https://cloud.google.com/storage/docs/reference-headers#contenttype"}
87 * @param mimeType of the Google Storage object.
88 * @return this for chaining.
90 public GSFileOptionsBuilder
setMimeType(String mimeType
) {
91 this.mimeType
= Preconditions
.checkNotNull(mimeType
);
96 * Sets the acl of the object. If not set, defaults to none (ie, bucket default).
97 * {@link "https://cloud.google.com/storage/docs/access-control"}
98 * @param acl to use for the Google Storage object.
99 * @return this for chaining.
101 public GSFileOptionsBuilder
setAcl(String acl
) {
102 this.acl
= Preconditions
.checkNotNull(acl
);
107 * Sets the cache control for the object. If not set, default value is used.
108 * {@link "https://cloud.google.com/storage/docs/reference-headers#cachecontrol"}
109 * @param cacheControl to use for the Google Storage object.
110 * @return this for chaining.
112 public GSFileOptionsBuilder
setCacheControl(String cacheControl
) {
113 this.cacheControl
= Preconditions
.checkNotNull(cacheControl
);
118 * Sets the content encoding for the object. If not set, default value is used.
119 * {@link "https://cloud.google.com/storage/docs/reference-headers#contentencoding"}
120 * @param contentEncoding to use for the Google Storage object.
121 * @return this for chaining.
123 public GSFileOptionsBuilder
setContentEncoding(String contentEncoding
) {
124 this.contentEncoding
= Preconditions
.checkNotNull(contentEncoding
);
129 * Sets the content disposition for the object. If not set, default value is used.
130 * {@link "https://cloud.google.com/storage/docs/reference-headers#contentdisposition"}
131 * @param contentDisposition to use for the Google Storage object.
132 * @return this for chaining.
134 public GSFileOptionsBuilder
setContentDisposition(String contentDisposition
) {
135 this.contentDisposition
= Preconditions
.checkNotNull(contentDisposition
);
140 * Adds user specific metadata that will be added to object headers when
141 * served through Google Storage:
142 * {@link "https://cloud.google.com/storage/docs/reference-headers#xgoogmeta"}
143 * Each entry will be prefixed with x-goog-meta- when serving out.
144 * For example, if you add 'foo'->'bar' entry to userMetadata map,
145 * it will be served out as a header:
146 * x-goog-meta-foo: bar
149 * @return this for chaining.
151 public GSFileOptionsBuilder
addUserMetadata(String key
, String value
) {
152 Preconditions
.checkArgument(key
!= null && !key
.isEmpty());
153 Preconditions
.checkArgument(value
!= null && !value
.isEmpty());
154 userMetadata
.put(key
, value
);
158 public GSFileOptions
build() {
159 return new GSFileOptions(bucket
,