Skip to content

Commit ab0d212

Browse files
authored
Merge pull request #1047 from marciw/mw-rest5
[DOCS] Rest 5 client fixes
2 parents 9adeb78 + fdd90ab commit ab0d212

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

docs/reference/transport/rest5-client/config/basic_authentication.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
# Basic authentication
33

4-
Configuring basic authentication can be done by providing an `HttpClientConfigCallback` while building the `RestClient` through its builder. The interface has one method that receives an instance of [`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`](https://hc.apache.org/httpcomponents-asyncclient-4.1.x/current/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html) as an argument and has the same return type. The http client builder can be modified and then returned. In the following example we set a default credentials provider that requires basic authentication.
4+
To use basic authentication in the REST 5 client, set a default authorization header:
55

66
% :::{include-code} src={{doc-tests-src}}/rest5_client/RestClientDocumentation.java tag=rest-client-config-basic-auth
77
```java
@@ -16,7 +16,9 @@ Rest5ClientBuilder restClient = Rest5Client
1616

1717
```
1818

19-
Preemptive Authentication can be disabled, which means that every request will be sent without authorization headers to see if it is accepted and, upon receiving an HTTP 401 response, it will resend the exact same request with the basic authentication header. If you wish to do this, then you can do so by disabling it via the `HttpClientConfigCallback`:
19+
To configure authentication behavior, pass an `HttpClientConfigCallback` when building the `Rest5Client`. The callback's single method takes an instance of [`org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder`](https://hc.apache.org/httpcomponents-client-5.5.x/current/httpclient5/apidocs/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.html) as an argument and returns the same type, so you can modify the provided builder and return it for the client to use.
20+
21+
By default, the HTTP client uses preemptive authentication: it includes credentials in the initial request. You might want to use non-preemptive authentication, which sends a request without credentials and retries with the header after a `401 Unauthorized` challenge. To do this, set an `HttpClientConfigCallback` with auth caching disabled:
2022

2123
% :::{include-code} src={{doc-tests-src}}/rest5_client/RestClientDocumentation.java tag=rest-client-config-disable-preemptive-auth
2224
```java
@@ -26,12 +28,12 @@ var creds = Base64.getEncoder().encodeToString("user:test-user-password".getByte
2628

2729
Rest5ClientBuilder restClient = Rest5Client
2830
.builder(new HttpHost("https", "localhost", 9200))
29-
.setHttpClientConfigCallback(HttpAsyncClientBuilder::disableAuthCaching)
31+
.setHttpClientConfigCallback(HttpAsyncClientBuilder::disableAuthCaching) <1>
3032
.setDefaultHeaders(new Header[]{
3133
new BasicHeader("Authorization", "Basic " + creds)
3234
});
3335
```
3436

35-
1. Disable preemptive authentication
36-
37+
1. Disables preemptive authentication
3738

39+
For more options, refer to [](other_authentication_methods.md).

docs/reference/transport/rest5-client/config/timeouts.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1-
21
# Timeouts
32

4-
Configuring requests timeouts can be done by using the `setRequestConfigCallback` method while building the `RestClient`. In the following example we increase the connect timeout (defaults to 30 second) and the response timeout (defaults to 0, which is infinite).
3+
You can set timeouts when building the `Rest5Client`:
4+
5+
- The **connect timeout** is the maximum time for establishing a TCP connection, including the TLS handshake. The connect timeout is set on `ConnectionConfig`.
6+
- The **socket timeout** is the maximum time to wait for I/O on an established socket. The socket timeout is set on `ConnectionConfig`.
7+
- The **response timeout** is the maximum period to wait for response data. The response timeout is set on `RequestConfig`.
8+
- The **connection request timeout** is the maximum time for leasing a connection from the pool. The connection request timeout is set on `RequestConfig`.
9+
10+
To configure timeouts, use `setConnectionConfigCallback` and `setRequestConfigCallback` while building the `Rest5Client`. The following example sets a 10-second connect timeout, a 10-second socket timeout, and a 20-second response timeout:
511

612
% :::{include-code} src={{doc-tests-src}}/rest5_client/RestClientDocumentation.java tag=rest-client-config-timeouts
713
```java
814
Rest5ClientBuilder builder = Rest5Client
9-
.builder(new HttpHost("localhost", 9200))
15+
.builder(new HttpHost("http", "localhost", 9200)) <1>
16+
.setConnectionConfigCallback(connectConf -> connectConf
17+
.setConnectTimeout(Timeout.ofSeconds(10))
18+
.setSocketTimeout(Timeout.ofSeconds(10)))
1019
.setRequestConfigCallback(r -> r
11-
.setConnectTimeout(Timeout.of(5000, TimeUnit.MILLISECONDS))
12-
.setResponseTimeout(Timeout.of(30000, TimeUnit.MILLISECONDS))
13-
.build()
20+
.setResponseTimeout(Timeout.ofSeconds(20))
1421
);
1522
```
1623

17-
Timeouts also can be set per request with RequestOptions, which overrides RestClient's builder. The RequestOptions can then be set in the Rest5ClientTransport constructor.
24+
1. Specify `https` for TLS.
25+
26+
You can also set per-request timeouts using `RequestOptions`, which override the builder defaults. The following example sets a response timeout of 60 seconds, as well as a connection request timeout of 1 second (to limit pooled connection wait time):
1827

1928
% :::{include-code} src={{doc-tests-src}}/rest5_client/RestClientDocumentation.java tag=rest-client-config-request-options-timeouts
2029
```java
21-
RequestConfig requestConfig = RequestConfig.custom()
22-
.setConnectTimeout(Timeout.ofMilliseconds(5000))
23-
.setConnectionRequestTimeout(Timeout.ofMilliseconds(60000))
30+
RequestConfig requestConfig = RequestConfig.custom()
31+
.setResponseTimeout(Timeout.ofSeconds(60))
32+
.setConnectionRequestTimeout(Timeout.ofSeconds(1))
2433
.build();
2534

2635
RequestOptions options = RequestOptions.DEFAULT.toBuilder()
@@ -30,4 +39,3 @@ RequestOptions options = RequestOptions.DEFAULT.toBuilder()
3039
ElasticsearchTransport transport = new Rest5ClientTransport(
3140
restClient, new JacksonJsonpMapper(), options);
3241
```
33-

0 commit comments

Comments
 (0)