57
57
* @author Peter-Josef Meisch
58
58
* @since 4.4
59
59
*/
60
+ @ SuppressWarnings ("unused" )
60
61
public final class ElasticsearchClients {
61
62
/**
62
63
* Name of whose value can be used to correlate log messages for this request.
63
64
*/
64
65
private static final String X_SPRING_DATA_ELASTICSEARCH_CLIENT = "X-SpringDataElasticsearch-Client" ;
65
- private static final String IMPERATIVE_CLIENT = "imperative" ;
66
- private static final String REACTIVE_CLIENT = "reactive" ;
66
+ public static final String IMPERATIVE_CLIENT = "imperative" ;
67
+ public static final String REACTIVE_CLIENT = "reactive" ;
67
68
68
69
private static final JsonpMapper DEFAULT_JSONP_MAPPER = new JacksonJsonpMapper ();
69
70
71
+ // region reactive client
70
72
/**
71
73
* Creates a new {@link ReactiveElasticsearchClient}
72
74
*
@@ -131,10 +133,28 @@ public static ReactiveElasticsearchClient createReactive(RestClient restClient)
131
133
*/
132
134
public static ReactiveElasticsearchClient createReactive (RestClient restClient ,
133
135
@ Nullable TransportOptions transportOptions , JsonpMapper jsonpMapper ) {
134
- return new ReactiveElasticsearchClient (
135
- getElasticsearchTransport (restClient , REACTIVE_CLIENT , transportOptions , jsonpMapper ));
136
+
137
+ Assert .notNull (restClient , "restClient must not be null" );
138
+
139
+ var transport = getElasticsearchTransport (restClient , REACTIVE_CLIENT , transportOptions , jsonpMapper );
140
+ return createReactive (transport );
136
141
}
137
142
143
+ /**
144
+ * Creates a new {@link ReactiveElasticsearchClient} that uses the given {@link ElasticsearchTransport}.
145
+ *
146
+ * @param transport the transport to use
147
+ * @return the {@link ElasticsearchClient
148
+ */
149
+ public static ReactiveElasticsearchClient createReactive (ElasticsearchTransport transport ) {
150
+
151
+ Assert .notNull (transport , "transport must not be null" );
152
+
153
+ return new ReactiveElasticsearchClient (transport );
154
+ }
155
+ // endregion
156
+
157
+ // region imperative client
138
158
/**
139
159
* Creates a new imperative {@link ElasticsearchClient}
140
160
*
@@ -183,8 +203,40 @@ public static ElasticsearchClient createImperative(RestClient restClient, @Nulla
183
203
ElasticsearchTransport transport = getElasticsearchTransport (restClient , IMPERATIVE_CLIENT , transportOptions ,
184
204
jsonpMapper );
185
205
206
+ return createImperative (transport );
207
+ }
208
+
209
+ /**
210
+ * Creates a new {@link ElasticsearchClient} that uses the given {@link ElasticsearchTransport}.
211
+ *
212
+ * @param transport the transport to use
213
+ * @return the {@link ElasticsearchClient
214
+ */
215
+ public static AutoCloseableElasticsearchClient createImperative (ElasticsearchTransport transport ) {
216
+
217
+ Assert .notNull (transport , "transport must not be null" );
218
+
186
219
return new AutoCloseableElasticsearchClient (transport );
187
220
}
221
+ // endregion
222
+
223
+ // region low level RestClient
224
+ private static RestClientOptions .Builder getRestClientOptionsBuilder (@ Nullable TransportOptions transportOptions ) {
225
+
226
+ if (transportOptions instanceof RestClientOptions restClientOptions ) {
227
+ return restClientOptions .toBuilder ();
228
+ }
229
+
230
+ var builder = new RestClientOptions .Builder (RequestOptions .DEFAULT .toBuilder ());
231
+
232
+ if (transportOptions != null ) {
233
+ transportOptions .headers ().forEach (header -> builder .addHeader (header .getKey (), header .getValue ()));
234
+ transportOptions .queryParameters ().forEach (builder ::setParameter );
235
+ builder .onWarnings (transportOptions .onWarnings ());
236
+ }
237
+
238
+ return builder ;
239
+ }
188
240
189
241
/**
190
242
* Creates a low level {@link RestClient} for the given configuration.
@@ -256,10 +308,26 @@ private static RestClientBuilder getRestClientBuilder(ClientConfiguration client
256
308
}
257
309
return builder ;
258
310
}
311
+ // endregion
259
312
260
- private static ElasticsearchTransport getElasticsearchTransport (RestClient restClient , String clientType ,
313
+ // region Elasticsearch transport
314
+ /**
315
+ * Creates an {@link ElasticsearchTransport} that will use the given client that additionally is customized with a
316
+ * header to contain the clientType
317
+ *
318
+ * @param restClient the client to use
319
+ * @param clientType the client type to pass in each request as header
320
+ * @param transportOptions options for the transport
321
+ * @param jsonpMapper mapper for the transport
322
+ * @return ElasticsearchTransport
323
+ */
324
+ public static ElasticsearchTransport getElasticsearchTransport (RestClient restClient , String clientType ,
261
325
@ Nullable TransportOptions transportOptions , JsonpMapper jsonpMapper ) {
262
326
327
+ Assert .notNull (restClient , "restClient must not be null" );
328
+ Assert .notNull (clientType , "clientType must not be null" );
329
+ Assert .notNull (jsonpMapper , "jsonpMapper must not be null" );
330
+
263
331
TransportOptions .Builder transportOptionsBuilder = transportOptions != null ? transportOptions .toBuilder ()
264
332
: new RestClientOptions (RequestOptions .DEFAULT ).toBuilder ();
265
333
@@ -285,26 +353,10 @@ private static ElasticsearchTransport getElasticsearchTransport(RestClient restC
285
353
286
354
return new RestClientTransport (restClient , jsonpMapper , restClientOptionsBuilder .build ());
287
355
}
288
-
289
- private static RestClientOptions .Builder getRestClientOptionsBuilder (@ Nullable TransportOptions transportOptions ) {
290
-
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 ;
304
- }
356
+ // endregion
305
357
306
358
private static List <String > formattedHosts (List <InetSocketAddress > hosts , boolean useSsl ) {
307
- return hosts .stream ().map (it -> (useSsl ? "https" : "http" ) + "://" + it .getHostString () + ":" + it .getPort ())
359
+ return hosts .stream ().map (it -> (useSsl ? "https" : "http" ) + "://" + it .getHostString () + ':' + it .getPort ())
308
360
.collect (Collectors .toList ());
309
361
}
310
362
@@ -320,13 +372,7 @@ private static org.apache.http.Header[] toHeaderArray(HttpHeaders headers) {
320
372
*
321
373
* @since 4.4
322
374
*/
323
- private static class CustomHeaderInjector implements HttpRequestInterceptor {
324
-
325
- public CustomHeaderInjector (Supplier <HttpHeaders > headersSupplier ) {
326
- this .headersSupplier = headersSupplier ;
327
- }
328
-
329
- private final Supplier <HttpHeaders > headersSupplier ;
375
+ private record CustomHeaderInjector (Supplier <HttpHeaders > headersSupplier ) implements HttpRequestInterceptor {
330
376
331
377
@ Override
332
378
public void process (HttpRequest request , HttpContext context ) {
0 commit comments