diff --git a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/RestNoopBulkAction.java b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/RestNoopBulkAction.java index 06082ed7d29..c3f3205fc2c 100644 --- a/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/RestNoopBulkAction.java +++ b/client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/RestNoopBulkAction.java @@ -103,9 +103,7 @@ public class RestNoopBulkAction extends BaseRestHandler { builder.field(Fields.ERRORS, false); builder.startArray(Fields.ITEMS); for (int idx = 0; idx < bulkRequest.numberOfActions(); idx++) { - builder.startObject(); ITEM_RESPONSE.toXContent(builder, request); - builder.endObject(); } builder.endArray(); builder.endObject(); diff --git a/core/src/main/java/org/elasticsearch/action/DocWriteResponse.java b/core/src/main/java/org/elasticsearch/action/DocWriteResponse.java index 2f0dd17418c..ee55fd9c2ad 100644 --- a/core/src/main/java/org/elasticsearch/action/DocWriteResponse.java +++ b/core/src/main/java/org/elasticsearch/action/DocWriteResponse.java @@ -27,8 +27,7 @@ import org.elasticsearch.common.Nullable; 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.StatusToXContent; -import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.seqno.SequenceNumbersService; @@ -43,7 +42,7 @@ import java.util.Locale; /** * A base class for the response of a write operation that involves a single doc */ -public abstract class DocWriteResponse extends ReplicationResponse implements WriteResponse, StatusToXContent, ToXContentObject { +public abstract class DocWriteResponse extends ReplicationResponse implements WriteResponse, StatusToXContentObject { /** * An enum that represents the the results of CRUD operations, primarily used to communicate the type of diff --git a/core/src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponse.java b/core/src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponse.java index 8a928a4fa2b..a9a2c36970e 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponse.java +++ b/core/src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponse.java @@ -28,8 +28,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.xcontent.StatusToXContent; -import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.RestStatus; @@ -37,7 +36,7 @@ import java.io.IOException; import java.util.Locale; import java.util.Map; -public class ClusterHealthResponse extends ActionResponse implements StatusToXContent, ToXContentObject { +public class ClusterHealthResponse extends ActionResponse implements StatusToXContentObject { private String clusterName; private int numberOfPendingTasks = 0; private int numberOfInFlightFetch = 0; diff --git a/core/src/main/java/org/elasticsearch/action/bulk/BulkItemResponse.java b/core/src/main/java/org/elasticsearch/action/bulk/BulkItemResponse.java index 2c4775665ea..93f211194a5 100644 --- a/core/src/main/java/org/elasticsearch/action/bulk/BulkItemResponse.java +++ b/core/src/main/java/org/elasticsearch/action/bulk/BulkItemResponse.java @@ -32,7 +32,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.xcontent.StatusToXContent; +import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.RestStatus; @@ -43,7 +43,7 @@ import java.io.IOException; * Represents a single item response for an action executed as part of the bulk API. Holds the index/type/id * of the relevant action, and if it has failed or not (with the failure message incase it failed). */ -public class BulkItemResponse implements Streamable, StatusToXContent { +public class BulkItemResponse implements Streamable, StatusToXContentObject { @Override public RestStatus status() { @@ -52,6 +52,7 @@ public class BulkItemResponse implements Streamable, StatusToXContent { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.startObject(); builder.startObject(opType.getLowercase()); if (failure == null) { response.innerToXContent(builder, params); @@ -66,6 +67,7 @@ public class BulkItemResponse implements Streamable, StatusToXContent { builder.endObject(); } builder.endObject(); + builder.endObject(); return builder; } diff --git a/core/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java b/core/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java index 9c4a52aef6c..30843bdff9b 100644 --- a/core/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java +++ b/core/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java @@ -22,8 +22,7 @@ package org.elasticsearch.action.ingest; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContent; -import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.ingest.PipelineConfiguration; import org.elasticsearch.rest.RestStatus; @@ -32,7 +31,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -public class GetPipelineResponse extends ActionResponse implements StatusToXContent, ToXContentObject { +public class GetPipelineResponse extends ActionResponse implements StatusToXContentObject { private List pipelines; diff --git a/core/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java b/core/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java index b218faa098b..d1ec790d01a 100644 --- a/core/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java +++ b/core/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java @@ -22,8 +22,7 @@ package org.elasticsearch.action.search; import org.elasticsearch.action.ActionResponse; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.StatusToXContent; -import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.RestStatus; @@ -32,7 +31,7 @@ import java.io.IOException; import static org.elasticsearch.rest.RestStatus.NOT_FOUND; import static org.elasticsearch.rest.RestStatus.OK; -public class ClearScrollResponse extends ActionResponse implements StatusToXContent, ToXContentObject { +public class ClearScrollResponse extends ActionResponse implements StatusToXContentObject { private boolean succeeded; private int numFreed; diff --git a/core/src/main/java/org/elasticsearch/action/search/SearchResponse.java b/core/src/main/java/org/elasticsearch/action/search/SearchResponse.java index d08ec195dbe..4b8ba5e64b6 100644 --- a/core/src/main/java/org/elasticsearch/action/search/SearchResponse.java +++ b/core/src/main/java/org/elasticsearch/action/search/SearchResponse.java @@ -25,8 +25,7 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.xcontent.StatusToXContent; -import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.RestActions; @@ -45,7 +44,7 @@ import static org.elasticsearch.search.internal.InternalSearchResponse.readInter /** * A response of a search request. */ -public class SearchResponse extends ActionResponse implements StatusToXContent, ToXContentObject { +public class SearchResponse extends ActionResponse implements StatusToXContentObject { private InternalSearchResponse internalResponse; diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/StatusToXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/StatusToXContentObject.java similarity index 94% rename from core/src/main/java/org/elasticsearch/common/xcontent/StatusToXContent.java rename to core/src/main/java/org/elasticsearch/common/xcontent/StatusToXContentObject.java index f22aa39613f..ba6ccdfffad 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/StatusToXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/StatusToXContentObject.java @@ -24,7 +24,7 @@ import org.elasticsearch.rest.RestStatus; * Objects that can both render themselves in as json/yaml/etc and can provide a {@link RestStatus} for their response. Usually should be * implemented by top level responses sent back to users from REST endpoints. */ -public interface StatusToXContent extends ToXContent { +public interface StatusToXContentObject extends ToXContentObject { /** * Returns the REST status to make sure it is returned correctly diff --git a/core/src/main/java/org/elasticsearch/rest/action/RestStatusToXContentListener.java b/core/src/main/java/org/elasticsearch/rest/action/RestStatusToXContentListener.java index 8c8531561f2..f17399743b4 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/RestStatusToXContentListener.java +++ b/core/src/main/java/org/elasticsearch/rest/action/RestStatusToXContentListener.java @@ -18,7 +18,7 @@ */ package org.elasticsearch.rest.action; -import org.elasticsearch.common.xcontent.StatusToXContent; +import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; @@ -30,7 +30,7 @@ import java.util.function.Function; /** * Content listener that extracts that {@link RestStatus} from the response. */ -public class RestStatusToXContentListener extends RestToXContentListener { +public class RestStatusToXContentListener extends RestToXContentListener { private final Function extractLocation; /** @@ -53,7 +53,8 @@ public class RestStatusToXContentListener ext @Override public RestResponse buildResponse(Response response, XContentBuilder builder) throws Exception { - toXContent(response, builder); + assert response.isFragment() == false; //would be nice if we could make default methods final + response.toXContent(builder, channel.request()); RestResponse restResponse = new BytesRestResponse(response.status(), builder); if (RestStatus.CREATED == restResponse.status()) { String location = extractLocation.apply(response); diff --git a/core/src/main/java/org/elasticsearch/rest/action/RestToXContentListener.java b/core/src/main/java/org/elasticsearch/rest/action/RestToXContentListener.java index e2ed722fd36..06fdd021a32 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/RestToXContentListener.java +++ b/core/src/main/java/org/elasticsearch/rest/action/RestToXContentListener.java @@ -20,19 +20,18 @@ package org.elasticsearch.rest.action; import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.BytesRestResponse; import org.elasticsearch.rest.RestChannel; import org.elasticsearch.rest.RestResponse; import org.elasticsearch.rest.RestStatus; -import java.io.IOException; - /** * A REST based action listener that assumes the response is of type {@link ToXContent} and automatically * builds an XContent based response (wrapping the toXContent in startObject/endObject). */ -public class RestToXContentListener extends RestResponseListener { +public class RestToXContentListener extends RestResponseListener { public RestToXContentListener(RestChannel channel) { super(channel); @@ -43,19 +42,9 @@ public class RestToXContentListener extends RestRes return buildResponse(response, channel.newBuilder()); } - protected final void toXContent(Response response, XContentBuilder builder) throws IOException { - final boolean needsNewObject = response.isFragment(); - if (needsNewObject) { - builder.startObject(); - } - response.toXContent(builder, channel.request()); - if (needsNewObject) { - builder.endObject(); - } - } - public RestResponse buildResponse(Response response, XContentBuilder builder) throws Exception { - toXContent(response, builder); + assert response.isFragment() == false; //would be nice if we could make default methods final + response.toXContent(builder, channel.request()); return new BytesRestResponse(getStatus(response), builder); } diff --git a/core/src/main/java/org/elasticsearch/rest/action/document/RestBulkAction.java b/core/src/main/java/org/elasticsearch/rest/action/document/RestBulkAction.java index ff603aceefb..46731a52960 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/document/RestBulkAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/document/RestBulkAction.java @@ -108,9 +108,7 @@ public class RestBulkAction extends BaseRestHandler { builder.field(Fields.ERRORS, response.hasFailures()); builder.startArray(Fields.ITEMS); for (BulkItemResponse itemResponse : response) { - builder.startObject(); itemResponse.toXContent(builder, request); - builder.endObject(); } builder.endArray(); diff --git a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java index 1bd53a42962..6158e80c241 100644 --- a/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java +++ b/modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/SearchTemplateResponse.java @@ -24,14 +24,13 @@ import org.elasticsearch.action.search.SearchResponse; 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.StatusToXContent; -import org.elasticsearch.common.xcontent.ToXContentObject; +import org.elasticsearch.common.xcontent.StatusToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.rest.RestStatus; import java.io.IOException; -public class SearchTemplateResponse extends ActionResponse implements StatusToXContent, ToXContentObject { +public class SearchTemplateResponse extends ActionResponse implements StatusToXContentObject { /** Contains the source of the rendered template **/ private BytesReference source;