Skip to content

Commit

Permalink
Upgrade spring version
Browse files Browse the repository at this point in the history
  • Loading branch information
Piszmog committed Oct 24, 2024
1 parent adf1b5f commit 7dfc6db
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 53 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<nexus-staging-maven-plugin.version>1.6.13</nexus-staging-maven-plugin.version>
<!--Dependency Management-->
<spring-cloud-services-dependencies.version>3.5.0</spring-cloud-services-dependencies.version>
<spring-boot-dependencies.version>2.7.4</spring-boot-dependencies.version>
<spring-boot-dependencies.version>3.3.4</spring-boot-dependencies.version>
<pitest-junit5-plugin.version>1.1.2</pitest-junit5-plugin.version>
<!-- Sonar -->
<sonar.projectKey>Piszmog_cloud-config-client</sonar.projectKey>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,27 +18,13 @@
* 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.
*
* @param configClientProperties the config server properties.
*/
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;
}

/**
Expand All @@ -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<String, String> headers = new HashMap<>(configClientProperties.getHeaders());
headers.remove(AUTHORIZATION);
if (!headers.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down

0 comments on commit 7dfc6db

Please sign in to comment.