diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GrokProcessorGetAction.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GrokProcessorGetAction.java index 45ffcc53a4d..c68f498c0ea 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GrokProcessorGetAction.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/GrokProcessorGetAction.java @@ -23,9 +23,9 @@ import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestBuilder; import org.elasticsearch.action.ActionRequestValidationException; +import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.HandledTransportAction; -import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.client.ElasticsearchClient; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; @@ -85,7 +85,7 @@ public class GrokProcessorGetAction extends Action grokPatterns; public Response(Map grokPatterns) { diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/IngestCommonPlugin.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/IngestCommonPlugin.java index 79c6dadb4fc..a29c994f10d 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/IngestCommonPlugin.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/IngestCommonPlugin.java @@ -19,19 +19,6 @@ package org.elasticsearch.ingest.common; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.UncheckedIOException; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Supplier; - import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; @@ -48,6 +35,13 @@ import org.elasticsearch.plugins.Plugin; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestHandler; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; + public class IngestCommonPlugin extends Plugin implements ActionPlugin, IngestPlugin { static final Map GROK_PATTERNS = Grok.getBuiltinPatterns(); diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponse.java index 74bf8f341f3..f3233f4147e 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponse.java @@ -24,13 +24,16 @@ import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.routing.allocation.RoutingExplanations; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; /** * Response returned after a cluster reroute request */ -public class ClusterRerouteResponse extends AcknowledgedResponse { +public class ClusterRerouteResponse extends AcknowledgedResponse implements ToXContentObject { private ClusterState state; private RoutingExplanations explanations; @@ -71,4 +74,14 @@ public class ClusterRerouteResponse extends AcknowledgedResponse { writeAcknowledged(out); RoutingExplanations.writeTo(explanations, out); } + + @Override + protected void addCustomFields(XContentBuilder builder, Params params) throws IOException { + builder.startObject("state"); + state.toXContent(builder, params); + builder.endObject(); + if (params.paramAsBoolean("explain", false)) { + explanations.toXContent(builder, ToXContent.EMPTY_PARAMS); + } + } } diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponse.java index 4fef5fa7a2d..9ce22268afd 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/settings/ClusterUpdateSettingsResponse.java @@ -25,7 +25,6 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -35,7 +34,7 @@ import java.util.Objects; /** * A response for a cluster update settings action. */ -public class ClusterUpdateSettingsResponse extends AcknowledgedResponse implements ToXContentObject { +public class ClusterUpdateSettingsResponse extends AcknowledgedResponse { private static final ParseField PERSISTENT = new ParseField("persistent"); private static final ParseField TRANSIENT = new ParseField("transient"); @@ -91,17 +90,13 @@ public class ClusterUpdateSettingsResponse extends AcknowledgedResponse implemen } @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - addAcknowledgedField(builder); + protected void addCustomFields(XContentBuilder builder, Params params) throws IOException { builder.startObject(PERSISTENT.getPreferredName()); persistentSettings.toXContent(builder, params); builder.endObject(); builder.startObject(TRANSIENT.getPreferredName()); transientSettings.toXContent(builder, params); builder.endObject(); - builder.endObject(); - return builder; } public static ClusterUpdateSettingsResponse fromXContent(XContentParser parser) { diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponse.java index cd0802619e4..b6402d5139a 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesResponse.java @@ -23,8 +23,6 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.ToXContentObject; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; @@ -32,7 +30,7 @@ import java.io.IOException; /** * A response for a add/remove alias action. */ -public class IndicesAliasesResponse extends AcknowledgedResponse implements ToXContentObject { +public class IndicesAliasesResponse extends AcknowledgedResponse { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("indices_aliases", true, args -> new IndicesAliasesResponse((boolean) args[0])); @@ -59,14 +57,6 @@ public class IndicesAliasesResponse extends AcknowledgedResponse implements ToXC writeAcknowledged(out); } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - addAcknowledgedField(builder); - builder.endObject(); - return builder; - } - public static IndicesAliasesResponse fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponse.java index 4f639400b01..de56c52f9f6 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/close/CloseIndexResponse.java @@ -23,8 +23,6 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.ToXContentObject; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; @@ -32,7 +30,7 @@ import java.io.IOException; /** * A response for a close index action. */ -public class CloseIndexResponse extends AcknowledgedResponse implements ToXContentObject { +public class CloseIndexResponse extends AcknowledgedResponse { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("close_index", true, args -> new CloseIndexResponse((boolean) args[0])); @@ -59,14 +57,6 @@ public class CloseIndexResponse extends AcknowledgedResponse implements ToXConte writeAcknowledged(out); } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - addAcknowledgedField(builder); - builder.endObject(); - return builder; - } - public static CloseIndexResponse fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponse.java index 38e7cf0c969..edc0852cb28 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexResponse.java @@ -26,7 +26,6 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ObjectParser; -import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -38,7 +37,7 @@ import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constru /** * A response for a create index action. */ -public class CreateIndexResponse extends ShardsAcknowledgedResponse implements ToXContentObject { +public class CreateIndexResponse extends ShardsAcknowledgedResponse { private static final ParseField INDEX = new ParseField("index"); @@ -89,13 +88,9 @@ public class CreateIndexResponse extends ShardsAcknowledgedResponse implements T } @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - addAcknowledgedField(builder); - addShardsAcknowledgedField(builder); + protected void addCustomFields(XContentBuilder builder, Params params) throws IOException { + super.addCustomFields(builder, params); builder.field(INDEX.getPreferredName(), index()); - builder.endObject(); - return builder; } public static CreateIndexResponse fromXContent(XContentParser parser) { diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/delete/DeleteIndexResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/delete/DeleteIndexResponse.java index 378f10bb820..a788f272d18 100755 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/delete/DeleteIndexResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/delete/DeleteIndexResponse.java @@ -23,7 +23,6 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -32,7 +31,7 @@ import java.io.IOException; /** * A response for a delete index action. */ -public class DeleteIndexResponse extends AcknowledgedResponse implements ToXContentObject { +public class DeleteIndexResponse extends AcknowledgedResponse { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("delete_index", true, args -> new DeleteIndexResponse((boolean) args[0])); @@ -60,14 +59,6 @@ public class DeleteIndexResponse extends AcknowledgedResponse implements ToXCont writeAcknowledged(out); } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - addAcknowledgedField(builder); - builder.endObject(); - return builder; - } - public static DeleteIndexResponse fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java index 0cddba3b73a..8ccc5c8006a 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingResponse.java @@ -23,8 +23,6 @@ import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.ToXContentObject; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import java.io.IOException; @@ -32,7 +30,7 @@ import java.io.IOException; /** * The response of put mapping operation. */ -public class PutMappingResponse extends AcknowledgedResponse implements ToXContentObject { +public class PutMappingResponse extends AcknowledgedResponse { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("put_mapping", true, args -> new PutMappingResponse((boolean) args[0])); @@ -61,14 +59,6 @@ public class PutMappingResponse extends AcknowledgedResponse implements ToXConte writeAcknowledged(out); } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - addAcknowledgedField(builder); - builder.endObject(); - return builder; - } - public static PutMappingResponse fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponse.java index 5bc115eaf1a..3918273cec9 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/open/OpenIndexResponse.java @@ -21,10 +21,10 @@ package org.elasticsearch.action.admin.indices.open; import org.elasticsearch.Version; import org.elasticsearch.action.support.master.ShardsAcknowledgedResponse; +import org.elasticsearch.common.ParseField; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ConstructingObjectParser; -import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -33,7 +33,7 @@ import java.io.IOException; /** * A response for a open index action. */ -public class OpenIndexResponse extends ShardsAcknowledgedResponse implements ToXContentObject { +public class OpenIndexResponse extends ShardsAcknowledgedResponse { private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("open_index", true, args -> new OpenIndexResponse((boolean) args[0], (boolean) args[1])); @@ -67,15 +67,6 @@ public class OpenIndexResponse extends ShardsAcknowledgedResponse implements ToX } } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); - addAcknowledgedField(builder); - addShardsAcknowledgedField(builder); - builder.endObject(); - return builder; - } - public static OpenIndexResponse fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverResponse.java index 2aabc569730..d3ba00cdfcf 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/rollover/RolloverResponse.java @@ -145,21 +145,17 @@ public final class RolloverResponse extends ShardsAcknowledgedResponse implement } @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(); + protected void addCustomFields(XContentBuilder builder, Params params) throws IOException { + super.addCustomFields(builder, params); builder.field(OLD_INDEX.getPreferredName(), oldIndex); builder.field(NEW_INDEX.getPreferredName(), newIndex); builder.field(ROLLED_OVER.getPreferredName(), rolledOver); builder.field(DRY_RUN.getPreferredName(), dryRun); - addAcknowledgedField(builder); - addShardsAcknowledgedField(builder); builder.startObject(CONDITIONS.getPreferredName()); for (Map.Entry entry : conditionStatus.entrySet()) { builder.field(entry.getKey(), entry.getValue()); } builder.endObject(); - builder.endObject(); - return builder; } public static RolloverResponse fromXContent(XContentParser parser) { diff --git a/server/src/main/java/org/elasticsearch/action/support/master/AcknowledgedResponse.java b/server/src/main/java/org/elasticsearch/action/support/master/AcknowledgedResponse.java index 0d2b26aecca..ea9a373efdd 100755 --- a/server/src/main/java/org/elasticsearch/action/support/master/AcknowledgedResponse.java +++ b/server/src/main/java/org/elasticsearch/action/support/master/AcknowledgedResponse.java @@ -24,6 +24,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.ObjectParser; +import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; @@ -35,7 +36,7 @@ import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constru * Abstract class that allows to mark action responses that support acknowledgements. * Facilitates consistency across different api. */ -public abstract class AcknowledgedResponse extends ActionResponse { +public abstract class AcknowledgedResponse extends ActionResponse implements ToXContentObject { private static final ParseField ACKNOWLEDGED = new ParseField("acknowledged"); @@ -76,8 +77,17 @@ public abstract class AcknowledgedResponse extends ActionResponse { out.writeBoolean(acknowledged); } - protected void addAcknowledgedField(XContentBuilder builder) throws IOException { + @Override + public final XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); builder.field(ACKNOWLEDGED.getPreferredName(), isAcknowledged()); + addCustomFields(builder, params); + builder.endObject(); + return builder; + } + + protected void addCustomFields(XContentBuilder builder, Params params) throws IOException { + } @Override diff --git a/server/src/main/java/org/elasticsearch/action/support/master/ShardsAcknowledgedResponse.java b/server/src/main/java/org/elasticsearch/action/support/master/ShardsAcknowledgedResponse.java index 36a492445fd..21ff8fe6449 100644 --- a/server/src/main/java/org/elasticsearch/action/support/master/ShardsAcknowledgedResponse.java +++ b/server/src/main/java/org/elasticsearch/action/support/master/ShardsAcknowledgedResponse.java @@ -71,7 +71,8 @@ public abstract class ShardsAcknowledgedResponse extends AcknowledgedResponse { out.writeBoolean(shardsAcknowledged); } - protected void addShardsAcknowledgedField(XContentBuilder builder) throws IOException { + @Override + protected void addCustomFields(XContentBuilder builder, Params params) throws IOException { builder.field(SHARDS_ACKNOWLEDGED.getPreferredName(), isShardsAcknowledged()); } diff --git a/server/src/main/java/org/elasticsearch/rest/action/AcknowledgedRestListener.java b/server/src/main/java/org/elasticsearch/rest/action/AcknowledgedRestListener.java deleted file mode 100644 index 7fce8b31067..00000000000 --- a/server/src/main/java/org/elasticsearch/rest/action/AcknowledgedRestListener.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.rest.action; - -import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.rest.BytesRestResponse; -import org.elasticsearch.rest.RestChannel; -import org.elasticsearch.rest.RestResponse; - -import java.io.IOException; - -import static org.elasticsearch.rest.RestStatus.OK; - -//TODO once all the responses that use this class implement ToXContent we can move to RestToXContentListener and remove this class -public class AcknowledgedRestListener extends RestBuilderListener { - - public AcknowledgedRestListener(RestChannel channel) { - super(channel); - } - - @Override - public RestResponse buildResponse(T response, XContentBuilder builder) throws Exception { - builder.startObject() - .field(Fields.ACKNOWLEDGED, response.isAcknowledged()); - addCustomFields(builder, response); - builder.endObject(); - return new BytesRestResponse(OK, builder); - } - - /** - * Adds api specific fields to the rest response - * Does nothing by default but can be overridden by subclasses - */ - protected void addCustomFields(XContentBuilder builder, T response) throws IOException { - - } - - static final class Fields { - static final String ACKNOWLEDGED = "acknowledged"; - } -} diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterRerouteAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterRerouteAction.java index 1eafe29cfe7..4b019e98e62 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterRerouteAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestClusterRerouteAction.java @@ -20,7 +20,6 @@ package org.elasticsearch.rest.action.admin.cluster; import org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteRequest; -import org.elasticsearch.action.admin.cluster.reroute.ClusterRerouteResponse; import org.elasticsearch.client.Requests; import org.elasticsearch.client.node.NodeClient; import org.elasticsearch.cluster.ClusterState; @@ -31,12 +30,10 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsFilter; import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.ObjectParser.ValueType; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; import java.util.Collections; @@ -71,27 +68,16 @@ public class RestClusterRerouteAction extends BaseRestHandler { @Override public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { ClusterRerouteRequest clusterRerouteRequest = createRequest(request); - + settingsFilter.addFilterSettingParams(request); + if (clusterRerouteRequest.explain()) { + request.params().put("explain", Boolean.TRUE.toString()); + } // by default, return everything but metadata final String metric = request.param("metric"); if (metric == null) { request.params().put("metric", DEFAULT_METRICS); } - - return channel -> - client.admin().cluster().reroute(clusterRerouteRequest, new AcknowledgedRestListener(channel) { - @Override - protected void addCustomFields(XContentBuilder builder, ClusterRerouteResponse response) throws IOException { - builder.startObject("state"); - settingsFilter.addFilterSettingParams(request); - response.getState().toXContent(builder, request); - builder.endObject(); - if (clusterRerouteRequest.explain()) { - assert response.getExplanations() != null; - response.getExplanations().toXContent(builder, ToXContent.EMPTY_PARAMS); - } - } - }); + return channel -> client.admin().cluster().reroute(clusterRerouteRequest, new RestToXContentListener<>(channel)); } private static final Set RESPONSE_PARAMS; diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestDeleteRepositoryAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestDeleteRepositoryAction.java index 33455365dcc..d2740827d1e 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestDeleteRepositoryAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestDeleteRepositoryAction.java @@ -25,7 +25,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -52,6 +52,6 @@ public class RestDeleteRepositoryAction extends BaseRestHandler { deleteRepositoryRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteRepositoryRequest.masterNodeTimeout())); deleteRepositoryRequest.timeout(request.paramAsTime("timeout", deleteRepositoryRequest.timeout())); deleteRepositoryRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteRepositoryRequest.masterNodeTimeout())); - return channel -> client.admin().cluster().deleteRepository(deleteRepositoryRequest, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().cluster().deleteRepository(deleteRepositoryRequest, new RestToXContentListener<>(channel)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestDeleteSnapshotAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestDeleteSnapshotAction.java index fbd8822a84e..6b18e9a3d50 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestDeleteSnapshotAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestDeleteSnapshotAction.java @@ -25,7 +25,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -50,6 +50,6 @@ public class RestDeleteSnapshotAction extends BaseRestHandler { public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { DeleteSnapshotRequest deleteSnapshotRequest = deleteSnapshotRequest(request.param("repository"), request.param("snapshot")); deleteSnapshotRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteSnapshotRequest.masterNodeTimeout())); - return channel -> client.admin().cluster().deleteSnapshot(deleteSnapshotRequest, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().cluster().deleteSnapshot(deleteSnapshotRequest, new RestToXContentListener<>(channel)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestDeleteStoredScriptAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestDeleteStoredScriptAction.java index 0085de36f19..2da902df9da 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestDeleteStoredScriptAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestDeleteStoredScriptAction.java @@ -24,7 +24,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -50,6 +50,6 @@ public class RestDeleteStoredScriptAction extends BaseRestHandler { deleteStoredScriptRequest.timeout(request.paramAsTime("timeout", deleteStoredScriptRequest.timeout())); deleteStoredScriptRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteStoredScriptRequest.masterNodeTimeout())); - return channel -> client.admin().cluster().deleteStoredScript(deleteStoredScriptRequest, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().cluster().deleteStoredScript(deleteStoredScriptRequest, new RestToXContentListener<>(channel)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutRepositoryAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutRepositoryAction.java index b87871e064e..62cc06cc404 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutRepositoryAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutRepositoryAction.java @@ -26,7 +26,7 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -58,6 +58,6 @@ public class RestPutRepositoryAction extends BaseRestHandler { putRepositoryRequest.verify(request.paramAsBoolean("verify", true)); putRepositoryRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putRepositoryRequest.masterNodeTimeout())); putRepositoryRequest.timeout(request.paramAsTime("timeout", putRepositoryRequest.timeout())); - return channel -> client.admin().cluster().putRepository(putRepositoryRequest, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().cluster().putRepository(putRepositoryRequest, new RestToXContentListener<>(channel)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutStoredScriptAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutStoredScriptAction.java index 27083503195..4a4530b6fa3 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutStoredScriptAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutStoredScriptAction.java @@ -26,7 +26,7 @@ import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import org.elasticsearch.script.StoredScriptSource; import java.io.IOException; @@ -61,6 +61,6 @@ public class RestPutStoredScriptAction extends BaseRestHandler { PutStoredScriptRequest putRequest = new PutStoredScriptRequest(id, context, content, request.getXContentType(), source); putRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putRequest.masterNodeTimeout())); putRequest.timeout(request.paramAsTime("timeout", putRequest.timeout())); - return channel -> client.admin().cluster().putStoredScript(putRequest, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().cluster().putStoredScript(putRequest, new RestToXContentListener<>(channel)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCloseIndexAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCloseIndexAction.java index d750dedd4b8..b2475cafcbe 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCloseIndexAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestCloseIndexAction.java @@ -27,7 +27,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -49,7 +49,7 @@ public class RestCloseIndexAction extends BaseRestHandler { closeIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", closeIndexRequest.masterNodeTimeout())); closeIndexRequest.timeout(request.paramAsTime("timeout", closeIndexRequest.timeout())); closeIndexRequest.indicesOptions(IndicesOptions.fromRequest(request, closeIndexRequest.indicesOptions())); - return channel -> client.admin().indices().close(closeIndexRequest, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().indices().close(closeIndexRequest, new RestToXContentListener<>(channel)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestDeleteIndexTemplateAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestDeleteIndexTemplateAction.java index c3fad980877..114e043e2e1 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestDeleteIndexTemplateAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestDeleteIndexTemplateAction.java @@ -24,7 +24,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -43,6 +43,6 @@ public class RestDeleteIndexTemplateAction extends BaseRestHandler { public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException { DeleteIndexTemplateRequest deleteIndexTemplateRequest = new DeleteIndexTemplateRequest(request.param("name")); deleteIndexTemplateRequest.masterNodeTimeout(request.paramAsTime("master_timeout", deleteIndexTemplateRequest.masterNodeTimeout())); - return channel -> client.admin().indices().deleteTemplate(deleteIndexTemplateRequest, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().indices().deleteTemplate(deleteIndexTemplateRequest, new RestToXContentListener<>(channel)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndexDeleteAliasesAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndexDeleteAliasesAction.java index 696b7768404..270a9c8fdc4 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndexDeleteAliasesAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndexDeleteAliasesAction.java @@ -26,7 +26,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -53,6 +53,6 @@ public class RestIndexDeleteAliasesAction extends BaseRestHandler { indicesAliasesRequest.addAliasAction(AliasActions.remove().indices(indices).aliases(aliases)); indicesAliasesRequest.masterNodeTimeout(request.paramAsTime("master_timeout", indicesAliasesRequest.masterNodeTimeout())); - return channel -> client.admin().indices().aliases(indicesAliasesRequest, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().indices().aliases(indicesAliasesRequest, new RestToXContentListener<>(channel)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndexPutAliasAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndexPutAliasAction.java index dc95e15802e..73e9bad45a5 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndexPutAliasAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndexPutAliasAction.java @@ -27,7 +27,7 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; import java.util.Map; @@ -118,6 +118,6 @@ public class RestIndexPutAliasAction extends BaseRestHandler { aliasAction.filter(filter); } indicesAliasesRequest.addAliasAction(aliasAction); - return channel -> client.admin().indices().aliases(indicesAliasesRequest, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().indices().aliases(indicesAliasesRequest, new RestToXContentListener<>(channel)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndicesAliasesAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndicesAliasesAction.java index faae93803c8..fe4155f8cb2 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndicesAliasesAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestIndicesAliasesAction.java @@ -20,16 +20,13 @@ package org.elasticsearch.rest.action.admin.indices; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; -import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions; import org.elasticsearch.client.node.NodeClient; -import org.elasticsearch.common.ParseField; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.ObjectParser; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -58,6 +55,6 @@ public class RestIndicesAliasesAction extends BaseRestHandler { if (indicesAliasesRequest.getAliasActions().isEmpty()) { throw new IllegalArgumentException("No action specified"); } - return channel -> client.admin().indices().aliases(indicesAliasesRequest, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().indices().aliases(indicesAliasesRequest, new RestToXContentListener<>(channel)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutIndexTemplateAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutIndexTemplateAction.java index c96f127ee7a..58f5dcb3e9c 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutIndexTemplateAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutIndexTemplateAction.java @@ -28,7 +28,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; import java.util.Arrays; @@ -63,7 +63,7 @@ public class RestPutIndexTemplateAction extends BaseRestHandler { putRequest.create(request.paramAsBoolean("create", false)); putRequest.cause(request.param("cause", "")); putRequest.source(request.requiredContent(), request.getXContentType()); - return channel -> client.admin().indices().putTemplate(putRequest, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().indices().putTemplate(putRequest, new RestToXContentListener<>(channel)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutMappingAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutMappingAction.java index cdac83037db..6d3804eddc9 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutMappingAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestPutMappingAction.java @@ -27,7 +27,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -73,6 +73,6 @@ public class RestPutMappingAction extends BaseRestHandler { putMappingRequest.timeout(request.paramAsTime("timeout", putMappingRequest.timeout())); putMappingRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putMappingRequest.masterNodeTimeout())); putMappingRequest.indicesOptions(IndicesOptions.fromRequest(request, putMappingRequest.indicesOptions())); - return channel -> client.admin().indices().putMapping(putMappingRequest, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().indices().putMapping(putMappingRequest, new RestToXContentListener<>(channel)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestUpdateSettingsAction.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestUpdateSettingsAction.java index c2b6b09fa3a..93090ba25ee 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestUpdateSettingsAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/RestUpdateSettingsAction.java @@ -28,7 +28,7 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; import java.util.HashMap; @@ -73,7 +73,7 @@ public class RestUpdateSettingsAction extends BaseRestHandler { } updateSettingsRequest.settings(settings); - return channel -> client.admin().indices().updateSettings(updateSettingsRequest, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().indices().updateSettings(updateSettingsRequest, new RestToXContentListener<>(channel)); } @Override diff --git a/server/src/main/java/org/elasticsearch/rest/action/ingest/RestDeletePipelineAction.java b/server/src/main/java/org/elasticsearch/rest/action/ingest/RestDeletePipelineAction.java index 5ad478deea6..8fca614f63c 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/ingest/RestDeletePipelineAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/ingest/RestDeletePipelineAction.java @@ -25,7 +25,7 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -45,6 +45,6 @@ public class RestDeletePipelineAction extends BaseRestHandler { DeletePipelineRequest request = new DeletePipelineRequest(restRequest.param("id")); request.masterNodeTimeout(restRequest.paramAsTime("master_timeout", request.masterNodeTimeout())); request.timeout(restRequest.paramAsTime("timeout", request.timeout())); - return channel -> client.admin().cluster().deletePipeline(request, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().cluster().deletePipeline(request, new RestToXContentListener<>(channel)); } } diff --git a/server/src/main/java/org/elasticsearch/rest/action/ingest/RestPutPipelineAction.java b/server/src/main/java/org/elasticsearch/rest/action/ingest/RestPutPipelineAction.java index aa3149fc13d..9cd66c8c9e4 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/ingest/RestPutPipelineAction.java +++ b/server/src/main/java/org/elasticsearch/rest/action/ingest/RestPutPipelineAction.java @@ -28,7 +28,7 @@ import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.rest.BaseRestHandler; import org.elasticsearch.rest.RestController; import org.elasticsearch.rest.RestRequest; -import org.elasticsearch.rest.action.AcknowledgedRestListener; +import org.elasticsearch.rest.action.RestToXContentListener; import java.io.IOException; @@ -50,7 +50,7 @@ public class RestPutPipelineAction extends BaseRestHandler { PutPipelineRequest request = new PutPipelineRequest(restRequest.param("id"), sourceTuple.v2(), sourceTuple.v1()); request.masterNodeTimeout(restRequest.paramAsTime("master_timeout", request.masterNodeTimeout())); request.timeout(restRequest.paramAsTime("timeout", request.timeout())); - return channel -> client.admin().cluster().putPipeline(request, new AcknowledgedRestListener<>(channel)); + return channel -> client.admin().cluster().putPipeline(request, new RestToXContentListener<>(channel)); } } diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponseTests.java new file mode 100644 index 00000000000..79b4fa6dedc --- /dev/null +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponseTests.java @@ -0,0 +1,202 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.action.admin.cluster.reroute; + +import org.elasticsearch.Version; +import org.elasticsearch.cluster.ClusterName; +import org.elasticsearch.cluster.ClusterState; +import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.cluster.metadata.MetaData; +import org.elasticsearch.cluster.node.DiscoveryNode; +import org.elasticsearch.cluster.node.DiscoveryNodes; +import org.elasticsearch.cluster.routing.allocation.RerouteExplanation; +import org.elasticsearch.cluster.routing.allocation.RoutingExplanations; +import org.elasticsearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand; +import org.elasticsearch.cluster.routing.allocation.decider.Decision; +import org.elasticsearch.common.collect.ImmutableOpenMap; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.transport.TransportAddress; +import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.json.JsonXContent; +import org.elasticsearch.index.IndexSettings; +import org.elasticsearch.test.ESTestCase; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class ClusterRerouteResponseTests extends ESTestCase { + + public void testToXContent() throws IOException { + DiscoveryNode node0 = new DiscoveryNode("node0", new TransportAddress(TransportAddress.META_ADDRESS, 9000), Version.CURRENT); + DiscoveryNodes nodes = new DiscoveryNodes.Builder().add(node0).masterNodeId(node0.getId()).build(); + IndexMetaData indexMetaData = IndexMetaData.builder("index").settings(Settings.builder() + .put(IndexSettings.INDEX_CHECK_ON_STARTUP.getKey(), true) + .put(IndexSettings.MAX_SCRIPT_FIELDS_SETTING.getKey(), 10) + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0) + .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build()).build(); + ImmutableOpenMap.Builder openMapBuilder = ImmutableOpenMap.builder(); + openMapBuilder.put("index", indexMetaData); + MetaData metaData = MetaData.builder().indices(openMapBuilder.build()).build(); + ClusterState clusterState = ClusterState.builder(new ClusterName("test")).nodes(nodes).metaData(metaData).build(); + + RoutingExplanations routingExplanations = new RoutingExplanations(); + routingExplanations.add(new RerouteExplanation(new AllocateReplicaAllocationCommand("index", 0, "node0"), Decision.YES)); + ClusterRerouteResponse clusterRerouteResponse = new ClusterRerouteResponse(true, clusterState, routingExplanations); + { + XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint(); + clusterRerouteResponse.toXContent(builder, ToXContent.EMPTY_PARAMS); + assertEquals("{\n" + + " \"acknowledged\" : true,\n" + + " \"state\" : {\n" + + " \"version\" : 0,\n" + + " \"state_uuid\" : \"" + clusterState.stateUUID() + "\",\n" + + " \"master_node\" : \"node0\",\n" + + " \"blocks\" : { },\n" + + " \"nodes\" : {\n" + + " \"node0\" : {\n" + + " \"name\" : \"\",\n" + + " \"ephemeral_id\" : \"" + node0.getEphemeralId() + "\",\n" + + " \"transport_address\" : \"0.0.0.0:9000\",\n" + + " \"attributes\" : { }\n" + + " }\n" + + " },\n" + + " \"metadata\" : {\n" + + " \"cluster_uuid\" : \"_na_\",\n" + + " \"templates\" : { },\n" + + " \"indices\" : {\n" + + " \"index\" : {\n" + + " \"state\" : \"open\",\n" + + " \"settings\" : {\n" + + " \"index\" : {\n" + + " \"shard\" : {\n" + + " \"check_on_startup\" : \"true\"\n" + + " },\n" + + " \"number_of_shards\" : \"1\",\n" + + " \"number_of_replicas\" : \"0\",\n" + + " \"version\" : {\n" + + " \"created\" : \"" + Version.CURRENT.id + "\"\n" + + " },\n" + + " \"max_script_fields\" : \"10\"\n" + + " }\n" + + " },\n" + + " \"mappings\" : { },\n" + + " \"aliases\" : [ ],\n" + + " \"primary_terms\" : {\n" + + " \"0\" : 0\n" + + " },\n" + + " \"in_sync_allocations\" : {\n" + + " \"0\" : [ ]\n" + + " }\n" + + " }\n" + + " },\n" + + " \"index-graveyard\" : {\n" + + " \"tombstones\" : [ ]\n" + + " }\n" + + " },\n" + + " \"routing_table\" : {\n" + + " \"indices\" : { }\n" + + " },\n" + + " \"routing_nodes\" : {\n" + + " \"unassigned\" : [ ],\n" + + " \"nodes\" : {\n" + + " \"node0\" : [ ]\n" + + " }\n" + + " }\n" + + " }\n" + + "}", builder.string()); + + } + { + XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint(); + Map params = new HashMap<>(); + params.put("explain", "true"); + params.put("metric", "version,master_node"); + clusterRerouteResponse.toXContent(builder, new ToXContent.MapParams(params)); + assertEquals("{\n" + + " \"acknowledged\" : true,\n" + + " \"state\" : {\n" + + " \"version\" : 0,\n" + + " \"state_uuid\" : \"" + clusterState.stateUUID() + "\",\n" + + " \"master_node\" : \"node0\"\n" + + " },\n" + + " \"explanations\" : [\n" + + " {\n" + + " \"command\" : \"allocate_replica\",\n" + + " \"parameters\" : {\n" + + " \"index\" : \"index\",\n" + + " \"shard\" : 0,\n" + + " \"node\" : \"node0\"\n" + + " },\n" + + " \"decisions\" : [\n" + + " {\n" + + " \"decider\" : null,\n" + + " \"decision\" : \"YES\",\n" + + " \"explanation\" : \"none\"\n" + + " }\n" + + " ]\n" + + " }\n" + + " ]\n" + + "}", builder.string()); + } + { + XContentBuilder builder = JsonXContent.contentBuilder().prettyPrint(); + Map params = new HashMap<>(); + params.put("metric", "metadata"); + params.put("settings_filter", "index.number*,index.version.created"); + clusterRerouteResponse.toXContent(builder, new ToXContent.MapParams(params)); + assertEquals("{\n" + + " \"acknowledged\" : true,\n" + + " \"state\" : {\n" + + " \"metadata\" : {\n" + + " \"cluster_uuid\" : \"_na_\",\n" + + " \"templates\" : { },\n" + + " \"indices\" : {\n" + + " \"index\" : {\n" + + " \"state\" : \"open\",\n" + + " \"settings\" : {\n" + + " \"index\" : {\n" + + " \"max_script_fields\" : \"10\",\n" + + " \"shard\" : {\n" + + " \"check_on_startup\" : \"true\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"mappings\" : { },\n" + + " \"aliases\" : [ ],\n" + + " \"primary_terms\" : {\n" + + " \"0\" : 0\n" + + " },\n" + + " \"in_sync_allocations\" : {\n" + + " \"0\" : [ ]\n" + + " }\n" + + " }\n" + + " },\n" + + " \"index-graveyard\" : {\n" + + " \"tombstones\" : [ ]\n" + + " }\n" + + " }\n" + + " }\n" + + "}", builder.string()); + } + } +}