Skip to content

Commit

Permalink
Modify the dependency httpclient5 to httpclient4. (#20191)
Browse files Browse the repository at this point in the history
* Modify the dependency httpclient5 to httpclient4.
  • Loading branch information
han-gao authored Apr 8, 2021
1 parent 078fd23 commit c34d33c
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 115 deletions.
41 changes: 41 additions & 0 deletions eng/code-quality-reports/src/main/resources/revapi/revapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,47 @@
"exampleUseChainInNewApi": ".*com\\.azure\\.core\\.util\\.serializer\\.JacksonAdapter.*",
"justification": "We allow this in com.azure.core.util.serializer.JacksonAdapter."
},
{
"regex": true,
"code": "java\\.missing\\.(oldClass|newClass)",
"new": "missing\\-class javax\\.servlet\\.ServletContextEvent",
"exampleUseChainInNewApi": ".*com\\.azure\\.keyvault\\.jca\\.org\\.apache\\.commons\\.logging\\.impl.*",
"justification": "We allow this in com.azure.keyvault.jca.org.apache.commons.logging.impl.ServletContextCleaner"
},
{
"regex": true,
"code": "java\\.missing\\.(oldClass|newClass)",
"new": "missing\\-class org\\.apache\\.avalon\\.framework\\.logger\\.Logger",
"exampleUseChainInNewApi": ".*com\\.azure\\.keyvault\\.jca\\.org\\.apache\\.commons\\.logging\\.impl\\.AvalonLogger.*",
"justification": "We allow this in com.azure.keyvault.jca.org.apache.commons.logging.impl.AvalonLogger"
},
{
"regex": true,
"code": "java\\.missing\\.(oldClass|newClass)",
"new": "missing\\-class org\\.apache\\.log\\.Logger",
"exampleUseChainInNewApi": ".*com\\.azure\\.keyvault\\.jca\\.org\\.apache\\.commons\\.logging\\.impl\\.LogKitLogger.*",
"justification": "We allow this in com.azure.keyvault.jca.org.apache.commons.logging.impl.LogKitLogger"
},
{
"regex": true,
"code": "java\\.missing\\.(oldClass|newClass)",
"new": "missing\\-class org\\.apache\\.log4j\\.Logger",
"exampleUseChainInNewApi": ".*com\\.azure\\.keyvault\\.jca\\.org\\.apache\\.commons\\.logging\\.impl\\.Log4JLogger.*",
"justification": "We allow this in com.azure.keyvault.jca.org.apache.commons.logging.impl.Log4JLogger"
},
{
"regex": true,
"code": "java.class.nonPublicPartOfAPI",
"new": "class org.apache.http.impl.client.HttpRequestTaskCallable<V extends java.lang.Object>",
"justification": "We allow this in org.apache.http.impl.client.HttpRequestFutureTask<V>"
},
{
"regex": true,
"code": "java.class.nonPublicPartOfAPI",
"new": "(class|interface) org\\.apache\\.http\\.impl\\.conn\\.CPoolEntry",
"exampleUseChainInNewApi": ".*org\\.apache\\.http\\.HttpClientConnection.*",
"justification": "We allow this in org.apache.http.HttpClientConnection>"
},
{
"regex": true,
"code": "java.class.nonPublicPartOfAPI",
Expand Down
1 change: 0 additions & 1 deletion eng/versioning/external_dependencies.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ org.apache.avro:avro-maven-plugin;1.10.1
org.apache.commons:commons-compress;1.20
org.apache.commons:commons-lang3;3.11
org.apache.httpcomponents:httpclient;4.5.13
org.apache.httpcomponents.client5:httpclient5;5.0.3
org.apache.logging.log4j:log4j-api;2.13.3
org.apache.logging.log4j:log4j-core;2.13.3
org.apache.logging.log4j:log4j-slf4j-impl;2.13.3
Expand Down
21 changes: 9 additions & 12 deletions sdk/keyvault/azure-security-keyvault-jca/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Note if you want to use Azure Managed Identity, you should set the value of `azu
### Client side SSL
If you are looking to integrate the JCA provider for client side socket connections, see the Apache HTTP client example below.

<!-- embedme ./src/samples/java/com/azure/security/keyvault/jca/ClientSSLSample.java#L28-L71 -->
<!-- embedme ./src/samples/java/com/azure/security/keyvault/jca/ClientSSLSample.java#L28-L68 -->
```java
KeyVaultJcaProvider provider = new KeyVaultJcaProvider();
Security.addProvider(provider);
Expand All @@ -86,23 +86,20 @@ SSLContext sslContext = SSLContexts
.loadTrustMaterial(keyStore, new TrustSelfSignedStrategy())
.build();

SSLConnectionSocketFactory factory = SSLConnectionSocketFactoryBuilder
.create()
.setSslContext(sslContext)
.setHostnameVerifier((hostname, session) -> true)
.build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
sslContext, (hostname, session) -> true);

PoolingHttpClientConnectionManager manager = PoolingHttpClientConnectionManagerBuilder
.create()
.setSSLSocketFactory(factory)
.build();
PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", sslConnectionSocketFactory)
.build());

String result = null;

try (CloseableHttpClient client = HttpClients.custom().setConnectionManager(manager).build()) {
HttpGet httpGet = new HttpGet("https://localhost:8766");
HttpClientResponseHandler<String> responseHandler = (ClassicHttpResponse response) -> {
int status = response.getCode();
ResponseHandler<String> responseHandler = (HttpResponse response) -> {
int status = response.getStatusLine().getStatusCode();
String result1 = "Not success";
if (status == 204) {
result1 = "Success";
Expand Down
13 changes: 4 additions & 9 deletions sdk/keyvault/azure-security-keyvault-jca/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@
<pattern>org.apache.commons</pattern>
<shadedPattern>com.azure.keyvault.jca.org.apache.commons</shadedPattern>
</relocation>
<relocation>
<pattern>org.apache.hc</pattern>
<shadedPattern>com.azure.keyvault.jca.org.apache.hc</shadedPattern>
</relocation>
<relocation>
<pattern>mozilla</pattern>
<shadedPattern>com.azure.keyvault.jca.mozilla</shadedPattern>
Expand Down Expand Up @@ -114,8 +110,8 @@
<bannedDependencies>
<includes>
<include>com.fasterxml.jackson.core:jackson-databind:[2.12.2]</include> <!-- {x-include-update;com.fasterxml.jackson.core:jackson-databind;external_dependency} -->
<include>org.apache.httpcomponents.client5:httpclient5:[5.0.3]</include> <!-- {x-include-update;org.apache.httpcomponents.client5:httpclient5;external_dependency} -->
<include>org.conscrypt:conscrypt-openjdk-uber:[2.2.1]</include> <!-- {x-include-update;org.conscrypt:conscrypt-openjdk-uber;external_dependency} -->
<include>org.apache.httpcomponents:httpclient:[4.5.13]</include> <!-- {x-include-update;org.apache.httpcomponents:httpclient;external_dependency} -->
<include>org.slf4j:slf4j-nop:[1.7.30]</include> <!-- {x-include-update;org.slf4j:slf4j-nop;external_dependency} -->
</includes>
</bannedDependencies>
Expand All @@ -127,10 +123,9 @@
<dependencies>
<!-- Apache HttpClient -->
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.0.3</version> <!-- {x-version-update;org.apache.httpcomponents.client5:httpclient5;external_dependency} -->
<optional>true</optional>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version> <!-- {x-version-update;org.apache.httpcomponents:httpclient;external_dependency} -->
</dependency>
<!-- Conscrypt -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
import com.azure.security.keyvault.jca.model.CertificatePolicy;
import com.azure.security.keyvault.jca.model.KeyProperties;
import com.azure.security.keyvault.jca.model.SecretBundle;
import java.io.BufferedReader;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.Key;
import java.security.KeyFactory;
Expand Down Expand Up @@ -154,8 +153,8 @@ private String getAccessToken() {
} else {
accessToken = authClient.getAccessToken(resource, managedIdentity);
}
} catch (UnsupportedEncodingException uee) {
LOGGER.log(WARNING, "Unsupported encoding", uee);
} catch (Throwable throwable) {
LOGGER.log(WARNING, "Unsupported encoding or missing Httpclient", throwable);
}
LOGGER.exiting("KeyVaultClient", "getAccessToken", accessToken);
return accessToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
// Licensed under the MIT License.
package com.azure.security.keyvault.jca;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.HttpEntities;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;
Expand Down Expand Up @@ -39,16 +39,7 @@ public String get(String url, Map<String, String> headers) {
httpGet.addHeader(key, value);
});
}
HttpClientResponseHandler<String> responseHandler = (ClassicHttpResponse response) -> {
int status = response.getCode();
String result1 = null;
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
result1 = entity != null ? EntityUtils.toString(entity) : null;
}
return result1;
};
result = client.execute(httpGet, responseHandler);
result = client.execute(httpGet, createResponseHandler());
} catch (IOException ioe) {
ioe.printStackTrace();
}
Expand All @@ -60,20 +51,24 @@ public String post(String url, String body, String contentType) {
String result = null;
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(HttpEntities.create(body, ContentType.create(contentType)));
HttpClientResponseHandler<String> responseHandler = (ClassicHttpResponse response) -> {
int status = response.getCode();
String result1 = null;
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
result1 = entity != null ? EntityUtils.toString(entity) : null;
}
return result1;
};
result = client.execute(httpPost, responseHandler);
httpPost.setEntity(
new StringEntity(body, ContentType.create(contentType)));
result = client.execute(httpPost, createResponseHandler());
} catch (IOException ioe) {
ioe.printStackTrace();
}
return result;
}

private ResponseHandler<String> createResponseHandler() {
return (HttpResponse response) -> {
int status = response.getStatusLine().getStatusCode();
String result = null;
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
result = entity != null ? EntityUtils.toString(entity) : null;
}
return result;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
// Licensed under the MIT License.
package com.azure.security.keyvault.jca;

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.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
import org.apache.hc.client5.http.ssl.TrustSelfSignedStrategy;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.ssl.SSLContexts;
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import javax.net.ssl.SSLContext;
import java.io.IOException;
Expand Down Expand Up @@ -42,23 +42,20 @@ public static void main(String[] args) throws Exception {
.loadTrustMaterial(keyStore, new TrustSelfSignedStrategy())
.build();

SSLConnectionSocketFactory factory = SSLConnectionSocketFactoryBuilder
.create()
.setSslContext(sslContext)
.setHostnameVerifier((hostname, session) -> true)
.build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
sslContext, (hostname, session) -> true);

PoolingHttpClientConnectionManager manager = PoolingHttpClientConnectionManagerBuilder
.create()
.setSSLSocketFactory(factory)
.build();
PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", sslConnectionSocketFactory)
.build());

String result = null;

try (CloseableHttpClient client = HttpClients.custom().setConnectionManager(manager).build()) {
HttpGet httpGet = new HttpGet("https://localhost:8766");
HttpClientResponseHandler<String> responseHandler = (ClassicHttpResponse response) -> {
int status = response.getCode();
ResponseHandler<String> responseHandler = (HttpResponse response) -> {
int status = response.getStatusLine().getStatusCode();
String result1 = "Not success";
if (status == 204) {
result1 = "Success";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

package com.azure.security.keyvault.jca;

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.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
import org.apache.hc.client5.http.ssl.TrustSelfSignedStrategy;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.ssl.SSLContexts;
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;
import org.junit.jupiter.api.Test;

import javax.net.ssl.KeyManagerFactory;
Expand Down Expand Up @@ -104,26 +104,23 @@ public void testServerSocket() throws Exception {
.loadTrustMaterial((final X509Certificate[] chain, final String authType) -> true)
.build();

SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder
.create()
.setSslContext(sslContext)
.setHostnameVerifier((hostname, session) -> true)
.build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
sslContext, (hostname, session) -> true);

PoolingHttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder
.create()
.setSSLSocketFactory(sslSocketFactory)
.build();
PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", sslConnectionSocketFactory)
.build());

/*
* And now execute the test.
*/
String result = null;

try (CloseableHttpClient client = HttpClients.custom().setConnectionManager(cm).build()) {
try (CloseableHttpClient client = HttpClients.custom().setConnectionManager(manager).build()) {
HttpGet httpGet = new HttpGet("https://localhost:8765");
HttpClientResponseHandler<String> responseHandler = (ClassicHttpResponse response) -> {
int status = response.getCode();
ResponseHandler<String> responseHandler = (HttpResponse response) -> {
int status = response.getStatusLine().getStatusCode();
String result1 = null;
if (status == 204) {
result1 = "Success";
Expand Down Expand Up @@ -210,26 +207,23 @@ public void testServerSocketWithSelfSignedClientTrust() throws Exception {
.loadTrustMaterial(ks, new TrustSelfSignedStrategy())
.build();

SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder
.create()
.setSslContext(sslContext)
.setHostnameVerifier((hostname, session) -> true)
.build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
sslContext, (hostname, session) -> true);

PoolingHttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder
.create()
.setSSLSocketFactory(sslSocketFactory)
.build();
PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", sslConnectionSocketFactory)
.build());

/*
* And now execute the test.
*/
String result = null;

try (CloseableHttpClient client = HttpClients.custom().setConnectionManager(cm).build()) {
try (CloseableHttpClient client = HttpClients.custom().setConnectionManager(manager).build()) {
HttpGet httpGet = new HttpGet("https://localhost:8766");
HttpClientResponseHandler<String> responseHandler = (ClassicHttpResponse response) -> {
int status = response.getCode();
ResponseHandler<String> responseHandler = (HttpResponse response) -> {
int status = response.getStatusLine().getStatusCode();
String result1 = null;
if (status == 204) {
result1 = "Success";
Expand Down
Loading

0 comments on commit c34d33c

Please sign in to comment.