Skip to content

Commit 282ab0d

Browse files
committed
Make StatusToXContent extend ToXContentObject and rename it to StatusToXContentObject
This also allows to make RestToXContentListener require ToXContentObject rather than ToXContent
1 parent f1690e4 commit 282ab0d

File tree

12 files changed

+25
-43
lines changed

12 files changed

+25
-43
lines changed

client/client-benchmark-noop-api-plugin/src/main/java/org/elasticsearch/plugin/noop/action/bulk/RestNoopBulkAction.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ public RestResponse buildResponse(BulkRequest bulkRequest, XContentBuilder build
102102
builder.field(Fields.ERRORS, false);
103103
builder.startArray(Fields.ITEMS);
104104
for (int idx = 0; idx < bulkRequest.numberOfActions(); idx++) {
105-
builder.startObject();
106105
ITEM_RESPONSE.toXContent(builder, request);
107-
builder.endObject();
108106
}
109107
builder.endArray();
110108
builder.endObject();

core/src/main/java/org/elasticsearch/action/DocWriteResponse.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
import org.elasticsearch.common.io.stream.StreamInput;
2727
import org.elasticsearch.common.io.stream.StreamOutput;
2828
import org.elasticsearch.common.io.stream.Writeable;
29-
import org.elasticsearch.common.xcontent.StatusToXContent;
30-
import org.elasticsearch.common.xcontent.ToXContentObject;
29+
import org.elasticsearch.common.xcontent.StatusToXContentObject;
3130
import org.elasticsearch.common.xcontent.XContentBuilder;
3231
import org.elasticsearch.index.IndexSettings;
3332
import org.elasticsearch.index.shard.ShardId;
@@ -41,7 +40,7 @@
4140
/**
4241
* A base class for the response of a write operation that involves a single doc
4342
*/
44-
public abstract class DocWriteResponse extends ReplicationResponse implements WriteResponse, StatusToXContent, ToXContentObject {
43+
public abstract class DocWriteResponse extends ReplicationResponse implements WriteResponse, StatusToXContentObject {
4544

4645
/**
4746
* An enum that represents the the results of CRUD operations, primarily used to communicate the type of

core/src/main/java/org/elasticsearch/action/admin/cluster/health/ClusterHealthResponse.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@
2828
import org.elasticsearch.common.io.stream.StreamInput;
2929
import org.elasticsearch.common.io.stream.StreamOutput;
3030
import org.elasticsearch.common.unit.TimeValue;
31-
import org.elasticsearch.common.xcontent.StatusToXContent;
32-
import org.elasticsearch.common.xcontent.ToXContentObject;
31+
import org.elasticsearch.common.xcontent.StatusToXContentObject;
3332
import org.elasticsearch.common.xcontent.XContentBuilder;
3433
import org.elasticsearch.rest.RestStatus;
3534

3635
import java.io.IOException;
3736
import java.util.Locale;
3837
import java.util.Map;
3938

40-
public class ClusterHealthResponse extends ActionResponse implements StatusToXContent, ToXContentObject {
39+
public class ClusterHealthResponse extends ActionResponse implements StatusToXContentObject {
4140
private String clusterName;
4241
private int numberOfPendingTasks = 0;
4342
private int numberOfInFlightFetch = 0;

core/src/main/java/org/elasticsearch/action/bulk/BulkItemResponse.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.elasticsearch.common.io.stream.StreamOutput;
3131
import org.elasticsearch.common.io.stream.Streamable;
3232
import org.elasticsearch.common.io.stream.Writeable;
33-
import org.elasticsearch.common.xcontent.StatusToXContent;
33+
import org.elasticsearch.common.xcontent.StatusToXContentObject;
3434
import org.elasticsearch.common.xcontent.ToXContent;
3535
import org.elasticsearch.common.xcontent.XContentBuilder;
3636
import org.elasticsearch.rest.RestStatus;
@@ -41,7 +41,7 @@
4141
* Represents a single item response for an action executed as part of the bulk API. Holds the index/type/id
4242
* of the relevant action, and if it has failed or not (with the failure message incase it failed).
4343
*/
44-
public class BulkItemResponse implements Streamable, StatusToXContent {
44+
public class BulkItemResponse implements Streamable, StatusToXContentObject {
4545

4646
@Override
4747
public RestStatus status() {
@@ -50,6 +50,7 @@ public RestStatus status() {
5050

5151
@Override
5252
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
53+
builder.startObject();
5354
builder.startObject(opType);
5455
if (failure == null) {
5556
response.innerToXContent(builder, params);
@@ -64,6 +65,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
6465
builder.endObject();
6566
}
6667
builder.endObject();
68+
builder.endObject();
6769
return builder;
6870
}
6971

core/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
import org.elasticsearch.action.ActionResponse;
2323
import org.elasticsearch.common.io.stream.StreamInput;
2424
import org.elasticsearch.common.io.stream.StreamOutput;
25-
import org.elasticsearch.common.xcontent.StatusToXContent;
26-
import org.elasticsearch.common.xcontent.ToXContentObject;
25+
import org.elasticsearch.common.xcontent.StatusToXContentObject;
2726
import org.elasticsearch.common.xcontent.XContentBuilder;
2827
import org.elasticsearch.ingest.PipelineConfiguration;
2928
import org.elasticsearch.rest.RestStatus;
@@ -32,7 +31,7 @@
3231
import java.util.ArrayList;
3332
import java.util.List;
3433

35-
public class GetPipelineResponse extends ActionResponse implements StatusToXContent, ToXContentObject {
34+
public class GetPipelineResponse extends ActionResponse implements StatusToXContentObject {
3635

3736
private List<PipelineConfiguration> pipelines;
3837

core/src/main/java/org/elasticsearch/action/search/ClearScrollResponse.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
import org.elasticsearch.action.ActionResponse;
2323
import org.elasticsearch.common.io.stream.StreamInput;
2424
import org.elasticsearch.common.io.stream.StreamOutput;
25-
import org.elasticsearch.common.xcontent.StatusToXContent;
26-
import org.elasticsearch.common.xcontent.ToXContentObject;
25+
import org.elasticsearch.common.xcontent.StatusToXContentObject;
2726
import org.elasticsearch.common.xcontent.XContentBuilder;
2827
import org.elasticsearch.rest.RestStatus;
2928

@@ -32,7 +31,7 @@
3231
import static org.elasticsearch.rest.RestStatus.NOT_FOUND;
3332
import static org.elasticsearch.rest.RestStatus.OK;
3433

35-
public class ClearScrollResponse extends ActionResponse implements StatusToXContent, ToXContentObject {
34+
public class ClearScrollResponse extends ActionResponse implements StatusToXContentObject {
3635

3736
private boolean succeeded;
3837
private int numFreed;

core/src/main/java/org/elasticsearch/action/search/SearchResponse.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
import org.elasticsearch.common.io.stream.StreamInput;
2626
import org.elasticsearch.common.io.stream.StreamOutput;
2727
import org.elasticsearch.common.unit.TimeValue;
28-
import org.elasticsearch.common.xcontent.StatusToXContent;
29-
import org.elasticsearch.common.xcontent.ToXContentObject;
28+
import org.elasticsearch.common.xcontent.StatusToXContentObject;
3029
import org.elasticsearch.common.xcontent.XContentBuilder;
3130
import org.elasticsearch.rest.RestStatus;
3231
import org.elasticsearch.rest.action.RestActions;
@@ -45,7 +44,7 @@
4544
/**
4645
* A response of a search request.
4746
*/
48-
public class SearchResponse extends ActionResponse implements StatusToXContent, ToXContentObject {
47+
public class SearchResponse extends ActionResponse implements StatusToXContentObject {
4948

5049
private InternalSearchResponse internalResponse;
5150

core/src/main/java/org/elasticsearch/common/xcontent/StatusToXContent.java renamed to core/src/main/java/org/elasticsearch/common/xcontent/StatusToXContentObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* Objects that can both render themselves in as json/yaml/etc and can provide a {@link RestStatus} for their response. Usually should be
2525
* implemented by top level responses sent back to users from REST endpoints.
2626
*/
27-
public interface StatusToXContent extends ToXContent {
27+
public interface StatusToXContentObject extends ToXContentObject {
2828

2929
/**
3030
* Returns the REST status to make sure it is returned correctly

core/src/main/java/org/elasticsearch/rest/action/RestStatusToXContentListener.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package org.elasticsearch.rest.action;
2020

21-
import org.elasticsearch.common.xcontent.StatusToXContent;
21+
import org.elasticsearch.common.xcontent.StatusToXContentObject;
2222
import org.elasticsearch.common.xcontent.XContentBuilder;
2323
import org.elasticsearch.rest.BytesRestResponse;
2424
import org.elasticsearch.rest.RestChannel;
@@ -30,7 +30,7 @@
3030
/**
3131
* Content listener that extracts that {@link RestStatus} from the response.
3232
*/
33-
public class RestStatusToXContentListener<Response extends StatusToXContent> extends RestToXContentListener<Response> {
33+
public class RestStatusToXContentListener<Response extends StatusToXContentObject> extends RestToXContentListener<Response> {
3434
private final Function<Response, String> extractLocation;
3535

3636
/**
@@ -53,7 +53,8 @@ public RestStatusToXContentListener(RestChannel channel, Function<Response, Stri
5353

5454
@Override
5555
public RestResponse buildResponse(Response response, XContentBuilder builder) throws Exception {
56-
toXContent(response, builder);
56+
assert response.isFragment() == false; //would be nice if we could make default methods final
57+
response.toXContent(builder, channel.request());
5758
RestResponse restResponse = new BytesRestResponse(response.status(), builder);
5859
if (RestStatus.CREATED == restResponse.status()) {
5960
String location = extractLocation.apply(response);

core/src/main/java/org/elasticsearch/rest/action/RestToXContentListener.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@
2020
package org.elasticsearch.rest.action;
2121

2222
import org.elasticsearch.common.xcontent.ToXContent;
23+
import org.elasticsearch.common.xcontent.ToXContentObject;
2324
import org.elasticsearch.common.xcontent.XContentBuilder;
2425
import org.elasticsearch.rest.BytesRestResponse;
2526
import org.elasticsearch.rest.RestChannel;
2627
import org.elasticsearch.rest.RestResponse;
2728
import org.elasticsearch.rest.RestStatus;
2829

29-
import java.io.IOException;
30-
3130
/**
3231
* A REST based action listener that assumes the response is of type {@link ToXContent} and automatically
3332
* builds an XContent based response (wrapping the toXContent in startObject/endObject).
3433
*/
35-
public class RestToXContentListener<Response extends ToXContent> extends RestResponseListener<Response> {
34+
public class RestToXContentListener<Response extends ToXContentObject> extends RestResponseListener<Response> {
3635

3736
public RestToXContentListener(RestChannel channel) {
3837
super(channel);
@@ -43,19 +42,9 @@ public final RestResponse buildResponse(Response response) throws Exception {
4342
return buildResponse(response, channel.newBuilder());
4443
}
4544

46-
protected final void toXContent(Response response, XContentBuilder builder) throws IOException {
47-
final boolean needsNewObject = response.isFragment();
48-
if (needsNewObject) {
49-
builder.startObject();
50-
}
51-
response.toXContent(builder, channel.request());
52-
if (needsNewObject) {
53-
builder.endObject();
54-
}
55-
}
56-
5745
public RestResponse buildResponse(Response response, XContentBuilder builder) throws Exception {
58-
toXContent(response, builder);
46+
assert response.isFragment() == false; //would be nice if we could make default methods final
47+
response.toXContent(builder, channel.request());
5948
return new BytesRestResponse(getStatus(response), builder);
6049
}
6150

0 commit comments

Comments
 (0)