Skip to content
Snippets Groups Projects
Commit c54e9d96 authored by Makhtar DIAGNE's avatar Makhtar DIAGNE
Browse files

[TECH] Define a different token for each REST clientfactory

parent 94ac90b0
No related branches found
No related tags found
1 merge request!1Feature/design/1
...@@ -47,11 +47,11 @@ import java.security.NoSuchAlgorithmException; ...@@ -47,11 +47,11 @@ import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException; import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.util.List; import java.util.List;
import java.util.UUID;
import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import fr.gouv.vitamui.commons.rest.client.configuration.RestClientConfiguration;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Registry; import org.apache.http.config.Registry;
...@@ -106,14 +106,14 @@ public class BaseRestClientFactory implements RestClientFactory { ...@@ -106,14 +106,14 @@ public class BaseRestClientFactory implements RestClientFactory {
} }
public BaseRestClientFactory(final RestClientConfiguration restClientConfig, final HttpPoolConfiguration httpPoolConfig, public BaseRestClientFactory(final RestClientConfiguration restClientConfig, final HttpPoolConfiguration httpPoolConfig,
final RestTemplateBuilder restTemplateBuilder) { final RestTemplateBuilder restTemplateBuilder) {
Assert.notNull(restClientConfig, "Rest client configuration must be specified"); Assert.notNull(restClientConfig, "Rest client configuration must be specified");
final boolean useSSL = restClientConfig.isSecure(); final boolean useSSL = restClientConfig.isSecure();
baseUrl = RestUtils.getScheme(useSSL) + restClientConfig.getServerHost() + ":" + restClientConfig.getServerPort(); baseUrl = RestUtils.getScheme(useSSL) + restClientConfig.getServerHost() + ":" + restClientConfig.getServerPort();
HttpPoolConfiguration myPoolConfig = httpPoolConfig; HttpPoolConfiguration myPoolConfig = httpPoolConfig;
// configure the pool from the restClientConfig if poolMaxTotal is not negative // configure the pool from the restClientConfig if the value of poolMaxTotal is positive
if(restClientConfig.getPoolMaxTotal() >= 0) { if(restClientConfig.getPoolMaxTotal() >= 0) {
myPoolConfig = new HttpPoolConfiguration(); myPoolConfig = new HttpPoolConfiguration();
myPoolConfig.setMaxTotal(restClientConfig.getPoolMaxTotal()); myPoolConfig.setMaxTotal(restClientConfig.getPoolMaxTotal());
...@@ -129,7 +129,7 @@ public class BaseRestClientFactory implements RestClientFactory { ...@@ -129,7 +129,7 @@ public class BaseRestClientFactory implements RestClientFactory {
.build(); .build();
restTemplate = restTemplateBuilder.errorHandler(new ErrorHandler()).build(); restTemplate = restTemplateBuilder.errorHandler(new ErrorHandler()).build();
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new CustomHttpComponentsClientHttpRequestFactory(httpClient))); restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new CustomHttpComponentsClientHttpRequestFactory(httpClient, UUID.randomUUID().toString())));
} }
/* /*
...@@ -156,7 +156,7 @@ public class BaseRestClientFactory implements RestClientFactory { ...@@ -156,7 +156,7 @@ public class BaseRestClientFactory implements RestClientFactory {
} }
sslContext = sslContextBuilder.loadTrustMaterial(new File(ts.getKeyPath()), ts.getKeyPassword().toCharArray()).setProtocol("TLS") sslContext = sslContextBuilder.loadTrustMaterial(new File(ts.getKeyPath()), ts.getKeyPassword().toCharArray()).setProtocol("TLS")
.setSecureRandom(new java.security.SecureRandom()).build(); .setSecureRandom(new java.security.SecureRandom()).build();
} }
catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException | CertificateException | IOException | UnrecoverableKeyException e) { catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException | CertificateException | IOException | UnrecoverableKeyException e) {
LOGGER.error("Unable to build the Registry<ConnectionSocketFactory>.", e); LOGGER.error("Unable to build the Registry<ConnectionSocketFactory>.", e);
...@@ -171,7 +171,7 @@ public class BaseRestClientFactory implements RestClientFactory { ...@@ -171,7 +171,7 @@ public class BaseRestClientFactory implements RestClientFactory {
} }
private KeyStore loadPkcs(final String type, final String filename, final char[] password) private KeyStore loadPkcs(final String type, final String filename, final char[] password)
throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException { throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException {
final KeyStore keyStore = KeyStore.getInstance(type); final KeyStore keyStore = KeyStore.getInstance(type);
final File key = ResourceUtils.getFile(filename); final File key = ResourceUtils.getFile(filename);
try (InputStream in = new FileInputStream(key)) { try (InputStream in = new FileInputStream(key)) {
...@@ -187,11 +187,11 @@ public class BaseRestClientFactory implements RestClientFactory { ...@@ -187,11 +187,11 @@ public class BaseRestClientFactory implements RestClientFactory {
* from the pool rather than creating a brand new connection. * from the pool rather than creating a brand new connection.
*/ */
private PoolingHttpClientConnectionManager buildConnectionManager(final HttpPoolConfiguration poolConfig, private PoolingHttpClientConnectionManager buildConnectionManager(final HttpPoolConfiguration poolConfig,
final Registry<ConnectionSocketFactory> socketFactoryRegistry) { final Registry<ConnectionSocketFactory> socketFactoryRegistry) {
final PoolingHttpClientConnectionManager connectionManager = (socketFactoryRegistry != null) final PoolingHttpClientConnectionManager connectionManager = (socketFactoryRegistry != null)
? new PoolingHttpClientConnectionManager(socketFactoryRegistry) ? new PoolingHttpClientConnectionManager(socketFactoryRegistry)
: new PoolingHttpClientConnectionManager(); : new PoolingHttpClientConnectionManager();
if (poolConfig != null) { if (poolConfig != null) {
connectionManager.setMaxTotal(poolConfig.getMaxTotal()); connectionManager.setMaxTotal(poolConfig.getMaxTotal());
...@@ -209,7 +209,7 @@ public class BaseRestClientFactory implements RestClientFactory { ...@@ -209,7 +209,7 @@ public class BaseRestClientFactory implements RestClientFactory {
private RequestConfig buildRequestConfig() { private RequestConfig buildRequestConfig() {
return RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout).setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout) return RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout).setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout)
.build(); .build();
} }
@Override @Override
......
package fr.gouv.vitamui.commons.rest.client; /**
* Copyright French Prime minister Office/SGMAP/DINSIC/Vitam Program (2019-2020)
* and the signatories of the "VITAM - Accord du Contributeur" agreement.
*
* contact@programmevitam.fr
*
* This software is a computer program whose purpose is to implement
* implement a digital archiving front-office system for the secure and
* efficient high volumetry VITAM solution.
*
* This software is governed by the CeCILL-C license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/ or redistribute the software under the terms of the CeCILL-C
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/
package fr.gouv.vitamui.commons.rest.client;
import fr.gouv.vitamui.commons.api.logger.VitamUILogger; import fr.gouv.vitamui.commons.api.logger.VitamUILogger;
import fr.gouv.vitamui.commons.api.logger.VitamUILoggerFactory; import fr.gouv.vitamui.commons.api.logger.VitamUILoggerFactory;
...@@ -6,8 +42,10 @@ import org.apache.http.client.protocol.HttpClientContext; ...@@ -6,8 +42,10 @@ import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.StringUtils;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import java.net.URI; import java.net.URI;
import java.util.UUID;
/** /**
* Custom HttpComponentsClientHttpRequestFactory to override createContext * Custom HttpComponentsClientHttpRequestFactory to override createContext
...@@ -16,6 +54,8 @@ public class CustomHttpComponentsClientHttpRequestFactory extends HttpComponents ...@@ -16,6 +54,8 @@ public class CustomHttpComponentsClientHttpRequestFactory extends HttpComponents
private static final VitamUILogger LOGGER = VitamUILoggerFactory.getInstance(CustomHttpComponentsClientHttpRequestFactory.class); private static final VitamUILogger LOGGER = VitamUILoggerFactory.getInstance(CustomHttpComponentsClientHttpRequestFactory.class);
private String userToken = UUID.randomUUID().toString();
/** /**
* default construct * default construct
*/ */
...@@ -31,6 +71,18 @@ public class CustomHttpComponentsClientHttpRequestFactory extends HttpComponents ...@@ -31,6 +71,18 @@ public class CustomHttpComponentsClientHttpRequestFactory extends HttpComponents
super(httpClient); super(httpClient);
} }
/**
* construct with httpClient
* @param httpClient
*/
public CustomHttpComponentsClientHttpRequestFactory(HttpClient httpClient, String userToken) {
super(httpClient);
// set factory userToken if defined or else use a random uuid
if(!StringUtils.isEmpty(userToken)) {
this.userToken = userToken;
}
}
/** /**
* Create the httpContext and init with a userToken * Create the httpContext and init with a userToken
* @param httpMethod * @param httpMethod
...@@ -39,9 +91,8 @@ public class CustomHttpComponentsClientHttpRequestFactory extends HttpComponents ...@@ -39,9 +91,8 @@ public class CustomHttpComponentsClientHttpRequestFactory extends HttpComponents
*/ */
@Override @Override
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) { protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
LOGGER.debug("Context creation");
HttpContext context = HttpClientContext.create(); HttpContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.USER_TOKEN, "fake_user_token_value"); context.setAttribute(HttpClientContext.USER_TOKEN, userToken);
return context; return context;
} }
......
...@@ -79,12 +79,12 @@ public class RestClientConfiguration { ...@@ -79,12 +79,12 @@ public class RestClientConfiguration {
private int writeTimeOut = 10; private int writeTimeOut = 10;
/** /**
* total pool size for httpClient * Total pool size for httpClient.
*/ */
private int poolMaxTotal = 20; private int poolMaxTotal = 20;
/** /**
* pool size per route(host) * Pool size per route(host).
*/ */
private int poolMaxPerRoute = 20; private int poolMaxPerRoute = 20;
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment