From 7dfc6db1cff6648606ba5afc53eb85f3866266d7 Mon Sep 17 00:00:00 2001 From: Randell Date: Wed, 23 Oct 2024 21:20:59 -0600 Subject: [PATCH] Upgrade spring version --- pom.xml | 2 +- .../client/impl/PublicKeyClient.java | 2 +- .../cloudconfig/template/ConfigTemplate.java | 32 ++++++++----------- .../template/impl/LocalConfigTemplate.java | 18 ++--------- .../template/impl/OAuth2ConfigTemplate.java | 3 +- .../template/ConfigTemplateTest.java | 11 +++---- .../impl/LocalConfigTemplateTest.java | 9 ------ 7 files changed, 24 insertions(+), 53 deletions(-) diff --git a/pom.xml b/pom.xml index 2e240d5..25bb0f0 100644 --- a/pom.xml +++ b/pom.xml @@ -40,7 +40,7 @@ 1.6.13 3.5.0 - 2.7.4 + 3.3.4 1.1.2 Piszmog_cloud-config-client diff --git a/src/main/java/io/github/piszmog/cloudconfig/client/impl/PublicKeyClient.java b/src/main/java/io/github/piszmog/cloudconfig/client/impl/PublicKeyClient.java index 673cded..99d0a83 100644 --- a/src/main/java/io/github/piszmog/cloudconfig/client/impl/PublicKeyClient.java +++ b/src/main/java/io/github/piszmog/cloudconfig/client/impl/PublicKeyClient.java @@ -1,8 +1,8 @@ package io.github.piszmog.cloudconfig.client.impl; import io.github.piszmog.cloudconfig.ConfigException; -import io.github.piszmog.cloudconfig.template.ConfigTemplate; import io.github.piszmog.cloudconfig.client.ConfigClient; +import io.github.piszmog.cloudconfig.template.ConfigTemplate; import org.apache.commons.lang3.StringUtils; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; diff --git a/src/main/java/io/github/piszmog/cloudconfig/template/ConfigTemplate.java b/src/main/java/io/github/piszmog/cloudconfig/template/ConfigTemplate.java index 7d64c4b..06d4687 100644 --- a/src/main/java/io/github/piszmog/cloudconfig/template/ConfigTemplate.java +++ b/src/main/java/io/github/piszmog/cloudconfig/template/ConfigTemplate.java @@ -2,21 +2,23 @@ import io.github.piszmog.cloudconfig.ConfigException; import org.apache.commons.lang3.StringUtils; -import org.apache.http.client.HttpClient; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.hc.client5.http.classic.HttpClient; +import org.apache.hc.client5.http.config.RequestConfig; +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.io.HttpClientConnectionManager; import org.springframework.cloud.config.client.ConfigClientProperties; import org.springframework.cloud.config.client.ConfigClientStateHolder; import org.springframework.http.*; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -import org.springframework.util.Base64Utils; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; +import java.util.Base64; import java.util.Collections; import static org.springframework.cloud.config.client.ConfigClientProperties.*; @@ -27,7 +29,6 @@ * Created by Piszmog on 5/5/2018 */ public abstract class ConfigTemplate { - protected static final int DEFAULT_READ_TIMEOUT = (60 * 1000 * 3) + 5000; private static final String HEADER_AUTHORIZATION = "Authorization"; private static final int DEFAULT_MAX_PER_ROUTE = 10; private static final int DEFAULT_TOTAL_CONNECTIONS = 100; @@ -128,7 +129,7 @@ private HttpHeaders addSecurityHeaders(final HttpHeaders httpHeaders, final Stri throw new IllegalArgumentException("You must set either 'password' or 'authorization.' Both cannot be used."); } if (password != null) { - byte[] credentialsEncoded = Base64Utils.encode((username + ":" + password).getBytes()); + byte[] credentialsEncoded = Base64.getEncoder().encode((username + ":" + password).getBytes()); headers.add(HEADER_AUTHORIZATION, "Basic " + new String(credentialsEncoded)); } else if (authorization != null) { headers.add(HEADER_AUTHORIZATION, authorization); @@ -203,22 +204,17 @@ public String getLabel() { * Creates a pooling request factory with the provided timeout. The pool of connections allow for 10 max connections * per route with a total of 100 connections. * - * @param timeout the timeout for the request timeout, connection timeout, and the socket timeout * @return The client http request factory. */ - protected ClientHttpRequestFactory createHttpClientFactory(final int timeout) { - final RequestConfig requestConfig = buildRequestConfig(timeout); - final PoolingHttpClientConnectionManager connectionManager = createConnectionManager(); + protected ClientHttpRequestFactory createHttpClientFactory() { + final RequestConfig requestConfig = buildRequestConfig(); + final HttpClientConnectionManager connectionManager = createConnectionManager(); final HttpClient httpClient = buildHttpClient(requestConfig, connectionManager); return new HttpComponentsClientHttpRequestFactory(httpClient); } - private RequestConfig buildRequestConfig(final int timeout) { - return RequestConfig.custom() - .setConnectionRequestTimeout(timeout) - .setConnectTimeout(timeout) - .setSocketTimeout(timeout) - .build(); + private RequestConfig buildRequestConfig() { + return RequestConfig.custom().build(); } private PoolingHttpClientConnectionManager createConnectionManager() { @@ -228,7 +224,7 @@ private PoolingHttpClientConnectionManager createConnectionManager() { return connectionManager; } - private HttpClient buildHttpClient(final RequestConfig requestConfig, final PoolingHttpClientConnectionManager connectionManager) { + private CloseableHttpClient buildHttpClient(final RequestConfig requestConfig, final HttpClientConnectionManager connectionManager) { return HttpClients.custom() .setConnectionManager(connectionManager) .setDefaultRequestConfig(requestConfig) diff --git a/src/main/java/io/github/piszmog/cloudconfig/template/impl/LocalConfigTemplate.java b/src/main/java/io/github/piszmog/cloudconfig/template/impl/LocalConfigTemplate.java index 290c219..d5b316d 100644 --- a/src/main/java/io/github/piszmog/cloudconfig/template/impl/LocalConfigTemplate.java +++ b/src/main/java/io/github/piszmog/cloudconfig/template/impl/LocalConfigTemplate.java @@ -1,11 +1,11 @@ package io.github.piszmog.cloudconfig.template.impl; import io.github.piszmog.cloudconfig.template.ConfigTemplate; +import jakarta.annotation.PostConstruct; import org.springframework.cloud.config.client.ConfigClientProperties; import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator; import org.springframework.web.client.RestTemplate; -import javax.annotation.PostConstruct; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -18,8 +18,6 @@ * Created by Piszmog on 5/5/2018 */ public class LocalConfigTemplate extends ConfigTemplate { - private final int readTimeout; - /** * Creates a local config template using the default read timeout. * @@ -27,18 +25,6 @@ public class LocalConfigTemplate extends ConfigTemplate { */ public LocalConfigTemplate(final ConfigClientProperties configClientProperties) { super(configClientProperties); - this.readTimeout = DEFAULT_READ_TIMEOUT; - } - - /** - * Creates a local config template using the provided read timeout. - * - * @param configClientProperties the config server properties - * @param readTimeout the read timeout - */ - public LocalConfigTemplate(final ConfigClientProperties configClientProperties, final int readTimeout) { - super(configClientProperties); - this.readTimeout = readTimeout; } /** @@ -47,7 +33,7 @@ public LocalConfigTemplate(final ConfigClientProperties configClientProperties, @PostConstruct public void init() { restTemplate = new RestTemplate(); - restTemplate.setRequestFactory(createHttpClientFactory(readTimeout)); + restTemplate.setRequestFactory(createHttpClientFactory()); final Map headers = new HashMap<>(configClientProperties.getHeaders()); headers.remove(AUTHORIZATION); if (!headers.isEmpty()) { diff --git a/src/main/java/io/github/piszmog/cloudconfig/template/impl/OAuth2ConfigTemplate.java b/src/main/java/io/github/piszmog/cloudconfig/template/impl/OAuth2ConfigTemplate.java index 929e28b..9f588e8 100644 --- a/src/main/java/io/github/piszmog/cloudconfig/template/impl/OAuth2ConfigTemplate.java +++ b/src/main/java/io/github/piszmog/cloudconfig/template/impl/OAuth2ConfigTemplate.java @@ -3,13 +3,12 @@ import io.github.piszmog.cloudconfig.template.ConfigTemplate; import io.pivotal.spring.cloud.config.client.ConfigClientOAuth2Properties; import io.pivotal.spring.cloud.config.client.OAuth2AuthorizedClientHttpRequestInterceptor; +import jakarta.annotation.PostConstruct; import org.springframework.cloud.config.client.ConfigClientProperties; import org.springframework.security.oauth2.client.registration.ClientRegistration; import org.springframework.security.oauth2.core.AuthorizationGrantType; import org.springframework.web.client.RestTemplate; -import javax.annotation.PostConstruct; - /** * Config template used when OAuth2 authentication is required with the config server. *

diff --git a/src/test/java/io/github/piszmog/cloudconfig/template/ConfigTemplateTest.java b/src/test/java/io/github/piszmog/cloudconfig/template/ConfigTemplateTest.java index 4d814ac..3870646 100644 --- a/src/test/java/io/github/piszmog/cloudconfig/template/ConfigTemplateTest.java +++ b/src/test/java/io/github/piszmog/cloudconfig/template/ConfigTemplateTest.java @@ -1,6 +1,7 @@ package io.github.piszmog.cloudconfig.template; import io.github.piszmog.cloudconfig.ConfigException; +import org.apache.hc.core5.util.Timeout; import org.assertj.core.api.AbstractObjectAssert; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -207,12 +208,10 @@ public void testGetLabel() { @Test public void testCreateHttpClientFactory() { - final ClientHttpRequestFactory factory = template.createFactory(10); + final ClientHttpRequestFactory factory = template.createFactory(); final AbstractObjectAssert httpClient = assertThat(factory).extracting("httpClient"); httpClient.extracting("defaultConfig") - .hasFieldOrPropertyWithValue("connectionRequestTimeout", 10) - .hasFieldOrPropertyWithValue("connectTimeout", 10) - .hasFieldOrPropertyWithValue("socketTimeout", 10); + .hasFieldOrPropertyWithValue("connectionRequestTimeout", Timeout.ofMinutes(3)); httpClient.extracting("connManager") .extracting("pool") .hasFieldOrPropertyWithValue("defaultMaxPerRoute", 10) @@ -225,8 +224,8 @@ public TestConfigTemplate(ConfigClientProperties configClientProperties, RestTem this.restTemplate = restTemplate; } - public ClientHttpRequestFactory createFactory(int timeout) { - return this.createHttpClientFactory(timeout); + public ClientHttpRequestFactory createFactory() { + return this.createHttpClientFactory(); } } } diff --git a/src/test/java/io/github/piszmog/cloudconfig/template/impl/LocalConfigTemplateTest.java b/src/test/java/io/github/piszmog/cloudconfig/template/impl/LocalConfigTemplateTest.java index 8b714c9..f39114f 100644 --- a/src/test/java/io/github/piszmog/cloudconfig/template/impl/LocalConfigTemplateTest.java +++ b/src/test/java/io/github/piszmog/cloudconfig/template/impl/LocalConfigTemplateTest.java @@ -18,15 +18,6 @@ public void testCreate_properties() { assertThat(template).hasFieldOrPropertyWithValue("readTimeout", 185000); } - @Test - public void testCreate_timeout() { - final LocalConfigTemplate template = new LocalConfigTemplate( - new ConfigClientProperties(new StandardEnvironment()), - 100); - assertNotNull(template); - assertThat(template).hasFieldOrPropertyWithValue("readTimeout", 100); - } - @Test public void testInit() { final LocalConfigTemplate template = new LocalConfigTemplate(new ConfigClientProperties(new StandardEnvironment()));