32
32
import org .elasticsearch .action .admin .cluster .settings .ClusterUpdateSettingsRequest ;
33
33
import org .elasticsearch .action .admin .indices .alias .IndicesAliasesRequest ;
34
34
import org .elasticsearch .action .admin .indices .alias .get .GetAliasesRequest ;
35
+ import org .elasticsearch .action .admin .indices .cache .clear .ClearIndicesCacheRequest ;
35
36
import org .elasticsearch .action .admin .indices .close .CloseIndexRequest ;
36
37
import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
37
38
import org .elasticsearch .action .admin .indices .delete .DeleteIndexRequest ;
@@ -170,13 +171,10 @@ static Request openIndex(OpenIndexRequest openIndexRequest) {
170
171
171
172
static Request closeIndex (CloseIndexRequest closeIndexRequest ) {
172
173
String endpoint = endpoint (closeIndexRequest .indices (), "_close" );
173
-
174
174
Params parameters = Params .builder ();
175
-
176
175
parameters .withTimeout (closeIndexRequest .timeout ());
177
176
parameters .withMasterTimeout (closeIndexRequest .masterNodeTimeout ());
178
177
parameters .withIndicesOptions (closeIndexRequest .indicesOptions ());
179
-
180
178
return new Request (HttpPost .METHOD_NAME , endpoint , parameters .getParams (), null );
181
179
}
182
180
@@ -218,21 +216,35 @@ static Request putMapping(PutMappingRequest putMappingRequest) throws IOExceptio
218
216
}
219
217
220
218
static Request refresh (RefreshRequest refreshRequest ) {
221
- String endpoint = endpoint (refreshRequest .indices (), "_refresh" );
219
+ String [] indices = refreshRequest .indices () == null ? Strings .EMPTY_ARRAY : refreshRequest .indices ();
220
+ String endpoint = endpoint (indices , "_refresh" );
222
221
Params parameters = Params .builder ();
223
222
parameters .withIndicesOptions (refreshRequest .indicesOptions ());
224
223
return new Request (HttpPost .METHOD_NAME , endpoint , parameters .getParams (), null );
225
224
}
226
225
227
226
static Request flush (FlushRequest flushRequest ) {
228
- String endpoint = endpoint (flushRequest .indices (), "_flush" );
227
+ String [] indices = flushRequest .indices () == null ? Strings .EMPTY_ARRAY : flushRequest .indices ();
228
+ String endpoint = endpoint (indices , "_flush" );
229
229
Params parameters = Params .builder ();
230
230
parameters .withIndicesOptions (flushRequest .indicesOptions ());
231
231
parameters .putParam ("wait_if_ongoing" , Boolean .toString (flushRequest .waitIfOngoing ()));
232
232
parameters .putParam ("force" , Boolean .toString (flushRequest .force ()));
233
233
return new Request (HttpPost .METHOD_NAME , endpoint , parameters .getParams (), null );
234
234
}
235
235
236
+ static Request clearCache (ClearIndicesCacheRequest clearIndicesCacheRequest ) {
237
+ String [] indices = clearIndicesCacheRequest .indices () == null ? Strings .EMPTY_ARRAY :clearIndicesCacheRequest .indices ();
238
+ String endpoint = endpoint (indices , "_cache/clear" );
239
+ Params parameters = Params .builder ();
240
+ parameters .withIndicesOptions (clearIndicesCacheRequest .indicesOptions ());
241
+ parameters .putParam ("query" , Boolean .toString (clearIndicesCacheRequest .queryCache ()));
242
+ parameters .putParam ("fielddata" , Boolean .toString (clearIndicesCacheRequest .fieldDataCache ()));
243
+ parameters .putParam ("request" , Boolean .toString (clearIndicesCacheRequest .requestCache ()));
244
+ parameters .putParam ("fields" , String .join ("," , clearIndicesCacheRequest .fields ()));
245
+ return new Request (HttpPost .METHOD_NAME , endpoint , parameters .getParams (), null );
246
+ }
247
+
236
248
static Request info () {
237
249
return new Request (HttpGet .METHOD_NAME , "/" , Collections .emptyMap (), null );
238
250
}
@@ -508,10 +520,13 @@ static Request existsAlias(GetAliasesRequest getAliasesRequest) {
508
520
Params params = Params .builder ();
509
521
params .withIndicesOptions (getAliasesRequest .indicesOptions ());
510
522
params .withLocal (getAliasesRequest .local ());
511
- if (getAliasesRequest .indices ().length == 0 && getAliasesRequest .aliases ().length == 0 ) {
523
+ if ((getAliasesRequest .indices () == null || getAliasesRequest .indices ().length == 0 ) &&
524
+ (getAliasesRequest .aliases () == null || getAliasesRequest .aliases ().length == 0 )) {
512
525
throw new IllegalArgumentException ("existsAlias requires at least an alias or an index" );
513
526
}
514
- String endpoint = endpoint (getAliasesRequest .indices (), "_alias" , getAliasesRequest .aliases ());
527
+ String [] indices = getAliasesRequest .indices () == null ? Strings .EMPTY_ARRAY : getAliasesRequest .indices ();
528
+ String [] aliases = getAliasesRequest .aliases () == null ? Strings .EMPTY_ARRAY : getAliasesRequest .aliases ();
529
+ String endpoint = endpoint (indices , "_alias" , aliases );
515
530
return new Request (HttpHead .METHOD_NAME , endpoint , params .getParams (), null );
516
531
}
517
532
@@ -540,8 +555,9 @@ private static Request resize(ResizeRequest resizeRequest) throws IOException {
540
555
params .withTimeout (resizeRequest .timeout ());
541
556
params .withMasterTimeout (resizeRequest .masterNodeTimeout ());
542
557
params .withWaitForActiveShards (resizeRequest .getTargetIndexRequest ().waitForActiveShards ());
543
- String endpoint = buildEndpoint (resizeRequest .getSourceIndex (), "_" + resizeRequest .getResizeType ().name ().toLowerCase (Locale .ROOT ),
544
- resizeRequest .getTargetIndexRequest ().index ());
558
+ String endpoint = new EndpointBuilder ().addPathPart (resizeRequest .getSourceIndex ())
559
+ .addPathPartAsIs ("_" + resizeRequest .getResizeType ().name ().toLowerCase (Locale .ROOT ))
560
+ .addPathPart (resizeRequest .getTargetIndexRequest ().index ()).build ();
545
561
HttpEntity entity = createEntity (resizeRequest , REQUEST_BODY_CONTENT_TYPE );
546
562
return new Request (HttpPut .METHOD_NAME , endpoint , params .getParams (), entity );
547
563
}
@@ -551,10 +567,8 @@ static Request clusterPutSettings(ClusterUpdateSettingsRequest clusterUpdateSett
551
567
parameters .withFlatSettings (clusterUpdateSettingsRequest .flatSettings ());
552
568
parameters .withTimeout (clusterUpdateSettingsRequest .timeout ());
553
569
parameters .withMasterTimeout (clusterUpdateSettingsRequest .masterNodeTimeout ());
554
-
555
- String endpoint = buildEndpoint ("_cluster" , "settings" );
556
570
HttpEntity entity = createEntity (clusterUpdateSettingsRequest , REQUEST_BODY_CONTENT_TYPE );
557
- return new Request (HttpPut .METHOD_NAME , endpoint , parameters .getParams (), entity );
571
+ return new Request (HttpPut .METHOD_NAME , "/_cluster/settings" , parameters .getParams (), entity );
558
572
}
559
573
560
574
static Request rollover (RolloverRequest rolloverRequest ) throws IOException {
@@ -565,64 +579,60 @@ static Request rollover(RolloverRequest rolloverRequest) throws IOException {
565
579
if (rolloverRequest .isDryRun ()) {
566
580
params .putParam ("dry_run" , Boolean .TRUE .toString ());
567
581
}
568
- String endpoint = buildEndpoint (rolloverRequest .getAlias (), "_rollover" , rolloverRequest .getNewIndexName ());
582
+ String endpoint = new EndpointBuilder ().addPathPart (rolloverRequest .getAlias ()).addPathPartAsIs ("_rollover" )
583
+ .addPathPart (rolloverRequest .getNewIndexName ()).build ();
569
584
HttpEntity entity = createEntity (rolloverRequest , REQUEST_BODY_CONTENT_TYPE );
570
585
return new Request (HttpPost .METHOD_NAME , endpoint , params .getParams (), entity );
571
586
}
572
587
588
+ static Request indicesExist (GetIndexRequest request ) {
589
+ //this can be called with no indices as argument by transport client, not via REST though
590
+ if (request .indices () == null || request .indices ().length == 0 ) {
591
+ throw new IllegalArgumentException ("indices are mandatory" );
592
+ }
593
+ String endpoint = endpoint (request .indices (), "" );
594
+ Params params = Params .builder ();
595
+ params .withLocal (request .local ());
596
+ params .withHuman (request .humanReadable ());
597
+ params .withIndicesOptions (request .indicesOptions ());
598
+ params .withFlatSettings (request .flatSettings ());
599
+ params .withIncludeDefaults (request .includeDefaults ());
600
+ return new Request (HttpHead .METHOD_NAME , endpoint , params .getParams (), null );
601
+ }
602
+
573
603
private static HttpEntity createEntity (ToXContent toXContent , XContentType xContentType ) throws IOException {
574
604
BytesRef source = XContentHelper .toXContent (toXContent , xContentType , false ).toBytesRef ();
575
605
return new ByteArrayEntity (source .bytes , source .offset , source .length , createContentType (xContentType ));
576
606
}
577
607
578
608
static String endpoint (String index , String type , String id ) {
579
- return buildEndpoint ( index , type , id );
609
+ return new EndpointBuilder (). addPathPart ( index , type , id ). build ( );
580
610
}
581
611
582
612
static String endpoint (String index , String type , String id , String endpoint ) {
583
- return buildEndpoint ( index , type , id , endpoint );
613
+ return new EndpointBuilder (). addPathPart ( index , type , id ). addPathPartAsIs ( endpoint ). build ( );
584
614
}
585
615
586
616
static String endpoint (String [] indices ) {
587
- return buildEndpoint ( String . join ( "," , indices ));
617
+ return new EndpointBuilder (). addCommaSeparatedPathParts ( indices ). build ( );
588
618
}
589
619
590
620
static String endpoint (String [] indices , String endpoint ) {
591
- return buildEndpoint ( String . join ( "," , indices ), endpoint );
621
+ return new EndpointBuilder (). addCommaSeparatedPathParts ( indices ). addPathPartAsIs ( endpoint ). build ( );
592
622
}
593
623
594
624
static String endpoint (String [] indices , String [] types , String endpoint ) {
595
- return buildEndpoint (String .join ("," , indices ), String .join ("," , types ), endpoint );
625
+ return new EndpointBuilder ().addCommaSeparatedPathParts (indices ).addCommaSeparatedPathParts (types )
626
+ .addPathPartAsIs (endpoint ).build ();
596
627
}
597
628
598
629
static String endpoint (String [] indices , String endpoint , String [] suffixes ) {
599
- return buildEndpoint (String .join ("," , indices ), endpoint , String .join ("," , suffixes ));
630
+ return new EndpointBuilder ().addCommaSeparatedPathParts (indices ).addPathPartAsIs (endpoint )
631
+ .addCommaSeparatedPathParts (suffixes ).build ();
600
632
}
601
633
602
634
static String endpoint (String [] indices , String endpoint , String type ) {
603
- return endpoint (String .join ("," , indices ), endpoint , type );
604
- }
605
-
606
- /**
607
- * Utility method to build request's endpoint given its parts as strings
608
- */
609
- static String buildEndpoint (String ... parts ) {
610
- StringJoiner joiner = new StringJoiner ("/" , "/" , "" );
611
- for (String part : parts ) {
612
- if (Strings .hasLength (part )) {
613
- try {
614
- //encode each part (e.g. index, type and id) separately before merging them into the path
615
- //we prepend "/" to the path part to make this pate absolute, otherwise there can be issues with
616
- //paths that start with `-` or contain `:`
617
- URI uri = new URI (null , null , null , -1 , "/" + part , null , null );
618
- //manually encode any slash that each part may contain
619
- joiner .add (uri .getRawPath ().substring (1 ).replaceAll ("/" , "%2F" ));
620
- } catch (URISyntaxException e ) {
621
- throw new IllegalArgumentException ("Path part [" + part + "] couldn't be encoded" , e );
622
- }
623
- }
624
- }
625
- return joiner .toString ();
635
+ return new EndpointBuilder ().addCommaSeparatedPathParts (indices ).addPathPartAsIs (endpoint ).addPathPart (type ).build ();
626
636
}
627
637
628
638
/**
@@ -636,17 +646,6 @@ public static ContentType createContentType(final XContentType xContentType) {
636
646
return ContentType .create (xContentType .mediaTypeWithoutParameters (), (Charset ) null );
637
647
}
638
648
639
- static Request indicesExist (GetIndexRequest request ) {
640
- String endpoint = endpoint (request .indices (), Strings .EMPTY_ARRAY , "" );
641
- Params params = Params .builder ();
642
- params .withLocal (request .local ());
643
- params .withHuman (request .humanReadable ());
644
- params .withIndicesOptions (request .indicesOptions ());
645
- params .withFlatSettings (request .flatSettings ());
646
- params .withIncludeDefaults (request .includeDefaults ());
647
- return new Request (HttpHead .METHOD_NAME , endpoint , params .getParams (), null );
648
- }
649
-
650
649
/**
651
650
* Utility class to build request's parameters map and centralize all parameter names.
652
651
*/
@@ -852,4 +851,50 @@ static XContentType enforceSameContentType(IndexRequest indexRequest, @Nullable
852
851
}
853
852
return xContentType ;
854
853
}
854
+
855
+ /**
856
+ * Utility class to build request's endpoint given its parts as strings
857
+ */
858
+ static class EndpointBuilder {
859
+
860
+ private final StringJoiner joiner = new StringJoiner ("/" , "/" , "" );
861
+
862
+ EndpointBuilder addPathPart (String ... parts ) {
863
+ for (String part : parts ) {
864
+ if (Strings .hasLength (part )) {
865
+ joiner .add (encodePart (part ));
866
+ }
867
+ }
868
+ return this ;
869
+ }
870
+
871
+ EndpointBuilder addCommaSeparatedPathParts (String [] parts ) {
872
+ addPathPart (String .join ("," , parts ));
873
+ return this ;
874
+ }
875
+
876
+ EndpointBuilder addPathPartAsIs (String part ) {
877
+ if (Strings .hasLength (part )) {
878
+ joiner .add (part );
879
+ }
880
+ return this ;
881
+ }
882
+
883
+ String build () {
884
+ return joiner .toString ();
885
+ }
886
+
887
+ private static String encodePart (String pathPart ) {
888
+ try {
889
+ //encode each part (e.g. index, type and id) separately before merging them into the path
890
+ //we prepend "/" to the path part to make this pate absolute, otherwise there can be issues with
891
+ //paths that start with `-` or contain `:`
892
+ URI uri = new URI (null , null , null , -1 , "/" + pathPart , null , null );
893
+ //manually encode any slash that each part may contain
894
+ return uri .getRawPath ().substring (1 ).replaceAll ("/" , "%2F" );
895
+ } catch (URISyntaxException e ) {
896
+ throw new IllegalArgumentException ("Path part [" + pathPart + "] couldn't be encoded" , e );
897
+ }
898
+ }
899
+ }
855
900
}
0 commit comments