2121
2222import co .elastic .clients .transport .TransportOptions ;
2323import co .elastic .clients .transport .Version ;
24+ import co .elastic .clients .util .VisibleForTesting ;
2425import org .apache .http .impl .nio .client .HttpAsyncClientBuilder ;
2526import org .apache .http .util .VersionInfo ;
2627import org .elasticsearch .client .RequestOptions ;
@@ -38,6 +39,14 @@ public class RestClientOptions implements TransportOptions {
3839
3940 private final RequestOptions options ;
4041
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+
4150 static RestClientOptions of (TransportOptions options ) {
4251 if (options instanceof RestClientOptions ) {
4352 return (RestClientOptions )options ;
@@ -52,7 +61,7 @@ static RestClientOptions of(TransportOptions options) {
5261 }
5362
5463 public RestClientOptions (RequestOptions options ) {
55- this .options = options ;
64+ this .options = addBuiltinHeaders ( options . toBuilder ()). build () ;
5665 }
5766
5867 /**
@@ -109,23 +118,13 @@ public RequestOptions.Builder restClientRequestOptionsBuilder() {
109118
110119 @ Override
111120 public TransportOptions .Builder addHeader (String name , String value ) {
112- if (name .equalsIgnoreCase (CLIENT_META )) {
121+ if (name .equalsIgnoreCase (CLIENT_META_HEADER )) {
122+ // Not overridable
113123 return this ;
114124 }
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 );
129128 }
130129 builder .addHeader (name , value );
131130 return this ;
@@ -159,32 +158,37 @@ public TransportOptions.Builder onWarnings(Function<List<String>, Boolean> liste
159158
160159 @ Override
161160 public RestClientOptions build () {
162- return new RestClientOptions (builder .build ());
161+ return new RestClientOptions (addBuiltinHeaders ( builder ) .build ());
163162 }
164163 }
165164
166- private static final String USER_AGENT = "User-Agent" ;
167- private static final String CLIENT_META = "X-Elastic-Client-Meta" ;
168-
169165 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 (
171184 Locale .ROOT ,
172185 "elastic-java/%s (Java/%s)" ,
173186 Version .VERSION == null ? "Unknown" : Version .VERSION .toString (),
174187 System .getProperty ("java.version" )
175188 );
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- );
184189 }
185190
186191 private static String getClientMeta () {
187-
188192 VersionInfo httpClientVersion = null ;
189193 try {
190194 httpClientVersion = VersionInfo .loadVersionInfo (
0 commit comments