diff --git a/src/main/java/org/openrewrite/apache/httpclient5/UsernamePasswordCredentials.java b/src/main/java/org/openrewrite/apache/httpclient5/UsernamePasswordCredentials.java new file mode 100644 index 0000000..6466a87 --- /dev/null +++ b/src/main/java/org/openrewrite/apache/httpclient5/UsernamePasswordCredentials.java @@ -0,0 +1,61 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.apache.httpclient5; + +import org.openrewrite.ExecutionContext; +import org.openrewrite.Preconditions; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.MethodMatcher; +import org.openrewrite.java.search.UsesMethod; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JavaType; +import org.openrewrite.java.tree.TypeUtils; + +public class UsernamePasswordCredentials extends Recipe { + private static final String FQN = "org.apache.http.auth.UsernamePasswordCredentials"; + private static final String METHOD_PATTERN = FQN + " (String, String)"; + + @Override + public String getDisplayName() { + return "Migrate `UsernamePasswordCredentials` to httpclient5"; + } + + @Override + public String getDescription() { + return "Change the password argument going into `UsernamePasswordCredentials` to be a `char[]`."; + } + + @Override + public TreeVisitor getVisitor() { + MethodMatcher methodMatcher = new MethodMatcher(METHOD_PATTERN); + return Preconditions.check(new UsesMethod<>(methodMatcher), new JavaIsoVisitor() { + @Override + public J.NewClass visitNewClass(J.NewClass newClass, ExecutionContext ctx) { + J.NewClass nc = super.visitNewClass(newClass, ctx); + if (methodMatcher.matches(nc)) { + Expression passwordArgument = nc.getArguments().get(1); + nc = JavaTemplate.apply("#{any(String)}.toCharArray()", + getCursor(), passwordArgument.getCoordinates().replace(), passwordArgument); + } + return nc; + } + }); + } +} diff --git a/src/main/resources/META-INF/rewrite/apache-httpclient-5.yml b/src/main/resources/META-INF/rewrite/apache-httpclient-5.yml index 4f967bb..d5ed625 100644 --- a/src/main/resources/META-INF/rewrite/apache-httpclient-5.yml +++ b/src/main/resources/META-INF/rewrite/apache-httpclient-5.yml @@ -41,6 +41,7 @@ recipeList: newGroupId: org.apache.httpcomponents.core5 newArtifactId: httpcore5 newVersion: 5.3.x + - org.openrewrite.apache.httpclient5.UsernamePasswordCredentials - org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_ClassMapping - org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_DeprecatedMethods - org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_TimeUnit diff --git a/src/test/java/org/openrewrite/apache/httpclient5/UpgradeApacheHttpClient5Test.java b/src/test/java/org/openrewrite/apache/httpclient5/UpgradeApacheHttpClient5Test.java index d4fb727..1ec83fe 100644 --- a/src/test/java/org/openrewrite/apache/httpclient5/UpgradeApacheHttpClient5Test.java +++ b/src/test/java/org/openrewrite/apache/httpclient5/UpgradeApacheHttpClient5Test.java @@ -49,39 +49,40 @@ void migrateDependencies() { rewriteRun( //language=xml pomXml( - """ - - 4.0.0 - org.example - example - 1.0.0 - - - org.apache.httpcomponents - httpclient - 4.5.14 - - - - """, spec -> spec.after(pom -> { - Matcher version = Pattern.compile("5\\.4\\.\\d+").matcher(pom); - assertThat(version.find()).describedAs("Expected 5.4.x in %s", pom).isTrue(); - return """ - - 4.0.0 - org.example - example - 1.0.0 - - - org.apache.httpcomponents.client5 - httpclient5 - %s - - - - """.formatted(version.group(0)); - }))); + """ + + 4.0.0 + org.example + example + 1.0.0 + + + org.apache.httpcomponents + httpclient + 4.5.14 + + + + """, + spec -> spec.after(pom -> { + Matcher version = Pattern.compile("5\\.4\\.\\d+").matcher(pom); + assertThat(version.find()).describedAs("Expected 5.4.x in %s", pom).isTrue(); + return """ + + 4.0.0 + org.example + example + 1.0.0 + + + org.apache.httpcomponents.client5 + httpclient5 + %s + + + + """.formatted(version.group(0)); + }))); } @DocumentExample @@ -90,31 +91,32 @@ void importReplacementsInGroupsWithSomeSpecificMappings() { rewriteRun( //language=java java( - """ - import org.apache.http.HttpEntity; - import org.apache.http.client.methods.HttpGet; - import org.apache.http.client.methods.HttpUriRequest; - import org.apache.http.util.EntityUtils; + """ + import org.apache.http.HttpEntity; + import org.apache.http.client.methods.HttpGet; + import org.apache.http.client.methods.HttpUriRequest; + import org.apache.http.util.EntityUtils; - class A { - void method(HttpEntity entity, String urlStr) throws Exception { - HttpUriRequest getRequest = new HttpGet(urlStr); - EntityUtils.consume(entity); - } - } - """, """ - import org.apache.hc.core5.http.io.entity.EntityUtils; - import org.apache.hc.core5.http.HttpEntity; - import org.apache.hc.client5.http.classic.methods.HttpGet; - import org.apache.hc.client5.http.classic.methods.HttpUriRequest; + class A { + void method(HttpEntity entity, String urlStr) throws Exception { + HttpUriRequest getRequest = new HttpGet(urlStr); + EntityUtils.consume(entity); + } + } + """, """ + import org.apache.hc.core5.http.io.entity.EntityUtils; + import org.apache.hc.core5.http.HttpEntity; + import org.apache.hc.client5.http.classic.methods.HttpGet; + import org.apache.hc.client5.http.classic.methods.HttpUriRequest; - class A { - void method(HttpEntity entity, String urlStr) throws Exception { - HttpUriRequest getRequest = new HttpGet(urlStr); - EntityUtils.consume(entity); - } - } - """) + class A { + void method(HttpEntity entity, String urlStr) throws Exception { + HttpUriRequest getRequest = new HttpGet(urlStr); + EntityUtils.consume(entity); + } + } + """ + ) ); } @@ -123,45 +125,47 @@ void addTimeUnitsToTimeoutAndDurationMethods() { rewriteRun( //language=java java( - """ - import org.apache.http.client.config.RequestConfig; - import org.apache.http.config.SocketConfig; + """ + import org.apache.http.client.config.RequestConfig; + import org.apache.http.config.SocketConfig; - class A { - void method() { - int connectTimeout = 500; - RequestConfig.custom() - .setConnectionRequestTimeout(300) - .setConnectTimeout(connectTimeout) - .setSocketTimeout(connectTimeout * 10); + class A { + void method() { + int connectTimeout = 500; + RequestConfig.custom() + .setConnectionRequestTimeout(300) + .setConnectTimeout(connectTimeout) + .setSocketTimeout(connectTimeout * 10); - long linger = 420; - SocketConfig.custom() - .setSoTimeout(1000) - .setSoLinger((int) linger); - } - } - """, """ - import org.apache.hc.client5.http.config.RequestConfig; - import org.apache.hc.core5.http.io.SocketConfig; + long linger = 420; + SocketConfig.custom() + .setSoTimeout(1000) + .setSoLinger((int) linger); + } + } + """, + """ + import org.apache.hc.client5.http.config.RequestConfig; + import org.apache.hc.core5.http.io.SocketConfig; - import java.util.concurrent.TimeUnit; + import java.util.concurrent.TimeUnit; - class A { - void method() { - int connectTimeout = 500; - RequestConfig.custom() - .setConnectionRequestTimeout(300, TimeUnit.MILLISECONDS) - .setConnectTimeout(connectTimeout, TimeUnit.MILLISECONDS) - .setResponseTimeout(connectTimeout * 10, TimeUnit.MILLISECONDS); + class A { + void method() { + int connectTimeout = 500; + RequestConfig.custom() + .setConnectionRequestTimeout(300, TimeUnit.MILLISECONDS) + .setConnectTimeout(connectTimeout, TimeUnit.MILLISECONDS) + .setResponseTimeout(connectTimeout * 10, TimeUnit.MILLISECONDS); - long linger = 420; - SocketConfig.custom() - .setSoTimeout(1000, TimeUnit.MILLISECONDS) - .setSoLinger((int) linger, TimeUnit.MILLISECONDS); - } - } - """) + long linger = 420; + SocketConfig.custom() + .setSoTimeout(1000, TimeUnit.MILLISECONDS) + .setSoLinger((int) linger, TimeUnit.MILLISECONDS); + } + } + """ + ) ); } @@ -170,53 +174,54 @@ void removeStatusLineHttpResponse() { rewriteRun( //language=java java( - """ - import org.apache.http.HttpStatus; - import org.apache.http.client.methods.CloseableHttpResponse; - import org.apache.http.client.methods.HttpGet; - import org.apache.http.impl.client.CloseableHttpClient; - import org.apache.http.impl.client.HttpClientBuilder; - import org.apache.http.ProtocolVersion; + """ + import org.apache.http.HttpStatus; + import org.apache.http.client.methods.CloseableHttpResponse; + import org.apache.http.client.methods.HttpGet; + import org.apache.http.impl.client.CloseableHttpClient; + import org.apache.http.impl.client.HttpClientBuilder; + import org.apache.http.ProtocolVersion; - import java.io.IOException; + import java.io.IOException; - class A { - void method() throws IOException { - HttpGet httpGet = new HttpGet("https://moderne.io"); - CloseableHttpClient instance = HttpClientBuilder.create().build(); - CloseableHttpResponse response = instance.execute(httpGet); + class A { + void method() throws IOException { + HttpGet httpGet = new HttpGet("https://moderne.io"); + CloseableHttpClient instance = HttpClientBuilder.create().build(); + CloseableHttpResponse response = instance.execute(httpGet); - System.out.println("response.getStatusLine() :: " + response.getStatusLine()); - int statusCode = response.getStatusLine().getStatusCode(); - String reason = response.getStatusLine().getReasonPhrase(); - ProtocolVersion version = response.getStatusLine().getProtocolVersion(); - } - } - """, + System.out.println("response.getStatusLine() :: " + response.getStatusLine()); + int statusCode = response.getStatusLine().getStatusCode(); + String reason = response.getStatusLine().getReasonPhrase(); + ProtocolVersion version = response.getStatusLine().getProtocolVersion(); + } + } + """, """ - import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; - import org.apache.hc.core5.http.HttpStatus; - import org.apache.hc.client5.http.classic.methods.HttpGet; - import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; - import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; - import org.apache.hc.core5.http.ProtocolVersion; - import org.apache.hc.core5.http.message.StatusLine; + import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; + import org.apache.hc.core5.http.HttpStatus; + import org.apache.hc.client5.http.classic.methods.HttpGet; + import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; + import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; + import org.apache.hc.core5.http.ProtocolVersion; + import org.apache.hc.core5.http.message.StatusLine; - import java.io.IOException; + import java.io.IOException; - class A { - void method() throws IOException { - HttpGet httpGet = new HttpGet("https://moderne.io"); - CloseableHttpClient instance = HttpClientBuilder.create().build(); - CloseableHttpResponse response = instance.execute(httpGet); + class A { + void method() throws IOException { + HttpGet httpGet = new HttpGet("https://moderne.io"); + CloseableHttpClient instance = HttpClientBuilder.create().build(); + CloseableHttpResponse response = instance.execute(httpGet); - System.out.println("response.getStatusLine() :: " + new StatusLine(response)); - int statusCode = response.getCode(); - String reason = response.getReasonPhrase(); - ProtocolVersion version = response.getVersion(); - } - } - """) + System.out.println("response.getStatusLine() :: " + new StatusLine(response)); + int statusCode = response.getCode(); + String reason = response.getReasonPhrase(); + ProtocolVersion version = response.getVersion(); + } + } + """ + ) ); } @@ -226,41 +231,69 @@ void convertRequestBuilderToClassicRequestBuilder() { //language=java java( """ - import org.apache.http.client.methods.CloseableHttpResponse; - import org.apache.http.client.methods.HttpUriRequest; - import org.apache.http.impl.client.CloseableHttpClient; - import org.apache.http.impl.client.HttpClientBuilder; - import org.apache.http.client.methods.RequestBuilder; + import org.apache.http.client.methods.CloseableHttpResponse; + import org.apache.http.client.methods.HttpUriRequest; + import org.apache.http.impl.client.CloseableHttpClient; + import org.apache.http.impl.client.HttpClientBuilder; + import org.apache.http.client.methods.RequestBuilder; - import java.io.IOException; + import java.io.IOException; - class A { - void method() throws IOException { - RequestBuilder requestBuilder = RequestBuilder.get("https://moderne.io"); - HttpUriRequest request = requestBuilder.build(); - CloseableHttpClient instance = HttpClientBuilder.create().build(); - CloseableHttpResponse response = instance.execute(request); - } - } - """, - """ - import org.apache.hc.client5.http.classic.methods.HttpUriRequest; - import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; - import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; - import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; - import org.apache.hc.core5.http.io.support.ClassicRequestBuilder; + class A { + void method() throws IOException { + RequestBuilder requestBuilder = RequestBuilder.get("https://moderne.io"); + HttpUriRequest request = requestBuilder.build(); + CloseableHttpClient instance = HttpClientBuilder.create().build(); + CloseableHttpResponse response = instance.execute(request); + } + } + """, + """ + import org.apache.hc.client5.http.classic.methods.HttpUriRequest; + import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; + import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; + import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; + import org.apache.hc.core5.http.io.support.ClassicRequestBuilder; + + import java.io.IOException; - import java.io.IOException; + class A { + void method() throws IOException { + ClassicRequestBuilder requestBuilder = ClassicRequestBuilder.get("https://moderne.io"); + HttpUriRequest request = requestBuilder.build(); + CloseableHttpClient instance = HttpClientBuilder.create().build(); + CloseableHttpResponse response = instance.execute(request); + } + } + """ + ) + ); + } + + @Test + void changeUsernamePasswordCredentials() { + rewriteRun( + //language=java + java( + """ + import org.apache.http.auth.UsernamePasswordCredentials; + + class Example { + void method() { + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("username", "password"); + } + } + """, + """ + import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; - class A { - void method() throws IOException { - ClassicRequestBuilder requestBuilder = ClassicRequestBuilder.get("https://moderne.io"); - HttpUriRequest request = requestBuilder.build(); - CloseableHttpClient instance = HttpClientBuilder.create().build(); - CloseableHttpResponse response = instance.execute(request); - } - } - """) + class Example { + void method() { + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("username", "password".toCharArray()); + } + } + """ + ) ); } }