Skip to content
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,13 @@

package foo.bar;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.S3Uri;
import software.amazon.awssdk.services.s3.S3Utilities;
import software.amazon.awssdk.services.s3.model.AbortMultipartUploadRequest;
import software.amazon.awssdk.services.s3.model.AccelerateConfiguration;
import software.amazon.awssdk.services.s3.model.AnalyticsConfiguration;
Expand Down Expand Up @@ -384,4 +388,18 @@ private void setBucketNameTest(S3Client s3, String bucket) {
GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(bucket).key("key").bucket(bucket)
.build();
}

private void s3Uri(URI uri, String uriAsString) {
S3Uri s3Uri = S3Utilities.builder().build().parseUri(uri);

String versionId = s3Uri.firstMatchingRawQueryParameter("versionId").orElse(null);
String bucket = s3Uri.bucket().orElse(null);
String key = s3Uri.key().orElse(null);
String region = s3Uri.region().map(Region::id).orElse(null);
boolean isPathStyle = s3Uri.isPathStyle();

S3Uri s3UriFromString = /*AWS SDK for Java v2 migration: v2 S3Uri does not URL-encode a String URI. If you relied on this functionality in v1 you must update your code to manually encode the String.*/S3Utilities.builder().build().parseUri(URI.create(uriAsString));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Comment is kind of in a weird spot, is that what we do elsewhere for similar warning in migration tool?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah Dongie had same concern. We can't maintain the spacing if put on separate line though. Also for this specific transform can't do it on the line before since we transform the left and right hand sides separately. We could possibly do it on the next line. On the other hand, having it here makes it really obvious to the user

We do the same for other comments fwiw
https://github.com/aws/aws-sdk-java-v2/blob/master/v2-migration/src/test/java/software/amazon/awssdk/v2migration/AddCommentToMethodTest.java#L51

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's great that it is really obvious, just a bit weird. I don't mind it.


S3Uri s3UriFromStringWithUrlEncodeFalse = S3Utilities.builder().build().parseUri(URI.create(uriAsString));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package foo.bar;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3URI;
import com.amazonaws.services.s3.model.AbortMultipartUploadRequest;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.BucketAccelerateConfiguration;
Expand Down Expand Up @@ -57,6 +58,7 @@
import com.amazonaws.services.s3.model.inventory.InventoryConfiguration;
import com.amazonaws.services.s3.model.metrics.MetricsConfiguration;
import com.amazonaws.services.s3.model.ownership.OwnershipControls;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -243,4 +245,18 @@ private void setBucketConfigs(AmazonS3 s3, String bucket) {
private void setBucketNameTest(AmazonS3 s3, String bucket) {
GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, "key").withBucketName(bucket);
}

private void s3Uri(URI uri, String uriAsString) {
AmazonS3URI s3Uri = new AmazonS3URI(uri);

String versionId = s3Uri.getVersionId();
String bucket = s3Uri.getBucket();
String key = s3Uri.getKey();
String region = s3Uri.getRegion();
boolean isPathStyle = s3Uri.isPathStyle();

AmazonS3URI s3UriFromString = new AmazonS3URI(uriAsString);

AmazonS3URI s3UriFromStringWithUrlEncodeFalse = new AmazonS3URI(uriAsString, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/*
* 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.V1_S3_PKG;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.V2_S3_PKG;
import static software.amazon.awssdk.v2migration.internal.utils.S3TransformUtils.createComments;
import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.isStringType;
import static software.amazon.awssdk.v2migration.internal.utils.SdkTypeUtils.isUriType;

import java.util.List;
import java.util.regex.Pattern;
import org.openrewrite.ExecutionContext;
import org.openrewrite.NlsRewrite;
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.RemoveImport;
import org.openrewrite.java.tree.Comment;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeTree;
import software.amazon.awssdk.annotations.SdkInternalApi;

@SdkInternalApi
public class S3UriToV2 extends Recipe {

private static final Pattern V1_AMAZON_S3_URI = Pattern.compile(V1_S3_PKG + "AmazonS3URI");
private static final String V2_S3_URI = V2_S3_PKG + "S3Uri";

@Override
public @NlsRewrite.DisplayName String getDisplayName() {
return "Convert v1 AmazonS3URI to v2 S3Uri";
}

@Override
public @NlsRewrite.Description String getDescription() {
return "Convert v1 AmazonS3URI to v2 S3Uri";
}

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

private static final class Visitor extends JavaVisitor<ExecutionContext> {

@Override
public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations variableDecls, ExecutionContext ctx) {
J.VariableDeclarations varDec = (J.VariableDeclarations) super.visitVariableDeclarations(variableDecls, ctx);

JavaType type = varDec.getType();
if (type == null) {
return varDec;
}

if (!type.isAssignableFrom(V1_AMAZON_S3_URI)) {
return varDec;
}

addV2S3UriImport();
removeV1S3UriImport();
JavaType v2Type = JavaType.buildType(V2_S3_URI);
TypeTree v2TypeTree = TypeTree.build("S3Uri");
return varDec.withType(v2Type).withTypeExpression(v2TypeTree);
}

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
method = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
JavaType.Method methodType = method.getMethodType();

if (methodType == null) {
return method;
}

Expression select = method.getSelect();
if (select == null) {
return method;
}
JavaType selectType = select.getType();
if (selectType == null || !selectType.isAssignableFrom(V1_AMAZON_S3_URI)) {
return method;
}

String methodName = method.getSimpleName();
String v2Method;

switch (methodName) {
case "getVersionId":
v2Method = "#{any()}.firstMatchingRawQueryParameter(\"versionId\").orElse(null)";
break;
case "getBucket":
v2Method = "#{any()}.bucket().orElse(null)";
break;
case "getKey":
v2Method = "#{any()}.key().orElse(null)";
break;
case "getRegion":
v2Method = "#{any()}.region().map(Region::id).orElse(null)";
addV2RegionImport();
break;
default:
return method;
}


return JavaTemplate.builder(v2Method).build().apply(getCursor(), method.getCoordinates().replace(),
method.getSelect());
}

@Override
public J visitNewClass(J.NewClass previousNewClass, ExecutionContext ctx) {
J.NewClass newClass = super.visitNewClass(previousNewClass, ctx).cast();

JavaType type = newClass.getType();
if (!(type instanceof JavaType.FullyQualified) || !type.isAssignableFrom(V1_AMAZON_S3_URI)) {
return newClass;
}

List<Expression> args = newClass.getArguments();
Expression uriArg = args.get(0);
JavaType uriType = uriArg.getType();
if (uriType == null) {
return newClass;
}

StringBuilder sb = new StringBuilder("S3Utilities.builder().build().parseUri(");

if (isUriType(uriType)) {
sb.append("#{any()})");
} else if (isStringType(uriType)) {
sb.append("URI.create(#{any()})");
addJavaUriImport();
}

J.MethodInvocation newMethod = JavaTemplate.builder(sb.toString()).build()
.apply(getCursor(), newClass.getCoordinates().replace(), uriArg);

if (shouldAddWarning(uriType, args)) {
newMethod = newMethod.withComments(v2DoesNotEncodeWarning());
}

removeV1S3UriImport();
addS3UtilitiesImport();
return newMethod;
}

private boolean shouldAddWarning(JavaType uriType, List<Expression> args) {
if (isUriType(uriType)) {
return false;
}
if (args.size() == 1) {
return true;
}
return !urlEncodeArgIsLiteralFalse(args.get(1));
}

private boolean urlEncodeArgIsLiteralFalse(Expression arg) {
if (arg instanceof J.Literal) {
J.Literal literal = (J.Literal) arg;
return literal.getValue().equals(Boolean.FALSE);
}
return false;
}


private List<Comment> v2DoesNotEncodeWarning() {
String warning = "v2 S3Uri does not URL-encode a String URI. If you relied on this functionality in v1 you must "
+ "update your code to manually encode the String.";
return createComments(warning);
}

private void removeV1S3UriImport() {
doAfterVisit(new RemoveImport<>(V1_AMAZON_S3_URI.toString(), true));
}

private void addV2S3UriImport() {
doAfterVisit(new AddImport<>(V2_S3_URI, null, false));
}

private void addS3UtilitiesImport() {
String fqcn = V2_S3_PKG + "S3Utilities";
doAfterVisit(new AddImport<>(fqcn, null, false));
}

private void addV2RegionImport() {
String fqcn = "software.amazon.awssdk.regions.Region";
doAfterVisit(new AddImport<>(fqcn, null, false));
}

private void addJavaUriImport() {
String fqcn = "java.net.URI";
doAfterVisit(new AddImport<>(fqcn, null, false));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@

package software.amazon.awssdk.v2migration.internal.utils;

import java.util.Collections;
import java.util.List;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Comment;
import org.openrewrite.java.tree.TextComment;
import org.openrewrite.marker.Markers;
import software.amazon.awssdk.annotations.SdkInternalApi;

@SdkInternalApi
public final class S3TransformUtils {

public static final String V1_S3_CLIENT = "com.amazonaws.services.s3.AmazonS3";
public static final String V1_S3_MODEL_PKG = "com.amazonaws.services.s3.model.";
public static final String V1_S3_PKG = "com.amazonaws.services.s3.";

public static final String V2_S3_CLIENT = "software.amazon.awssdk.services.s3.S3Client";
public static final String V2_S3_MODEL_PKG = "software.amazon.awssdk.services.s3.model.";
public static final String V2_S3_PKG = "software.amazon.awssdk.services.s3.";

public static final String V2_TM_CLIENT = "software.amazon.awssdk.transfer.s3.S3TransferManager";
public static final String V2_TM_MODEL_PKG = "software.amazon.awssdk.transfer.s3.model.";
Expand All @@ -45,4 +52,9 @@ public static MethodMatcher v2S3MethodMatcher(String methodSignature) {
public static MethodMatcher v2TmMethodMatcher(String methodSignature) {
return new MethodMatcher(V2_TM_CLIENT + " " + methodSignature, true);
}

public static List<Comment> createComments(String comment) {
return Collections.singletonList(
new TextComment(true, "AWS SDK for Java v2 migration: " + comment, "", Markers.EMPTY));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -671,4 +671,12 @@ public static boolean isInputStreamType(JavaType type) {
public static boolean isFileType(JavaType type) {
return TypeUtils.isAssignableTo("java.io.File", type);
}

public static boolean isStringType(JavaType type) {
return TypeUtils.isAssignableTo("java.lang.String", type);
}

public static boolean isUriType(JavaType type) {
return TypeUtils.isAssignableTo("java.net.URI", type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ recipeList:
- software.amazon.awssdk.v2migration.S3NonStreamingRequestToV2
- software.amazon.awssdk.v2migration.S3MethodsToV2
- software.amazon.awssdk.v2migration.S3MethodsConstructorToFluent
- software.amazon.awssdk.v2migration.S3UriToV2
- software.amazon.awssdk.v2migration.EnumGettersToV2
- software.amazon.awssdk.v2migration.ChangeTransferManagerTypes
- software.amazon.awssdk.v2migration.ChangeSdkType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ recipeList:
- software.amazon.awssdk.v2migration.S3NonStreamingRequestToV2
- software.amazon.awssdk.v2migration.S3MethodsToV2
- software.amazon.awssdk.v2migration.S3MethodsConstructorToFluent
- software.amazon.awssdk.v2migration.S3UriToV2
- software.amazon.awssdk.v2migration.EnumGettersToV2
- software.amazon.awssdk.v2migration.ChangeSdkType
- software.amazon.awssdk.v2migration.ChangeSdkCoreTypes
Expand Down
Loading