Adapt to ToXContentObject introduction

Changes required are for two reasons:

1) SearchResponse is a self contained valid object which doesn't need to be wrapped in a new object anymore
2) RestToXContentBuilderListener requires ToXContentObject, hence GraphExploreResponse needs to be moved over

Original commit: elastic/x-pack-elasticsearch@12277d0220
This commit is contained in:
javanna 2017-01-05 16:41:55 +01:00 committed by Luca Cavanna
parent 19cbab4ac3
commit c23de42f29
5 changed files with 21 additions and 27 deletions

View File

@ -6,14 +6,13 @@
package org.elasticsearch.xpack.graph.action;
import com.carrotsearch.hppc.ObjectIntHashMap;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.action.search.ShardSearchFailure;
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.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.graph.action.Connection.ConnectionId;
import org.elasticsearch.xpack.graph.action.Vertex.VertexId;
@ -31,7 +30,7 @@ import static org.elasticsearch.action.search.ShardSearchFailure.readShardSearch
*
* @see GraphExploreRequest
*/
public class GraphExploreResponse extends ActionResponse implements ToXContent {
public class GraphExploreResponse extends ActionResponse implements ToXContentObject {
private long tookInMillis;
private boolean timedOut = false;
@ -159,6 +158,7 @@ public class GraphExploreResponse extends ActionResponse implements ToXContent {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(Fields.TOOK, tookInMillis);
builder.field(Fields.TIMED_OUT, timedOut);
@ -194,7 +194,7 @@ public class GraphExploreResponse extends ActionResponse implements ToXContent {
builder.endObject();
}
builder.endArray();
builder.endObject();
return builder;
}

View File

@ -12,9 +12,9 @@ import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.xpack.watcher.execution.WatchExecutionContext;
@ -27,7 +27,6 @@ import org.elasticsearch.xpack.watcher.watch.Payload;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.xpack.watcher.input.search.SearchInput.TYPE;
/**
@ -80,9 +79,9 @@ public class ExecutableSearchInput extends ExecutableInput<SearchInput, SearchIn
final Payload payload;
if (input.getExtractKeys() != null) {
XContentBuilder builder = jsonBuilder().startObject().value(response).endObject();
BytesReference bytes = XContentHelper.toXContent(response, XContentType.JSON);
// EMPTY is safe here because we never use namedObject
XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, builder.bytes());
XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, bytes);
Map<String, Object> filteredKeys = XContentFilterKeysUtils.filterMapOrdered(input.getExtractKeys(), parser);
payload = new Payload.Simple(filteredKeys);
} else {

View File

@ -6,9 +6,9 @@
package org.elasticsearch.xpack.watcher.support;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.joda.time.DateTime;
import java.io.IOException;
@ -16,18 +16,15 @@ import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.xpack.watcher.support.WatcherDateTimeUtils.formatDate;
public final class WatcherUtils {
private WatcherUtils() {
}
public static Map<String, Object> responseToData(ToXContent response) throws IOException {
XContentBuilder builder = jsonBuilder().startObject().value(response).endObject();
return XContentHelper.convertToMap(builder.bytes(), false).v2();
public static Map<String, Object> responseToData(ToXContentObject response) throws IOException {
return XContentHelper.convertToMap(XContentHelper.toXContent(response, XContentType.JSON), false).v2();
}
public static Map<String, Object> flattenModel(Map<String, Object> map) {

View File

@ -7,6 +7,7 @@ package org.elasticsearch.xpack.watcher.watch;
import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import java.io.IOException;
@ -73,10 +74,8 @@ public interface Payload extends ToXContent {
}
class XContent extends Simple {
public XContent(ToXContent response) throws IOException {
public XContent(ToXContentObject response) throws IOException {
super(responseToData(response));
}
}
}

View File

@ -11,6 +11,7 @@ import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
@ -22,7 +23,6 @@ import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.watcher.support.search.WatcherSearchTemplateRequest;
import org.joda.time.DateTime;
import java.io.IOException;
import java.time.Clock;
import java.util.Arrays;
import java.util.HashMap;
@ -72,15 +72,14 @@ public class WatcherUtilsTests extends ESTestCase {
Map<String, Object> otherMap = new HashMap<>();
otherMap.putAll(expected);
expected.put("key5", otherMap);
ToXContent content = new ToXContent() {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
for (Map.Entry<String, ?> entry : expected.entrySet()) {
builder.field(entry.getKey());
builder.value(entry.getValue());
}
return builder;
ToXContentObject content = (builder, params) -> {
builder.startObject();
for (Map.Entry<String, ?> entry : expected.entrySet()) {
builder.field(entry.getKey());
builder.value(entry.getValue());
}
builder.endObject();
return builder;
};
Map<String, Object> result = WatcherUtils.responseToData(content);
assertThat(result, equalTo(expected));