Make StatusToXContent extend ToXContentObject and rename it to StatusToXContentObject

This also allows to make RestToXContentListener require ToXContentObject rather than ToXContent
This commit is contained in:
javanna 2017-01-05 16:39:04 +01:00 committed by Luca Cavanna
parent 4e49860f68
commit ded694fc83
12 changed files with 25 additions and 43 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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<PipelineConfiguration> pipelines;

View File

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

View File

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

View File

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

View File

@ -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<Response extends StatusToXContent> extends RestToXContentListener<Response> {
public class RestStatusToXContentListener<Response extends StatusToXContentObject> extends RestToXContentListener<Response> {
private final Function<Response, String> extractLocation;
/**
@ -53,7 +53,8 @@ public class RestStatusToXContentListener<Response extends StatusToXContent> 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);

View File

@ -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<Response extends ToXContent> extends RestResponseListener<Response> {
public class RestToXContentListener<Response extends ToXContentObject> extends RestResponseListener<Response> {
public RestToXContentListener(RestChannel channel) {
super(channel);
@ -43,19 +42,9 @@ public class RestToXContentListener<Response extends ToXContent> 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);
}

View File

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

View File

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