19
19
20
20
package co .elastic .clients .base .rest_client ;
21
21
22
- import co .elastic .clients .base .ApiException ;
23
- import co .elastic .clients .base .BooleanEndpoint ;
24
- import co .elastic .clients .base .BooleanResponse ;
25
- import co .elastic .clients .base .ElasticsearchCatRequest ;
26
- import co .elastic .clients .base .Endpoint ;
27
- import co .elastic .clients .base .Transport ;
22
+ import co .elastic .clients .base .*;
28
23
import co .elastic .clients .json .JsonpDeserializer ;
29
24
import co .elastic .clients .json .JsonpMapper ;
30
25
import co .elastic .clients .json .NdJsonpSerializable ;
31
26
import jakarta .json .stream .JsonGenerator ;
32
27
import jakarta .json .stream .JsonParser ;
28
+ import org .apache .http .Header ;
33
29
import org .apache .http .entity .ByteArrayEntity ;
34
30
import org .apache .http .entity .ContentType ;
35
31
import org .elasticsearch .client .Cancellable ;
@@ -51,12 +47,30 @@ public class RestClientTransport implements Transport {
51
47
52
48
private final RestClient restClient ;
53
49
private final JsonpMapper mapper ;
54
- private RequestOptions requestOptions ;
50
+ private final RequestOptions requestOptions ;
55
51
56
- public RestClientTransport (RestClient restClient , JsonpMapper mapper , @ Nullable RequestOptions options ) {
52
+ public RestClientTransport (RestClient restClient , JsonpMapper mapper , @ Nullable RequestOptions options ,
53
+ @ Nullable UserAgent userAgent ) {
57
54
this .restClient = restClient ;
58
55
this .mapper = mapper ;
59
- this .requestOptions = options ;
56
+ RequestOptions baseOptions = options == null ? RequestOptions .DEFAULT : options ;
57
+ String manualUserAgent = findUserAgentIn (baseOptions );
58
+ if (manualUserAgent == null && userAgent == null ) {
59
+ this .requestOptions = baseOptions .toBuilder ().addHeader ("User-Agent" , UserAgent .DEFAULT .toString ()).build ();
60
+ }
61
+ else if (manualUserAgent == null ) {
62
+ this .requestOptions = baseOptions .toBuilder ().addHeader ("User-Agent" , userAgent .toString ()).build ();
63
+ }
64
+ else if (userAgent == null ) {
65
+ this .requestOptions = baseOptions ;
66
+ }
67
+ else {
68
+ throw new IllegalArgumentException ("Multiple user agents specified" );
69
+ }
70
+ }
71
+
72
+ public RestClientTransport (RestClient restClient , JsonpMapper mapper , @ Nullable RequestOptions options ) {
73
+ this (restClient , mapper , options , null );
60
74
}
61
75
62
76
public RestClientTransport (RestClient restClient , JsonpMapper mapper ) {
@@ -84,16 +98,41 @@ public RestClientTransport withRequestOptions(Function<RequestOptions.Builder, R
84
98
return withRequestOptions (fn .apply (builder ).build ());
85
99
}
86
100
101
+ public RestClientTransport withUserAgent (UserAgent userAgent ) {
102
+ return new RestClientTransport (this .restClient , this .mapper , this .requestOptions , userAgent );
103
+ }
104
+
87
105
@ Override
88
106
public JsonpMapper jsonpMapper () {
89
107
return mapper ;
90
108
}
91
109
110
+ /**
111
+ * Find and return the first header value which is keyed under any
112
+ * case-insensitive variant of "User-Agent".
113
+ *
114
+ * @return user agent string
115
+ */
116
+ @ Override
117
+ public String userAgent () {
118
+ return findUserAgentIn (requestOptions );
119
+ }
120
+
92
121
@ Override
93
122
public void close () throws IOException {
94
123
this .restClient .close ();
95
124
}
96
125
126
+ private static String findUserAgentIn (RequestOptions options ) {
127
+ // TODO: move this into RequestOptions?
128
+ for (Header header : options .getHeaders ()) {
129
+ if (header .getName ().equalsIgnoreCase ("User-Agent" )) {
130
+ return header .getValue ();
131
+ }
132
+ }
133
+ return null ;
134
+ }
135
+
97
136
public <RequestT , ResponseT , ErrorT > ResponseT performRequest (
98
137
RequestT request ,
99
138
Endpoint <RequestT , ResponseT , ErrorT > endpoint
@@ -154,9 +193,7 @@ private <RequestT> org.elasticsearch.client.Request prepareLowLevelRequest(
154
193
155
194
org .elasticsearch .client .Request clientReq = new org .elasticsearch .client .Request (method , path );
156
195
clientReq .addParameters (params );
157
- if (requestOptions != null ) {
158
- clientReq .setOptions (requestOptions );
159
- }
196
+ clientReq .setOptions (requestOptions );
160
197
161
198
// Request-type specific parameters.
162
199
if (request instanceof ElasticsearchCatRequest ) {
0 commit comments