Remove AcknowledgedRestListener in favour of RestToXContentListener (#28724)

This commit makes AcknowledgedResponse implement ToXContentObject, so that the response knows how to print its own content out to XContent, which allows us to remove AcknowledgedRestListener.
This commit is contained in:
Luca Cavanna 2018-02-22 09:13:30 +01:00 committed by GitHub
parent 014e90d903
commit 1df711c5b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 288 additions and 205 deletions

View File

@ -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<GrokProcessorGetAction.Reques
}
}
public static class Response extends AcknowledgedResponse implements ToXContentObject {
public static class Response extends ActionResponse implements ToXContentObject {
private Map<String, String> grokPatterns;
public Response(Map<String, String> grokPatterns) {

View File

@ -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<String, String> GROK_PATTERNS = Grok.getBuiltinPatterns();

View File

@ -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);
}
}
}

View File

@ -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) {

View File

@ -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<IndicesAliasesResponse, Void> 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);
}

View File

@ -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<CloseIndexResponse, Void> 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);
}

View File

@ -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) {

View File

@ -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<DeleteIndexResponse, Void> 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);
}

View File

@ -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<PutMappingResponse, Void> 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);
}

View File

@ -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<OpenIndexResponse, Void> 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);
}

View File

@ -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<String, Boolean> entry : conditionStatus.entrySet()) {
builder.field(entry.getKey(), entry.getValue());
}
builder.endObject();
builder.endObject();
return builder;
}
public static RolloverResponse fromXContent(XContentParser parser) {

View File

@ -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

View File

@ -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());
}

View File

@ -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<T extends AcknowledgedResponse> extends RestBuilderListener<T> {
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";
}
}

View File

@ -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<ClusterRerouteResponse>(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<String> RESPONSE_PARAMS;

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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<String, IndexMetaData> 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<String, String> 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<String, String> 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());
}
}
}