diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/graph/action/GraphExploreResponse.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/graph/action/GraphExploreResponse.java index 2869871a67d..7dc630da404 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/graph/action/GraphExploreResponse.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/graph/action/GraphExploreResponse.java @@ -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; } diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/input/search/ExecutableSearchInput.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/input/search/ExecutableSearchInput.java index 55e01d5fd85..4adcfec196a 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/input/search/ExecutableSearchInput.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/input/search/ExecutableSearchInput.java @@ -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 filteredKeys = XContentFilterKeysUtils.filterMapOrdered(input.getExtractKeys(), parser); payload = new Payload.Simple(filteredKeys); } else { diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherUtils.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherUtils.java index cbc790d0988..8747a254ed7 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherUtils.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/support/WatcherUtils.java @@ -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 responseToData(ToXContent response) throws IOException { - XContentBuilder builder = jsonBuilder().startObject().value(response).endObject(); - return XContentHelper.convertToMap(builder.bytes(), false).v2(); + public static Map responseToData(ToXContentObject response) throws IOException { + return XContentHelper.convertToMap(XContentHelper.toXContent(response, XContentType.JSON), false).v2(); } public static Map flattenModel(Map map) { diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/watch/Payload.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/watch/Payload.java index 9e97016d7b7..e4935c8e8c1 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/watch/Payload.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/watch/Payload.java @@ -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)); } - } } diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherUtilsTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherUtilsTests.java index e861a4fa5dc..b7ab542f5a0 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherUtilsTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherUtilsTests.java @@ -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 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 entry : expected.entrySet()) { - builder.field(entry.getKey()); - builder.value(entry.getValue()); - } - return builder; + ToXContentObject content = (builder, params) -> { + builder.startObject(); + for (Map.Entry entry : expected.entrySet()) { + builder.field(entry.getKey()); + builder.value(entry.getValue()); } + builder.endObject(); + return builder; }; Map result = WatcherUtils.responseToData(content); assertThat(result, equalTo(expected));