Skip to content

Commit 103aa10

Browse files
committed
extend rest5 client builder with more setters
1 parent 1988f11 commit 103aa10

File tree

1 file changed

+252
-13
lines changed

1 file changed

+252
-13
lines changed

java-client/src/main/java/co/elastic/clients/transport/rest5_client/low_level/Rest5ClientBuilder.java

Lines changed: 252 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,33 @@
2020
package co.elastic.clients.transport.rest5_client.low_level;
2121

2222

23+
import org.apache.hc.client5.http.AuthenticationStrategy;
24+
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
25+
import org.apache.hc.client5.http.SchemePortResolver;
26+
import org.apache.hc.client5.http.UserTokenHandler;
27+
import org.apache.hc.client5.http.auth.AuthSchemeFactory;
28+
import org.apache.hc.client5.http.auth.CredentialsProvider;
2329
import org.apache.hc.client5.http.config.ConnectionConfig;
2430
import org.apache.hc.client5.http.config.RequestConfig;
2531
import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy;
2632
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
2733
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
28-
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
2934
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
35+
import org.apache.hc.client5.http.nio.AsyncClientConnectionManager;
3036
import org.apache.hc.client5.http.routing.HttpRoutePlanner;
37+
import org.apache.hc.core5.function.Callback;
38+
import org.apache.hc.core5.function.Decorator;
39+
import org.apache.hc.core5.http.ConnectionReuseStrategy;
3140
import org.apache.hc.core5.http.Header;
3241
import org.apache.hc.core5.http.HttpHost;
42+
import org.apache.hc.core5.http.config.CharCodingConfig;
43+
import org.apache.hc.core5.http.config.Http1Config;
44+
import org.apache.hc.core5.http.config.Lookup;
3345
import org.apache.hc.core5.http.nio.ssl.BasicClientTlsStrategy;
46+
import org.apache.hc.core5.http2.config.H2Config;
47+
import org.apache.hc.core5.reactor.IOReactorConfig;
48+
import org.apache.hc.core5.reactor.IOSession;
49+
import org.apache.hc.core5.reactor.IOSessionListener;
3450
import org.apache.hc.core5.util.Timeout;
3551
import org.apache.hc.core5.util.VersionInfo;
3652

@@ -78,6 +94,25 @@ public final class Rest5ClientBuilder {
7894
private Header[] defaultHeaders = EMPTY_HEADERS;
7995
private Rest5Client.FailureListener failureListener;
8096
private SSLContext sslContext;
97+
private Http1Config h1Config;
98+
private H2Config h2Config;
99+
private IOReactorConfig ioReactorConfig;
100+
private IOSessionListener ioSessionListener;
101+
private ConnectionReuseStrategy connectionReuseStrategy;
102+
private CredentialsProvider credentialsProvider;
103+
private Callback<Exception> ioReactorExceptionCallback;
104+
private CharCodingConfig charCodingConfig;
105+
private UserTokenHandler userTokenHandler;
106+
private AuthenticationStrategy authStrategy;
107+
private AuthenticationStrategy proxyAuthStrategy;
108+
private Decorator<IOSession> ioSessionDecorator;
109+
private HttpRequestRetryStrategy retryStrategy;
110+
private SchemePortResolver schemePortResolver;
111+
private ThreadFactory threadFactory;
112+
private Lookup<AuthSchemeFactory> authSchemeRegistry;
113+
private RequestConfig customRequestConfig;
114+
private ConnectionConfig customConnectionConfig;
115+
private AsyncClientConnectionManager customConnectionManager;
81116
private HttpHost proxy;
82117
private ProxySelector proxySelector;
83118
private HttpRoutePlanner routePlanner;
@@ -204,6 +239,142 @@ public Rest5ClientBuilder setRoutePlanner(HttpRoutePlanner routePlanner) {
204239
return this;
205240
}
206241

242+
public Rest5ClientBuilder setHttp1Config(Http1Config h1Config) {
243+
Objects.requireNonNull(h1Config, "http1 config must not be null");
244+
this.h1Config = h1Config;
245+
return this;
246+
}
247+
248+
public Rest5ClientBuilder setH2Config(H2Config h2Config) {
249+
Objects.requireNonNull(h2Config, "h2 config must not be null");
250+
this.h2Config = h2Config;
251+
return this;
252+
}
253+
254+
public Rest5ClientBuilder setIOReactorConfig(IOReactorConfig ioReactorConfig) {
255+
Objects.requireNonNull(ioReactorConfig, "io reactor config must not be null");
256+
this.ioReactorConfig = ioReactorConfig;
257+
return this;
258+
}
259+
260+
public Rest5ClientBuilder setIOSessionListener(IOSessionListener ioSessionListener) {
261+
Objects.requireNonNull(ioSessionListener, "io session listener must not be null");
262+
this.ioSessionListener = ioSessionListener;
263+
return this;
264+
}
265+
266+
267+
public Rest5ClientBuilder setConnectionReuseStrategy(ConnectionReuseStrategy connectionReuseStrategy) {
268+
Objects.requireNonNull(connectionReuseStrategy, "connection reuse strategy must not be null");
269+
this.connectionReuseStrategy = connectionReuseStrategy;
270+
return this;
271+
}
272+
273+
/**
274+
* Sets the callback that will be invoked when the client's IOReactor encounters an uncaught exception.
275+
*/
276+
public Rest5ClientBuilder setIoReactorExceptionCallback(Callback<Exception> ioReactorExceptionCallback) {
277+
Objects.requireNonNull(ioReactorExceptionCallback, "io exception callback must not be null");
278+
this.ioReactorExceptionCallback = ioReactorExceptionCallback;
279+
return this;
280+
}
281+
282+
public Rest5ClientBuilder setCharCodingConfig(CharCodingConfig charCodingConfig) {
283+
Objects.requireNonNull(charCodingConfig, "char coding config must not be null");
284+
this.charCodingConfig = charCodingConfig;
285+
return this;
286+
}
287+
288+
/**
289+
* For user specific contexts, to determine whether to share user specific resources.
290+
*/
291+
public Rest5ClientBuilder setUserTokenHandler(UserTokenHandler userTokenHandler) {
292+
Objects.requireNonNull(userTokenHandler, "user token handler must not be null");
293+
this.userTokenHandler = userTokenHandler;
294+
return this;
295+
}
296+
297+
public Rest5ClientBuilder setTargetAuthenticationStrategy(AuthenticationStrategy authStrategy) {
298+
Objects.requireNonNull(authStrategy, "authentication strategy must not be null");
299+
this.authStrategy = authStrategy;
300+
return this;
301+
}
302+
303+
public Rest5ClientBuilder setProxyAuthenticationStrategy(AuthenticationStrategy proxyAuthStrategy) {
304+
Objects.requireNonNull(proxyAuthStrategy, "proxy authentication strategy must not be null");
305+
this.proxyAuthStrategy = proxyAuthStrategy;
306+
return this;
307+
}
308+
309+
public Rest5ClientBuilder setIoSessionDecorator(Decorator<IOSession> ioSessionDecorator) {
310+
Objects.requireNonNull(ioSessionDecorator, "io session decorator must not be null");
311+
this.ioSessionDecorator = ioSessionDecorator;
312+
return this;
313+
}
314+
315+
public Rest5ClientBuilder setRetryStrategy(HttpRequestRetryStrategy retryStrategy) {
316+
Objects.requireNonNull(retryStrategy, "retry strategy must not be null");
317+
this.retryStrategy = retryStrategy;
318+
return this;
319+
}
320+
321+
public Rest5ClientBuilder setSchemePortResolver(SchemePortResolver schemePortResolver) {
322+
Objects.requireNonNull(schemePortResolver, "scheme port resolver must not be null");
323+
this.schemePortResolver = schemePortResolver;
324+
return this;
325+
}
326+
327+
public Rest5ClientBuilder setThreadFactory(ThreadFactory threadFactory) {
328+
Objects.requireNonNull(threadFactory, "thread factory must not be null");
329+
this.threadFactory = threadFactory;
330+
return this;
331+
}
332+
333+
public Rest5ClientBuilder setDefaultCredentialsProvider(CredentialsProvider credentialsProvider) {
334+
Objects.requireNonNull(credentialsProvider, "credentials provider must not be null");
335+
this.credentialsProvider = credentialsProvider;
336+
return this;
337+
}
338+
339+
public Rest5ClientBuilder setDefaultAuthSchemeRegistry(Lookup<AuthSchemeFactory> authSchemeRegistry) {
340+
Objects.requireNonNull(authSchemeRegistry, "auth scheme registry must not be null");
341+
this.authSchemeRegistry = authSchemeRegistry;
342+
return this;
343+
}
344+
345+
/**
346+
* Override default values for request specific settings.
347+
* This also overrides default timeouts set by the client configuration.
348+
* Avoid using this method unless specific configuration is needed.
349+
*/
350+
public Rest5ClientBuilder setCustomRequestConfig(RequestConfig config) {
351+
Objects.requireNonNull(config, "request config must not be null");
352+
this.customRequestConfig = config;
353+
return this;
354+
}
355+
356+
/**
357+
* Override default values for connection specific settings.
358+
* This also overrides default timeouts set by the client configuration.
359+
* Avoid using this method unless specific configuration is needed.
360+
*/
361+
public Rest5ClientBuilder setCustomConnectionConfig(ConnectionConfig config) {
362+
Objects.requireNonNull(config, "connection config must not be null");
363+
this.customConnectionConfig = config;
364+
return this;
365+
}
366+
367+
/**
368+
* Override default connection manager.
369+
* This also overrides default timeouts and route settings set by the client configuration.
370+
* Avoid using this method unless specific configuration is needed.
371+
*/
372+
public Rest5ClientBuilder setCustomConnectionManager(AsyncClientConnectionManager connectionManager) {
373+
Objects.requireNonNull(connectionManager, "connection manager must not be null");
374+
this.customConnectionManager = connectionManager;
375+
return this;
376+
}
377+
207378
/**
208379
* Sets the default request headers, which will be sent along with each request.
209380
* <p>
@@ -369,31 +540,50 @@ private CloseableHttpAsyncClient createHttpClient() {
369540
return this.httpClient;
370541
}
371542
// otherwise, creating a default instance of CloseableHttpAsyncClient
372-
// default timeouts are all 3 mins
373-
RequestConfig requestConfigBuilder = RequestConfig.custom()
374-
.setConnectionRequestTimeout(Timeout.of(DEFAULT_SOCKET_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS))
375-
.setResponseTimeout(Timeout.of(DEFAULT_RESPONSE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS))
376-
.build();
543+
544+
// if the user provided a custom RequestConfig, using that one
545+
RequestConfig requestConfig;
546+
if (this.customRequestConfig != null) {
547+
requestConfig = customRequestConfig;
548+
} else {
549+
// otherwise, building one to override default timeouts, which are all 3 mins
550+
requestConfig = RequestConfig.custom()
551+
.setConnectionRequestTimeout(Timeout.of(DEFAULT_SOCKET_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS))
552+
.setResponseTimeout(Timeout.of(DEFAULT_RESPONSE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS))
553+
.build();
554+
}
377555

378556
try {
379557

380558
SSLContext sslContext = this.sslContext != null ? this.sslContext : SSLContext.getDefault();
381559

382-
ConnectionConfig connectionConfig = ConnectionConfig.custom()
383-
.setConnectTimeout(Timeout.of(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS))
384-
.build();
560+
// if the user provided a custom ConnectionConfig, using that one
561+
ConnectionConfig connectionConfig;
562+
if (this.customConnectionConfig != null) {
563+
connectionConfig = customConnectionConfig;
564+
} else {
565+
// otherwise, building one to override default timeout of 3 minutes
566+
connectionConfig = ConnectionConfig.custom()
567+
.setConnectTimeout(Timeout.of(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS))
568+
.build();
569+
}
385570

386-
PoolingAsyncClientConnectionManager defaultConnectionManager =
387-
PoolingAsyncClientConnectionManagerBuilder.create()
571+
// if the user provided a custom ConnectionManager, using that one
572+
AsyncClientConnectionManager connectionManager;
573+
if (this.customConnectionManager != null) {
574+
connectionManager = customConnectionManager;
575+
} else {
576+
connectionManager = PoolingAsyncClientConnectionManagerBuilder.create()
388577
.setDefaultConnectionConfig(connectionConfig)
389578
.setMaxConnPerRoute(DEFAULT_MAX_CONN_PER_ROUTE)
390579
.setMaxConnTotal(DEFAULT_MAX_CONN_TOTAL)
391580
.setTlsStrategy(new BasicClientTlsStrategy(sslContext))
392581
.build();
582+
}
393583

394584
HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClientBuilder.create()
395-
.setDefaultRequestConfig(requestConfigBuilder)
396-
.setConnectionManager(defaultConnectionManager)
585+
.setDefaultRequestConfig(requestConfig)
586+
.setConnectionManager(connectionManager)
397587
.setUserAgent(USER_AGENT_HEADER_VALUE)
398588
.setTargetAuthenticationStrategy(new DefaultAuthenticationStrategy())
399589
.setThreadFactory(new RestClientThreadFactory());
@@ -407,6 +597,55 @@ private CloseableHttpAsyncClient createHttpClient() {
407597
if (this.routePlanner != null) {
408598
httpClientBuilder.setRoutePlanner(this.routePlanner);
409599
}
600+
if (this.h1Config != null) {
601+
httpClientBuilder.setHttp1Config(this.h1Config);
602+
}
603+
if (this.h2Config != null) {
604+
httpClientBuilder.setH2Config(this.h2Config);
605+
}
606+
if (this.ioReactorConfig != null) {
607+
httpClientBuilder.setIOReactorConfig(this.ioReactorConfig);
608+
}
609+
if (this.ioSessionListener != null) {
610+
httpClientBuilder.setIOSessionListener(this.ioSessionListener);
611+
}
612+
if (this.connectionReuseStrategy != null) {
613+
httpClientBuilder.setConnectionReuseStrategy(this.connectionReuseStrategy);
614+
}
615+
if (this.credentialsProvider != null) {
616+
httpClientBuilder.setDefaultCredentialsProvider(this.credentialsProvider);
617+
}
618+
if (this.ioReactorExceptionCallback != null) {
619+
httpClientBuilder.setIoReactorExceptionCallback(this.ioReactorExceptionCallback);
620+
}
621+
if (this.charCodingConfig != null) {
622+
httpClientBuilder.setCharCodingConfig(this.charCodingConfig);
623+
}
624+
if (this.userTokenHandler != null) {
625+
httpClientBuilder.setUserTokenHandler(this.userTokenHandler);
626+
}
627+
if (this.authStrategy != null) {
628+
httpClientBuilder.setTargetAuthenticationStrategy(authStrategy);
629+
}
630+
if (this.proxyAuthStrategy != null) {
631+
httpClientBuilder.setProxyAuthenticationStrategy(proxyAuthStrategy);
632+
}
633+
if (this.ioSessionDecorator != null) {
634+
httpClientBuilder.setIoSessionDecorator(this.ioSessionDecorator);
635+
}
636+
if (this.retryStrategy != null) {
637+
httpClientBuilder.setRetryStrategy(this.retryStrategy);
638+
}
639+
if (this.schemePortResolver != null) {
640+
httpClientBuilder.setSchemePortResolver(this.schemePortResolver);
641+
}
642+
if (this.threadFactory != null) {
643+
httpClientBuilder.setThreadFactory(this.threadFactory);
644+
}
645+
if (this.authSchemeRegistry != null) {
646+
httpClientBuilder.setDefaultAuthSchemeRegistry(this.authSchemeRegistry);
647+
}
648+
410649

411650
return httpClientBuilder.build();
412651
} catch (NoSuchAlgorithmException e) {

0 commit comments

Comments
 (0)