|
33 | 33 | import java.util.ArrayDeque;
|
34 | 34 | import java.util.Arrays;
|
35 | 35 | import java.util.HashMap;
|
| 36 | +import java.util.List; |
36 | 37 | import java.util.Map;
|
37 | 38 | import java.util.Queue;
|
38 | 39 | import java.util.regex.Pattern;
|
|
43 | 44 | import org.openrewrite.java.JavaTemplate;
|
44 | 45 | import org.openrewrite.java.JavaVisitor;
|
45 | 46 | import org.openrewrite.java.MethodMatcher;
|
| 47 | +import org.openrewrite.java.tree.Comment; |
46 | 48 | import org.openrewrite.java.tree.Expression;
|
47 | 49 | import org.openrewrite.java.tree.J;
|
48 | 50 | import org.openrewrite.java.tree.TypeUtils;
|
@@ -290,15 +292,13 @@ private J.MethodInvocation addInputStreamToPutObject(J.MethodInvocation method,
|
290 | 292 | String v2Method;
|
291 | 293 |
|
292 | 294 | if (contentLen == null) {
|
293 |
| - String comment = "When using InputStream to upload with S3Client, Content-Length should be specified and used " |
294 |
| - + "with RequestBody.fromInputStream(). Otherwise, the entire stream will be buffered in memory."; |
295 | 295 | // CHECKSTYLE:OFF: Regexp
|
296 | 296 | v2Method = "#{any()}, RequestBody.fromContentProvider(() -> #{any()}, \"application/octet-stream\")";
|
297 | 297 | // CHECKSTYLE:ON: Regexp
|
298 | 298 | return JavaTemplate.builder(v2Method).build()
|
299 | 299 | .apply(getCursor(), method.getCoordinates().replaceArguments(),
|
300 | 300 | method.getArguments().get(0), inputStream)
|
301 |
| - .withComments(createComments(comment)); |
| 301 | + .withComments(inputStreamBufferingWarningComment()); |
302 | 302 | }
|
303 | 303 |
|
304 | 304 | StringBuilder sb = new StringBuilder("#{any()}, RequestBody.fromInputStream(#{any()}, #{any()}");
|
@@ -327,18 +327,23 @@ private J.MethodInvocation transformPutObjectWithStreamAndMetadata(J.MethodInvoc
|
327 | 327 | method.getArguments().get(2)};
|
328 | 328 |
|
329 | 329 | if (contentLen == null) {
|
330 |
| - sb.append(".build(), RequestBody.fromInputStream(#{any()}, -1L)"); |
331 |
| - } else { |
332 |
| - sb.append(".build(), RequestBody.fromInputStream(#{any()}, #{any()}"); |
| 330 | + // CHECKSTYLE:OFF: Regexp |
| 331 | + sb.append(".build(), RequestBody.fromContentProvider(() -> #{any()}, \"application/octet-stream\")"); |
| 332 | + // CHECKSTYLE:ON: Regexp |
| 333 | + return JavaTemplate.builder(sb.toString()).build() |
| 334 | + .apply(getCursor(), method.getCoordinates().replaceArguments(), params) |
| 335 | + .withComments(inputStreamBufferingWarningComment()); |
| 336 | + } |
333 | 337 |
|
334 |
| - if (contentLen instanceof J.Literal) { |
335 |
| - sb.append("L"); |
336 |
| - } |
337 |
| - sb.append(")"); |
| 338 | + sb.append(".build(), RequestBody.fromInputStream(#{any()}, #{any()}"); |
338 | 339 |
|
339 |
| - params = Arrays.copyOf(params, 4); |
340 |
| - params[3] = contentLen; |
| 340 | + if (contentLen instanceof J.Literal) { |
| 341 | + sb.append("L"); |
341 | 342 | }
|
| 343 | + sb.append(")"); |
| 344 | + |
| 345 | + params = Arrays.copyOf(params, 4); |
| 346 | + params[3] = contentLen; |
342 | 347 |
|
343 | 348 | return JavaTemplate.builder(sb.toString()).build()
|
344 | 349 | .apply(getCursor(), method.getCoordinates().replaceArguments(), params);
|
@@ -401,6 +406,9 @@ private String getMetadataForRequest(J.MethodInvocation method) {
|
401 | 406 |
|
402 | 407 | private Expression retrieveContentLengthForMetadataIfSet(String metadataName) {
|
403 | 408 | Map<String, Expression> map = metadataMap.get(metadataName);
|
| 409 | + if (map == null) { |
| 410 | + return null; |
| 411 | + } |
404 | 412 | return map.get("contentLength");
|
405 | 413 | }
|
406 | 414 |
|
@@ -496,5 +504,11 @@ private void addTmImport(String pojoName) {
|
496 | 504 | String fqcn = V2_TM_MODEL_PKG + pojoName;
|
497 | 505 | doAfterVisit(new AddImport<>(fqcn, null, false));
|
498 | 506 | }
|
| 507 | + |
| 508 | + private List<Comment> inputStreamBufferingWarningComment() { |
| 509 | + String warning = "When using InputStream to upload with S3Client, Content-Length should be specified and used " |
| 510 | + + "with RequestBody.fromInputStream(). Otherwise, the entire stream will be buffered in memory."; |
| 511 | + return createComments(warning); |
| 512 | + } |
499 | 513 | }
|
500 | 514 | }
|
0 commit comments