Clarify some ToXContent implementations behaviour (#41000)

This change adds either ToXContentObject or ToXContentFragment to classes
directly implementing ToXContent currently. This helps in reasoning about
whether those implementations output full xcontent object or just fragments.

Relates to #16347
This commit is contained in:
Christoph Büscher 2019-04-15 09:41:34 +02:00
parent 5ef247dc91
commit 2980a6c70f
31 changed files with 123 additions and 107 deletions

View File

@ -133,9 +133,7 @@ public class GraphExploreResponse implements ToXContentObject {
builder.startArray(FAILURES.getPreferredName());
if (shardFailures != null) {
for (ShardOperationFailedException shardFailure : shardFailures) {
builder.startObject();
shardFailure.toXContent(builder, params);
builder.endObject();
}
}
builder.endArray();

View File

@ -31,7 +31,7 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
@ -56,7 +56,7 @@ import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
/**
* A request to create an index template.
*/
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest, ToXContent {
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest, ToXContentFragment {
private String name;
@ -191,7 +191,7 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
public Settings settings() {
return this.settings;
}
/**
* Adds mapping that will be added when the index gets created.
*
@ -201,7 +201,7 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
public PutIndexTemplateRequest mapping(String source, XContentType xContentType) {
internalMapping(XContentHelper.convertToMap(new BytesArray(source), true, xContentType).v2());
return this;
}
}
/**
* The cause for this index template creation.
@ -221,11 +221,11 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
* @param source The mapping source
*/
public PutIndexTemplateRequest mapping(XContentBuilder source) {
internalMapping(XContentHelper.convertToMap(BytesReference.bytes(source),
internalMapping(XContentHelper.convertToMap(BytesReference.bytes(source),
true, source.contentType()).v2());
return this;
}
return this;
}
/**
* Adds mapping that will be added when the index gets created.
*
@ -235,8 +235,8 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
public PutIndexTemplateRequest mapping(BytesReference source, XContentType xContentType) {
internalMapping(XContentHelper.convertToMap(source, true, xContentType).v2());
return this;
}
}
/**
* Adds mapping that will be added when the index gets created.
*
@ -244,7 +244,7 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
*/
public PutIndexTemplateRequest mapping(Map<String, Object> source) {
return internalMapping(source);
}
}
private PutIndexTemplateRequest internalMapping(Map<String, Object> source) {
try {
@ -257,12 +257,12 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
return this;
} catch (IOException e) {
throw new UncheckedIOException("failed to convert source to json", e);
}
}
} catch (IOException e) {
throw new ElasticsearchGenerationException("Failed to generate [" + source + "]", e);
}
}
}
public BytesReference mappings() {
return this.mappings;
}
@ -349,8 +349,8 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
*/
public PutIndexTemplateRequest source(BytesReference source, XContentType xContentType) {
return source(XContentHelper.convertToMap(source, true, xContentType).v2());
}
}
public Set<Alias> aliases() {
return this.aliases;
@ -441,7 +441,7 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
builder.copyCurrentStructure(parser);
}
}
builder.startObject("aliases");
for (Alias alias : aliases) {
alias.toXContent(builder, params);

View File

@ -25,7 +25,7 @@ import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -34,7 +34,7 @@ import java.util.List;
import java.util.Objects;
import java.util.Optional;
public class FindFileStructureRequest implements Validatable, ToXContent {
public class FindFileStructureRequest implements Validatable, ToXContentFragment {
public static final ParseField LINES_TO_SAMPLE = new ParseField("lines_to_sample");
public static final ParseField TIMEOUT = new ParseField("timeout");

View File

@ -20,6 +20,7 @@
package org.elasticsearch.client;
import com.fasterxml.jackson.core.JsonParseException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
@ -61,6 +62,7 @@ import org.elasticsearch.common.collect.Tuple;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.cbor.CborXContent;
@ -176,7 +178,7 @@ public class RestHighLevelClientTests extends ESTestCase {
MainResponse testInfo = new MainResponse("nodeName", new MainResponse.Version("number", "buildFlavor", "buildType", "buildHash",
"buildDate", true, "luceneVersion", "minimumWireCompatibilityVersion", "minimumIndexCompatibilityVersion"),
"clusterName", "clusterUuid", "You Know, for Search");
mockResponse((builder, params) -> {
mockResponse((ToXContentFragment) (builder, params) -> {
// taken from the server side MainResponse
builder.field("name", testInfo.getNodeName());
builder.field("cluster_name", testInfo.getClusterName());
@ -762,12 +764,12 @@ public class RestHighLevelClientTests extends ESTestCase {
Collectors.mapping(Tuple::v2, Collectors.toSet())));
// TODO remove in 8.0 - we will undeprecate indices.get_template because the current getIndexTemplate
// impl will replace the existing getTemplate method.
// impl will replace the existing getTemplate method.
// The above general-purpose code ignores all deprecated methods which in this case leaves `getTemplate`
// looking like it doesn't have a valid implementatation when it does.
// looking like it doesn't have a valid implementatation when it does.
apiUnsupported.remove("indices.get_template");
for (Map.Entry<String, Set<Method>> entry : methods.entrySet()) {
String apiName = entry.getKey();
@ -830,7 +832,7 @@ public class RestHighLevelClientTests extends ESTestCase {
assertThat("the return type for method [" + method + "] is incorrect",
method.getReturnType().getSimpleName(), equalTo("boolean"));
} else {
// It's acceptable for 404s to be represented as empty Optionals
// It's acceptable for 404s to be represented as empty Optionals
if (!method.getReturnType().isAssignableFrom(Optional.class)) {
assertThat("the return type for method [" + method + "] is incorrect",
method.getReturnType().getSimpleName(), endsWith("Response"));

View File

@ -55,7 +55,6 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
@ -107,7 +106,7 @@ public class PainlessExecuteAction extends Action<PainlessExecuteAction.Response
return new Response();
}
public static class Request extends SingleShardRequest<Request> implements ToXContent {
public static class Request extends SingleShardRequest<Request> implements ToXContentObject {
private static final ParseField SCRIPT_FIELD = new ParseField("script");
private static final ParseField CONTEXT_FIELD = new ParseField("context");

View File

@ -21,7 +21,7 @@ package org.elasticsearch.action;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.rest.RestStatus;
import java.util.Objects;
@ -30,7 +30,7 @@ import java.util.Objects;
* An exception indicating that a failure occurred performing an operation on the shard.
*
*/
public abstract class ShardOperationFailedException implements Streamable, ToXContent {
public abstract class ShardOperationFailedException implements Streamable, ToXContentObject {
protected String index;
protected int shardId = -1;

View File

@ -25,7 +25,7 @@ import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.common.bytes.BytesReference;
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.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
@ -36,7 +36,7 @@ import java.util.Objects;
import static org.elasticsearch.action.ValidateActions.addValidationError;
public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptRequest> implements ToXContent {
public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptRequest> implements ToXContentFragment {
private String id;
private String context;

View File

@ -21,6 +21,7 @@ package org.elasticsearch.action.admin.indices.shards;
import com.carrotsearch.hppc.cursors.IntObjectCursor;
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionResponse;
@ -267,8 +268,10 @@ public class IndicesShardStoresResponse extends ActionResponse implements ToXCon
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("node", nodeId());
super.toXContent(builder, params);
super.innerToXContent(builder, params);
builder.endObject();
return builder;
}
}
@ -361,9 +364,7 @@ public class IndicesShardStoresResponse extends ActionResponse implements ToXCon
if (failures.size() > 0) {
builder.startArray(Fields.FAILURES);
for (Failure failure : failures) {
builder.startObject();
failure.toXContent(builder, params);
builder.endObject();
}
builder.endArray();
}

View File

@ -39,7 +39,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.DeprecationHandler;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
@ -60,14 +60,14 @@ import java.util.Set;
import java.util.stream.Collectors;
import static org.elasticsearch.action.ValidateActions.addValidationError;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
import static org.elasticsearch.common.settings.Settings.readSettingsFromStream;
import static org.elasticsearch.common.settings.Settings.writeSettingsToStream;
import static org.elasticsearch.common.settings.Settings.Builder.EMPTY_SETTINGS;
/**
* A request to create an index template.
*/
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest, ToXContent {
public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateRequest> implements IndicesRequest, ToXContentObject {
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(PutIndexTemplateRequest.class));
@ -519,32 +519,35 @@ public class PutIndexTemplateRequest extends MasterNodeRequest<PutIndexTemplateR
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("index_patterns", indexPatterns);
builder.field("order", order);
if (version != null) {
builder.field("version", version);
}
builder.startObject("settings");
settings.toXContent(builder, params);
builder.endObject();
builder.startObject("mappings");
for (Map.Entry<String, String> entry : mappings.entrySet()) {
builder.field(entry.getKey());
try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, entry.getValue())) {
builder.copyCurrentStructure(parser);
builder.startObject();
{
builder.field("index_patterns", indexPatterns);
builder.field("order", order);
if (version != null) {
builder.field("version", version);
}
builder.startObject("settings");
settings.toXContent(builder, params);
builder.endObject();
builder.startObject("mappings");
for (Map.Entry<String, String> entry : mappings.entrySet()) {
builder.field(entry.getKey());
try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY,
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, entry.getValue())) {
builder.copyCurrentStructure(parser);
}
}
builder.endObject();
builder.startObject("aliases");
for (Alias alias : aliases) {
alias.toXContent(builder, params);
}
builder.endObject();
}
builder.endObject();
builder.startObject("aliases");
for (Alias alias : aliases) {
alias.toXContent(builder, params);
}
builder.endObject();
return builder;
}
}

View File

@ -140,9 +140,7 @@ public class SearchPhaseExecutionException extends ElasticsearchException {
builder.startArray();
ShardOperationFailedException[] failures = ExceptionsHelper.groupBy(shardFailures);
for (ShardOperationFailedException failure : failures) {
builder.startObject();
failure.toXContent(builder, params);
builder.endObject();
}
builder.endArray();
}

View File

@ -30,7 +30,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.StatusToXContentObject;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParser.Token;
@ -408,7 +408,7 @@ public class SearchResponse extends ActionResponse implements StatusToXContentOb
* Holds info about the clusters that the search was executed on: how many in total, how many of them were successful
* and how many of them were skipped.
*/
public static class Clusters implements ToXContent, Writeable {
public static class Clusters implements ToXContentFragment, Writeable {
public static final Clusters EMPTY = new Clusters(0, 0, 0);

View File

@ -118,14 +118,18 @@ public class ShardSearchFailure extends ShardOperationFailedException {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field(SHARD_FIELD, shardId());
builder.field(INDEX_FIELD, index());
if (shardTarget != null) {
builder.field(NODE_FIELD, shardTarget.getNodeId());
}
builder.field(REASON_FIELD);
builder.startObject();
ElasticsearchException.generateThrowableXContent(builder, params, cause);
{
builder.field(SHARD_FIELD, shardId());
builder.field(INDEX_FIELD, index());
if (shardTarget != null) {
builder.field(NODE_FIELD, shardTarget.getNodeId());
}
builder.field(REASON_FIELD);
builder.startObject();
ElasticsearchException.generateThrowableXContent(builder, params, cause);
builder.endObject();
}
builder.endObject();
return builder;
}

View File

@ -91,6 +91,13 @@ public class DefaultShardOperationFailedException extends ShardOperationFailedEx
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
innerToXContent(builder, params);
builder.endObject();
return builder;
}
protected XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("shard", shardId());
builder.field("index", index());
builder.field("status", status.name());

View File

@ -24,6 +24,8 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -150,7 +152,7 @@ public abstract class Decision implements ToXContent, Writeable {
/**
* Simple class representing a single decision
*/
public static class Single extends Decision {
public static class Single extends Decision implements ToXContentObject {
private Type type;
private String label;
private String explanation;
@ -269,7 +271,7 @@ public abstract class Decision implements ToXContent, Writeable {
/**
* Simple class representing a list of decisions
*/
public static class Multi extends Decision {
public static class Multi extends Decision implements ToXContentFragment {
private final List<Decision> decisions = new ArrayList<>();

View File

@ -31,8 +31,8 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.analysis.NamedAnalyzer;
@ -457,7 +457,7 @@ public abstract class IntervalsSourceProvider implements NamedWriteable, ToXCont
}
}
public static class IntervalFilter implements ToXContent, Writeable {
public static class IntervalFilter implements ToXContentObject, Writeable {
public static final String NAME = "filter";

View File

@ -24,7 +24,7 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
@ -37,7 +37,7 @@ import java.util.Objects;
* otherwise merge away operations that have been soft deleted). Each retention lease contains a unique identifier, the retaining sequence
* number, the timestamp of when the lease was created or renewed, and the source of the retention lease (e.g., "ccr").
*/
public final class RetentionLease implements ToXContent, Writeable {
public final class RetentionLease implements ToXContentObject, Writeable {
private final String id;

View File

@ -25,6 +25,7 @@ import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.gateway.MetaDataStateFormat;
@ -42,7 +43,7 @@ import java.util.stream.Collectors;
* Represents a versioned collection of retention leases. We version the collection of retention leases to ensure that sync requests that
* arrive out of order on the replica, using the version to ensure that older sync requests are rejected.
*/
public class RetentionLeases implements ToXContent, Writeable {
public class RetentionLeases implements ToXContentFragment, Writeable {
private final long primaryTerm;

View File

@ -91,9 +91,7 @@ public class RestActions {
if (shardFailures != null && shardFailures.length > 0) {
builder.startArray(FAILURES_FIELD.getPreferredName());
for (ShardOperationFailedException shardFailure : ExceptionsHelper.groupBy(shardFailures)) {
builder.startObject();
shardFailure.toXContent(builder, params);
builder.endObject();
}
builder.endArray();
}

View File

@ -514,9 +514,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
if (verbose || !shardFailures.isEmpty()) {
builder.startArray(FAILURES);
for (SnapshotShardFailure shardFailure : shardFailures) {
builder.startObject();
shardFailure.toXContent(builder, params);
builder.endObject();
}
builder.endArray();
}
@ -555,9 +553,7 @@ public final class SnapshotInfo implements Comparable<SnapshotInfo>, ToXContent,
builder.field(SUCCESSFUL_SHARDS, successfulShards);
builder.startArray(FAILURES);
for (SnapshotShardFailure shardFailure : shardFailures) {
builder.startObject();
shardFailure.toXContent(builder, params);
builder.endObject();
}
builder.endArray();
builder.endObject();

View File

@ -187,6 +187,7 @@ public class SnapshotShardFailure extends ShardOperationFailedException {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("index", shardId.getIndexName());
builder.field("index_uuid", shardId.getIndexName());
builder.field("shard_id", shardId.id());
@ -195,6 +196,7 @@ public class SnapshotShardFailure extends ShardOperationFailedException {
builder.field("node_id", nodeId);
}
builder.field("status", status.name());
builder.endObject();
return builder;
}

View File

@ -22,6 +22,7 @@ package org.elasticsearch.common.xcontent;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.Constants;
import org.elasticsearch.cluster.metadata.IndexMetaData;
@ -752,7 +753,7 @@ public abstract class BaseXContentTestCase extends ESTestCase {
.field("xcontent", xcontent0)
.endObject());
ToXContent xcontent1 = (builder, params) -> {
ToXContentObject xcontent1 = (builder, params) -> {
builder.startObject();
builder.field("field", "value");
builder.startObject("foo");
@ -762,7 +763,7 @@ public abstract class BaseXContentTestCase extends ESTestCase {
return builder;
};
ToXContent xcontent2 = (builder, params) -> {
ToXContentObject xcontent2 = (builder, params) -> {
builder.startObject();
builder.field("root", xcontent0);
builder.array("childs", xcontent0, xcontent1);

View File

@ -168,9 +168,7 @@ public class GraphExploreResponse extends ActionResponse implements ToXContentOb
builder.startArray(FAILURES.getPreferredName());
if (shardFailures != null) {
for (ShardOperationFailedException shardFailure : shardFailures) {
builder.startObject();
shardFailure.toXContent(builder, params);
builder.endObject();
}
}
builder.endArray();

View File

@ -17,7 +17,8 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContent.Params;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.rollup.RollupField;
@ -43,7 +44,7 @@ public class GetRollupCapsAction extends Action<GetRollupCapsAction.Response> {
return new Response();
}
public static class Request extends ActionRequest implements ToXContent {
public static class Request extends ActionRequest implements ToXContentFragment {
private String indexPattern;
public Request(String indexPattern) {

View File

@ -19,7 +19,8 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContent.Params;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.rollup.RollupField;
@ -47,7 +48,7 @@ public class GetRollupIndexCapsAction extends Action<GetRollupIndexCapsAction.Re
return new Response();
}
public static class Request extends ActionRequest implements IndicesRequest.Replaceable, ToXContent {
public static class Request extends ActionRequest implements IndicesRequest.Replaceable, ToXContentFragment {
private String[] indices;
private IndicesOptions options;

View File

@ -21,7 +21,6 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.tasks.Task;
@ -58,7 +57,7 @@ public class GetRollupJobsAction extends Action<GetRollupJobsAction.Response> {
return Response::new;
}
public static class Request extends BaseTasksRequest<Request> implements ToXContent {
public static class Request extends BaseTasksRequest<Request> implements ToXContentObject {
private String id;
public Request(String id) {
@ -107,7 +106,9 @@ public class GetRollupJobsAction extends Action<GetRollupJobsAction.Response> {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(RollupField.ID.getPreferredName(), id);
builder.endObject();
return builder;
}

View File

@ -15,7 +15,6 @@ import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
@ -44,7 +43,7 @@ public class StartRollupJobAction extends Action<StartRollupJobAction.Response>
return Response::new;
}
public static class Request extends BaseTasksRequest<Request> implements ToXContent {
public static class Request extends BaseTasksRequest<Request> implements ToXContentObject {
private String id;
public Request(String id) {
@ -75,7 +74,9 @@ public class StartRollupJobAction extends Action<StartRollupJobAction.Response>
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(RollupField.ID.getPreferredName(), id);
builder.endObject();
return builder;
}

View File

@ -18,7 +18,6 @@ import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
@ -51,7 +50,7 @@ public class StopRollupJobAction extends Action<StopRollupJobAction.Response> {
return Response::new;
}
public static class Request extends BaseTasksRequest<Request> implements ToXContent {
public static class Request extends BaseTasksRequest<Request> implements ToXContentObject {
private String id;
private boolean waitForCompletion = false;
private TimeValue timeout = null;
@ -106,11 +105,13 @@ public class StopRollupJobAction extends Action<StopRollupJobAction.Response> {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(RollupField.ID.getPreferredName(), id);
builder.field(WAIT_FOR_COMPLETION.getPreferredName(), waitForCompletion);
if (timeout != null) {
builder.field(TIMEOUT.getPreferredName(), timeout);
}
builder.endObject();
return builder;
}

View File

@ -9,6 +9,7 @@ import org.elasticsearch.action.ActionResponse;
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 org.elasticsearch.xpack.core.security.authc.support.TokensInvalidationResult;
@ -18,7 +19,7 @@ import java.util.Objects;
/**
* Response for a invalidation of one or multiple tokens.
*/
public final class InvalidateTokenResponse extends ActionResponse implements ToXContent {
public final class InvalidateTokenResponse extends ActionResponse implements ToXContentObject {
private TokensInvalidationResult result;

View File

@ -16,7 +16,7 @@ import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
@ -44,7 +44,7 @@ import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optiona
/**
* Representation of a Mustache template for expressing one or more roles names in a {@link ExpressionRoleMapping}.
*/
public class TemplateRoleName implements ToXContent, Writeable {
public class TemplateRoleName implements ToXContentObject, Writeable {
private static final ConstructingObjectParser<TemplateRoleName, Void> PARSER = new ConstructingObjectParser<>(
"role-mapping-template", false, arr -> new TemplateRoleName((BytesReference) arr[0], (Format) arr[1]));

View File

@ -10,7 +10,7 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.seqno.SequenceNumbers;
import org.elasticsearch.xpack.core.watcher.support.xcontent.XContentSource;
@ -19,7 +19,7 @@ import org.elasticsearch.xpack.core.watcher.watch.WatchStatus;
import java.io.IOException;
import java.util.Objects;
public class GetWatchResponse extends ActionResponse implements ToXContent {
public class GetWatchResponse extends ActionResponse implements ToXContentObject {
private String id;
private WatchStatus status;
@ -122,6 +122,7 @@ public class GetWatchResponse extends ActionResponse implements ToXContent {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("found", found);
builder.field("_id", id);
if (found) {
@ -131,6 +132,7 @@ public class GetWatchResponse extends ActionResponse implements ToXContent {
builder.field("status", status, params);
builder.field("watch", source, params);
}
builder.endObject();
return builder;
}

View File

@ -48,9 +48,7 @@ public class RestGetWatchAction extends WatcherRestHandler {
return channel -> client.getWatch(getWatchRequest, new RestBuilderListener<GetWatchResponse>(channel) {
@Override
public RestResponse buildResponse(GetWatchResponse response, XContentBuilder builder) throws Exception {
builder.startObject();
response.toXContent(builder, request);
builder.endObject();
RestStatus status = response.isFound() ? OK : NOT_FOUND;
return new BytesRestResponse(status, builder);
}