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="http://code.google.com/apis/storage/">Google Storage
16 public class GSFileOptions
{
17 final String fileName
;
18 final String mimeType
;
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
,
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
;
39 this.cacheControl
= cacheControl
;
40 this.contentEncoding
= contentEncoding
;
41 this.contentDisposition
= contentDisposition
;
42 if (userMetadata
!= null) {
43 this.userMetadata
.putAll(userMetadata
);
48 * A builder of GSFileOptions.
50 public static class GSFileOptionsBuilder
{
56 String contentEncoding
;
57 String contentDisposition
;
58 Map
<String
, String
> userMetadata
= new TreeMap
<String
, String
>();
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
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
);
155 public GSFileOptions
build() {
156 return new GSFileOptions(bucket
,