-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathConvertSDKRequests.java
More file actions
228 lines (221 loc) · 9.43 KB
/
ConvertSDKRequests.java
File metadata and controls
228 lines (221 loc) · 9.43 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
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 {
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.locationName() + " 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);
}
}