continue abstracting json into xcontent

This commit is contained in:
kimchy 2010-04-29 00:34:25 +03:00
parent 37dfc443aa
commit cfc8ec5165
44 changed files with 186 additions and 141 deletions

View File

@ -27,6 +27,8 @@ import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.json.ToJson;
import org.elasticsearch.util.xcontent.ToXContent;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -38,7 +40,7 @@ import static org.elasticsearch.search.internal.InternalSearchResponse.*;
*
* @author kimchy (shay.banon)
*/
public class SearchResponse implements ActionResponse, ToJson {
public class SearchResponse implements ActionResponse, ToXContent {
private InternalSearchResponse internalResponse;
@ -161,7 +163,7 @@ public class SearchResponse implements ActionResponse, ToJson {
return scrollId;
}
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
if (scrollId != null) {
builder.field("_scrollId", scrollId);
}
@ -185,7 +187,7 @@ public class SearchResponse implements ActionResponse, ToJson {
}
builder.endObject();
internalResponse.toJson(builder, params);
internalResponse.toXContent(builder, params);
}
public static SearchResponse readSearchResponse(StreamInput in) throws IOException {

View File

@ -21,7 +21,7 @@ package org.elasticsearch.rest;
import org.apache.lucene.util.UnicodeUtil;
import org.elasticsearch.util.ThreadLocals;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -49,16 +49,16 @@ public class JsonRestResponse extends AbstractRestResponse {
private final Status status;
private final JsonBuilder jsonBuilder;
private final XContentBuilder builder;
public JsonRestResponse(RestRequest request, Status status) {
this.jsonBuilder = null;
this.builder = null;
this.status = status;
this.prefixUtf8Result = startJsonp(request);
}
public JsonRestResponse(RestRequest request, Status status, JsonBuilder jsonBuilder) throws IOException {
this.jsonBuilder = jsonBuilder;
public JsonRestResponse(RestRequest request, Status status, XContentBuilder builder) throws IOException {
this.builder = builder;
this.status = status;
this.prefixUtf8Result = startJsonp(request);
}
@ -72,11 +72,11 @@ public class JsonRestResponse extends AbstractRestResponse {
}
@Override public byte[] content() throws IOException {
return jsonBuilder.unsafeBytes();
return builder.unsafeBytes();
}
@Override public int contentLength() throws IOException {
return jsonBuilder.unsafeBytesLength();
return builder.unsafeBytesLength();
}
@Override public Status status() {

View File

@ -19,12 +19,12 @@
package org.elasticsearch.rest;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import static org.elasticsearch.ExceptionsHelper.*;
import static org.elasticsearch.util.json.JsonBuilder.*;
import static org.elasticsearch.rest.action.support.RestXContentBuilder.*;
/**
* @author kimchy (Shay Banon)
@ -39,8 +39,8 @@ public class JsonThrowableRestResponse extends JsonRestResponse {
super(request, status, convert(request, t));
}
private static JsonBuilder convert(RestRequest request, Throwable t) throws IOException {
JsonBuilder builder = binaryJsonBuilder().prettyPrint()
private static XContentBuilder convert(RestRequest request, Throwable t) throws IOException {
XContentBuilder builder = restContentBuilder(request)
.startObject().field("error", detailedMessage(t));
if (t != null && request.paramAsBoolean("error_trace", false)) {
builder.startObject("error_trace");
@ -62,7 +62,7 @@ public class JsonThrowableRestResponse extends JsonRestResponse {
return builder;
}
private static void buildThrowable(Throwable t, JsonBuilder builder) throws IOException {
private static void buildThrowable(Throwable t, XContentBuilder builder) throws IOException {
builder.field("message", t.getMessage());
for (StackTraceElement stElement : t.getStackTrace()) {
builder.startObject("at")

View File

@ -21,7 +21,7 @@ package org.elasticsearch.rest;
import org.elasticsearch.util.SizeValue;
import org.elasticsearch.util.TimeValue;
import org.elasticsearch.util.json.ToJson;
import org.elasticsearch.util.xcontent.ToXContent;
import java.io.InputStream;
import java.util.List;
@ -31,7 +31,7 @@ import java.util.Set;
/**
* @author kimchy (shay.banon)
*/
public interface RestRequest extends ToJson.Params {
public interface RestRequest extends ToXContent.Params {
enum Method {
GET, POST, PUT, DELETE

View File

@ -19,15 +19,16 @@
package org.elasticsearch.rest.action.admin.cluster.health;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.health.*;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -68,7 +69,7 @@ public class RestClusterHealthAction extends BaseRestHandler {
}
} catch (Exception e) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
channel.sendResponse(new JsonRestResponse(request, PRECONDITION_FAILED, builder.startObject().field("error", e.getMessage()).endObject()));
} catch (IOException e1) {
logger.error("Failed to send failure response", e1);
@ -79,7 +80,7 @@ public class RestClusterHealthAction extends BaseRestHandler {
client.admin().cluster().health(clusterHealthRequest, new ActionListener<ClusterHealthResponse>() {
@Override public void onResponse(ClusterHealthResponse response) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field("status", response.status().name().toLowerCase());

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest.action.admin.cluster.node.info;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
@ -27,9 +28,9 @@ import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.Map;
@ -54,7 +55,7 @@ public class RestNodesInfoAction extends BaseRestHandler {
client.admin().cluster().nodesInfo(nodesInfoRequest, new ActionListener<NodesInfoResponse>() {
@Override public void onResponse(NodesInfoResponse result) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field("cluster_name", result.clusterName().value());

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest.action.admin.cluster.node.shutdown;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.node.shutdown.NodesShutdownRequest;
@ -26,12 +27,14 @@ import org.elasticsearch.action.admin.cluster.node.shutdown.NodesShutdownRespons
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import static org.elasticsearch.rest.action.support.RestXContentBuilder.restContentBuilder;
/**
* @author kimchy (shay.banon)
*/
@ -52,7 +55,7 @@ public class RestNodesShutdownAction extends BaseRestHandler {
client.admin().cluster().nodesShutdown(nodesShutdownRequest, new ActionListener<NodesShutdownResponse>() {
@Override public void onResponse(NodesShutdownResponse result) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = restContentBuilder(request);
builder.startObject();
builder.field("cluster_name", result.clusterName().value());

View File

@ -27,9 +27,10 @@ import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -59,7 +60,7 @@ public class RestBroadcastPingAction extends BaseRestHandler {
client.admin().cluster().ping(broadcastPingRequest, new ActionListener<BroadcastPingResponse>() {
@Override public void onResponse(BroadcastPingResponse response) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field("ok", true);
buildBroadcastShardsHeader(builder, response);

View File

@ -28,9 +28,10 @@ import org.elasticsearch.action.admin.cluster.ping.replication.ShardReplicationP
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -54,7 +55,7 @@ public class RestReplicationPingAction extends BaseRestHandler {
client.admin().cluster().ping(replicationPingRequest, new ActionListener<ReplicationPingResponse>() {
@Override public void onResponse(ReplicationPingResponse result) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field("ok", true);
for (IndexReplicationPingResponse indexResponse : result.indices().values()) {

View File

@ -19,15 +19,16 @@
package org.elasticsearch.rest.action.admin.cluster.ping.single;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.ping.single.SinglePingRequest;
import org.elasticsearch.action.admin.cluster.ping.single.SinglePingResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -53,7 +54,7 @@ public class RestSinglePingAction extends BaseRestHandler {
client.admin().cluster().ping(singlePingRequest, new ActionListener<SinglePingResponse>() {
@Override public void onResponse(SinglePingResponse result) {
try {
JsonBuilder generator = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder generator = RestXContentBuilder.restContentBuilder(request);
generator.startObject().field("ok", true).endObject();
channel.sendResponse(new JsonRestResponse(request, OK, generator));
} catch (Exception e) {

View File

@ -19,7 +19,6 @@
package org.elasticsearch.rest.action.admin.cluster.state;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
@ -31,9 +30,10 @@ import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
import org.elasticsearch.cluster.routing.RoutingNode;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.Map;
@ -54,7 +54,7 @@ public class RestClusterStateAction extends BaseRestHandler {
@Override public void onResponse(ClusterStateResponse response) {
try {
ClusterState state = response.state();
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
// meta data
@ -125,7 +125,7 @@ public class RestClusterStateAction extends BaseRestHandler {
}
}
private void jsonShardRouting(JsonBuilder builder, ShardRouting shardRouting) throws IOException {
private void jsonShardRouting(XContentBuilder builder, ShardRouting shardRouting) throws IOException {
builder.startObject()
.field("state", shardRouting.state())
.field("primary", shardRouting.primary())

View File

@ -19,7 +19,6 @@
package org.elasticsearch.rest.action.admin.indices.alias;
import org.elasticsearch.util.guice.inject.Inject;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
@ -29,15 +28,16 @@ import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.metadata.AliasAction;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.json.Jackson;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.*;
import static org.elasticsearch.rest.RestResponse.Status.*;
import static org.elasticsearch.rest.action.support.RestXContentBuilder.*;
/**
* @author kimchy (shay.banon)
@ -116,7 +116,7 @@ public class RestIndicesAliasesAction extends BaseRestHandler {
client.admin().indices().aliases(indicesAliasesRequest, new ActionListener<IndicesAliasesResponse>() {
@Override public void onResponse(IndicesAliasesResponse response) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = restContentBuilder(request);
builder.startObject()
.field("ok", true)
.endObject();

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest.action.admin.indices.cache.clear;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest;
@ -27,9 +28,8 @@ import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -66,7 +66,7 @@ public class RestClearIndicesCacheAction extends BaseRestHandler {
clearIndicesCacheRequest.operationThreading(operationThreading);
} catch (Exception e) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
channel.sendResponse(new JsonRestResponse(request, BAD_REQUEST, builder.startObject().field("error", e.getMessage()).endObject()));
} catch (IOException e1) {
logger.error("Failed to send failure response", e1);
@ -76,7 +76,7 @@ public class RestClearIndicesCacheAction extends BaseRestHandler {
client.admin().indices().clearCache(clearIndicesCacheRequest, new ActionListener<ClearIndicesCacheResponse>() {
@Override public void onResponse(ClearIndicesCacheResponse response) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field("ok", true);

View File

@ -19,7 +19,6 @@
package org.elasticsearch.rest.action.admin.indices.create;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
@ -27,12 +26,13 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.indices.IndexAlreadyExistsException;
import org.elasticsearch.indices.InvalidIndexNameException;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.Strings;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.settings.ImmutableSettings;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.settings.SettingsException;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -71,7 +71,7 @@ public class RestCreateIndexAction extends BaseRestHandler {
client.admin().indices().create(createIndexRequest, new ActionListener<CreateIndexResponse>() {
@Override public void onResponse(CreateIndexResponse response) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject()
.field("ok", true)
.field("acknowledged", response.acknowledged())
@ -86,7 +86,7 @@ public class RestCreateIndexAction extends BaseRestHandler {
try {
Throwable t = unwrapCause(e);
if (t instanceof IndexAlreadyExistsException || t instanceof InvalidIndexNameException) {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
channel.sendResponse(new JsonRestResponse(request, BAD_REQUEST, builder.startObject().field("error", t.getMessage()).endObject()));
} else {
channel.sendResponse(new JsonThrowableRestResponse(request, e));

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest.action.admin.indices.delete;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
@ -26,9 +27,9 @@ import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -52,7 +53,7 @@ public class RestDeleteIndexAction extends BaseRestHandler {
client.admin().indices().delete(deleteIndexRequest, new ActionListener<DeleteIndexResponse>() {
@Override public void onResponse(DeleteIndexResponse response) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject()
.field("ok", true)
.field("acknowledged", response.acknowledged())
@ -67,7 +68,7 @@ public class RestDeleteIndexAction extends BaseRestHandler {
try {
Throwable t = unwrapCause(e);
if (t instanceof IndexMissingException) {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
channel.sendResponse(new JsonRestResponse(request, BAD_REQUEST, builder.startObject().field("error", t.getMessage()).endObject()));
} else {
channel.sendResponse(new JsonThrowableRestResponse(request, e));

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest.action.admin.indices.flush;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.flush.FlushRequest;
@ -27,9 +28,9 @@ import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -65,7 +66,7 @@ public class RestFlushAction extends BaseRestHandler {
client.admin().indices().flush(flushRequest, new ActionListener<FlushResponse>() {
@Override public void onResponse(FlushResponse response) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field("ok", true);

View File

@ -27,9 +27,10 @@ import org.elasticsearch.action.admin.indices.gateway.snapshot.IndexGatewaySnaps
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -55,7 +56,7 @@ public class RestGatewaySnapshotAction extends BaseRestHandler {
client.admin().indices().gatewaySnapshot(gatewaySnapshotRequest, new ActionListener<GatewaySnapshotResponse>() {
@Override public void onResponse(GatewaySnapshotResponse result) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field("ok", true);
builder.startObject("indices");

View File

@ -28,9 +28,10 @@ import org.elasticsearch.index.mapper.InvalidTypeNameException;
import org.elasticsearch.index.mapper.MergeMappingException;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -64,7 +65,7 @@ public class RestPutMappingAction extends BaseRestHandler {
client.admin().indices().putMapping(putMappingRequest, new ActionListener<PutMappingResponse>() {
@Override public void onResponse(PutMappingResponse response) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject()
.field("ok", true)
.field("acknowledged", response.acknowledged());
@ -77,7 +78,7 @@ public class RestPutMappingAction extends BaseRestHandler {
@Override public void onFailure(Throwable e) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
Throwable t = unwrapCause(e);
if (t instanceof IndexMissingException || t instanceof InvalidTypeNameException || t instanceof MergeMappingException) {
channel.sendResponse(new JsonRestResponse(request, BAD_REQUEST, builder.startObject().field("error", t.getMessage()).endObject()));

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest.action.admin.indices.optimize;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.optimize.OptimizeRequest;
@ -27,9 +28,8 @@ import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -70,7 +70,7 @@ public class RestOptimizeAction extends BaseRestHandler {
optimizeRequest.operationThreading(operationThreading);
} catch (Exception e) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
channel.sendResponse(new JsonRestResponse(request, BAD_REQUEST, builder.startObject().field("error", e.getMessage()).endObject()));
} catch (IOException e1) {
logger.error("Failed to send failure response", e1);
@ -80,7 +80,7 @@ public class RestOptimizeAction extends BaseRestHandler {
client.admin().indices().optimize(optimizeRequest, new ActionListener<OptimizeResponse>() {
@Override public void onResponse(OptimizeResponse response) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field("ok", true);

View File

@ -27,9 +27,10 @@ import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -64,7 +65,7 @@ public class RestRefreshAction extends BaseRestHandler {
client.admin().indices().refresh(refreshRequest, new ActionListener<RefreshResponse>() {
@Override public void onResponse(RefreshResponse response) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field("ok", true);

View File

@ -19,15 +19,16 @@
package org.elasticsearch.rest.action.admin.indices.status;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.status.*;
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.Map;
@ -60,7 +61,7 @@ public class RestIndicesStatusAction extends BaseRestHandler {
client.admin().indices().status(indicesStatusRequest, new ActionListener<IndicesStatusResponse>() {
@Override public void onResponse(IndicesStatusResponse response) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field("ok", true);

View File

@ -19,7 +19,6 @@
package org.elasticsearch.rest.action.count;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.count.CountRequest;
import org.elasticsearch.action.count.CountResponse;
@ -27,9 +26,10 @@ import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -74,7 +74,7 @@ public class RestCountAction extends BaseRestHandler {
}
} catch (Exception e) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
channel.sendResponse(new JsonRestResponse(request, BAD_REQUEST, builder.startObject().field("error", e.getMessage()).endObject()));
} catch (IOException e1) {
logger.error("Failed to send failure response", e1);
@ -85,7 +85,7 @@ public class RestCountAction extends BaseRestHandler {
client.count(countRequest, new ActionListener<CountResponse>() {
@Override public void onResponse(CountResponse response) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
builder.field("count", response.count());

View File

@ -19,15 +19,16 @@
package org.elasticsearch.rest.action.delete;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -54,7 +55,7 @@ public class RestDeleteAction extends BaseRestHandler {
client.delete(deleteRequest, new ActionListener<DeleteResponse>() {
@Override public void onResponse(DeleteResponse result) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject()
.field("ok", true)
.field("_index", result.index())

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest.action.deletebyquery;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.deletebyquery.DeleteByQueryRequest;
@ -28,9 +29,9 @@ import org.elasticsearch.action.deletebyquery.ShardDeleteByQueryRequest;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -63,7 +64,7 @@ public class RestDeleteByQueryAction extends BaseRestHandler {
deleteByQueryRequest.timeout(request.paramAsTime("timeout", ShardDeleteByQueryRequest.DEFAULT_TIMEOUT));
} catch (Exception e) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
channel.sendResponse(new JsonRestResponse(request, PRECONDITION_FAILED, builder.startObject().field("error", e.getMessage()).endObject()));
} catch (IOException e1) {
logger.error("Failed to send failure response", e1);
@ -73,7 +74,7 @@ public class RestDeleteByQueryAction extends BaseRestHandler {
client.deleteByQuery(deleteByQueryRequest, new ActionListener<DeleteByQueryResponse>() {
@Override public void onResponse(DeleteByQueryResponse result) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject().field("ok", true);
builder.startObject("_indices");

View File

@ -28,13 +28,14 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.regex.Pattern;
import static org.elasticsearch.rest.RestRequest.Method.*;
import static org.elasticsearch.rest.RestResponse.Status.*;
import static org.elasticsearch.rest.action.support.RestJsonBuilder.*;
import static org.elasticsearch.rest.action.support.RestXContentBuilder.*;
/**
* @author kimchy (Shay Banon)
@ -73,7 +74,7 @@ public class RestGetAction extends BaseRestHandler {
@Override public void onResponse(GetResponse response) {
try {
if (!response.exists()) {
JsonBuilder builder = restJsonBuilder(request);
XContentBuilder builder = restContentBuilder(request);
builder.startObject();
builder.field("_index", response.index());
builder.field("_type", response.type());
@ -81,14 +82,13 @@ public class RestGetAction extends BaseRestHandler {
builder.endObject();
channel.sendResponse(new JsonRestResponse(request, NOT_FOUND, builder));
} else {
JsonBuilder builder = restJsonBuilder(request);
XContentBuilder builder = restContentBuilder(request);
builder.startObject();
builder.field("_index", response.index());
builder.field("_type", response.type());
builder.field("_id", response.id());
if (response.source() != null) {
builder.raw(", \"_source\" : ");
builder.raw(response.source());
builder.rawField("_source", response.source());
}
if (response.fields() != null && !response.fields().isEmpty()) {

View File

@ -19,15 +19,16 @@
package org.elasticsearch.rest.action.index;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -66,7 +67,7 @@ public class RestIndexAction extends BaseRestHandler {
indexRequest.opType(IndexRequest.OpType.CREATE);
} else {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
channel.sendResponse(new JsonRestResponse(request, BAD_REQUEST, builder.startObject().field("error", "opType [" + sOpType + "] not allowed, either [index] or [create] are allowed").endObject()));
} catch (IOException e1) {
logger.warn("Failed to send response", e1);
@ -81,7 +82,7 @@ public class RestIndexAction extends BaseRestHandler {
client.index(indexRequest, new ActionListener<IndexResponse>() {
@Override public void onResponse(IndexResponse result) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject()
.field("ok", true)
.field("_index", result.index())

View File

@ -19,19 +19,19 @@
package org.elasticsearch.rest.action.main;
import org.elasticsearch.util.gcommon.collect.Iterators;
import org.elasticsearch.util.guice.inject.Inject;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ArrayNode;
import org.elasticsearch.Version;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.Classes;
import org.elasticsearch.util.concurrent.jsr166y.ThreadLocalRandom;
import org.elasticsearch.util.gcommon.collect.Iterators;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.json.Jackson;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -66,7 +66,7 @@ public class RestMainAction extends BaseRestHandler {
@Override public void handleRequest(RestRequest request, RestChannel channel) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request).prettyPrint();
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request).prettyPrint();
builder.startObject();
builder.field("ok", true);
if (settings.get("name") != null) {

View File

@ -30,13 +30,14 @@ import org.elasticsearch.search.Scroll;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import static org.elasticsearch.client.Requests.*;
import static org.elasticsearch.rest.RestRequest.Method.*;
import static org.elasticsearch.rest.RestResponse.Status.*;
import static org.elasticsearch.rest.action.support.RestJsonBuilder.*;
import static org.elasticsearch.rest.action.support.RestXContentBuilder.*;
import static org.elasticsearch.util.TimeValue.*;
/**
@ -82,7 +83,7 @@ public class RestMoreLikeThisAction extends BaseRestHandler {
}
} catch (Exception e) {
try {
JsonBuilder builder = restJsonBuilder(request);
XContentBuilder builder = restContentBuilder(request);
channel.sendResponse(new JsonRestResponse(request, BAD_REQUEST, builder.startObject().field("error", e.getMessage()).endObject()));
} catch (IOException e1) {
logger.error("Failed to send failure response", e1);
@ -93,9 +94,9 @@ public class RestMoreLikeThisAction extends BaseRestHandler {
client.moreLikeThis(mltRequest, new ActionListener<SearchResponse>() {
@Override public void onResponse(SearchResponse response) {
try {
JsonBuilder builder = restJsonBuilder(request);
XContentBuilder builder = restContentBuilder(request);
builder.startObject();
response.toJson(builder, request);
response.toXContent(builder, request);
builder.endObject();
channel.sendResponse(new JsonRestResponse(request, OK, builder));
} catch (Exception e) {

View File

@ -19,29 +19,29 @@
package org.elasticsearch.rest.action.search;
import org.elasticsearch.index.query.xcontent.QueryBuilders;
import org.elasticsearch.index.query.xcontent.QueryStringQueryBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.search.SearchOperationThreading;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.xcontent.QueryBuilders;
import org.elasticsearch.index.query.xcontent.QueryStringQueryBuilder;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.regex.Pattern;
import static org.elasticsearch.rest.RestRequest.Method.*;
import static org.elasticsearch.rest.RestResponse.Status.*;
import static org.elasticsearch.rest.action.support.RestJsonBuilder.*;
import static org.elasticsearch.rest.action.support.RestXContentBuilder.*;
import static org.elasticsearch.util.TimeValue.*;
/**
@ -81,7 +81,7 @@ public class RestSearchAction extends BaseRestHandler {
searchRequest.operationThreading(operationThreading);
} catch (Exception e) {
try {
JsonBuilder builder = restJsonBuilder(request);
XContentBuilder builder = restContentBuilder(request);
channel.sendResponse(new JsonRestResponse(request, BAD_REQUEST, builder.startObject().field("error", e.getMessage()).endObject()));
} catch (IOException e1) {
logger.error("Failed to send failure response", e1);
@ -91,9 +91,9 @@ public class RestSearchAction extends BaseRestHandler {
client.search(searchRequest, new ActionListener<SearchResponse>() {
@Override public void onResponse(SearchResponse response) {
try {
JsonBuilder builder = restJsonBuilder(request);
XContentBuilder builder = restContentBuilder(request);
builder.startObject();
response.toJson(builder, request);
response.toXContent(builder, request);
builder.endObject();
channel.sendResponse(new JsonRestResponse(request, OK, builder));
} catch (Exception e) {

View File

@ -29,12 +29,13 @@ import org.elasticsearch.rest.*;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import static org.elasticsearch.rest.RestRequest.Method.*;
import static org.elasticsearch.rest.RestResponse.Status.*;
import static org.elasticsearch.rest.action.support.RestJsonBuilder.*;
import static org.elasticsearch.rest.action.support.RestXContentBuilder.*;
import static org.elasticsearch.util.TimeValue.*;
/**
@ -67,7 +68,7 @@ public class RestSearchScrollAction extends BaseRestHandler {
searchScrollRequest.operationThreading(operationThreading);
} catch (Exception e) {
try {
JsonBuilder builder = restJsonBuilder(request);
XContentBuilder builder = restContentBuilder(request);
channel.sendResponse(new JsonRestResponse(request, BAD_REQUEST, builder.startObject().field("error", e.getMessage()).endObject()));
} catch (IOException e1) {
logger.error("Failed to send failure response", e1);
@ -78,9 +79,9 @@ public class RestSearchScrollAction extends BaseRestHandler {
client.searchScroll(searchScrollRequest, new ActionListener<SearchResponse>() {
@Override public void onResponse(SearchResponse response) {
try {
JsonBuilder builder = restJsonBuilder(request);
XContentBuilder builder = restContentBuilder(request);
builder.startObject();
response.toJson(builder, request);
response.toXContent(builder, request);
builder.endObject();
channel.sendResponse(new JsonRestResponse(request, OK, builder));
} catch (Exception e) {

View File

@ -27,7 +27,7 @@ import org.elasticsearch.index.query.xcontent.QueryStringQueryBuilder;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.util.Strings;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.regex.Pattern;
@ -41,7 +41,7 @@ public class RestActions {
public final static Pattern typesPattern = Pattern.compile(",");
public final static Pattern nodesIdsPattern = Pattern.compile(",");
public static void buildBroadcastShardsHeader(JsonBuilder builder, BroadcastOperationResponse response) throws IOException {
public static void buildBroadcastShardsHeader(XContentBuilder builder, BroadcastOperationResponse response) throws IOException {
builder.startObject("_shards");
builder.field("total", response.totalShards());
builder.field("successful", response.successfulShards());

View File

@ -20,19 +20,19 @@
package org.elasticsearch.rest.action.support;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.util.json.BinaryJsonBuilder;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.XContentType;
import org.elasticsearch.util.xcontent.builder.BinaryXContentBuilder;
import java.io.IOException;
import static org.elasticsearch.util.json.JsonBuilder.*;
/**
* @author kimchy (Shay Banon)
* @author kimchy (shay.banon)
*/
public class RestJsonBuilder {
public class RestXContentBuilder {
public static BinaryJsonBuilder restJsonBuilder(RestRequest request) throws IOException {
BinaryJsonBuilder builder = binaryJsonBuilder();
public static BinaryXContentBuilder restContentBuilder(RestRequest request) throws IOException {
BinaryXContentBuilder builder = XContentFactory.contentBinaryBuilder(XContentType.JSON);
if (request.paramAsBoolean("pretty", false)) {
builder.prettyPrint();
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.rest.action.terms;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
import org.elasticsearch.util.guice.inject.Inject;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.broadcast.BroadcastOperationThreading;
@ -28,9 +29,9 @@ import org.elasticsearch.action.terms.TermsRequest;
import org.elasticsearch.action.terms.TermsResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestJsonBuilder;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.settings.Settings;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.regex.Pattern;
@ -108,7 +109,7 @@ public class RestTermsAction extends BaseRestHandler {
termsRequest.sortType(request.param("sort"));
} catch (Exception e) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
channel.sendResponse(new JsonRestResponse(request, BAD_REQUEST, builder.startObject().field("error", e.getMessage()).endObject()));
} catch (IOException e1) {
logger.error("Failed to send failure response", e1);
@ -120,7 +121,7 @@ public class RestTermsAction extends BaseRestHandler {
client.terms(termsRequest, new ActionListener<TermsResponse>() {
@Override public void onResponse(TermsResponse response) {
try {
JsonBuilder builder = RestJsonBuilder.restJsonBuilder(request);
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
builder.startObject();
buildBroadcastShardsHeader(builder, response);

View File

@ -24,6 +24,7 @@ import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.search.highlight.HighlightField;
import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.json.ToJson;
import org.elasticsearch.util.xcontent.ToXContent;
import java.util.Map;
@ -33,7 +34,7 @@ import java.util.Map;
* @author kimchy (shay.banon)
* @see SearchHits
*/
public interface SearchHit extends Streamable, ToJson, Iterable<SearchHitField> {
public interface SearchHit extends Streamable, ToXContent, Iterable<SearchHitField> {
/**
* The index of the hit.

View File

@ -21,13 +21,14 @@ package org.elasticsearch.search;
import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.json.ToJson;
import org.elasticsearch.util.xcontent.ToXContent;
/**
* The hits of a search request.
*
* @author kimchy (shay.banon)
*/
public interface SearchHits extends Streamable, ToJson, Iterable<SearchHit> {
public interface SearchHits extends Streamable, ToXContent, Iterable<SearchHit> {
/**
* The total number of hits that matches the search request.

View File

@ -21,7 +21,7 @@ package org.elasticsearch.search.facets;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -82,7 +82,7 @@ public class CountFacet implements Facet {
count += increment;
}
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field(name, count);
}

View File

@ -22,13 +22,14 @@ package org.elasticsearch.search.facets;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.json.ToJson;
import org.elasticsearch.util.xcontent.ToXContent;
/**
* A search facet.
*
* @author kimchy (shay.banon)
*/
public interface Facet extends Streamable, ToJson {
public interface Facet extends Streamable, ToXContent {
enum Type {
/**

View File

@ -23,24 +23,24 @@ import org.elasticsearch.util.gcommon.collect.ImmutableList;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.json.ToJson;
import org.elasticsearch.util.xcontent.ToXContent;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.search.facets.CountFacet.*;
import static org.elasticsearch.util.gcommon.collect.Lists.*;
import static org.elasticsearch.util.gcommon.collect.Maps.*;
import static org.elasticsearch.search.facets.CountFacet.*;
/**
* Facets of search action.
*
* @author kimchy (shay.banon)
*/
public class Facets implements Streamable, ToJson, Iterable<Facet> {
public class Facets implements Streamable, ToXContent, Iterable<Facet> {
private final List<Facet> EMPTY = ImmutableList.of();
@ -109,10 +109,10 @@ public class Facets implements Streamable, ToJson, Iterable<Facet> {
return facetsAsMap().get(name);
}
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("facets");
for (Facet facet : facets) {
facet.toJson(builder, params);
facet.toXContent(builder, params);
}
builder.endObject();
}

View File

@ -19,7 +19,6 @@
package org.elasticsearch.search.internal;
import org.elasticsearch.util.gcommon.collect.ImmutableMap;
import org.apache.lucene.search.Explanation;
import org.elasticsearch.ElasticSearchParseException;
import org.elasticsearch.search.SearchHit;
@ -27,10 +26,12 @@ import org.elasticsearch.search.SearchHitField;
import org.elasticsearch.search.SearchShardTarget;
import org.elasticsearch.search.highlight.HighlightField;
import org.elasticsearch.util.Unicode;
import org.elasticsearch.util.gcommon.collect.ImmutableMap;
import org.elasticsearch.util.gnu.trove.TIntObjectHashMap;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.xcontent.XContentFactory;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import javax.annotation.Nullable;
import java.io.IOException;
@ -189,7 +190,7 @@ public class InternalSearchHit implements SearchHit {
this.shard = target;
}
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field("_index", shard.index());
// builder.field("_shard", shard.shardId());
@ -197,8 +198,12 @@ public class InternalSearchHit implements SearchHit {
builder.field("_type", type());
builder.field("_id", id());
if (source() != null) {
builder.raw(", \"_source\" : ");
builder.raw(source());
if (XContentFactory.xContentType(source()) == builder.contentType()) {
builder.field("_source");
builder.value(source());
} else {
builder.rawField("_source", source());
}
}
if (fields != null && !fields.isEmpty()) {
builder.startObject("fields");
@ -242,7 +247,7 @@ public class InternalSearchHit implements SearchHit {
builder.endObject();
}
private void buildExplanation(JsonBuilder builder, Explanation explanation) throws IOException {
private void buildExplanation(XContentBuilder builder, Explanation explanation) throws IOException {
builder.startObject();
builder.field("value", explanation.getValue());
builder.field("description", explanation.getDescription());

View File

@ -27,6 +27,7 @@ import org.elasticsearch.util.gnu.trove.TIntObjectHashMap;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.IdentityHashMap;
@ -84,13 +85,13 @@ public class InternalSearchHits implements SearchHits {
return this.hits;
}
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("hits");
builder.field("total", totalHits);
builder.field("hits");
builder.startArray();
for (SearchHit hit : hits) {
hit.toJson(builder, params);
hit.toXContent(builder, params);
}
builder.endArray();
builder.endObject();

View File

@ -26,6 +26,8 @@ import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.json.JsonBuilder;
import org.elasticsearch.util.json.ToJson;
import org.elasticsearch.util.xcontent.ToXContent;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
@ -35,7 +37,7 @@ import static org.elasticsearch.search.internal.InternalSearchHits.*;
/**
* @author kimchy (Shay Banon)
*/
public class InternalSearchResponse implements Streamable, ToJson {
public class InternalSearchResponse implements Streamable, ToXContent {
private InternalSearchHits hits;
@ -57,10 +59,10 @@ public class InternalSearchResponse implements Streamable, ToJson {
return facets;
}
@Override public void toJson(JsonBuilder builder, Params params) throws IOException {
hits.toJson(builder, params);
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
hits.toXContent(builder, params);
if (facets != null) {
facets.toJson(builder, params);
facets.toXContent(builder, params);
}
}

View File

@ -89,6 +89,8 @@ public interface XContentGenerator {
void writeObjectFieldStart(String fieldName) throws IOException;
void writeRawFieldStart(String fieldName) throws IOException;
void flush() throws IOException;
void close() throws IOException;

View File

@ -280,7 +280,12 @@ public abstract class XContentBuilder<T extends XContentBuilder> {
return builder;
}
public abstract T raw(byte[] json) throws IOException;
public T rawField(String fieldName, byte[] content) throws IOException {
generator.writeRawFieldStart(fieldName);
return raw(content);
}
public abstract T raw(byte[] content) throws IOException;
public T value(Boolean value) throws IOException {
return value(value.booleanValue());

View File

@ -158,6 +158,11 @@ public class JsonXContentGenerator implements XContentGenerator {
generator.writeObjectFieldStart(fieldName);
}
@Override public void writeRawFieldStart(String fieldName) throws IOException {
generator.writeRaw(", \"" + fieldName + "\" : ");
}
@Override public void flush() throws IOException {
generator.flush();
}