3535public class PostPolicy {
3636 private static final String ALGORITHM = "AWS4-HMAC-SHA256" ;
3737
38- private String bucketName ;
39- private String objectName ;
40- private boolean startsWith ;
41- private DateTime expirationDate ;
38+ private final String bucketName ;
39+ private final String objectName ;
40+ private final boolean startsWith ;
41+ private final DateTime expirationDate ;
4242 private String contentType ;
43+ private String contentEncoding ;
4344 private long contentRangeStart ;
4445 private long contentRangeEnd ;
4546
@@ -87,6 +88,18 @@ public void setContentType(String contentType) throws InvalidArgumentException {
8788 }
8889
8990
91+ /**
92+ * Sets content encoding.
93+ */
94+ public void setContentEncoding (String contentEncoding ) throws InvalidArgumentException {
95+ if (Strings .isNullOrEmpty (contentEncoding )) {
96+ throw new InvalidArgumentException ("empty content encoding" );
97+ }
98+
99+ this .contentEncoding = contentEncoding ;
100+ }
101+
102+
90103 /**
91104 * Sets content length.
92105 */
@@ -153,12 +166,30 @@ private byte[] marshalJson(ArrayList<String[]> conditions) {
153166 return sb .toString ().getBytes (StandardCharsets .UTF_8 );
154167 }
155168
156-
157169 /**
158170 * Returns form data of this post policy.
159171 */
160172 public Map <String ,String > formData (String accessKey , String secretKey )
161- throws NoSuchAlgorithmException , InvalidKeyException {
173+ throws InvalidKeyException , NoSuchAlgorithmException {
174+ return makeFormData (accessKey , secretKey , BucketRegionCache .INSTANCE .region (this .bucketName ));
175+ }
176+
177+ /**
178+ * Returns form data of this post policy setting the provided region.
179+ */
180+ public Map <String ,String > formData (String accessKey , String secretKey , String region )
181+ throws NoSuchAlgorithmException , InvalidKeyException , InvalidArgumentException {
182+
183+ if (Strings .isNullOrEmpty (region )) {
184+ throw new InvalidArgumentException ("empty region" );
185+ }
186+
187+ return makeFormData (accessKey , secretKey , region );
188+ }
189+
190+ protected Map <String ,String > makeFormData (String accessKey , String secretKey , String region )
191+ throws NoSuchAlgorithmException , InvalidKeyException {
192+
162193 ArrayList <String []> conditions = new ArrayList <>();
163194 Map <String , String > formData = new HashMap <>();
164195
@@ -178,6 +209,11 @@ public Map<String,String> formData(String accessKey, String secretKey)
178209 formData .put ("Content-Type" , this .contentType );
179210 }
180211
212+ if (this .contentEncoding != null ) {
213+ conditions .add (new String []{"eq" , "$Content-Encoding" , this .contentEncoding });
214+ formData .put ("Content-Encoding" , this .contentEncoding );
215+ }
216+
181217 if (this .contentRangeStart > 0 && this .contentRangeEnd > 0 ) {
182218 conditions .add (new String []{"content-length-range" , Long .toString (this .contentRangeStart ),
183219 Long .toString (this .contentRangeEnd )});
@@ -187,7 +223,6 @@ public Map<String,String> formData(String accessKey, String secretKey)
187223 formData .put ("x-amz-algorithm" , ALGORITHM );
188224
189225 DateTime date = new DateTime ();
190- String region = BucketRegionCache .INSTANCE .region (this .bucketName );
191226 String credential = Signer .credential (accessKey , date , region );
192227 conditions .add (new String []{"eq" , "$x-amz-credential" , credential });
193228 formData .put ("x-amz-credential" , credential );
0 commit comments