|
16 | 16 | package software.amazon.awssdk.v2migration;
|
17 | 17 |
|
18 | 18 | import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_MODEL_PKG;
|
| 19 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_PKG; |
| 20 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.assignedVariableHttpMethodNotSupportedComment; |
| 21 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.httpMethodNotSupportedComment; |
| 22 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isCompleteMpuRequestMultipartUploadSetter; |
| 23 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isGeneratePresignedUrl; |
| 24 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.isUnsupportedHttpMethod; |
| 25 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.presignerSingleInstanceSuggestion; |
| 26 | +import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.requestPojoTransformNotSupportedComment; |
19 | 27 | import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.v2S3MethodMatcher;
|
20 |
| -import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.fullyQualified; |
21 | 28 |
|
| 29 | +import java.util.List; |
| 30 | +import java.util.Locale; |
22 | 31 | import org.openrewrite.ExecutionContext;
|
23 | 32 | import org.openrewrite.Recipe;
|
24 | 33 | import org.openrewrite.TreeVisitor;
|
25 | 34 | import org.openrewrite.java.AddImport;
|
26 | 35 | import org.openrewrite.java.JavaIsoVisitor;
|
27 | 36 | import org.openrewrite.java.JavaTemplate;
|
28 | 37 | import org.openrewrite.java.MethodMatcher;
|
| 38 | +import org.openrewrite.java.RemoveImport; |
| 39 | +import org.openrewrite.java.tree.Expression; |
29 | 40 | import org.openrewrite.java.tree.J;
|
30 |
| -import org.openrewrite.java.tree.JavaType; |
31 |
| -import org.openrewrite.java.tree.TypeUtils; |
32 | 41 | import software.amazon.awssdk.annotations.SdkInternalApi;
|
33 | 42 |
|
34 | 43 | /**
|
@@ -74,58 +83,100 @@ private static final class Visitor extends JavaIsoVisitor<ExecutionContext> {
|
74 | 83 | public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
|
75 | 84 |
|
76 | 85 | if (isCompleteMpuRequestMultipartUploadSetter(method)) {
|
77 |
| - method = transformCompleteMpuRequestCompletedPartsArg(method); |
78 |
| - return super.visitMethodInvocation(method, executionContext); |
| 86 | + return transformCompleteMpuRequestCompletedPartsArg(method); |
| 87 | + } |
| 88 | + if (isGeneratePresignedUrl(method)) { |
| 89 | + return maybeAutoFormat(method, transformGeneratePresignedUrl(method), executionContext); |
79 | 90 | }
|
80 |
| - |
81 | 91 | if (DISABLE_REQUESTER_PAYS.matches(method, false)) {
|
82 |
| - method = transformSetRequesterPays(method, false); |
83 |
| - return super.visitMethodInvocation(method, executionContext); |
| 92 | + return transformSetRequesterPays(method, false); |
84 | 93 | }
|
85 | 94 | if (ENABLE_REQUESTER_PAYS.matches(method, false)) {
|
86 |
| - method = transformSetRequesterPays(method, true); |
87 |
| - return super.visitMethodInvocation(method, executionContext); |
| 95 | + return transformSetRequesterPays(method, true); |
88 | 96 | }
|
89 | 97 | if (IS_REQUESTER_PAYS_ENABLED.matches(method, false)) {
|
90 |
| - method = transformIsRequesterPays(method); |
91 |
| - return super.visitMethodInvocation(method, executionContext); |
| 98 | + return transformIsRequesterPays(method); |
92 | 99 | }
|
93 | 100 | if (GET_OBJECT_AS_STRING.matches(method, false)) {
|
94 |
| - method = transformGetObjectAsString(method); |
95 |
| - return super.visitMethodInvocation(method, executionContext); |
| 101 | + return transformGetObjectAsString(method); |
96 | 102 | }
|
97 | 103 | if (GET_URL.matches(method, false)) {
|
98 |
| - method = transformGetUrl(method); |
99 |
| - return super.visitMethodInvocation(method, executionContext); |
| 104 | + return transformGetUrl(method); |
100 | 105 | }
|
101 | 106 | if (LIST_BUCKETS.matches(method, false)) {
|
102 |
| - method = transformListBuckets(method); |
103 |
| - return super.visitMethodInvocation(method, executionContext); |
| 107 | + return transformListBuckets(method); |
104 | 108 | }
|
105 | 109 | if (RESTORE_OBJECT.matches(method, false)) {
|
106 |
| - method = transformRestoreObject(method); |
107 |
| - return super.visitMethodInvocation(method, executionContext); |
| 110 | + return transformRestoreObject(method); |
108 | 111 | }
|
109 | 112 | if (SET_OBJECT_REDIRECT_LOCATION.matches(method, false)) {
|
110 |
| - method = transformSetObjectRedirectLocation(method); |
111 |
| - return super.visitMethodInvocation(method, executionContext); |
| 113 | + return transformSetObjectRedirectLocation(method); |
112 | 114 | }
|
113 | 115 | if (CHANGE_OBJECT_STORAGE_CLASS.matches(method, false)) {
|
114 |
| - method = transformChangeObjectStorageClass(method); |
115 |
| - return super.visitMethodInvocation(method, executionContext); |
| 116 | + return transformChangeObjectStorageClass(method); |
116 | 117 | }
|
117 | 118 | if (CREATE_BUCKET.matches(method, false)) {
|
118 |
| - method = transformCreateBucket(method); |
119 |
| - return super.visitMethodInvocation(method, executionContext); |
| 119 | + return transformCreateBucket(method); |
120 | 120 | }
|
121 | 121 | return super.visitMethodInvocation(method, executionContext);
|
122 | 122 | }
|
123 | 123 |
|
124 |
| - private boolean isCompleteMpuRequestMultipartUploadSetter(J.MethodInvocation method) { |
125 |
| - JavaType.FullyQualified completeMpuRequest = |
126 |
| - fullyQualified("software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest.Builder"); |
127 |
| - return "multipartUpload".equals(method.getSimpleName()) && |
128 |
| - TypeUtils.isAssignableTo(completeMpuRequest, method.getSelect().getType()); |
| 124 | + private J.MethodInvocation transformGeneratePresignedUrl(J.MethodInvocation method) { |
| 125 | + List<Expression> args = method.getArguments(); |
| 126 | + if (args.size() == 1) { |
| 127 | + return method.withComments(requestPojoTransformNotSupportedComment()); |
| 128 | + } |
| 129 | + |
| 130 | + String httpMethod = determineHttpMethod(args); |
| 131 | + |
| 132 | + if (isUnsupportedHttpMethod(httpMethod)) { |
| 133 | + return method.withComments(httpMethodNotSupportedComment(httpMethod)); |
| 134 | + } |
| 135 | + if (httpMethod == null) { |
| 136 | + return method.withComments(assignedVariableHttpMethodNotSupportedComment()); |
| 137 | + } |
| 138 | + |
| 139 | + String v2Method = String.format("S3Presigner.builder().s3Client(#{any()}).build()%n" |
| 140 | + + ".presign%sObject(p -> p.%sObjectRequest(r -> r.bucket(#{any()}).key(#{any()}))%n" |
| 141 | + + ".signatureDuration(Duration.between(Instant.now(), #{any()}.toInstant())))%n" |
| 142 | + + ".url()", |
| 143 | + httpMethod, httpMethod.toLowerCase(Locale.ROOT)); |
| 144 | + |
| 145 | + removeV1HttpMethodImport(); |
| 146 | + addInstantImport(); |
| 147 | + addDurationImport(); |
| 148 | + addS3PresignerImport(); |
| 149 | + |
| 150 | + return JavaTemplate.builder(v2Method).build() |
| 151 | + .apply(getCursor(), method.getCoordinates().replace(), method.getSelect(), |
| 152 | + args.get(0), args.get(1), args.get(2)) |
| 153 | + .withComments(presignerSingleInstanceSuggestion()); |
| 154 | + } |
| 155 | + |
| 156 | + private String determineHttpMethod(List<Expression> args) { |
| 157 | + if (args.size() == 3) { |
| 158 | + return "Get"; |
| 159 | + } |
| 160 | + Expression argVal = args.get(3); |
| 161 | + String httpMethod = argVal.printTrimmed(getCursor()); |
| 162 | + |
| 163 | + switch (httpMethod) { |
| 164 | + case "HttpMethod.GET": |
| 165 | + return "Get"; |
| 166 | + case "HttpMethod.PUT": |
| 167 | + return "Put"; |
| 168 | + case "HttpMethod.DELETE": |
| 169 | + return "Delete"; |
| 170 | + case "HttpMethod.HEAD": |
| 171 | + return "Head"; |
| 172 | + case "HttpMethod.POST": |
| 173 | + return "Post"; |
| 174 | + case "HttpMethod.PATCH": |
| 175 | + return "Patch"; |
| 176 | + default: |
| 177 | + // enum value assigned to variable |
| 178 | + return null; |
| 179 | + } |
129 | 180 | }
|
130 | 181 |
|
131 | 182 | private J.MethodInvocation transformCompleteMpuRequestCompletedPartsArg(J.MethodInvocation method) {
|
@@ -251,6 +302,25 @@ private J.MethodInvocation transformSetRequesterPays(J.MethodInvocation method,
|
251 | 302 | return method;
|
252 | 303 | }
|
253 | 304 |
|
| 305 | + private void removeV1HttpMethodImport() { |
| 306 | + doAfterVisit(new RemoveImport<>("com.amazonaws.HttpMethod", true)); |
| 307 | + } |
| 308 | + |
| 309 | + private void addInstantImport() { |
| 310 | + String fqcn = "java.time.Instant"; |
| 311 | + doAfterVisit(new AddImport<>(fqcn, null, false)); |
| 312 | + } |
| 313 | + |
| 314 | + private void addDurationImport() { |
| 315 | + String fqcn = "java.time.Duration"; |
| 316 | + doAfterVisit(new AddImport<>(fqcn, null, false)); |
| 317 | + } |
| 318 | + |
| 319 | + private void addS3PresignerImport() { |
| 320 | + String fqcn = V2_S3_PKG + "presigner.S3Presigner"; |
| 321 | + doAfterVisit(new AddImport<>(fqcn, null, false)); |
| 322 | + } |
| 323 | + |
254 | 324 | private void addImport(String pojoName) {
|
255 | 325 | String fqcn = V2_S3_MODEL_PKG + pojoName;
|
256 | 326 | doAfterVisit(new AddImport<>(fqcn, null, false));
|
|
0 commit comments