21
21
22
22
import co .elastic .clients .transport .TransportOptions ;
23
23
import co .elastic .clients .transport .Version ;
24
+ import co .elastic .clients .util .VisibleForTesting ;
24
25
import org .apache .http .impl .nio .client .HttpAsyncClientBuilder ;
25
26
import org .apache .http .util .VersionInfo ;
26
27
import org .elasticsearch .client .RequestOptions ;
@@ -38,6 +39,14 @@ public class RestClientOptions implements TransportOptions {
38
39
39
40
private final RequestOptions options ;
40
41
42
+ private static final String CLIENT_META_HEADER = "X-Elastic-Client-Meta" ;
43
+ private static final String USER_AGENT_HEADER = "User-Agent" ;
44
+
45
+ @ VisibleForTesting
46
+ static final String CLIENT_META_VALUE = getClientMeta ();
47
+ @ VisibleForTesting
48
+ static final String USER_AGENT_VALUE = getUserAgent ();
49
+
41
50
static RestClientOptions of (TransportOptions options ) {
42
51
if (options instanceof RestClientOptions ) {
43
52
return (RestClientOptions )options ;
@@ -52,7 +61,7 @@ static RestClientOptions of(TransportOptions options) {
52
61
}
53
62
54
63
public RestClientOptions (RequestOptions options ) {
55
- this .options = options ;
64
+ this .options = addBuiltinHeaders ( options . toBuilder ()). build () ;
56
65
}
57
66
58
67
/**
@@ -109,23 +118,13 @@ public RequestOptions.Builder restClientRequestOptionsBuilder() {
109
118
110
119
@ Override
111
120
public TransportOptions .Builder addHeader (String name , String value ) {
112
- if (name .equalsIgnoreCase (CLIENT_META )) {
121
+ if (name .equalsIgnoreCase (CLIENT_META_HEADER )) {
122
+ // Not overridable
113
123
return this ;
114
124
}
115
- if (name .equalsIgnoreCase (USER_AGENT )) {
116
- // We must filter out our own user-agent from the options or they'll end up as multiple values for the header
117
- RequestOptions options = builder .build ();
118
- builder = RequestOptions .DEFAULT .toBuilder ();
119
- options .getParameters ().forEach ((k , v ) -> builder .addParameter (k , v ));
120
- options .getHeaders ().forEach (h -> {
121
- if (!h .getName ().equalsIgnoreCase (USER_AGENT )) {
122
- builder .addHeader (h .getName (), h .getValue ());
123
- }
124
- });
125
- builder .setWarningsHandler (options .getWarningsHandler ());
126
- if (options .getHttpAsyncResponseConsumerFactory () != null ) {
127
- builder .setHttpAsyncResponseConsumerFactory (options .getHttpAsyncResponseConsumerFactory ());
128
- }
125
+ if (name .equalsIgnoreCase (USER_AGENT_HEADER )) {
126
+ // We must remove our own user-agent from the options, or we'll end up with multiple values for the header
127
+ builder .removeHeader (USER_AGENT_HEADER );
129
128
}
130
129
builder .addHeader (name , value );
131
130
return this ;
@@ -159,32 +158,37 @@ public TransportOptions.Builder onWarnings(Function<List<String>, Boolean> liste
159
158
160
159
@ Override
161
160
public RestClientOptions build () {
162
- return new RestClientOptions (builder .build ());
161
+ return new RestClientOptions (addBuiltinHeaders ( builder ) .build ());
163
162
}
164
163
}
165
164
166
- private static final String USER_AGENT = "User-Agent" ;
167
- private static final String CLIENT_META = "X-Elastic-Client-Meta" ;
168
-
169
165
static RestClientOptions initialOptions () {
170
- String ua = String .format (
166
+ return new RestClientOptions (RequestOptions .DEFAULT );
167
+ }
168
+
169
+ private static RequestOptions .Builder addBuiltinHeaders (RequestOptions .Builder builder ) {
170
+ builder .removeHeader (CLIENT_META_HEADER );
171
+ builder .addHeader (CLIENT_META_HEADER , CLIENT_META_VALUE );
172
+ if (builder .getHeaders ().stream ().noneMatch (h -> h .getName ().equalsIgnoreCase (USER_AGENT_HEADER ))) {
173
+ builder .addHeader (USER_AGENT_HEADER , USER_AGENT_VALUE );
174
+ }
175
+ if (builder .getHeaders ().stream ().noneMatch (h -> h .getName ().equalsIgnoreCase ("Accept" ))) {
176
+ builder .addHeader ("Accept" , RestClientTransport .JsonContentType .toString ());
177
+ }
178
+
179
+ return builder ;
180
+ }
181
+
182
+ private static String getUserAgent () {
183
+ return String .format (
171
184
Locale .ROOT ,
172
185
"elastic-java/%s (Java/%s)" ,
173
186
Version .VERSION == null ? "Unknown" : Version .VERSION .toString (),
174
187
System .getProperty ("java.version" )
175
188
);
176
-
177
- return new RestClientOptions (
178
- RequestOptions .DEFAULT .toBuilder ()
179
- .addHeader (USER_AGENT , ua )
180
- .addHeader (CLIENT_META , getClientMeta ())
181
- .addHeader ("Accept" , RestClientTransport .JsonContentType .toString ())
182
- .build ()
183
- );
184
189
}
185
190
186
191
private static String getClientMeta () {
187
-
188
192
VersionInfo httpClientVersion = null ;
189
193
try {
190
194
httpClientVersion = VersionInfo .loadVersionInfo (
0 commit comments