mirror of
https://github.com/Alfresco/alfresco-transform-core.git
synced 2025-05-26 17:24:47 +00:00
[ACS-4460] simplify WebClient.Builder creation
This commit is contained in:
parent
3b2d1c5639
commit
5a2665dc2f
@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2015-2023 Alfresco Software, Ltd. All rights reserved.
|
||||||
|
*
|
||||||
|
* License rights for this program may be obtained from Alfresco Software, Ltd.
|
||||||
|
* pursuant to a written agreement and any use of this program without such an
|
||||||
|
* agreement is prohibited.
|
||||||
|
*/
|
||||||
|
package org.alfresco.transform.base;
|
||||||
|
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface WebClientBuilderAdjuster
|
||||||
|
{
|
||||||
|
void adjust(WebClient.Builder builder);
|
||||||
|
}
|
@ -28,6 +28,7 @@ package org.alfresco.transform.base.config;
|
|||||||
|
|
||||||
import io.netty.handler.ssl.SslContext;
|
import io.netty.handler.ssl.SslContext;
|
||||||
import io.netty.handler.ssl.SslContextBuilder;
|
import io.netty.handler.ssl.SslContextBuilder;
|
||||||
|
import org.alfresco.transform.base.WebClientBuilderAdjuster;
|
||||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClients;
|
import org.apache.http.impl.client.HttpClients;
|
||||||
@ -35,7 +36,6 @@ import org.apache.http.ssl.SSLContextBuilder;
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Scope;
|
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||||
@ -46,6 +46,7 @@ import reactor.netty.http.client.HttpClient;
|
|||||||
|
|
||||||
import javax.net.ssl.KeyManagerFactory;
|
import javax.net.ssl.KeyManagerFactory;
|
||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
|
import javax.net.ssl.SSLException;
|
||||||
import javax.net.ssl.TrustManagerFactory;
|
import javax.net.ssl.TrustManagerFactory;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -78,15 +79,20 @@ public class MTLSConfig {
|
|||||||
private String trustStoreType;
|
private String trustStoreType;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Scope("prototype")
|
public WebClientBuilderAdjuster webClientBuilderAdjuster(SslContextBuilder nettySslContextBuilder)
|
||||||
public WebClient.Builder clientBuilder(SslContextBuilder nettySslContextBuilder) throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException
|
|
||||||
{
|
{
|
||||||
if(isTlsOrMtlsConfigured())
|
return builder -> {
|
||||||
{
|
if(isTlsOrMtlsConfigured())
|
||||||
return createWebClientBuilderWithSslContext(nettySslContextBuilder);
|
{
|
||||||
} else {
|
HttpClient httpClientWithSslContext = null;
|
||||||
return WebClient.builder();
|
try {
|
||||||
}
|
httpClientWithSslContext = createHttpClientWithSslContext(nettySslContextBuilder);
|
||||||
|
} catch (SSLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
builder.clientConnector(new ReactorClientHttpConnector(httpClientWithSslContext));
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ -149,15 +155,12 @@ public class MTLSConfig {
|
|||||||
return keyStoreResource != null;
|
return keyStoreResource != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private WebClient.Builder createWebClientBuilderWithSslContext(SslContextBuilder sslContextBuilder) throws UnrecoverableKeyException, CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException
|
private HttpClient createHttpClientWithSslContext(SslContextBuilder sslContextBuilder) throws SSLException {
|
||||||
{
|
|
||||||
SslContext sslContext = sslContextBuilder.build();
|
SslContext sslContext = sslContextBuilder.build();
|
||||||
HttpClient httpClient = HttpClient.create().secure(p -> p.sslContext(sslContext));
|
return HttpClient.create().secure(p -> p.sslContext(sslContext));
|
||||||
return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private RestTemplate createRestTemplateWithSslContext(SSLContextBuilder sslContextBuilder) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, KeyManagementException, UnrecoverableKeyException
|
private RestTemplate createRestTemplateWithSslContext(SSLContextBuilder sslContextBuilder) throws NoSuchAlgorithmException, KeyManagementException {
|
||||||
{
|
|
||||||
SSLContext sslContext = sslContextBuilder.build();
|
SSLContext sslContext = sslContextBuilder.build();
|
||||||
SSLConnectionSocketFactory sslContextFactory = new SSLConnectionSocketFactory(sslContext);
|
SSLConnectionSocketFactory sslContextFactory = new SSLConnectionSocketFactory(sslContext);
|
||||||
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslContextFactory).build();
|
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslContextFactory).build();
|
||||||
|
@ -34,6 +34,7 @@ import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
|
import org.alfresco.transform.base.WebClientBuilderAdjuster;
|
||||||
import org.alfresco.transform.exceptions.TransformException;
|
import org.alfresco.transform.exceptions.TransformException;
|
||||||
import org.alfresco.transform.base.model.FileRefResponse;
|
import org.alfresco.transform.base.model.FileRefResponse;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -53,6 +54,7 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
import javax.net.ssl.SSLException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple Rest client that call Alfresco Shared File Store
|
* Simple Rest client that call Alfresco Shared File Store
|
||||||
@ -69,13 +71,14 @@ public class SharedFileStoreClient
|
|||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private WebClient.Builder clientBuilder;
|
private WebClientBuilderAdjuster adjuster;
|
||||||
|
|
||||||
private WebClient client;
|
private WebClient client;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init()
|
public void init() throws SSLException {
|
||||||
{
|
final WebClient.Builder clientBuilder = WebClient.builder();
|
||||||
|
adjuster.adjust(clientBuilder);
|
||||||
client = clientBuilder.baseUrl(url.endsWith("/") ? url : url + "/")
|
client = clientBuilder.baseUrl(url.endsWith("/") ? url : url + "/")
|
||||||
.defaultHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
|
.defaultHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
|
||||||
.defaultHeader(ACCEPT, APPLICATION_JSON_VALUE)
|
.defaultHeader(ACCEPT, APPLICATION_JSON_VALUE)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user