Skip to content

Transforming S3 PUT override #5411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@

package foo.bar;

import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;
import software.amazon.awssdk.services.s3.model.S3Object;
import software.amazon.awssdk.services.sqs.SqsClient;
import software.amazon.awssdk.services.sqs.model.QueueDoesNotExistException;
Expand All @@ -32,6 +33,8 @@
import java.nio.file.StandardCopyOption;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;

public class Application {
Expand Down Expand Up @@ -90,4 +93,11 @@ private static Path downloadFile(S3Client s3, String bucket, String key, Path ds

return dst;
}

private static PutObjectResponse uploadFile(S3Client s3, String bucket, String key, Path source) throws IOException {
PutObjectResponse result = s3.putObject(PutObjectRequest.builder()
.build(), RequestBody.fromFile(source.toFile()));

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public static void main(String... args) {
System.out.println(qan);
System.out.println(qan2);

ReceiveMessageRequest v1Request = ReceiveMessageRequest.builder().build();
ReceiveMessageRequest v1Request = ReceiveMessageRequest.builder()
.build();
List<String> attributes = v1Request.attributeNamesAsStrings();
System.out.println(attributes);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.model.AmazonSQSException;
Expand Down Expand Up @@ -86,4 +88,10 @@ private static Path downloadFile(AmazonS3 s3, String bucket, String key, Path ds

return dst;
}

private static PutObjectResult uploadFile(AmazonS3 s3, String bucket, String key, Path source) throws IOException {
PutObjectResult result = s3.putObject(bucket, key, source.toFile());

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ public J.Identifier visitIdentifier(J.Identifier identifier, ExecutionContext ct
J.Identifier id = super.visitIdentifier(identifier, ctx);

if (ENUMS.contains(id.getSimpleName())) {
return id.withFieldType(id.getFieldType().withName(id.getSimpleName()));
JavaType.Variable fieldType = id.getFieldType();
if (fieldType == null) {
return id;
}
JavaType.Variable variable = fieldType.withName(id.getSimpleName());
return id.withFieldType(variable);
}

return id;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* 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 java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JContainer;
import org.openrewrite.java.tree.JRightPadded;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.marker.Markers;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.v2migration.internal.utils.IdentifierUtils;

@SdkInternalApi
public class S3StreamingRequestToV2 extends Recipe {
private static final MethodMatcher PUT_OBJECT =
new MethodMatcher("com.amazonaws.services.s3.AmazonS3 "
+ "putObject(java.lang.String, java.lang.String, java.io.File)",
true);
private static final JavaType.FullyQualified V1_PUT_OBJECT_REQUEST =
TypeUtils.asFullyQualified(JavaType.buildType("com.amazonaws.services.s3.model.PutObjectRequest"));
private static final JavaType.FullyQualified REQUEST_BODY =
TypeUtils.asFullyQualified(JavaType.buildType("software.amazon.awssdk.core.sync.RequestBody"));

@Override
public String getDisplayName() {
return "S3StreamingRequestToV2";
}

@Override
public String getDescription() {
return "S3StreamingRequestToV2.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new Visitor();
}

private static final class Visitor extends JavaIsoVisitor<ExecutionContext> {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
if (PUT_OBJECT.matches(method, false)) {
method = transformPutFileOverload(method);
}
return super.visitMethodInvocation(method, executionContext);
}

private J.MethodInvocation transformPutFileOverload(J.MethodInvocation method) {
JavaType.Method methodType = method.getMethodType();
if (methodType == null) {
return method;
}

List<Expression> originalArgs = method.getArguments();

Expression bucketExpr = originalArgs.get(0);
Expression keyExpr = originalArgs.get(1);
Expression fileExpr = originalArgs.get(2);

List<Expression> newArgs = new ArrayList<>();
Expression getObjectExpr = bucketAndKeyToPutObject(bucketExpr, keyExpr);
newArgs.add(getObjectExpr);

Space fileArgPrefix = fileExpr.getPrefix();
fileExpr = fileToRequestBody(fileExpr.withPrefix(Space.EMPTY)).withPrefix(fileArgPrefix);
newArgs.add(fileExpr);

List<String> paramNames = Arrays.asList("request", "file");
List<JavaType> paramTypes = newArgs.stream()
.map(Expression::getType)
.collect(Collectors.toList());


methodType = methodType.withParameterTypes(paramTypes)
.withParameterNames(paramNames);

return method.withMethodType(methodType).withArguments(newArgs);
}

private J.MethodInvocation fileToRequestBody(Expression fileExpr) {
maybeAddImport(REQUEST_BODY);

J.Identifier requestBodyId = IdentifierUtils.makeId(REQUEST_BODY.getClassName(), REQUEST_BODY);

JavaType.Method fromFileType = new JavaType.Method(
null,
0L,
REQUEST_BODY,
"fromFile",
REQUEST_BODY,
Collections.singletonList("file"),
Collections.singletonList(JavaType.buildType("java.io.File")),
null,
null
);

J.Identifier fromFileId = IdentifierUtils.makeId("fromFile", fromFileType);

return new J.MethodInvocation(
Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
JRightPadded.build(requestBodyId),
null,
fromFileId,
JContainer.build(Collections.singletonList(JRightPadded.build(fileExpr))),
fromFileType
);
}

private Expression bucketAndKeyToPutObject(Expression bucketExpr, Expression keyExpr) {
maybeAddImport(V1_PUT_OBJECT_REQUEST);

J.Identifier putObjRequestId = IdentifierUtils.makeId(V1_PUT_OBJECT_REQUEST.getClassName(), V1_PUT_OBJECT_REQUEST);

JavaType.Method ctorType = new JavaType.Method(
null,
0L,
V1_PUT_OBJECT_REQUEST,
"<init>",
V1_PUT_OBJECT_REQUEST,
Arrays.asList("bucket", "key"),
Arrays.asList(bucketExpr.getType(), keyExpr.getType()),
null,
null
);

return new J.NewClass(
Tree.randomId(),
Space.EMPTY,
Markers.EMPTY,
null,
Space.EMPTY,
putObjRequestId.withPrefix(Space.SINGLE_SPACE),
JContainer.build(
Arrays.asList(
JRightPadded.build(bucketExpr),
JRightPadded.build(keyExpr)
)
),
null,
ctorType
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ tags:
- sdk
recipeList:
- software.amazon.awssdk.v2migration.UpgradeSdkDependencies
- software.amazon.awssdk.v2migration.S3GetObjectConstructorToFluent
- software.amazon.awssdk.v2migration.S3StreamingResponseToV2
- software.amazon.awssdk.v2migration.S3StreamingRequestToV2
- software.amazon.awssdk.v2migration.S3GetObjectConstructorToFluent
- software.amazon.awssdk.v2migration.S3PutObjectConstructorToFluent
- software.amazon.awssdk.v2migration.EnumGettersToV2
- software.amazon.awssdk.v2migration.ChangeSdkType
- software.amazon.awssdk.v2migration.ChangeSdkCoreTypes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# 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.
#
---
type: specs.openrewrite.org/v1beta/recipe
name: software.amazon.awssdk.S3PutObjectConstructorToFluent
displayName: Change PutObject constructors to fluent builder calls
recipeList:
- software.amazon.awssdk.migration.internal.recipe.ConstructorToFluent:
clzzFqcn: com.amazonaws.services.s3.model.PutObjectRequest
parameterTypes:
- java.lang.String
- java.lang.String
fluentNames:
- withBucket
- withKey
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ void shouldChangeSingleItemEnumGetter() {
" String routeType = phoneNumberInformation.getRouteType();\n" +
" }\n" +
"}\n",
"import software.amazon.awssdk.services.sns.model.PhoneNumberInformation;\n" +
"\n" +
"class Test {\n" +
" static void method() {\n" +
" PhoneNumberInformation phoneNumberInformation = PhoneNumberInformation.builder().build();\n" +
" String routeType = phoneNumberInformation.routeTypeAsString();\n" +
" }\n" +
"}\n"
"import software.amazon.awssdk.services.sns.model.PhoneNumberInformation;\n"
+ "\n"
+ "class Test {\n"
+ " static void method() {\n"
+ " PhoneNumberInformation phoneNumberInformation = PhoneNumberInformation.builder()\n"
+ " .build();\n"
+ " String routeType = phoneNumberInformation.routeTypeAsString();\n"
+ " }\n"
+ "}"
)
);
}
Expand All @@ -87,14 +88,15 @@ void shouldChangeCollectionItemEnumGetter() {
" List<String> numberCapabilities = phoneNumberInformation.getNumberCapabilities();\n" +
" }\n" +
"}\n",
"import software.amazon.awssdk.services.sns.model.PhoneNumberInformation;\n" +
"\n" +
"class Test {\n" +
" static void method() {\n" +
" PhoneNumberInformation phoneNumberInformation = PhoneNumberInformation.builder().build();\n" +
" List<String> numberCapabilities = phoneNumberInformation.numberCapabilitiesAsStrings();\n" +
" }\n" +
"}\n"
"import software.amazon.awssdk.services.sns.model.PhoneNumberInformation;\n"
+ "\n"
+ "class Test {\n"
+ " static void method() {\n"
+ " PhoneNumberInformation phoneNumberInformation = PhoneNumberInformation.builder()\n"
+ " .build();\n"
+ " List<String> numberCapabilities = phoneNumberInformation.numberCapabilitiesAsStrings();\n"
+ " }\n"
+ "}"
)
);
}
Expand Down
Loading
Loading