X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=enrichment-coordinator-service%2Fsrc%2Fmain%2Fjava%2Forg%2Foransc%2Fenrichment%2Fclients%2FAsyncRestClient.java;h=f0f6c4b1c4fae8d3add614b64963d919e34834e4;hb=1da5c887797ae8cb715ad0b16b388aae18cda948;hp=76da6246996b03b9d993355ac0ce4b553b30cbb0;hpb=d1702a33a3527cf46527c7cd61f435bd7ed08522;p=nonrtric.git diff --git a/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/clients/AsyncRestClient.java b/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/clients/AsyncRestClient.java index 76da6246..f0f6c4b1 100644 --- a/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/clients/AsyncRestClient.java +++ b/enrichment-coordinator-service/src/main/java/org/oransc/enrichment/clients/AsyncRestClient.java @@ -28,6 +28,7 @@ import io.netty.handler.timeout.WriteTimeoutHandler; import java.lang.invoke.MethodHandles; import java.util.concurrent.atomic.AtomicInteger; +import org.oransc.enrichment.configuration.WebClientConfig.HttpProxyConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.MediaType; @@ -42,6 +43,7 @@ import org.springframework.web.reactive.function.client.WebClientResponseExcepti import reactor.core.publisher.Mono; import reactor.netty.http.client.HttpClient; import reactor.netty.resources.ConnectionProvider; +import reactor.netty.tcp.ProxyProvider.Proxy; import reactor.netty.tcp.TcpClient; /** @@ -54,19 +56,12 @@ public class AsyncRestClient { private final String baseUrl; private static final AtomicInteger sequenceNumber = new AtomicInteger(); private final SslContext sslContext; + private final HttpProxyConfig httpProxyConfig; - /** - * Note that only http (not https) will work when this constructor is used. - * - * @param baseUrl - */ - public AsyncRestClient(String baseUrl) { - this(baseUrl, null); - } - - public AsyncRestClient(String baseUrl, SslContext sslContext) { + public AsyncRestClient(String baseUrl, @Nullable SslContext sslContext, @Nullable HttpProxyConfig httpProxyConfig) { this.baseUrl = baseUrl; this.sslContext = sslContext; + this.httpProxyConfig = httpProxyConfig; } public Mono> postForEntity(String uri, @Nullable String body) { @@ -188,7 +183,7 @@ public class AsyncRestClient { logger.debug("{} HTTP error status = '{}', body '{}'", traceTag, exception.getStatusCode(), exception.getResponseBodyAsString()); } else { - logger.debug("{} HTTP error", traceTag, t); + logger.debug("{} HTTP error {}", traceTag, t.getMessage()); } } @@ -200,27 +195,31 @@ public class AsyncRestClient { } } - private TcpClient createTcpClientSecure(SslContext sslContext) { - return TcpClient.create(ConnectionProvider.newConnection()) // - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10_000) // - .secure(c -> c.sslContext(sslContext)) // - .doOnConnected(connection -> { - connection.addHandlerLast(new ReadTimeoutHandler(30)); - connection.addHandlerLast(new WriteTimeoutHandler(30)); - }); + private boolean isHttpProxyConfigured() { + return httpProxyConfig != null && httpProxyConfig.httpProxyPort() > 0 + && !httpProxyConfig.httpProxyHost().isEmpty(); } - private TcpClient createTcpClientInsecure() { - return TcpClient.create(ConnectionProvider.newConnection()) // + private TcpClient createTcpClient() { + TcpClient client = TcpClient.create(ConnectionProvider.newConnection()) // .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10_000) // .doOnConnected(connection -> { connection.addHandlerLast(new ReadTimeoutHandler(30)); connection.addHandlerLast(new WriteTimeoutHandler(30)); }); + if (this.sslContext != null) { + client = client.secure(c -> c.sslContext(sslContext)); + } + if (isHttpProxyConfigured()) { + client = client.proxy(proxy -> proxy.type(Proxy.HTTP).host(httpProxyConfig.httpProxyHost()) + .port(httpProxyConfig.httpProxyPort())); + } + return client; } private WebClient createWebClient(String baseUrl, TcpClient tcpClient) { HttpClient httpClient = HttpClient.from(tcpClient); + ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient); ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder() // .codecs(configurer -> configurer.defaultCodecs().maxInMemorySize(-1)) // @@ -235,13 +234,8 @@ public class AsyncRestClient { private Mono getWebClient() { if (this.webClient == null) { try { - if (this.sslContext != null) { - TcpClient tcpClient = createTcpClientSecure(sslContext); - this.webClient = createWebClient(this.baseUrl, tcpClient); - } else { - TcpClient tcpClient = createTcpClientInsecure(); - this.webClient = createWebClient(this.baseUrl, tcpClient); - } + TcpClient tcpClient = createTcpClient(); + this.webClient = createWebClient(this.baseUrl, tcpClient); } catch (Exception e) { logger.error("Could not create WebClient {}", e.getMessage()); return Mono.error(e);