|
34 | 34 | import java.util.function.Supplier;
|
35 | 35 | import java.util.stream.Collectors;
|
36 | 36 |
|
37 |
| -import org.apache.http.*; |
| 37 | +import org.apache.http.HttpHost; |
| 38 | +import org.apache.http.HttpRequest; |
| 39 | +import org.apache.http.HttpRequestInterceptor; |
38 | 40 | import org.apache.http.client.config.RequestConfig;
|
39 | 41 | import org.apache.http.entity.ContentType;
|
40 | 42 | import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
|
@@ -261,26 +263,44 @@ private static ElasticsearchTransport getElasticsearchTransport(RestClient restC
|
261 | 263 | TransportOptions.Builder transportOptionsBuilder = transportOptions != null ? transportOptions.toBuilder()
|
262 | 264 | : new RestClientOptions(RequestOptions.DEFAULT).toBuilder();
|
263 | 265 |
|
| 266 | + RestClientOptions.Builder restClientOptionsBuilder = getRestClientOptionsBuilder(transportOptions); |
| 267 | + |
264 | 268 | ContentType jsonContentType = Version.VERSION == null ? ContentType.APPLICATION_JSON
|
265 | 269 | : ContentType.create("application/vnd.elasticsearch+json",
|
266 | 270 | new BasicNameValuePair("compatible-with", String.valueOf(Version.VERSION.major())));
|
267 | 271 |
|
268 | 272 | Consumer<String> setHeaderIfNotPresent = header -> {
|
269 |
| - if (transportOptionsBuilder.build().headers().stream() // |
| 273 | + if (restClientOptionsBuilder.build().headers().stream() // |
270 | 274 | .noneMatch((h) -> h.getKey().equalsIgnoreCase(header))) {
|
271 | 275 | // need to add the compatibility header, this is only done automatically when not passing in custom options.
|
272 | 276 | // code copied from RestClientTransport as it is not available outside the package
|
273 |
| - transportOptionsBuilder.addHeader(header, jsonContentType.toString()); |
| 277 | + restClientOptionsBuilder.addHeader(header, jsonContentType.toString()); |
274 | 278 | }
|
275 | 279 | };
|
276 |
| - |
| 280 | +§ |
277 | 281 | setHeaderIfNotPresent.accept("Content-Type");
|
278 | 282 | setHeaderIfNotPresent.accept("Accept");
|
279 | 283 |
|
280 |
| - TransportOptions transportOptionsWithHeader = transportOptionsBuilder |
281 |
| - .addHeader(X_SPRING_DATA_ELASTICSEARCH_CLIENT, clientType).build(); |
| 284 | + restClientOptionsBuilder.addHeader(X_SPRING_DATA_ELASTICSEARCH_CLIENT, clientType); |
| 285 | + |
| 286 | + return new RestClientTransport(restClient, jsonpMapper, restClientOptionsBuilder.build()); |
| 287 | + } |
| 288 | + |
| 289 | + private static RestClientOptions.Builder getRestClientOptionsBuilder(@Nullable TransportOptions transportOptions) { |
282 | 290 |
|
283 |
| - return new RestClientTransport(restClient, jsonpMapper, transportOptionsWithHeader); |
| 291 | + if (transportOptions instanceof RestClientOptions restClientOptions) { |
| 292 | + return restClientOptions.toBuilder(); |
| 293 | + } |
| 294 | + |
| 295 | + var builder = new RestClientOptions.Builder(RequestOptions.DEFAULT.toBuilder()); |
| 296 | + |
| 297 | + if (transportOptions != null) { |
| 298 | + transportOptions.headers().forEach(header -> builder.addHeader(header.getKey(), header.getValue())); |
| 299 | + transportOptions.queryParameters().forEach(builder::setParameter); |
| 300 | + builder.onWarnings(transportOptions.onWarnings()); |
| 301 | + } |
| 302 | + |
| 303 | + return builder; |
284 | 304 | }
|
285 | 305 |
|
286 | 306 | private static List<String> formattedHosts(List<InetSocketAddress> hosts, boolean useSsl) {
|
|
0 commit comments