-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathConvertSDKRequests.java
More file actions
366 lines (360 loc) · 16 KB
/
ConvertSDKRequests.java
File metadata and controls
366 lines (360 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package software.amazon.encryption.s3.internal;
import java.time.Instant;
import java.util.Map;
import org.apache.commons.logging.LogFactory;
import software.amazon.awssdk.services.s3.model.ChecksumType;
import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadResponse;
import software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
public class ConvertSDKRequests {
/**
* Converts a CreateMultipartUploadRequest to a PutObjectRequest. This conversion is necessary when
* Instruction File PutObject is enabled and a multipart upload is performed.The method copies all the
* relevant fields from the CreateMultipartUploadRequest to the PutObjectRequest.
* @param request The CreateMultipartUploadRequest to convert
* @return The converted PutObjectRequest
* @throws IllegalArgumentException if the request contains an invalid field
*/
public static PutObjectRequest convertRequest(CreateMultipartUploadRequest request) {
final PutObjectRequest.Builder output = PutObjectRequest.builder();
request
.toBuilder()
.sdkFields()
.forEach(f -> {
final Object value = f.getValueOrDefault(request);
if (value != null) {
switch (f.memberName()) {
case "ACL":
output.acl((String) value);
break;
case "Bucket":
output.bucket((String) value);
break;
case "BucketKeyEnabled":
output.bucketKeyEnabled((Boolean) value);
break;
case "CacheControl":
output.cacheControl((String) value);
break;
case "ChecksumAlgorithm":
output.checksumAlgorithm((String) value);
break;
case "ContentDisposition":
assert value instanceof String;
output.contentDisposition((String) value);
break;
case "ContentEncoding":
output.contentEncoding((String) value);
break;
case "ContentLanguage":
output.contentLanguage((String) value);
break;
case "ContentType":
output.contentType((String) value);
break;
case "ExpectedBucketOwner":
output.expectedBucketOwner((String) value);
break;
case "Expires":
output.expires((Instant) value);
break;
case "GrantFullControl":
output.grantFullControl((String) value);
break;
case "GrantRead":
output.grantRead((String) value);
break;
case "GrantReadACP":
output.grantReadACP((String) value);
break;
case "GrantWriteACP":
output.grantWriteACP((String) value);
break;
case "Key":
output.key((String) value);
break;
case "Metadata":
if (!isStringStringMap(value)) {
throw new IllegalArgumentException("Metadata must be a Map<String, String>");
}
@SuppressWarnings("unchecked")
Map<String, String> metadata = (Map<String, String>) value;
output.metadata(metadata);
break;
case "ObjectLockLegalHoldStatus":
output.objectLockLegalHoldStatus((String) value);
break;
case "ObjectLockMode":
output.objectLockMode((String) value);
break;
case "ObjectLockRetainUntilDate":
output.objectLockRetainUntilDate((Instant) value);
break;
case "RequestPayer":
output.requestPayer((String) value);
break;
case "ServerSideEncryption":
output.serverSideEncryption((String) value);
break;
case "SSECustomerAlgorithm":
output.sseCustomerAlgorithm((String) value);
break;
case "SSECustomerKey":
output.sseCustomerKey((String) value);
break;
case "SSEKMSEncryptionContext":
output.ssekmsEncryptionContext((String) value);
break;
case "SSEKMSKeyId":
output.ssekmsKeyId((String) value);
break;
case "StorageClass":
output.storageClass((String) value);
break;
case "Tagging":
output.tagging((String) value);
break;
case "WebsiteRedirectLocation":
output.websiteRedirectLocation((String) value);
break;
default:
// Rather than silently dropping the value,
// we loudly signal that we don't know how to handle this field.
throw new IllegalArgumentException(
f.memberName() + " is an unknown field. " +
"The S3 Encryption Client does not recognize this option and cannot set it on the PutObjectRequest." +
"This may be a new S3 feature." +
"Please report this to the Amazon S3 Encryption Client for Java: " +
"https://github.com/aws/amazon-s3-encryption-client-java/issues." +
"To work around this issue, you can disable Instruction File on PutObject or disable" +
"multi part upload, or use the Async client, or not set this value on PutObject." +
"You may be able to update this value after the PutObject request completes."
);
}
}
});
return output
// OverrideConfiguration is not as SDKField but still needs to be supported
.overrideConfiguration(request.overrideConfiguration().orElse(null))
.build();
}
/**
* Converts a PutObjectRequest to CreateMultipartUploadRequest.This conversion is necessary to convert an
* original PutObjectRequest into a CreateMultipartUploadRequest to initiate the
* multipart upload while maintaining the original request's configuration.
* @param request The PutObjectRequest to convert
* @return The converted CreateMultipartUploadRequest
* @throws IllegalArgumentException if the request contains an invalid field
*/
public static CreateMultipartUploadRequest convertRequest(PutObjectRequest request) {
final CreateMultipartUploadRequest.Builder output = CreateMultipartUploadRequest.builder();
request
.toBuilder()
.sdkFields()
.forEach(f -> {
final Object value = f.getValueOrDefault(request);
if (value != null) {
switch (f.memberName()) {
case "ACL":
output.acl((String) value);
break;
case "Bucket":
output.bucket((String) value);
break;
case "BucketKeyEnabled":
output.bucketKeyEnabled((Boolean) value);
break;
case "CacheControl":
output.cacheControl((String) value);
break;
case "ChecksumAlgorithm":
output.checksumAlgorithm((String) value);
break;
case "ChecksumType":
output.checksumType((ChecksumType) value);
case "ContentDisposition":
assert value instanceof String;
output.contentDisposition((String) value);
break;
case "ContentEncoding":
output.contentEncoding((String) value);
break;
case "ContentLanguage":
output.contentLanguage((String) value);
break;
case "ContentType":
output.contentType((String) value);
break;
case "ExpectedBucketOwner":
output.expectedBucketOwner((String) value);
break;
case "Expires":
output.expires((Instant) value);
break;
case "GrantFullControl":
output.grantFullControl((String) value);
break;
case "GrantRead":
output.grantRead((String) value);
break;
case "GrantReadACP":
output.grantReadACP((String) value);
break;
case "GrantWriteACP":
output.grantWriteACP((String) value);
break;
case "Key":
output.key((String) value);
break;
case "Metadata":
// The PutObjectRequest.builder().metadata(value)
// only takes Map<String, String> therefore it should not be possible
// to get here with anything other than a Map<String, String>
// This may be overkill, but this map should be small
// so the performance hit to verify this is worth the correctness.
if (!isStringStringMap(value)) {
throw new IllegalArgumentException("Metadata must be a Map<String, String>");
}
@SuppressWarnings("unchecked")
Map<String, String> metadata = (Map<String, String>) value;
output.metadata(metadata);
break;
case "ObjectLockLegalHoldStatus":
output.objectLockLegalHoldStatus((String) value);
break;
case "ObjectLockMode":
output.objectLockMode((String) value);
break;
case "ObjectLockRetainUntilDate":
output.objectLockRetainUntilDate((Instant) value);
break;
case "RequestPayer":
output.requestPayer((String) value);
break;
case "ServerSideEncryption":
output.serverSideEncryption((String) value);
break;
case "SSECustomerAlgorithm":
output.sseCustomerAlgorithm((String) value);
break;
case "SSECustomerKey":
output.sseCustomerKey((String) value);
break;
case "SSEKMSEncryptionContext":
output.ssekmsEncryptionContext((String) value);
break;
case "SSEKMSKeyId":
output.ssekmsKeyId((String) value);
break;
case "StorageClass":
output.storageClass((String) value);
break;
case "Tagging":
output.tagging((String) value);
break;
case "WebsiteRedirectLocation":
output.websiteRedirectLocation((String) value);
break;
default:
// Rather than silently dropping the value,
// we loudly signal that we don't know how to handle this field.
throw new IllegalArgumentException(
f.memberName() + " is an unknown field. " +
"The S3 Encryption Client does not recognize this option and cannot set it on the CreateMultipartUploadRequest." +
"This may be a new S3 feature." +
"Please report this to the Amazon S3 Encryption Client for Java: " +
"https://github.com/aws/amazon-s3-encryption-client-java/issues." +
"To work around this issue you can disable multi part upload," +
"use the Async client, or not set this value on PutObject." +
"You may be able to update this value after the PutObject request completes."
);
}
}
});
return output
// OverrideConfiguration is not as SDKField but still needs to be supported
.overrideConfiguration(request.overrideConfiguration().orElse(null))
.build();
}
public static PutObjectResponse convertResponse(CompleteMultipartUploadResponse response) {
final PutObjectResponse.Builder output = PutObjectResponse.builder();
response
.toBuilder()
.sdkFields()
.forEach(f -> {
final Object value = f.getValueOrDefault(response);
if (value != null) {
switch (f.memberName()) {
case "ETag":
output.eTag((String) value);
break;
case "Expiration":
output.expiration((String) value);
break;
case "ChecksumCRC32":
output.checksumCRC32((String) value);
break;
case "ChecksumCRC32C":
output.checksumCRC32C((String) value);
break;
case "ChecksumCRC64NVME":
output.checksumCRC64NVME((String) value);
break;
case "ChecksumSHA1":
output.checksumSHA1((String) value);
break;
case "ChecksumSHA256":
output.checksumSHA256((String) value);
break;
case "ChecksumType":
output.checksumType((String) value);
break;
case "ServerSideEncryption":
output.serverSideEncryption((String) value);
break;
case "VersionId":
output.versionId((String) value);
break;
case "SSEKMSKeyId":
output.ssekmsKeyId((String) value);
break;
case "BucketKeyEnabled":
output.bucketKeyEnabled((Boolean) value);
break;
case "RequestCharged":
output.requestCharged((String) value);
break;
// Ignored fields: Location, Bucket, Key
case "Location":
case "Bucket":
case "Key":
// These fields exist only in CompleteMultipartUploadResponse, not in PutObjectResponse
break;
default:
// We should NOT throw an exception for unknown fields because
// once the object is stored, we expect to return a successful response.
// Emit a log at info level for awareness.
LogFactory.getLog(ConvertSDKRequests.class).info(f.memberName() + " returned in CompleteMultipartUploadResponse for "
+ response.key() + " is an unknown field." +
"The S3 Encryption Client does not recognize this option and cannot set it on the CompleteMultipartUploadResponse." +
"This may be a new S3 feature." +
"Please report this to the Amazon S3 Encryption Client for Java: " +
"https://github.com/aws/amazon-s3-encryption-client-java/issues."
);
}
}
});
return output.build();
}
private static boolean isStringStringMap(Object value) {
if (!(value instanceof Map)) {
return false;
}
Map<?, ?> map = (Map<?, ?>) value;
return map.entrySet().stream()
.allMatch(entry -> entry != null
&& ((Map.Entry<?, ?>) entry).getKey() instanceof String
&& ((Map.Entry<?, ?>) entry).getValue() instanceof String);
}
}