|
8 | 8 |
|
9 | 9 | package org.opensearch.client.transport.httpclient5; |
10 | 10 |
|
| 11 | +import javax.net.ssl.SSLContext; |
11 | 12 | import java.security.AccessController; |
12 | 13 | import java.security.NoSuchAlgorithmException; |
13 | 14 | import java.security.PrivilegedAction; |
|
16 | 17 | import java.util.Objects; |
17 | 18 | import java.util.Optional; |
18 | 19 | import java.util.stream.Collectors; |
19 | | -import javax.net.ssl.SSLContext; |
20 | | -import javax.net.ssl.SSLEngine; |
21 | | -import org.apache.hc.client5.http.auth.CredentialsProvider; |
| 20 | +import org.apache.hc.client5.http.config.ConnectionConfig; |
22 | 21 | import org.apache.hc.client5.http.config.RequestConfig; |
23 | 22 | import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy; |
24 | 23 | import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient; |
25 | 24 | import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder; |
| 25 | +import org.apache.hc.client5.http.impl.async.InternalHttpAsyncClient; |
26 | 26 | import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
27 | 27 | import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; |
28 | 28 | import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager; |
29 | 29 | import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder; |
30 | 30 | import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder; |
31 | | -import org.apache.hc.core5.function.Factory; |
32 | 31 | import org.apache.hc.core5.http.Header; |
33 | 32 | import org.apache.hc.core5.http.HttpHost; |
34 | 33 | import org.apache.hc.core5.http.nio.ssl.TlsStrategy; |
@@ -65,6 +64,7 @@ public class ApacheHttpClient5TransportBuilder { |
65 | 64 | private ApacheHttpClient5Transport.FailureListener failureListener; |
66 | 65 | private HttpClientConfigCallback httpClientConfigCallback; |
67 | 66 | private RequestConfigCallback requestConfigCallback; |
| 67 | + private ConnectionConfigCallback connectionConfigCallback; |
68 | 68 | private String pathPrefix; |
69 | 69 | private NodeSelector nodeSelector = NodeSelector.ANY; |
70 | 70 | private boolean strictDeprecationMode = false; |
@@ -144,6 +144,18 @@ public ApacheHttpClient5TransportBuilder setRequestConfigCallback(RequestConfigC |
144 | 144 | return this; |
145 | 145 | } |
146 | 146 |
|
| 147 | + /** |
| 148 | + * Sets the {@link ConnectionConfigCallback} to be used to customize http client configuration |
| 149 | + * |
| 150 | + * @param connectionConfigCallback the {@link ConnectionConfigCallback} to be used |
| 151 | + * @throws NullPointerException if {@code connectionConfigCallback} is {@code null}. |
| 152 | + */ |
| 153 | + public ApacheHttpClient5TransportBuilder setConnectionConfigCallback(ConnectionConfigCallback connectionConfigCallback) { |
| 154 | + Objects.requireNonNull(connectionConfigCallback, "connectionConfigCallback must not be null"); |
| 155 | + this.connectionConfigCallback = connectionConfigCallback; |
| 156 | + return this; |
| 157 | + } |
| 158 | + |
147 | 159 | /** |
148 | 160 | * Sets the path's prefix for every request used by the http client. |
149 | 161 | * <p> |
@@ -324,24 +336,25 @@ private CloseableHttpAsyncClient createHttpClient() { |
324 | 336 | // default timeouts are all infinite |
325 | 337 | RequestConfig.Builder requestConfigBuilder = RequestConfig.custom() |
326 | 338 | .setResponseTimeout(Timeout.ofMilliseconds(DEFAULT_RESPONSE_TIMEOUT_MILLIS)); |
| 339 | + ConnectionConfig.Builder connectionConfigBuilder = ConnectionConfig.custom(); |
327 | 340 |
|
328 | 341 | if (requestConfigCallback != null) { |
329 | 342 | requestConfigBuilder = requestConfigCallback.customizeRequestConfig(requestConfigBuilder); |
330 | 343 | } |
331 | 344 |
|
| 345 | + if (connectionConfigCallback != null) { |
| 346 | + connectionConfigBuilder = connectionConfigCallback.customizeConnectionConfig(connectionConfigBuilder); |
| 347 | + } |
| 348 | + |
332 | 349 | try { |
333 | 350 | final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create() |
334 | 351 | .setSslContext(SSLContext.getDefault()) |
335 | 352 | // See https://issues.apache.org/jira/browse/HTTPCLIENT-2219 |
336 | | - .setTlsDetailsFactory(new Factory<SSLEngine, TlsDetails>() { |
337 | | - @Override |
338 | | - public TlsDetails create(final SSLEngine sslEngine) { |
339 | | - return new TlsDetails(sslEngine.getSession(), sslEngine.getApplicationProtocol()); |
340 | | - } |
341 | | - }) |
| 353 | + .setTlsDetailsFactory(sslEngine -> new TlsDetails(sslEngine.getSession(), sslEngine.getApplicationProtocol())) |
342 | 354 | .build(); |
343 | 355 |
|
344 | 356 | final PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder.create() |
| 357 | + .setDefaultConnectionConfig(connectionConfigBuilder.build()) |
345 | 358 | .setMaxConnPerRoute(DEFAULT_MAX_CONN_PER_ROUTE) |
346 | 359 | .setMaxConnTotal(DEFAULT_MAX_CONN_TOTAL) |
347 | 360 | .setTlsStrategy(tlsStrategy) |
@@ -379,16 +392,27 @@ public interface RequestConfigCallback { |
379 | 392 | } |
380 | 393 |
|
381 | 394 | /** |
382 | | - * Callback used to customize the {@link CloseableHttpClient} instance used by a {@link RestClient} instance. |
| 395 | + * Callback used to customize the default {@link ConnectionConfig} that will be set on the {@link CloseableHttpClient}. |
| 396 | + * @see PoolingAsyncClientConnectionManagerBuilder#setDefaultConnectionConfig |
| 397 | + */ |
| 398 | + public interface ConnectionConfigCallback { |
| 399 | + /** |
| 400 | + * Allows to customize the {@link ConnectionConfig} used by the connection manager of the {@link InternalHttpAsyncClient}. |
| 401 | + * Commonly used to adjust connection-related settings without losing other default values |
| 402 | + * |
| 403 | + * @param connectionConfigBuilder the {@link ConnectionConfig.Builder} for customizing the connection configuration. |
| 404 | + */ |
| 405 | + ConnectionConfig.Builder customizeConnectionConfig(ConnectionConfig.Builder connectionConfigBuilder); |
| 406 | + } |
| 407 | + |
| 408 | + /** |
| 409 | + * Callback used to customize the {@link CloseableHttpClient} instance used by a {@link InternalHttpAsyncClient} instance. |
383 | 410 | * Allows to customize default {@link RequestConfig} being set to the client and any parameter that |
384 | 411 | * can be set through {@link HttpClientBuilder} |
385 | 412 | */ |
386 | 413 | public interface HttpClientConfigCallback { |
387 | 414 | /** |
388 | | - * Allows to customize the {@link CloseableHttpAsyncClient} being created and used by the {@link RestClient}. |
389 | | - * Commonly used to customize the default {@link CredentialsProvider} for authentication for communication |
390 | | - * through TLS/SSL without losing any other useful default value that the {@link RestClientBuilder} internally |
391 | | - * sets, like connection pooling. |
| 415 | + * Allows to customize the {@link CloseableHttpAsyncClient} being created and used by the {@link InternalHttpAsyncClient}. |
392 | 416 | * |
393 | 417 | * @param httpClientBuilder the {@link HttpClientBuilder} for customizing the client instance. |
394 | 418 | */ |
|
0 commit comments