From 768384d8a01f296770ca8071cf4a9535b8736dc3 Mon Sep 17 00:00:00 2001 From: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com> Date: Tue, 15 Nov 2022 12:54:32 -0500 Subject: [PATCH] Remove httpclient package. Fixes #1171 --- pom.xml | 13 ++ spring-cloud-commons/pom.xml | 14 +- ...cheHttpClientConnectionManagerFactory.java | 55 ------- .../httpclient/ApacheHttpClientFactory.java | 36 ----- ...cheHttpClientConnectionManagerFactory.java | 104 ------------ .../DefaultApacheHttpClientFactory.java | 44 ----- ...aultOkHttpClientConnectionPoolFactory.java | 35 ---- .../DefaultOkHttpClientFactory.java | 66 -------- .../httpclient/HttpClientConfiguration.java | 85 ---------- .../OkHttpClientConnectionPoolFactory.java | 39 ----- .../httpclient/OkHttpClientFactory.java | 73 --------- .../configuration/SSLContextFactory.java | 2 +- ...ot.autoconfigure.AutoConfiguration.imports | 1 - ...omHttpClientBuilderConfigurationTests.java | 72 --------- .../CustomHttpClientConfigurationTests.java | 153 ------------------ ...OkHttpClientBuilderConfigurationTests.java | 78 --------- ...tpClientConnectionManagerFactoryTests.java | 115 ------------- .../DefaultApacheHttpClientFactoryTests.java | 61 ------- .../DefaultHttpClientConfigurationTests.java | 82 ---------- ...OkHttpClientConnectionPoolFactoryTest.java | 56 ------- .../DefaultOkHttpClientFactoryTest.java | 46 ------ 21 files changed, 16 insertions(+), 1214 deletions(-) delete mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/ApacheHttpClientConnectionManagerFactory.java delete mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/ApacheHttpClientFactory.java delete mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientConnectionManagerFactory.java delete mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientFactory.java delete mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientConnectionPoolFactory.java delete mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientFactory.java delete mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/HttpClientConfiguration.java delete mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/OkHttpClientConnectionPoolFactory.java delete mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/OkHttpClientFactory.java delete mode 100644 spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/CustomHttpClientBuilderConfigurationTests.java delete mode 100644 spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/CustomHttpClientConfigurationTests.java delete mode 100644 spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/CustomOkHttpClientBuilderConfigurationTests.java delete mode 100644 spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientConnectionManagerFactoryTests.java delete mode 100644 spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientFactoryTests.java delete mode 100644 spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultHttpClientConfigurationTests.java delete mode 100644 spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientConnectionPoolFactoryTest.java delete mode 100644 spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientFactoryTest.java diff --git a/pom.xml b/pom.xml index c53610cb..3d8f6aa3 100644 --- a/pom.xml +++ b/pom.xml @@ -69,6 +69,19 @@ org.basepom.maven duplicate-finder-maven-plugin + + + + org.apache.httpcomponents + httpclient + true + + + org.apache.httpcomponents.client5 + httpclient5 + + + diff --git a/spring-cloud-commons/pom.xml b/spring-cloud-commons/pom.xml index e96e548d..a5671c6a 100644 --- a/spring-cloud-commons/pom.xml +++ b/spring-cloud-commons/pom.xml @@ -158,18 +158,8 @@ true - com.squareup.okhttp3 - okhttp - true - - - com.squareup.okhttp3 - logging-interceptor - true - - - org.apache.httpcomponents - httpclient + org.apache.httpcomponents.client5 + httpclient5 true diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/ApacheHttpClientConnectionManagerFactory.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/ApacheHttpClientConnectionManagerFactory.java deleted file mode 100644 index 9c4fc0a8..00000000 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/ApacheHttpClientConnectionManagerFactory.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import java.util.concurrent.TimeUnit; - -import org.apache.http.config.RegistryBuilder; -import org.apache.http.conn.HttpClientConnectionManager; - -/** - * Interface for creating an {@link HttpClientConnectionManager}. - * - * @author Ryan Baxter - */ -public interface ApacheHttpClientConnectionManagerFactory { - - /** - * Scheme for HTTP based communication. - */ - String HTTP_SCHEME = "http"; - - /** - * Scheme for HTTPS based communication. - */ - String HTTPS_SCHEME = "https"; - - /** - * Creates a new {@link HttpClientConnectionManager}. - * @param disableSslValidation If true, SSL validation will be disabled. - * @param maxTotalConnections The total number of connections. - * @param maxConnectionsPerRoute The total number of connections per route. - * @param timeToLive The time a connection is allowed to exist. - * @param timeUnit The time unit for the time-to-live value. - * @param registryBuilder The {@link RegistryBuilder} to use in the connection - * manager. - * @return A new {@link HttpClientConnectionManager}. - */ - HttpClientConnectionManager newConnectionManager(boolean disableSslValidation, int maxTotalConnections, - int maxConnectionsPerRoute, long timeToLive, TimeUnit timeUnit, RegistryBuilder registryBuilder); - -} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/ApacheHttpClientFactory.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/ApacheHttpClientFactory.java deleted file mode 100644 index 2afcbaec..00000000 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/ApacheHttpClientFactory.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; - -/** - * Factory for creating a new {@link CloseableHttpClient}. - * - * @author Ryan Baxter - */ -public interface ApacheHttpClientFactory { - - /** - * Creates an {@link HttpClientBuilder} that can be used to create a new - * {@link CloseableHttpClient}. - * @return A {@link HttpClientBuilder}. - */ - HttpClientBuilder createBuilder(); - -} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientConnectionManagerFactory.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientConnectionManagerFactory.java deleted file mode 100644 index ed57c3c1..00000000 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientConnectionManagerFactory.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.security.cert.X509Certificate; -import java.util.concurrent.TimeUnit; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.http.config.Registry; -import org.apache.http.config.RegistryBuilder; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.conn.socket.ConnectionSocketFactory; -import org.apache.http.conn.socket.PlainConnectionSocketFactory; -import org.apache.http.conn.ssl.NoopHostnameVerifier; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; - -/** - * Default implementation of {@link ApacheHttpClientConnectionManagerFactory}. - * - * @author Ryan Baxter - * @author Michael Wirth - */ -public class DefaultApacheHttpClientConnectionManagerFactory implements ApacheHttpClientConnectionManagerFactory { - - private static final Log LOG = LogFactory.getLog(DefaultApacheHttpClientConnectionManagerFactory.class); - - public HttpClientConnectionManager newConnectionManager(boolean disableSslValidation, int maxTotalConnections, - int maxConnectionsPerRoute) { - return newConnectionManager(disableSslValidation, maxTotalConnections, maxConnectionsPerRoute, -1, - TimeUnit.MILLISECONDS, null); - } - - @Override - public HttpClientConnectionManager newConnectionManager(boolean disableSslValidation, int maxTotalConnections, - int maxConnectionsPerRoute, long timeToLive, TimeUnit timeUnit, RegistryBuilder registryBuilder) { - if (registryBuilder == null) { - registryBuilder = RegistryBuilder.create().register(HTTP_SCHEME, - PlainConnectionSocketFactory.INSTANCE); - } - if (disableSslValidation) { - try { - final SSLContext sslContext = SSLContext.getInstance("SSL"); - sslContext.init(null, new TrustManager[] { new DisabledValidationTrustManager() }, new SecureRandom()); - registryBuilder.register(HTTPS_SCHEME, - new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE)); - } - catch (NoSuchAlgorithmException | KeyManagementException e) { - LOG.warn("Error creating SSLContext", e); - } - } - else { - registryBuilder.register("https", SSLConnectionSocketFactory.getSocketFactory()); - } - final Registry registry = registryBuilder.build(); - - PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry, null, - null, null, timeToLive, timeUnit); - connectionManager.setMaxTotal(maxTotalConnections); - connectionManager.setDefaultMaxPerRoute(maxConnectionsPerRoute); - - return connectionManager; - } - - static class DisabledValidationTrustManager implements X509TrustManager { - - @Override - public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { - } - - @Override - public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { - } - - @Override - public X509Certificate[] getAcceptedIssuers() { - return null; - } - - } - -} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientFactory.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientFactory.java deleted file mode 100644 index 2ce7d208..00000000 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientFactory.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import org.apache.http.impl.client.HttpClientBuilder; - -/** - * Default implementation of {@link ApacheHttpClientFactory}. - * - * @author Ryan Baxter - */ -public class DefaultApacheHttpClientFactory implements ApacheHttpClientFactory { - - private HttpClientBuilder builder; - - public DefaultApacheHttpClientFactory(HttpClientBuilder builder) { - this.builder = builder; - } - - /** - * A default {@link HttpClientBuilder}. The {@link HttpClientBuilder} returned will - * have content compression disabled, have cookie management disabled, and use system - * properties. - */ - @Override - public HttpClientBuilder createBuilder() { - return this.builder.disableContentCompression().disableCookieManagement().useSystemProperties(); - } - -} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientConnectionPoolFactory.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientConnectionPoolFactory.java deleted file mode 100644 index f578dce9..00000000 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientConnectionPoolFactory.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import java.util.concurrent.TimeUnit; - -import okhttp3.ConnectionPool; - -/** - * Default implementation of {@link OkHttpClientConnectionPoolFactory}. - * - * @author Ryan Baxter - */ -public class DefaultOkHttpClientConnectionPoolFactory implements OkHttpClientConnectionPoolFactory { - - @Override - public ConnectionPool create(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) { - return new ConnectionPool(maxIdleConnections, keepAliveDuration, timeUnit); - } - -} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientFactory.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientFactory.java deleted file mode 100644 index 74246acb..00000000 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientFactory.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSocketFactory; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; - -import okhttp3.OkHttpClient; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -/** - * Default implementation of {@link OkHttpClientFactory}. - * - * @author Ryan Baxter - */ -public class DefaultOkHttpClientFactory implements OkHttpClientFactory { - - private static final Log LOG = LogFactory.getLog(DefaultOkHttpClientFactory.class); - - private OkHttpClient.Builder builder; - - public DefaultOkHttpClientFactory(OkHttpClient.Builder builder) { - this.builder = builder; - } - - @Override - public OkHttpClient.Builder createBuilder(boolean disableSslValidation) { - if (disableSslValidation) { - try { - X509TrustManager disabledTrustManager = new DisableValidationTrustManager(); - TrustManager[] trustManagers = new TrustManager[1]; - trustManagers[0] = disabledTrustManager; - SSLContext sslContext = SSLContext.getInstance("SSL"); - sslContext.init(null, trustManagers, new java.security.SecureRandom()); - SSLSocketFactory disabledSSLSocketFactory = sslContext.getSocketFactory(); - this.builder.sslSocketFactory(disabledSSLSocketFactory, disabledTrustManager); - this.builder.hostnameVerifier(new TrustAllHostnames()); - } - catch (NoSuchAlgorithmException | KeyManagementException e) { - LOG.warn("Error setting SSLSocketFactory in OKHttpClient", e); - } - } - return this.builder; - } - -} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/HttpClientConfiguration.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/HttpClientConfiguration.java deleted file mode 100644 index 712e9c79..00000000 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/HttpClientConfiguration.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import okhttp3.OkHttpClient; -import org.apache.http.client.HttpClient; -import org.apache.http.impl.client.HttpClientBuilder; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * @author Ryan Baxter - */ -@Configuration(proxyBeanMethods = false) -public class HttpClientConfiguration { - - @Configuration(proxyBeanMethods = false) - @ConditionalOnProperty(name = "spring.cloud.httpclientfactories.apache.enabled", matchIfMissing = true) - @ConditionalOnClass(HttpClient.class) - static class ApacheHttpClientConfiguration { - - @Bean - @ConditionalOnMissingBean - public ApacheHttpClientConnectionManagerFactory connManFactory() { - return new DefaultApacheHttpClientConnectionManagerFactory(); - } - - @Bean - @ConditionalOnMissingBean - public HttpClientBuilder apacheHttpClientBuilder() { - return HttpClientBuilder.create(); - } - - @Bean - @ConditionalOnMissingBean - public ApacheHttpClientFactory apacheHttpClientFactory(HttpClientBuilder builder) { - return new DefaultApacheHttpClientFactory(builder); - } - - } - - @Configuration(proxyBeanMethods = false) - @ConditionalOnProperty(name = "spring.cloud.httpclientfactories.ok.enabled", matchIfMissing = true) - @ConditionalOnClass(OkHttpClient.class) - static class OkHttpClientConfiguration { - - @Bean - @ConditionalOnMissingBean - public OkHttpClientConnectionPoolFactory connPoolFactory() { - return new DefaultOkHttpClientConnectionPoolFactory(); - } - - @Bean - @ConditionalOnMissingBean - public OkHttpClient.Builder okHttpClientBuilder() { - return new OkHttpClient.Builder(); - } - - @Bean - @ConditionalOnMissingBean - public OkHttpClientFactory okHttpClientFactory(OkHttpClient.Builder builder) { - return new DefaultOkHttpClientFactory(builder); - } - - } - -} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/OkHttpClientConnectionPoolFactory.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/OkHttpClientConnectionPoolFactory.java deleted file mode 100644 index 62907d28..00000000 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/OkHttpClientConnectionPoolFactory.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import java.util.concurrent.TimeUnit; - -import okhttp3.ConnectionPool; - -/** - * Creates {@link ConnectionPool}s for {@link okhttp3.OkHttpClient}s. - * - * @author Ryan Baxter - */ -public interface OkHttpClientConnectionPoolFactory { - - /** - * Creates a new {@link ConnectionPool}. - * @param maxIdleConnections Number of max idle connections to allow. - * @param keepAliveDuration Amount of time to keep connections alive. - * @param timeUnit The time unit for the keep-alive duration. - * @return A new {@link ConnectionPool}. - */ - ConnectionPool create(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit); - -} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/OkHttpClientFactory.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/OkHttpClientFactory.java deleted file mode 100644 index cba0c79f..00000000 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/httpclient/OkHttpClientFactory.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import java.security.cert.X509Certificate; - -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.SSLSession; -import javax.net.ssl.X509TrustManager; - -import okhttp3.OkHttpClient; - -/** - * Creates new {@link OkHttpClient}s. - * - * @author Ryan Baxter - */ -public interface OkHttpClientFactory { - - /** - * Creates a {@link OkHttpClient.Builder} used to build an {@link OkHttpClient}. - * @param disableSslValidation Disables SSL validation - * @return A new {@link OkHttpClient.Builder} - */ - OkHttpClient.Builder createBuilder(boolean disableSslValidation); - - /** - * A {@link X509TrustManager} that does not validate SSL certificates. - */ - class DisableValidationTrustManager implements X509TrustManager { - - @Override - public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { - } - - @Override - public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { - } - - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - - } - - /** - * A {@link HostnameVerifier} that does not validate any hostnames. - */ - class TrustAllHostnames implements HostnameVerifier { - - @Override - public boolean verify(String s, SSLSession sslSession) { - return true; - } - - } - -} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/configuration/SSLContextFactory.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/configuration/SSLContextFactory.java index fd907a56..74cc5d8e 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/configuration/SSLContextFactory.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/configuration/SSLContextFactory.java @@ -25,7 +25,7 @@ import java.security.UnrecoverableKeyException; import javax.net.ssl.SSLContext; -import org.apache.http.ssl.SSLContextBuilder; +import org.apache.hc.core5.ssl.SSLContextBuilder; import org.springframework.core.io.Resource; diff --git a/spring-cloud-commons/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-cloud-commons/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index f6359aa0..721734ba 100644 --- a/spring-cloud-commons/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-cloud-commons/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -10,7 +10,6 @@ org.springframework.cloud.client.loadbalancer.LoadBalancerDefaultMappingsProvide org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerClientAutoConfiguration org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration -org.springframework.cloud.commons.httpclient.HttpClientConfiguration org.springframework.cloud.commons.util.UtilAutoConfiguration org.springframework.cloud.configuration.CompatibilityVerifierAutoConfiguration org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/CustomHttpClientBuilderConfigurationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/CustomHttpClientBuilderConfigurationTests.java deleted file mode 100644 index 8373856d..00000000 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/CustomHttpClientBuilderConfigurationTests.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import org.apache.http.impl.client.HttpClientBuilder; -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.AutoConfigureBefore; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import static org.assertj.core.api.BDDAssertions.then; - -/** - * @author Ryan Baxter - */ -@SpringBootTest(classes = CustomHttpClientBuilderApplication.class) -public class CustomHttpClientBuilderConfigurationTests { - - @Autowired - ApacheHttpClientFactory apacheHttpClientFactory; - - @Test - public void testCustomBuilder() { - HttpClientBuilder builder = this.apacheHttpClientFactory.createBuilder(); - then(CustomHttpClientBuilderApplication.MyHttpClientBuilder.class.isInstance(builder)).isTrue(); - } - -} - -@Configuration(proxyBeanMethods = false) -@EnableAutoConfiguration -class CustomHttpClientBuilderApplication { - - public static void main(String[] args) { - SpringApplication.run(MyApplication.class, args); - } - - @Configuration(proxyBeanMethods = false) - @AutoConfigureBefore(HttpClientConfiguration.class) - static class MyConfig { - - @Bean - public MyHttpClientBuilder builder() { - return new MyHttpClientBuilder(); - } - - } - - static class MyHttpClientBuilder extends HttpClientBuilder { - - } - -} diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/CustomHttpClientConfigurationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/CustomHttpClientConfigurationTests.java deleted file mode 100644 index b6a07b69..00000000 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/CustomHttpClientConfigurationTests.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import java.util.concurrent.TimeUnit; - -import okhttp3.ConnectionPool; -import okhttp3.OkHttpClient; -import org.apache.http.config.RegistryBuilder; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.impl.client.HttpClientBuilder; -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import static org.assertj.core.api.BDDAssertions.then; - -/** - * @author Ryan Baxter - */ -@SpringBootTest(classes = CustomApplication.class, properties = { "spring.cloud.httpclient.ok.enabled: true" }) -public class CustomHttpClientConfigurationTests { - - @Autowired - ApacheHttpClientFactory httpClientFactory; - - @Autowired - ApacheHttpClientConnectionManagerFactory connectionManagerFactory; - - @Autowired - OkHttpClientFactory okHttpClientFactory; - - @Autowired - OkHttpClientConnectionPoolFactory okHttpClientConnectionPoolFactory; - - @Test - public void connManFactory() { - then(ApacheHttpClientConnectionManagerFactory.class.isInstance(this.connectionManagerFactory)).isTrue(); - then(CustomApplication.MyApacheHttpClientConnectionManagerFactory.class - .isInstance(this.connectionManagerFactory)).isTrue(); - } - - @Test - public void apacheHttpClientFactory() { - then(ApacheHttpClientFactory.class.isInstance(this.httpClientFactory)).isTrue(); - then(CustomApplication.MyApacheHttpClientFactory.class.isInstance(this.httpClientFactory)).isTrue(); - } - - @Test - public void connectionPoolFactory() { - then(OkHttpClientConnectionPoolFactory.class.isInstance(this.okHttpClientConnectionPoolFactory)).isTrue(); - then(CustomApplication.MyOkHttpConnectionPoolFactory.class.isInstance(this.okHttpClientConnectionPoolFactory)) - .isTrue(); - } - - @Test - public void okHttpClientFactory() { - then(OkHttpClientFactory.class.isInstance(this.okHttpClientFactory)).isTrue(); - then(CustomApplication.MyOkHttpClientFactory.class.isInstance(this.okHttpClientFactory)).isTrue(); - } - -} - -@Configuration(proxyBeanMethods = false) -@EnableAutoConfiguration -class CustomApplication { - - public static void main(String[] args) { - SpringApplication.run(MyApplication.class, args); - } - - @Configuration(proxyBeanMethods = false) - static class MyConfig { - - @Bean - public ApacheHttpClientFactory clientFactory() { - return new MyApacheHttpClientFactory(); - } - - @Bean - public ApacheHttpClientConnectionManagerFactory connectionManagerFactory() { - return new MyApacheHttpClientConnectionManagerFactory(); - } - - @Bean - public OkHttpClientConnectionPoolFactory connectionPoolFactory() { - return new MyOkHttpConnectionPoolFactory(); - } - - @Bean - public OkHttpClientFactory okHttpClientFactory() { - return new MyOkHttpClientFactory(); - } - - } - - static class MyApacheHttpClientFactory implements ApacheHttpClientFactory { - - @Override - public HttpClientBuilder createBuilder() { - return HttpClientBuilder.create(); - } - - } - - static class MyApacheHttpClientConnectionManagerFactory implements ApacheHttpClientConnectionManagerFactory { - - @Override - public HttpClientConnectionManager newConnectionManager(boolean disableSslValidation, int maxTotalConnections, - int maxConnectionsPerRoute, long timeToLive, TimeUnit timeUnit, RegistryBuilder registryBuilder) { - return null; - } - - } - - static class MyOkHttpClientFactory implements OkHttpClientFactory { - - @Override - public OkHttpClient.Builder createBuilder(boolean disableSslValidation) { - return new OkHttpClient.Builder(); - } - - } - - static class MyOkHttpConnectionPoolFactory implements OkHttpClientConnectionPoolFactory { - - @Override - public ConnectionPool create(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) { - return null; - } - - } - -} diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/CustomOkHttpClientBuilderConfigurationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/CustomOkHttpClientBuilderConfigurationTests.java deleted file mode 100644 index 6bd2eeef..00000000 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/CustomOkHttpClientBuilderConfigurationTests.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import java.lang.reflect.Field; -import java.util.concurrent.TimeUnit; - -import okhttp3.OkHttpClient; -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.util.ReflectionUtils; - -import static org.assertj.core.api.BDDAssertions.then; - -/** - * @author Ryan Baxter - */ -@SpringBootTest(classes = CustomOkHttpClientBuilderApplication.class) -public class CustomOkHttpClientBuilderConfigurationTests { - - @Autowired - private OkHttpClientFactory okHttpClientFactory; - - @Test - public void testCustomBuilder() { - OkHttpClient.Builder builder = this.okHttpClientFactory.createBuilder(false); - Integer timeout = getField(builder, "connectTimeout"); - then(timeout.intValue()).isEqualTo(1); - } - - protected T getField(Object target, String name) { - Field field = ReflectionUtils.findField(target.getClass(), name); - ReflectionUtils.makeAccessible(field); - Object value = ReflectionUtils.getField(field, target); - return (T) value; - } - -} - -@Configuration(proxyBeanMethods = false) -@EnableAutoConfiguration -class CustomOkHttpClientBuilderApplication { - - public static void main(String[] args) { - SpringApplication.run(MyApplication.class, args); - } - - @Configuration(proxyBeanMethods = false) - static class MyConfig { - - @Bean - public OkHttpClient.Builder builder() { - return new OkHttpClient.Builder().connectTimeout(1, TimeUnit.MILLISECONDS); - } - - } - -} diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientConnectionManagerFactoryTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientConnectionManagerFactoryTests.java deleted file mode 100644 index 2d819bbc..00000000 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientConnectionManagerFactoryTests.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import java.lang.reflect.Field; -import java.util.concurrent.TimeUnit; - -import javax.net.ssl.SSLContextSpi; -import javax.net.ssl.SSLSocketFactory; -import javax.net.ssl.X509TrustManager; - -import org.apache.http.config.Lookup; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.conn.socket.ConnectionSocketFactory; -import org.apache.http.impl.conn.DefaultHttpClientConnectionOperator; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledForJreRange; -import org.junit.jupiter.api.condition.JRE; - -import org.springframework.util.ReflectionUtils; - -import static org.assertj.core.api.BDDAssertions.then; - -/** - * @author Ryan Baxter - * @author Michael Wirth - */ -public class DefaultApacheHttpClientConnectionManagerFactoryTests { - - @Test - public void newConnectionManager() { - HttpClientConnectionManager connectionManager = new DefaultApacheHttpClientConnectionManagerFactory() - .newConnectionManager(false, 2, 6); - then(((PoolingHttpClientConnectionManager) connectionManager).getDefaultMaxPerRoute()).isEqualTo(6); - then(((PoolingHttpClientConnectionManager) connectionManager).getMaxTotal()).isEqualTo(2); - Object pool = getField((connectionManager), "pool"); - then((Long) getField(pool, "timeToLive")).isEqualTo(new Long(-1)); - TimeUnit timeUnit = getField(pool, "timeUnit"); - then(timeUnit).isEqualTo(TimeUnit.MILLISECONDS); - } - - @Test - public void newConnectionManagerWithTTL() { - HttpClientConnectionManager connectionManager = new DefaultApacheHttpClientConnectionManagerFactory() - .newConnectionManager(false, 2, 6, 56L, TimeUnit.DAYS, null); - then(((PoolingHttpClientConnectionManager) connectionManager).getDefaultMaxPerRoute()).isEqualTo(6); - then(((PoolingHttpClientConnectionManager) connectionManager).getMaxTotal()).isEqualTo(2); - Object pool = getField((connectionManager), "pool"); - then((Long) getField(pool, "timeToLive")).isEqualTo(new Long(56)); - TimeUnit timeUnit = getField(pool, "timeUnit"); - then(timeUnit).isEqualTo(TimeUnit.DAYS); - } - - @Test - @DisabledForJreRange(min = JRE.JAVA_16) - public void newConnectionManagerWithSSL() { - HttpClientConnectionManager connectionManager = new DefaultApacheHttpClientConnectionManagerFactory() - .newConnectionManager(false, 2, 6); - - Lookup socketFactoryRegistry = getConnectionSocketFactoryLookup(connectionManager); - then(socketFactoryRegistry.lookup("https")).isNotNull(); - then(getX509TrustManager(socketFactoryRegistry).getAcceptedIssuers()).isNotNull(); - } - - @Test - @DisabledForJreRange(min = JRE.JAVA_16) - public void newConnectionManagerWithDisabledSSLValidation() { - HttpClientConnectionManager connectionManager = new DefaultApacheHttpClientConnectionManagerFactory() - .newConnectionManager(true, 2, 6); - - Lookup socketFactoryRegistry = getConnectionSocketFactoryLookup(connectionManager); - then(socketFactoryRegistry.lookup("https")).isNotNull(); - then(getX509TrustManager(socketFactoryRegistry).getAcceptedIssuers()).isNull(); - } - - private Lookup getConnectionSocketFactoryLookup( - HttpClientConnectionManager connectionManager) { - DefaultHttpClientConnectionOperator connectionOperator = getField(connectionManager, "connectionOperator"); - return getField(connectionOperator, "socketFactoryRegistry"); - } - - private X509TrustManager getX509TrustManager(Lookup socketFactoryRegistry) { - ConnectionSocketFactory connectionSocketFactory = socketFactoryRegistry.lookup("https"); - SSLSocketFactory sslSocketFactory = getField(connectionSocketFactory, "socketfactory"); - SSLContextSpi sslContext = getField(sslSocketFactory, "context"); - return getField(sslContext, "trustManager"); - } - - @SuppressWarnings("unchecked") - protected T getField(Object target, String name) { - Field field = ReflectionUtils.findField(target.getClass(), name); - if (field == null) { - throw new IllegalArgumentException("Can not find field " + name + " in " + target.getClass()); - } - ReflectionUtils.makeAccessible(field); - Object value = ReflectionUtils.getField(field, target); - return (T) value; - } - -} diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientFactoryTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientFactoryTests.java deleted file mode 100644 index f59eaf96..00000000 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientFactoryTests.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import java.lang.reflect.Field; - -import org.apache.http.client.config.CookieSpecs; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.Configurable; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.assertj.core.api.BDDAssertions; -import org.junit.jupiter.api.Test; - -import org.springframework.util.ReflectionUtils; - -import static org.assertj.core.api.BDDAssertions.then; -import static org.mockito.Mockito.mock; - -/** - * @author Ryan Baxter - */ -public class DefaultApacheHttpClientFactoryTests { - - @Test - public void createClient() { - final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(100).setConnectTimeout(200) - .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); - CloseableHttpClient httpClient = new DefaultApacheHttpClientFactory(HttpClientBuilder.create()).createBuilder() - .setConnectionManager(mock(HttpClientConnectionManager.class)).setDefaultRequestConfig(requestConfig) - .build(); - BDDAssertions.then(httpClient).isInstanceOf(Configurable.class); - RequestConfig config = ((Configurable) httpClient).getConfig(); - then(config.getSocketTimeout()).isEqualTo(100); - then(config.getConnectTimeout()).isEqualTo(200); - then(config.getCookieSpec()).isEqualTo(CookieSpecs.IGNORE_COOKIES); - } - - protected T getField(Object target, String name) { - Field field = ReflectionUtils.findField(target.getClass(), name); - ReflectionUtils.makeAccessible(field); - Object value = ReflectionUtils.getField(field, target); - return (T) value; - } - -} diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultHttpClientConfigurationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultHttpClientConfigurationTests.java deleted file mode 100644 index aba80a8e..00000000 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultHttpClientConfigurationTests.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import org.junit.jupiter.api.Test; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.Configuration; - -import static org.assertj.core.api.BDDAssertions.then; - -/** - * @author Ryan Baxter - */ -@SpringBootTest(classes = MyApplication.class, properties = { "spring.cloud.httpclient.ok.enabled: true" }) -public class DefaultHttpClientConfigurationTests { - - @Autowired - ApacheHttpClientFactory httpClientFactory; - - @Autowired - ApacheHttpClientConnectionManagerFactory connectionManagerFactory; - - @Autowired - OkHttpClientFactory okHttpClientFactory; - - @Autowired - OkHttpClientConnectionPoolFactory okHttpClientConnectionPoolFactory; - - @Test - public void connManFactory() { - then(ApacheHttpClientConnectionManagerFactory.class.isInstance(this.connectionManagerFactory)).isTrue(); - then(DefaultApacheHttpClientConnectionManagerFactory.class.isInstance(this.connectionManagerFactory)).isTrue(); - } - - @Test - public void apacheHttpClientFactory() { - then(ApacheHttpClientFactory.class.isInstance(this.httpClientFactory)).isTrue(); - then(DefaultApacheHttpClientFactory.class.isInstance(this.httpClientFactory)).isTrue(); - } - - @Test - public void connPoolFactory() { - then(OkHttpClientConnectionPoolFactory.class.isInstance(this.okHttpClientConnectionPoolFactory)).isTrue(); - then(DefaultOkHttpClientConnectionPoolFactory.class.isInstance(this.okHttpClientConnectionPoolFactory)) - .isTrue(); - } - - @Test - public void setOkHttpClientFactory() { - then(OkHttpClientFactory.class.isInstance(this.okHttpClientFactory)).isTrue(); - then(DefaultOkHttpClientFactory.class.isInstance(this.okHttpClientFactory)).isTrue(); - } - -} - -@Configuration(proxyBeanMethods = false) -@EnableAutoConfiguration -class MyApplication { - - public static void main(String[] args) { - SpringApplication.run(MyApplication.class, args); - } - -} diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientConnectionPoolFactoryTest.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientConnectionPoolFactoryTest.java deleted file mode 100644 index 99f2e4f0..00000000 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientConnectionPoolFactoryTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import java.lang.reflect.Field; -import java.util.concurrent.TimeUnit; - -import okhttp3.ConnectionPool; -import okhttp3.internal.connection.RealConnectionPool; -import org.junit.jupiter.api.Test; - -import org.springframework.util.ReflectionUtils; - -import static org.assertj.core.api.BDDAssertions.then; - -/** - * @author Ryan Baxter - */ -public class DefaultOkHttpClientConnectionPoolFactoryTest { - - @Test - public void create() { - DefaultOkHttpClientConnectionPoolFactory connectionPoolFactory = new DefaultOkHttpClientConnectionPoolFactory(); - ConnectionPool connectionPool = connectionPoolFactory.create(2, 3, TimeUnit.MILLISECONDS); - RealConnectionPool delegate = getField(connectionPool, "delegate"); - int idleConnections = getField(delegate, "maxIdleConnections"); - long keepAliveDuration = getField(delegate, "keepAliveDurationNs"); - then(idleConnections).isEqualTo(2); - then(keepAliveDuration).isEqualTo(TimeUnit.MILLISECONDS.toNanos(3)); - } - - protected T getField(Object target, String name) { - Field field = ReflectionUtils.findField(target.getClass(), name); - if (field == null) { - throw new IllegalArgumentException("Can not find field " + name + " in " + target.getClass()); - } - ReflectionUtils.makeAccessible(field); - Object value = ReflectionUtils.getField(field, target); - return (T) value; - } - -} diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientFactoryTest.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientFactoryTest.java deleted file mode 100644 index cb2b66f0..00000000 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientFactoryTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2012-2020 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.springframework.cloud.commons.httpclient; - -import java.util.concurrent.TimeUnit; - -import okhttp3.ConnectionPool; -import okhttp3.OkHttpClient; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.BDDAssertions.then; - -/** - * @author Ryan Baxter - */ -public class DefaultOkHttpClientFactoryTest { - - @Test - public void create() throws Exception { - DefaultOkHttpClientFactory okHttpClientFactory = new DefaultOkHttpClientFactory(new OkHttpClient.Builder()); - DefaultOkHttpClientConnectionPoolFactory poolFactory = new DefaultOkHttpClientConnectionPoolFactory(); - ConnectionPool pool = poolFactory.create(4, 5, TimeUnit.DAYS); - OkHttpClient httpClient = okHttpClientFactory.createBuilder(true).connectTimeout(2, TimeUnit.MILLISECONDS) - .readTimeout(3, TimeUnit.HOURS).followRedirects(true).connectionPool(pool).build(); - then(httpClient.connectTimeoutMillis()).isEqualTo(2); - then(httpClient.readTimeoutMillis()).isEqualTo(TimeUnit.HOURS.toMillis(3)); - then(httpClient.followRedirects()).isTrue(); - then(httpClient.connectionPool()).isEqualTo(pool); - then(OkHttpClientFactory.TrustAllHostnames.class.isInstance(httpClient.hostnameVerifier())).isTrue(); - } - -}