Skip to content

Commit

Permalink
Added Migrate DefaultHttpClient & some cookie constants (#401)
Browse files Browse the repository at this point in the history
* Added Migrate DefaultHttpClient & some cookie constants

* Update src/main/java/org/openrewrite/java/apache/httpclient4/MigrateDefaultHttpClient.java

Co-authored-by: Tim te Beek <[email protected]>

* Update src/main/java/org/openrewrite/java/apache/httpclient4/MigrateDefaultHttpClient.java

Co-authored-by: Tim te Beek <[email protected]>

* Update src/main/resources/META-INF/rewrite/apache-httpclient-4-5.yml

Co-authored-by: Tim te Beek <[email protected]>

* removed pinned dependencies

* Update src/main/resources/META-INF/rewrite/apache-httpclient-4-5.yml

Co-authored-by: Raquel Pau <[email protected]>

* Update src/main/resources/META-INF/rewrite/apache-httpclient-4-5.yml

Co-authored-by: Raquel Pau <[email protected]>

* Update src/main/java/org/openrewrite/java/apache/httpclient4/MigrateDefaultHttpClient.java

Co-authored-by: Raquel Pau <[email protected]>

---------

Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: Raquel Pau <[email protected]>
  • Loading branch information
3 people authored Aug 2, 2023
1 parent cbc7381 commit a277a21
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 2 deletions.
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. " +
"It only covers the default scenario with no custom HttpParams or ConnectionManager.";
}

@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;
52 changes: 52 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,52 @@
#
# 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 modifies
application's build files, make changes to deprecated/preferred APIs, and migrates configuration settings that have
changes between versions.
tags:
- apache
- httpclient
recipeList:
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
groupId: org.apache.httpcomponents
artifactId: httpclient
newVersion: 4.5.x
- org.openrewrite.java.apache.httpclient4.MappingDeprecatedClasses
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.apache.httpclient4.MappingDeprecatedClasses
displayName: Maps deprecated classes from Apache HttpClient 4.5.x to suggested replacements
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

20 changes: 18 additions & 2 deletions src/main/resources/META-INF/rewrite/apache-httpclient-5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ tags:
- apache
- httpclient
recipeList:
- org.openrewrite.java.apache.httpclient4.UpgradeApacheHttpClient_4_5
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: org.apache.httpcomponents
oldArtifactId: httpclient
newGroupId: org.apache.httpcomponents.client5
newArtifactId: httpclient5
newVersion: 5.1.x
overrideManagedVersion: true
- org.openrewrite.java.apache.httpclient5.UpgradeApacheHttpClient_5_ClassMapping
- org.openrewrite.java.apache.httpclient5.UpgradeApacheHttpClient_5_DeprecatedMethods
- 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 +131,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 +196,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 +270,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

0 comments on commit a277a21

Please sign in to comment.