Skip to content
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

Added Migrate DefaultHttpClient & some cookie constants #401

Merged
merged 8 commits into from
Aug 2, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 org.openrewrite.java.apache.httpclient4;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.*;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;

public class MigrateDefaultHttpClient extends Recipe {
@Override
public String getDisplayName() {
return "Migrates deprecated `DefaultHttpClient`";
}

@Override
public String getDescription() {
return "Since DefaultHttpClient is deprecated, we need to change it to the CloseableHttpClient. " +
"Only covering the default scenario with no custom HttpParams or ConnectionManager.";
joanvr marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>("org.apache.http.impl.client.DefaultHttpClient", false), new JavaVisitor<ExecutionContext>() {
final MethodMatcher noArgsMatcher = new MethodMatcher("org.apache.http.impl.client.DefaultHttpClient <constructor>()");
final JavaTemplate noArgsTemplate = JavaTemplate.builder("HttpClients.createDefault()")
.javaParser(JavaParser.fromJavaVersion().classpath("httpclient"))
.imports("org.apache.http.impl.client.HttpClients")
.build();

@Override
public J visitNewClass(J.NewClass newClass, ExecutionContext executionContext) {
if (noArgsMatcher.matches(newClass.getConstructorType())) {
maybeAddImport("org.apache.http.impl.client.HttpClients");
doAfterVisit(new ChangeType(
"org.apache.http.impl.client.DefaultHttpClient",
"org.apache.http.impl.client.CloseableHttpClient", true
).getVisitor());
return noArgsTemplate.apply(getCursor(), newClass.getCoordinates().replace());
}
return super.visitNewClass(newClass, executionContext);
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.
*/
@NonNullApi @NonNullFields
package org.openrewrite.java.apache.httpclient4;

import org.openrewrite.internal.lang.NonNullApi;
import org.openrewrite.internal.lang.NonNullFields;
53 changes: 53 additions & 0 deletions src/main/resources/META-INF/rewrite/apache-httpclient-4-5.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#
# Copyright 2023 the original author or authors.
# <p>
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# <p>
# https://www.apache.org/licenses/LICENSE-2.0
# <p>
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.
#

########################################################################################################################
# Apache HttpClient 4.5.x
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.apache.httpclient4.UpgradeApacheHttpClient_4_5
displayName: Migrates to ApacheHttpClient 4.5.x
description: >
Migrate applications to the latest Apache HttpClient 4.5.x release. This recipe will modify an
joanvr marked this conversation as resolved.
Show resolved Hide resolved
application's build files, make changes to deprecated/preferred APIs, and migrate configuration settings that have
joanvr marked this conversation as resolved.
Show resolved Hide resolved
changes between versions.
tags:
- apache
- httpclient
recipeList:
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
groupId: org.apache.httpcomponents
artifactId: httpclient
newVersion: 4.5.x
overrideManagedVersion: true
joanvr marked this conversation as resolved.
Show resolved Hide resolved
- org.openrewrite.java.apache.httpclient4.MappingDeprecatedClasses
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.apache.httpclient4.MappingDeprecatedClasses
displayName: Maps deprecated classes from ApacheHttpClient 4.5.x to Newer ones
joanvr marked this conversation as resolved.
Show resolved Hide resolved
description: Uses new classes/methods instead of the deprecated ones
recipeList:
- org.openrewrite.java.ReplaceConstantWithAnotherConstant:
existingFullyQualifiedConstantName: org.apache.http.client.params.CookiePolicy.RFC_2109
fullyQualifiedConstantName: org.apache.http.client.config.CookieSpecs.STANDARD
- org.openrewrite.java.ReplaceConstantWithAnotherConstant:
existingFullyQualifiedConstantName: org.apache.http.client.params.CookiePolicy.RFC_2965
fullyQualifiedConstantName: org.apache.http.client.config.CookieSpecs.STANDARD_STRICT
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.http.client.params.CookiePolicy
newFullyQualifiedTypeName: org.apache.http.client.config.CookieSpecs
- org.openrewrite.java.apache.httpclient4.MigrateDefaultHttpClient

19 changes: 18 additions & 1 deletion src/main/resources/META-INF/rewrite/apache-httpclient-5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tags:
- apache
- httpclient
recipeList:
- org.openrewrite.java.apache.httpclient4.UpgradeApacheHttpClient_4_5
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.apache.httpcomponents
oldArtifactId: httpclient
Expand All @@ -40,7 +41,6 @@ recipeList:
- org.openrewrite.java.apache.httpclient5.UseTimeout
- org.openrewrite.java.apache.httpclient5.UseTimeValue
- org.openrewrite.java.apache.httpclient5.StatusLine

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.apache.httpclient5.UpgradeApacheHttpClient_5_ClassMapping
Expand Down Expand Up @@ -132,19 +132,33 @@ recipeList:
- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.http.auth
newPackageName: org.apache.hc.client5.http.auth

- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.http.cookie
newPackageName: org.apache.hc.client5.http.cookie

- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.http.cookie
newPackageName: org.apache.hc.client5.http.cookie

- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.http.annotation
newPackageName: org.apache.hc.core5.annotation

- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.http.client.config
newPackageName: org.apache.hc.client5.http.config
# Fixing deprecated constants
- org.openrewrite.java.ReplaceConstantWithAnotherConstant:
existingFullyQualifiedConstantName: org.apache.hc.client5.http.config.CookieSpecs.IGNORE_COOKIES
fullyQualifiedConstantName: org.apache.hc.client5.http.cookie.StandardCookieSpec.IGNORE
- org.openrewrite.java.ReplaceConstantWithAnotherConstant:
existingFullyQualifiedConstantName: org.apache.hc.client5.http.config.CookieSpecs.STANDARD
fullyQualifiedConstantName: org.apache.hc.client5.http.cookie.StandardCookieSpec.RELAXED
- org.openrewrite.java.ReplaceConstantWithAnotherConstant:
existingFullyQualifiedConstantName: org.apache.hc.client5.http.config.CookieSpecs.STANDARD_STRICT
fullyQualifiedConstantName: org.apache.hc.client5.http.cookie.StandardCookieSpec.STRICT

- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.http.annotation
newPackageName: org.apache.hc.core5.annotation
Expand Down Expand Up @@ -183,9 +197,11 @@ recipeList:
- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.http.io
newPackageName: org.apache.hc.core5.http.io

- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.http.message
newPackageName: org.apache.hc.core5.http.message

- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.http.pool
newPackageName: org.apache.hc.core5.pool
Expand Down Expand Up @@ -255,6 +271,7 @@ recipeList:
- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.http.config
newPackageName: org.apache.hc.core5.http.config
# Fixing specific mappings
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.hc.core5.http.config.SocketConfig
newFullyQualifiedTypeName: org.apache.hc.core5.http.io.SocketConfig
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 org.openrewrite.java.apache.httpclient4;

import org.junit.jupiter.api.Test;
import org.openrewrite.config.Environment;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

public class CookieConstantsTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpath("httpclient", "httpcore"))
.recipe(Environment.builder()
.scanRuntimeClasspath("org.openrewrite")
.build()
.activateRecipes("org.openrewrite.java.apache.httpclient4.UpgradeApacheHttpClient_4_5")
);
}

@Test
void testCookieConstantsMapping() {
rewriteRun(
//language=java
java(
"""
import org.apache.http.client.params.CookiePolicy;

class A {
void method() {
String c1 = CookiePolicy.BROWSER_COMPATIBILITY;
String c2 = CookiePolicy.NETSCAPE;
String c3 = CookiePolicy.RFC_2109;
String c4 = CookiePolicy.RFC_2965;
String c5 = CookiePolicy.BEST_MATCH;
String c6 = CookiePolicy.IGNORE_COOKIES;
}
}
""", """
import org.apache.http.client.config.CookieSpecs;

class A {
void method() {
String c1 = CookieSpecs.BROWSER_COMPATIBILITY;
String c2 = CookieSpecs.NETSCAPE;
String c3 = CookieSpecs.STANDARD;
String c4 = CookieSpecs.STANDARD_STRICT;
String c5 = CookieSpecs.BEST_MATCH;
String c6 = CookieSpecs.IGNORE_COOKIES;
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 org.openrewrite.java.apache.httpclient4;

import org.junit.jupiter.api.Test;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

public class MigrateDefaultHttpClientTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpath("httpclient", "httpcore"))
.recipe(new MigrateDefaultHttpClient());
}

@Test
void noArgsDefaultHttpClient() {
rewriteRun(
//language=java
java(
"""
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;

class A {
void method() throws IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://moderne.io");
HttpResponse httpResponse = httpClient.execute(httpPost);
}
}
""", """
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;

class A {
void method() throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://moderne.io");
HttpResponse httpResponse = httpClient.execute(httpPost);
}
}
"""
)
);
}
}
Loading
Loading