0

Here I am creating a web client builder using HttpClient. How to create HttpClient builder to configure multiple configs on same httpclient. For example: in one function I am creating httpClient with proxy and in 2nd example with proxy and security. SO I called first proxy function which apply proxy and then security on top of httpclient object to get htttpclient object with proxy and security. But its not working as expected. Instead I am getting error

((TcpClientConnect)((TcpClientBootstrap)((HttpTcpClient)((HttpClientConnect)httpClient).defaultClient).defaultClient).source).provider = Cannot find local variable 'httpClient'

Can anyone help to fix this issue?

Class WebClientBuilderConfig{  
  private HttpClient httpClient;
   @Autowired
    private ConfigProperties configProperties;

    public WebClientBuilderConfig() {
        this.httpClient = HttpClient.create();
    }


    public WebClient.Builder webClientBuilderWithProxy() {
        Function<String, String> httpsProxyPassword = username -> configProperties.getHttpsProxyPassword();
        this.httpClient.tcpConfiguration(tcpClient -> tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, configProperties.getTcpTimeout())
                .doOnConnected(conn -> conn.addHandlerLast(new ReadTimeoutHandler(configProperties.getTcpTimeout(), TimeUnit.MILLISECONDS)))
                .proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
                        .host(configProperties.getHttpsProxyHost())
                        .port(configProperties.getHttpsProxyPort())
                        .nonProxyHosts(configProperties.getHttpsNonProxyHost())
                        .username(configProperties.getHttpsProxyUser())
                        .password(httpsProxyPassword)))
                .wiretap(configProperties.getTcpWireTap());
        return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient));
    }

    public WebClient.Builder webClientBuilderWithSSL() {
        **this.webClientBuilderWithProxy();**
        // truststore
        try {
            KeyStore trustStore = KeyStore.getInstance(configProperties.getTrustStoreType());
            trustStore.load(new FileInputStream((ResourceUtils.getFile(configProperties.getTrustStorePath()))),
                    configProperties.getTrustStorePassword().toCharArray());

            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(trustStore);

            SslContext sslContext = SslContextBuilder.forClient().trustManager(trustManagerFactory).build();

            httpClient.secure(spec -> spec.sslContext(sslContext));
        } catch (Exception e) {
            log.warn("Unable to set SSL Context", e);
        }

        return WebClient.builder().clientConnector(new ReactorClientHttpConnector(this.httpClient));
    }
Neeraj Bansal
  • 380
  • 7
  • 23

1 Answers1

0

When you configure the HttpClient you always receive a new instance, so in your examples you should do something like this:

    public WebClient.Builder webClientBuilderWithProxy() {
    Function<String, String> httpsProxyPassword = username -> configProperties.getHttpsProxyPassword();
    HttpClient localClient = this.httpClient.tcpConfiguration(tcpClient -> tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, configProperties.getTcpTimeout())
            .doOnConnected(conn -> conn.addHandlerLast(new ReadTimeoutHandler(configProperties.getTcpTimeout(), TimeUnit.MILLISECONDS)))
            .proxy(proxy -> proxy.type(ProxyProvider.Proxy.HTTP)
                    .host(configProperties.getHttpsProxyHost())
                    .port(configProperties.getHttpsProxyPort())
                    .nonProxyHosts(configProperties.getHttpsNonProxyHost())
                    .username(configProperties.getHttpsProxyUser())
                    .password(httpsProxyPassword)))
            .wiretap(configProperties.getTcpWireTap());
    return WebClient.builder().clientConnector(new ReactorClientHttpConnector(localClient));
}
Violeta Georgieva
  • 1,927
  • 1
  • 12
  • 13