[cleanup] XContentSource now requires XContentType
We need this as the `XContentSource` supports all xcontent constructs as the root construct, while xcontent in core only supports objects. For this reason, we can't rely on xcontent auto-detection of the xcontent type. We need to be explicit about it. Original commit: elastic/x-pack-elasticsearch@a2ed944a21
This commit is contained in:
parent
797945b586
commit
3cf6b32f6a
|
@ -72,13 +72,13 @@ public class ExecutableIndexAction extends ExecutableAction<IndexAction> {
|
|||
indexRequest.source(jsonBuilder().prettyPrint().map(data));
|
||||
|
||||
if (ctx.simulateAction(actionId)) {
|
||||
return new IndexAction.Result.Simulated(action.index, action.docType, new XContentSource(indexRequest.source()));
|
||||
return new IndexAction.Result.Simulated(action.index, action.docType, new XContentSource(indexRequest.source(), XContentType.JSON));
|
||||
}
|
||||
|
||||
IndexResponse response = client.index(indexRequest);
|
||||
XContentBuilder jsonBuilder = jsonBuilder();
|
||||
indexResponseToXContent(jsonBuilder, response);
|
||||
return new IndexAction.Result.Success(new XContentSource(jsonBuilder.bytes()));
|
||||
return new IndexAction.Result.Success(new XContentSource(jsonBuilder));
|
||||
}
|
||||
|
||||
Action.Result indexBulk(Iterable list, String actionId, WatchExecutionContext ctx) throws Exception {
|
||||
|
|
|
@ -15,12 +15,12 @@ import org.elasticsearch.common.xcontent.XContentType;
|
|||
import org.elasticsearch.search.builder.SearchSourceBuilderException;
|
||||
import org.elasticsearch.watcher.WatcherException;
|
||||
import org.elasticsearch.watcher.actions.Action;
|
||||
import org.elasticsearch.watcher.actions.throttler.Throttler;
|
||||
import org.elasticsearch.watcher.condition.Condition;
|
||||
import org.elasticsearch.watcher.condition.always.AlwaysCondition;
|
||||
import org.elasticsearch.watcher.input.Input;
|
||||
import org.elasticsearch.watcher.input.none.NoneInput;
|
||||
import org.elasticsearch.watcher.support.xcontent.XContentSource;
|
||||
import org.elasticsearch.watcher.actions.throttler.Throttler;
|
||||
import org.elasticsearch.watcher.transform.Transform;
|
||||
import org.elasticsearch.watcher.trigger.Trigger;
|
||||
import org.elasticsearch.watcher.watch.Watch;
|
||||
|
@ -112,7 +112,7 @@ public class WatchSourceBuilder implements ToXContent {
|
|||
}
|
||||
|
||||
public XContentSource build() throws IOException {
|
||||
return new XContentSource(toXContent(jsonBuilder(), ToXContent.EMPTY_PARAMS).bytes());
|
||||
return new XContentSource(toXContent(jsonBuilder(), ToXContent.EMPTY_PARAMS));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -8,7 +8,6 @@ package org.elasticsearch.watcher.support.xcontent;
|
|||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.ElasticsearchParseException;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.collect.Tuple;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.xcontent.*;
|
||||
|
@ -37,8 +36,11 @@ public class XContentSource implements ToXContent {
|
|||
this.contentType = xContentType;
|
||||
}
|
||||
|
||||
public XContentSource(BytesReference bytes) {
|
||||
this(bytes, XContentFactory.xContentType(bytes));
|
||||
/**
|
||||
* Constructs a new xcontent source from the bytes of the given xcontent builder
|
||||
*/
|
||||
public XContentSource(XContentBuilder builder) {
|
||||
this(builder.bytes(), builder.contentType());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -9,6 +9,7 @@ import org.elasticsearch.action.ActionResponse;
|
|||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.watcher.support.xcontent.XContentSource;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -24,9 +25,9 @@ public class ExecuteWatchResponse extends ActionResponse {
|
|||
public ExecuteWatchResponse() {
|
||||
}
|
||||
|
||||
public ExecuteWatchResponse(String recordId, BytesReference recordSource) {
|
||||
public ExecuteWatchResponse(String recordId, BytesReference recordSource, XContentType contentType) {
|
||||
this.recordId = recordId;
|
||||
this.recordSource = new XContentSource(recordSource);
|
||||
this.recordSource = new XContentSource(recordSource, contentType);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -13,6 +13,7 @@ import org.elasticsearch.cluster.ClusterState;
|
|||
import org.elasticsearch.cluster.block.ClusterBlockException;
|
||||
import org.elasticsearch.cluster.block.ClusterBlockLevel;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.joda.time.DateTime;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
|
@ -114,7 +115,7 @@ public class TransportExecuteWatchAction extends WatcherTransportAction<ExecuteW
|
|||
WatchRecord record = executionService.execute(ctxBuilder.build());
|
||||
XContentBuilder builder = XContentFactory.jsonBuilder();
|
||||
record.toXContent(builder, WatcherParams.builder().hideSecrets(true).debug(request.isDebug()).build());
|
||||
ExecuteWatchResponse response = new ExecuteWatchResponse(record.id().value(), builder.bytes());
|
||||
ExecuteWatchResponse response = new ExecuteWatchResponse(record.id().value(), builder.bytes(), XContentType.JSON);
|
||||
listener.onResponse(response);
|
||||
} catch (Exception e) {
|
||||
logger.error("failed to execute [{}]", e, request.getId());
|
||||
|
|
|
@ -9,6 +9,7 @@ import org.elasticsearch.action.ActionResponse;
|
|||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.watcher.support.xcontent.XContentSource;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -23,12 +24,24 @@ public class GetWatchResponse extends ActionResponse {
|
|||
GetWatchResponse() {
|
||||
}
|
||||
|
||||
public GetWatchResponse(String id, long version, boolean found, BytesReference source) {
|
||||
assert !found && source == null || found && source.length() > 0;
|
||||
/**
|
||||
* ctor for missing watch
|
||||
*/
|
||||
public GetWatchResponse(String id) {
|
||||
this.id = id;
|
||||
this.version = -1;
|
||||
this.found = false;
|
||||
this.source = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* ctor for found watch
|
||||
*/
|
||||
public GetWatchResponse(String id, long version, BytesReference source, XContentType contentType) {
|
||||
this.id = id;
|
||||
this.version = version;
|
||||
this.found = found;
|
||||
this.source = found ? new XContentSource(source) : null;
|
||||
this.found = true;
|
||||
this.source = new XContentSource(source, contentType);
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
|
|
|
@ -16,7 +16,7 @@ import org.elasticsearch.common.bytes.BytesReference;
|
|||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.json.JsonXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.threadpool.ThreadPool;
|
||||
import org.elasticsearch.transport.TransportService;
|
||||
import org.elasticsearch.watcher.WatcherService;
|
||||
|
@ -28,6 +28,8 @@ import org.elasticsearch.watcher.watch.WatchStore;
|
|||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
|
||||
|
||||
/**
|
||||
* Performs the get operation.
|
||||
*/
|
||||
|
@ -57,19 +59,17 @@ public class TransportGetWatchAction extends WatcherTransportAction<GetWatchRequ
|
|||
try {
|
||||
Watch watch = watcherService.getWatch(request.getId());
|
||||
if (watch == null) {
|
||||
listener.onResponse(new GetWatchResponse(request.getId(), -1, false, null));
|
||||
listener.onResponse(new GetWatchResponse(request.getId()));
|
||||
return;
|
||||
}
|
||||
|
||||
BytesReference watchSource = null;
|
||||
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
|
||||
try (XContentBuilder builder = jsonBuilder()) {
|
||||
watch.toXContent(builder, WatcherParams.builder().hideSecrets(true).build());
|
||||
watchSource = builder.bytes();
|
||||
BytesReference watchSource = builder.bytes();
|
||||
listener.onResponse(new GetWatchResponse(watch.id(), watch.status().version(), watchSource, XContentType.JSON));
|
||||
} catch (IOException e) {
|
||||
listener.onFailure(e);
|
||||
return;
|
||||
}
|
||||
listener.onResponse(new GetWatchResponse(watch.id(), watch.status().version(), true, watchSource));
|
||||
|
||||
} catch (Throwable t) {
|
||||
logger.error("failed to get watch [{}]", t, request.getId());
|
||||
|
|
|
@ -11,11 +11,12 @@ import com.squareup.okhttp.mockwebserver.QueueDispatcher;
|
|||
import com.squareup.okhttp.mockwebserver.RecordedRequest;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.util.Callback;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.watcher.actions.ActionBuilders;
|
||||
import org.elasticsearch.watcher.history.HistoryStore;
|
||||
import org.elasticsearch.watcher.history.WatchRecord;
|
||||
import org.elasticsearch.watcher.support.http.HttpClient;
|
||||
import org.elasticsearch.watcher.support.http.HttpRequestTemplate;
|
||||
|
@ -35,6 +36,7 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFa
|
|||
import static org.elasticsearch.watcher.client.WatchSourceBuilders.watchBuilder;
|
||||
import static org.elasticsearch.watcher.condition.ConditionBuilders.alwaysCondition;
|
||||
import static org.elasticsearch.watcher.input.InputBuilders.simpleInput;
|
||||
import static org.elasticsearch.watcher.test.WatcherTestUtils.xContentSource;
|
||||
import static org.elasticsearch.watcher.trigger.TriggerBuilders.schedule;
|
||||
import static org.elasticsearch.watcher.trigger.schedule.Schedules.interval;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
@ -107,11 +109,14 @@ public class WebhookHttpsIntegrationTests extends AbstractWatcherIntegrationTest
|
|||
assertThat(recordedRequest.getPath(), equalTo("/test/_id"));
|
||||
assertThat(recordedRequest.getBody().readUtf8Line(), equalTo("{key=value}"));
|
||||
|
||||
SearchResponse response = client().prepareSearch(HistoryStore.INDEX_PREFIX + "*")
|
||||
.setQuery(QueryBuilders.termQuery(WatchRecord.Field.STATE.getPreferredName(), "executed"))
|
||||
.get();
|
||||
SearchResponse response = searchWatchRecords(new Callback<SearchRequestBuilder>() {
|
||||
@Override
|
||||
public void handle(SearchRequestBuilder builder) {
|
||||
builder.setQuery(QueryBuilders.termQuery(WatchRecord.Field.STATE.getPreferredName(), "executed"));
|
||||
}
|
||||
});
|
||||
assertNoFailures(response);
|
||||
XContentSource source = new XContentSource(response.getHits().getAt(0).sourceRef());
|
||||
XContentSource source = xContentSource(response.getHits().getAt(0).sourceRef());
|
||||
String body = source.getValue("result.actions.0.webhook.response.body");
|
||||
assertThat(body, notNullValue());
|
||||
assertThat(body, is("body"));
|
||||
|
|
|
@ -11,10 +11,11 @@ import com.squareup.okhttp.mockwebserver.MockWebServer;
|
|||
import com.squareup.okhttp.mockwebserver.QueueDispatcher;
|
||||
import com.squareup.okhttp.mockwebserver.RecordedRequest;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.util.Callback;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.watcher.actions.ActionBuilders;
|
||||
import org.elasticsearch.watcher.history.HistoryStore;
|
||||
import org.elasticsearch.watcher.history.WatchRecord;
|
||||
import org.elasticsearch.watcher.support.http.HttpRequestTemplate;
|
||||
import org.elasticsearch.watcher.support.http.auth.basic.BasicAuth;
|
||||
|
@ -31,6 +32,7 @@ import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFa
|
|||
import static org.elasticsearch.watcher.client.WatchSourceBuilders.watchBuilder;
|
||||
import static org.elasticsearch.watcher.condition.ConditionBuilders.alwaysCondition;
|
||||
import static org.elasticsearch.watcher.input.InputBuilders.simpleInput;
|
||||
import static org.elasticsearch.watcher.test.WatcherTestUtils.xContentSource;
|
||||
import static org.elasticsearch.watcher.trigger.TriggerBuilders.schedule;
|
||||
import static org.elasticsearch.watcher.trigger.schedule.Schedules.interval;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
@ -91,12 +93,15 @@ public class WebhookIntegrationTests extends AbstractWatcherIntegrationTests {
|
|||
assertThat(recordedRequest.getPath(), anyOf(equalTo("/test/_id?watch_id=_id¶m1=value1"), equalTo("/test/_id?param1=value1&watch_id=_id")));
|
||||
assertThat(recordedRequest.getBody().readUtf8Line(), equalTo("{key=value}"));
|
||||
|
||||
SearchResponse response = client().prepareSearch(HistoryStore.INDEX_PREFIX + "*")
|
||||
.setQuery(QueryBuilders.termQuery(WatchRecord.Field.STATE.getPreferredName(), "executed"))
|
||||
.get();
|
||||
SearchResponse response = searchWatchRecords(new Callback<SearchRequestBuilder>() {
|
||||
@Override
|
||||
public void handle(SearchRequestBuilder builder) {
|
||||
QueryBuilders.termQuery(WatchRecord.Field.STATE.getPreferredName(), "executed");
|
||||
}
|
||||
});
|
||||
|
||||
assertNoFailures(response);
|
||||
XContentSource source = new XContentSource(response.getHits().getAt(0).getSourceRef());
|
||||
XContentSource source = xContentSource(response.getHits().getAt(0).getSourceRef());
|
||||
String body = source.getValue("result.actions.0.webhook.response.body");
|
||||
assertThat(body, notNullValue());
|
||||
assertThat(body, is("body"));
|
||||
|
|
|
@ -32,6 +32,7 @@ import static org.elasticsearch.watcher.actions.ActionBuilders.loggingAction;
|
|||
import static org.elasticsearch.watcher.client.WatchSourceBuilders.watchBuilder;
|
||||
import static org.elasticsearch.watcher.condition.ConditionBuilders.scriptCondition;
|
||||
import static org.elasticsearch.watcher.input.InputBuilders.httpInput;
|
||||
import static org.elasticsearch.watcher.test.WatcherTestUtils.xContentSource;
|
||||
import static org.elasticsearch.watcher.trigger.TriggerBuilders.schedule;
|
||||
import static org.elasticsearch.watcher.trigger.schedule.Schedules.interval;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
@ -146,7 +147,7 @@ public class HttpInputIntegrationTests extends AbstractWatcherIntegrationTests {
|
|||
.setSize(1)
|
||||
.get();
|
||||
assertHitCount(searchResponse, 1);
|
||||
XContentSource source = new XContentSource(searchResponse.getHits().getAt(0).getSourceRef());
|
||||
XContentSource source = xContentSource(searchResponse.getHits().getAt(0).getSourceRef());
|
||||
assertThat(source.getValue("result.input.payload.hits.total"), equalTo((Object) 1));
|
||||
}
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ public class XContentSourceTests extends ElasticsearchTestCase {
|
|||
.nullField("key_null")
|
||||
.endObject()
|
||||
.bytes();
|
||||
XContentSource source = new XContentSource(bytes);
|
||||
XContentSource source = new XContentSource(bytes, builder.contentType());
|
||||
XContentBuilder builder2 = XContentFactory.contentBuilder(builder.contentType());
|
||||
BytesReference bytes2 = source.toXContent(builder2, ToXContent.EMPTY_PARAMS).bytes();
|
||||
assertThat(bytes.array(), is(bytes2.array()));
|
||||
|
|
|
@ -11,15 +11,13 @@ import org.elasticsearch.action.support.IndicesOptions;
|
|||
import org.elasticsearch.common.Strings;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.elasticsearch.common.bytes.BytesArray;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.xcontent.*;
|
||||
import org.elasticsearch.script.ScriptContextRegistry;
|
||||
import org.joda.time.DateTime;
|
||||
import org.elasticsearch.common.logging.ESLogger;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
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.env.Environment;
|
||||
import org.elasticsearch.script.ScriptEngineService;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
|
@ -94,6 +92,11 @@ public final class WatcherTestUtils {
|
|||
private WatcherTestUtils() {
|
||||
}
|
||||
|
||||
public static XContentSource xContentSource(BytesReference bytes) {
|
||||
XContent xContent = XContentFactory.xContent(bytes);
|
||||
return new XContentSource(bytes, xContent.type());
|
||||
}
|
||||
|
||||
public static void assertValue(Map<String, Object> map, String path, Matcher<?> matcher) {
|
||||
assertThat(ObjectPath.eval(path, map), (Matcher<Object>) matcher);
|
||||
}
|
||||
|
|
|
@ -7,12 +7,11 @@ package org.elasticsearch.watcher.test.integration;
|
|||
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.support.IndicesOptions;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.watcher.support.clock.SystemClock;
|
||||
import org.joda.time.DateTime;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.util.Callback;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
|
@ -21,8 +20,8 @@ import org.elasticsearch.watcher.WatcherException;
|
|||
import org.elasticsearch.watcher.client.WatchSourceBuilder;
|
||||
import org.elasticsearch.watcher.client.WatcherClient;
|
||||
import org.elasticsearch.watcher.condition.ConditionBuilders;
|
||||
import org.elasticsearch.watcher.history.HistoryStore;
|
||||
import org.elasticsearch.watcher.support.WatcherUtils;
|
||||
import org.elasticsearch.watcher.support.clock.SystemClock;
|
||||
import org.elasticsearch.watcher.support.template.Template;
|
||||
import org.elasticsearch.watcher.support.xcontent.XContentSource;
|
||||
import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTests;
|
||||
|
@ -50,6 +49,7 @@ import static org.elasticsearch.watcher.condition.ConditionBuilders.scriptCondit
|
|||
import static org.elasticsearch.watcher.input.InputBuilders.searchInput;
|
||||
import static org.elasticsearch.watcher.input.InputBuilders.simpleInput;
|
||||
import static org.elasticsearch.watcher.test.WatcherTestUtils.newInputSearchRequest;
|
||||
import static org.elasticsearch.watcher.test.WatcherTestUtils.xContentSource;
|
||||
import static org.elasticsearch.watcher.trigger.TriggerBuilders.schedule;
|
||||
import static org.elasticsearch.watcher.trigger.schedule.Schedules.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
@ -357,13 +357,14 @@ public class BasicWatcherTests extends AbstractWatcherIntegrationTests {
|
|||
|
||||
// Check that the input result payload has been filtered
|
||||
refresh();
|
||||
SearchResponse searchResponse = client().prepareSearch(HistoryStore.INDEX_PREFIX + "*")
|
||||
.setIndicesOptions(IndicesOptions.lenientExpandOpen())
|
||||
.setQuery(matchQuery("watch_id", "_name1"))
|
||||
.setSize(1)
|
||||
.get();
|
||||
SearchResponse searchResponse = searchWatchRecords(new Callback<SearchRequestBuilder>() {
|
||||
@Override
|
||||
public void handle(SearchRequestBuilder builder) {
|
||||
builder.setQuery(matchQuery("watch_id", "_name1"));
|
||||
}
|
||||
});
|
||||
assertHitCount(searchResponse, 1);
|
||||
XContentSource source = new XContentSource(searchResponse.getHits().getAt(0).getSourceRef());
|
||||
XContentSource source = xContentSource(searchResponse.getHits().getAt(0).getSourceRef());
|
||||
assertThat(source.getValue("result.input.payload.hits.total"), equalTo((Object) 1));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue