From 7c391a8ad426944b018aa32439e2647427e9d1e2 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 10 Oct 2013 11:45:43 +0200 Subject: [PATCH] Add toString and toXContent to IndexResponse --- .../action/index/IndexResponse.java | 37 ++++++++++++++++++- .../rest/action/index/RestIndexAction.java | 19 +++------- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/index/IndexResponse.java b/src/main/java/org/elasticsearch/action/index/IndexResponse.java index 1f3d1f3eb67a6..d3da6f8a9c761 100644 --- a/src/main/java/org/elasticsearch/action/index/IndexResponse.java +++ b/src/main/java/org/elasticsearch/action/index/IndexResponse.java @@ -22,16 +22,20 @@ 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.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentBuilderString; +import org.elasticsearch.common.xcontent.XContentFactory; import java.io.IOException; /** * A response of an index operation, - * + * * @see org.elasticsearch.action.index.IndexRequest * @see org.elasticsearch.client.Client#index(IndexRequest) */ -public class IndexResponse extends ActionResponse { +public class IndexResponse extends ActionResponse implements ToXContent { private String index; private String id; @@ -105,4 +109,33 @@ public void writeTo(StreamOutput out) throws IOException { out.writeLong(version); out.writeBoolean(created); } + + static final class Fields { + static final XContentBuilderString _INDEX = new XContentBuilderString("_index"); + static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); + static final XContentBuilderString _ID = new XContentBuilderString("_id"); + static final XContentBuilderString _VERSION = new XContentBuilderString("_version"); + } + + @Override + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { + builder.field(Fields._INDEX, index); + builder.field(Fields._TYPE, type); + builder.field(Fields._ID, id); + builder.field(Fields._VERSION, version); + return builder; + } + + @Override + public String toString() { + try { + XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); + builder.startObject(); + toXContent(builder, EMPTY_PARAMS); + builder.endObject(); + return builder.string(); + } catch (IOException e) { + return "{ \"error\" : \"" + e.getMessage() + "\"}"; + } + } } diff --git a/src/main/java/org/elasticsearch/rest/action/index/RestIndexAction.java b/src/main/java/org/elasticsearch/rest/action/index/RestIndexAction.java index c00367911c46e..4601b9cc1b0c7 100644 --- a/src/main/java/org/elasticsearch/rest/action/index/RestIndexAction.java +++ b/src/main/java/org/elasticsearch/rest/action/index/RestIndexAction.java @@ -108,12 +108,9 @@ public void handleRequest(final RestRequest request, final RestChannel channel) public void onResponse(IndexResponse response) { try { XContentBuilder builder = RestXContentBuilder.restContentBuilder(request); - builder.startObject() - .field(Fields.OK, true) - .field(Fields._INDEX, response.getIndex()) - .field(Fields._TYPE, response.getType()) - .field(Fields._ID, response.getId()) - .field(Fields._VERSION, response.getVersion()); + builder.startObject(); + builder.field(Fields.OK, true); + response.toXContent(builder, request); builder.endObject(); RestStatus status = OK; if (response.isCreated()) { @@ -135,14 +132,10 @@ public void onFailure(Throwable e) { } }); } - - static final class Fields { + private static class Fields { static final XContentBuilderString OK = new XContentBuilderString("ok"); - static final XContentBuilderString _INDEX = new XContentBuilderString("_index"); - static final XContentBuilderString _TYPE = new XContentBuilderString("_type"); - static final XContentBuilderString _ID = new XContentBuilderString("_id"); - static final XContentBuilderString _VERSION = new XContentBuilderString("_version"); - static final XContentBuilderString MATCHES = new XContentBuilderString("matches"); } + + }