-
Notifications
You must be signed in to change notification settings - Fork 997
Expand file tree
/
Copy pathS3PutObjectRequestToV2.java
More file actions
488 lines (404 loc) · 22.2 KB
/
S3PutObjectRequestToV2.java
File metadata and controls
488 lines (404 loc) · 22.2 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.awssdk.v2migration;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.SUPPORTED_METADATA_TRANSFORMS;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_MODEL_PKG;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_TM_MODEL_PKG;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.addCommentForUnsupportedPutObjectRequestSetter;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.addMetadataFields;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.createComments;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.getArgumentName;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.getSelectName;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.inputStreamBufferingWarningComment;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isObjectMetadataGetter;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isObjectMetadataSetter;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isPayloadSetter;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isPutObjectRequestBuilderSetter;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isPutObjectRequestSetter;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isRequestMetadataSetter;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isRequestPayerSetter;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isUnsupportedPutObjectRequestSetter;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v2S3MethodMatcher;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v2TmMethodMatcher;
import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.isFileType;
import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.isInputStreamType;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.AddImport;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import software.amazon.awssdk.annotations.SdkInternalApi;
@SdkInternalApi
public class S3PutObjectRequestToV2 extends Recipe {
private static final MethodMatcher PUT_OBJECT_STREAM_METADATA =
v2S3MethodMatcher(String.format("putObject(String, String, java.io.InputStream, %sHeadObjectResponse)",
V2_S3_MODEL_PKG));
private static final MethodMatcher PUT_OBJ_WITH_REQUEST =
v2S3MethodMatcher(String.format("putObject(%sPutObjectRequest)", V2_S3_MODEL_PKG));
private static final MethodMatcher UPLOAD_WITH_REQUEST =
v2TmMethodMatcher(String.format("upload(%sPutObjectRequest)", V2_S3_MODEL_PKG));
@Override
public String getDisplayName() {
return "V1 S3 PutObjectRequest, AmazonS3.putObject(PutObjectRequest), and TransferManager.upload(PutObjectRequest) to V2";
}
@Override
public String getDescription() {
return "Transform V1 S3 PutObjectRequest to V2, as well as methods that take it as an argument.";
}
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new Visitor();
}
private static final class Visitor extends JavaVisitor<ExecutionContext> {
// queues for when setter is on PutObjectRequest that is instantiated directly inside putObject(), no assigned variable
private final Queue<Expression> payloadQueue = new ArrayDeque<>();
private final Queue<String> metadataNameQueue = new ArrayDeque<>();
private final Map<String, Expression> requestToPayloadMap = new HashMap<>();
private final Map<String, String> requestToMetadataNamesMap = new HashMap<>();
private final Map<String, Map<String, Expression>> metadataMap = new HashMap<>();
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (isObjectMetadataSetter(method)) {
return saveMetadataValueAndRemoveStatement(method);
}
if (isObjectMetadataGetter(method)) {
if (method.getSelect() != null) {
return method.getSelect().withPrefix(method.getPrefix());
}
}
if (isPutObjectRequestBuilderSetter(method)) {
if (isRequestMetadataSetter(method)) {
method = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
return transformWithMetadata(method);
}
if (isPayloadSetter(method)) {
return transformRequestBuilderPayloadSetter(method);
}
if (isRequestPayerSetter(method)) {
return transformWithRequesterPays(method);
}
if (isUnsupportedPutObjectRequestSetter(method)) {
method = addCommentForUnsupportedPutObjectRequestSetter(method);
return super.visitMethodInvocation(method, ctx);
}
}
if (isPutObjectRequestSetter(method)) {
if (isRequestMetadataSetter(method)) {
return convertSetMetadataToBuilder(method);
}
if (isPayloadSetter(method)) {
return transformRequestPayloadSetter(method);
}
}
if (PUT_OBJECT_STREAM_METADATA.matches(method)) {
method = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
return transformPutObjectWithStreamAndMetadata(method);
}
if (PUT_OBJ_WITH_REQUEST.matches(method)) {
method = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
return transformPutObjectWithRequest(method);
}
if (UPLOAD_WITH_REQUEST.matches(method)) {
method = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
return transformUploadWithRequest(method);
}
return super.visitMethodInvocation(method, ctx);
}
private J.MethodInvocation transformRequestBuilderPayloadSetter(J.MethodInvocation method) {
Expression payload = method.getArguments().get(0);
String variableName = retrieveVariableNameIfRequestPojoIsAssigned();
if (variableName != null) {
requestToPayloadMap.put(variableName, payload);
} else {
payloadQueue.add(payload);
}
// Remove setter .file(file)
return (J.MethodInvocation) method.getSelect();
}
private J.MethodInvocation transformRequestPayloadSetter(J.MethodInvocation method) {
J.Identifier requestPojo = (J.Identifier) method.getSelect();
String variableName = requestPojo.getSimpleName();
Expression payload = method.getArguments().get(0);
requestToPayloadMap.put(variableName, payload);
// Remove entire statement e.g., request.setFile(file)
return null;
}
/**
* Request POJO may be assigned to a variable, or instantiated directly in putObject() API
*/
private String retrieveVariableNameIfRequestPojoIsAssigned() {
J parent = getCursor()
.dropParentUntil(p -> p instanceof J.VariableDeclarations.NamedVariable || p instanceof J.Block)
.getValue();
if (parent instanceof J.VariableDeclarations.NamedVariable) {
J.VariableDeclarations.NamedVariable namedVariable = (J.VariableDeclarations.NamedVariable) parent;
return namedVariable.getSimpleName();
}
return null;
}
private J.MethodInvocation transformPutObjectWithRequest(J.MethodInvocation method) {
Expression payload = retrieveRequestPayload(method);
if (payload == null) {
method = addEmptyRequestBodyToPutObject(method);
} else if (isFileType(payload.getType())) {
method = addFileToPutObject(method, payload);
} else if (isInputStreamType(payload.getType())) {
method = addInputStreamToPutObject(method, payload);
}
addRequestBodyImport();
return method;
}
private Expression retrieveRequestPayload(J.MethodInvocation method) {
Expression requestPojo = method.getArguments().get(0);
Expression payload;
if (requestPojo instanceof J.Identifier) {
J.Identifier pojo = (J.Identifier) requestPojo;
payload = requestToPayloadMap.get(pojo.getSimpleName());
} else {
payload = payloadQueue.poll();
}
return payload;
}
private J.MethodInvocation transformUploadWithRequest(J.MethodInvocation method) {
Expression payload = retrieveRequestPayload(method);
if (payload == null) {
method = addEmptyAsyncRequestBodyToUpload(method);
} else if (isFileType(payload.getType())) {
method = addFileAndChangeMethodToUploadFile(method, payload);
} else if (isInputStreamType(payload.getType())) {
method = addInputStreamToUpload(method, payload);
}
return method;
}
private J.MethodInvocation addEmptyAsyncRequestBodyToUpload(J.MethodInvocation method) {
String v2Method = "UploadRequest.builder().putObjectRequest(#{any()}).requestBody(AsyncRequestBody.empty()).build()";
addTmImport("UploadRequest");
addAsyncRequestBodyImport();
return JavaTemplate.builder(v2Method).build()
.apply(getCursor(), method.getCoordinates().replaceArguments(), method.getArguments().get(0));
}
private J.MethodInvocation addEmptyRequestBodyToPutObject(J.MethodInvocation method) {
String v2Method = "#{any()}, RequestBody.empty()";
return JavaTemplate.builder(v2Method).build()
.apply(getCursor(), method.getCoordinates().replaceArguments(), method.getArguments().get(0));
}
private J.MethodInvocation addFileAndChangeMethodToUploadFile(J.MethodInvocation method, Expression file) {
String v2Method = "#{any()}.uploadFile(UploadFileRequest.builder()"
+ ".putObjectRequest(#{any()}).source(#{any()}).build())";
addTmImport("UploadFileRequest");
return JavaTemplate.builder(v2Method).build()
.apply(getCursor(), method.getCoordinates().replace(), method.getSelect(),
method.getArguments().get(0), file);
}
private J.MethodInvocation addInputStreamToUpload(J.MethodInvocation method, Expression inputStream) {
addTmImport("UploadRequest");
addAsyncRequestBodyImport();
StringBuilder sb = new StringBuilder("UploadRequest.builder().putObjectRequest(#{any()})"
+ ".requestBody(AsyncRequestBody.fromInputStream(#{any()}, ");
String metadataName = getMetadataForRequest(method);
Expression contentLength;
if (metadataName == null) {
contentLength = null;
} else {
contentLength = retrieveContentLengthForMetadataIfSet(metadataName);
}
Expression[] params = {method.getArguments().get(0), inputStream};
if (contentLength == null) {
sb.append("-1L");
} else {
sb.append("#{any()}");
params = Arrays.copyOf(params, 3);
params[2] = contentLength;
}
sb.append(", newExecutorServiceVariableToDefine)).build()");
String comment = "When using InputStream to upload with TransferManager, you must specify Content-Length and "
+ "ExecutorService.";
return JavaTemplate.builder(sb.toString()).build()
.apply(getCursor(), method.getCoordinates().replaceArguments(), params)
.withComments(createComments(comment));
}
private J.MethodInvocation addFileToPutObject(J.MethodInvocation method, Expression file) {
String v2Method = "#{any()}, RequestBody.fromFile(#{any()})";
return JavaTemplate.builder(v2Method).build()
.apply(getCursor(), method.getCoordinates().replaceArguments(),
method.getArguments().get(0), file);
}
private J.MethodInvocation addInputStreamToPutObject(J.MethodInvocation method, Expression inputStream) {
String metadataName = getMetadataForRequest(method);
Expression contentLen;
if (metadataName == null) {
contentLen = null;
} else {
contentLen = retrieveContentLengthForMetadataIfSet(metadataName);
}
String v2Method;
if (contentLen == null) {
// CHECKSTYLE:OFF: Regexp
v2Method = "#{any()}, RequestBody.fromContentProvider(() -> #{any()}, \"application/octet-stream\")";
// CHECKSTYLE:ON: Regexp
return JavaTemplate.builder(v2Method).build()
.apply(getCursor(), method.getCoordinates().replaceArguments(),
method.getArguments().get(0), inputStream)
.withComments(inputStreamBufferingWarningComment());
}
v2Method = "#{any()}, RequestBody.fromInputStream(#{any()}, #{any()})";
return JavaTemplate.builder(v2Method).build()
.apply(getCursor(), method.getCoordinates().replaceArguments(),
method.getArguments().get(0), inputStream, contentLen);
}
private J.MethodInvocation transformPutObjectWithStreamAndMetadata(J.MethodInvocation method) {
addRequestBodyImport();
addS3Import("PutObjectRequest");
Expression metadata = method.getArguments().get(3);
String metadataName = null;
if (metadata instanceof J.Identifier) {
metadataName = ((J.Identifier) metadata).getSimpleName();
}
StringBuilder sb = new StringBuilder("PutObjectRequest.builder().bucket(#{any()}).key(#{any()})");
Expression contentLen = null;
if (metadataName != null) {
addMetadataFields(sb, metadataName, metadataMap);
contentLen = retrieveContentLengthForMetadataIfSet(metadataName);
}
Expression[] params = {method.getArguments().get(0), method.getArguments().get(1),
method.getArguments().get(2)};
if (contentLen == null) {
// CHECKSTYLE:OFF: Regexp
sb.append(".build(), RequestBody.fromContentProvider(() -> #{any()}, \"application/octet-stream\")");
// CHECKSTYLE:ON: Regexp
return JavaTemplate.builder(sb.toString()).build()
.apply(getCursor(), method.getCoordinates().replaceArguments(), params)
.withComments(inputStreamBufferingWarningComment());
}
sb.append(".build(), RequestBody.fromInputStream(#{any()}, #{any()})");
params = Arrays.copyOf(params, 4);
params[3] = contentLen;
return JavaTemplate.builder(sb.toString()).build()
.apply(getCursor(), method.getCoordinates().replaceArguments(), params);
}
private J.MethodInvocation transformWithMetadata(J.MethodInvocation method) {
String metadataName = getArgumentName(method);
String requestName = retrieveVariableNameIfRequestPojoIsAssigned();
if (requestName == null) {
metadataNameQueue.add(metadataName);
} else {
requestToMetadataNamesMap.put(requestName, metadataName);
}
Map<String, Expression> map = metadataMap.get(metadataName);
if (map == null) {
// should never happen unless user passes in empty ObjectMetadata
// remove metadata setter
return (J.MethodInvocation) method.getSelect();
}
StringBuilder sb = new StringBuilder("#{any()}");
addMetadataFields(sb, metadataName, metadataMap);
return JavaTemplate.builder(sb.toString()).build()
.apply(getCursor(), method.getCoordinates().replace(), method.getSelect());
}
private J convertSetMetadataToBuilder(J.MethodInvocation method) {
String requestName = getSelectName(method);
String metadataName = getArgumentName(method);
requestToMetadataNamesMap.put(requestName, metadataName);
Map<String, Expression> map = metadataMap.get(metadataName);
if (map == null) {
// should never happen unless user passes in empty ObjectMetadata
// remove entire line
return null;
}
StringBuilder sb = new StringBuilder("#{any()} = #{any()}.toBuilder()");
addMetadataFields(sb, metadataName, metadataMap);
sb.append(".build()");
// This class must be JavaVisitor instead of JavaIsoVisitor in order to return a Statement here
return JavaTemplate.builder(sb.toString()).build()
.apply(getCursor(), method.getCoordinates().replace(), method.getSelect(), method.getSelect());
}
private String getMetadataForRequest(J.MethodInvocation method) {
Expression arg = method.getArguments().get(0);
if (arg instanceof J.Identifier) {
String requestName = ((J.Identifier) arg).getSimpleName();
return requestToMetadataNamesMap.get(requestName);
}
return metadataNameQueue.poll();
}
private Expression retrieveContentLengthForMetadataIfSet(String metadataName) {
Map<String, Expression> map = metadataMap.get(metadataName);
if (map == null) {
return null;
}
return map.get("contentLength");
}
private J.MethodInvocation saveMetadataValueAndRemoveStatement(J.MethodInvocation method) {
if (!(method.getSelect() instanceof J.Identifier)) {
return method;
}
J.Identifier metadataPojo = (J.Identifier) method.getSelect();
String variableName = metadataPojo.getSimpleName();
String methodName = method.getSimpleName();
if (!SUPPORTED_METADATA_TRANSFORMS.contains(methodName)) {
String comment = String.format("Transform for ObjectMetadata setter - %s - is not supported, please manually "
+ "migrate the code by setting it on the v2 request/response object.", methodName);
return method.withComments(createComments(comment));
}
Expression value = method.getArguments().get(0);
Map<String, Expression> map = metadataMap.get(variableName);
if (map == null) {
map = new HashMap<>();
}
map.put(methodName, value);
metadataMap.put(variableName, map);
// remove entire line
return null;
}
private J.MethodInvocation transformWithRequesterPays(J.MethodInvocation method) {
Expression expression = method.getArguments().get(0);
if (expression instanceof J.Literal) {
J.Literal literal = (J.Literal) expression;
if (Boolean.TRUE.equals(literal.getValue())) {
addS3Import("RequestPayer");
} else {
// Removes method - there is no enum value for false
return (J.MethodInvocation) method.getSelect();
}
}
return JavaTemplate.builder("RequestPayer.REQUESTER").build()
.apply(getCursor(), method.getCoordinates().replaceArguments());
}
private void addRequestBodyImport() {
String fqcn = "software.amazon.awssdk.core.sync.RequestBody";
doAfterVisit(new AddImport<>(fqcn, null, false));
}
private void addAsyncRequestBodyImport() {
String fqcn = "software.amazon.awssdk.core.async.AsyncRequestBody";
doAfterVisit(new AddImport<>(fqcn, null, false));
}
private void addS3Import(String pojoName) {
String fqcn = V2_S3_MODEL_PKG + pojoName;
doAfterVisit(new AddImport<>(fqcn, null, false));
}
private void addTmImport(String pojoName) {
String fqcn = V2_TM_MODEL_PKG + pojoName;
doAfterVisit(new AddImport<>(fqcn, null, false));
}
}
}