From 0b2b50be94f1e54f130c900fb4fc1c31859bd347 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Mon, 25 Jul 2016 14:57:57 +0200 Subject: [PATCH] Watcher: Put response code in payload in http input (elastic/elasticsearch#2888) The response status code was stored in the result of an http input, but inaccessible in the payload itself and could not be used in scripts. This puts the status code in the payload under the name '_status_code', similar to the '_headers' variable, which already stores the headers. Original commit: elastic/x-pack-elasticsearch@dff2a39535d7e326d9f85e7915b01a95b471a720 --- .../watcher/input/http/ExecutableHttpInput.java | 12 ++++++------ .../xpack/watcher/input/http/HttpInput.java | 2 +- .../xpack/watcher/input/http/HttpInputTests.java | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java index c38dde7af28..34bf5467650 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/ExecutableHttpInput.java @@ -53,10 +53,14 @@ public class ExecutableHttpInput extends ExecutableInput> headers = response.headers(); + Map payloadMap = new HashMap<>(); + payloadMap.put("_status_code", response.status()); + if (headers.isEmpty() == false) { + payloadMap.put("_headers", headers); + } if (!response.hasContent()) { - Payload payload = headers.size() > 0 ? new Payload.Simple("_headers", headers) : Payload.EMPTY; - return new HttpInput.Result(request, -1, payload); + return new HttpInput.Result(request, response.status(), new Payload.Simple(payloadMap)); } final XContentType contentType; @@ -72,7 +76,6 @@ public class ExecutableHttpInput extends ExecutableInput payloadMap = new HashMap<>(); if (contentType != null) { try (XContentParser parser = contentType.xContent().createParser(response.body())) { if (input.getExtractKeys() != null) { @@ -88,9 +91,6 @@ public class ExecutableHttpInput extends ExecutableInput 0) { - payloadMap.put("_headers", headers); - } return new HttpInput.Result(request, response.status(), new Payload.Simple(payloadMap)); } } diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/HttpInput.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/HttpInput.java index 1b53ca5b690..5434b98d7f5 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/HttpInput.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/input/http/HttpInput.java @@ -143,7 +143,7 @@ public class HttpInput implements Input { public static class Result extends Input.Result { @Nullable private final HttpRequest request; - private final int statusCode; + final int statusCode; public Result(HttpRequest request, int statusCode, Payload payload) { super(TYPE, payload); diff --git a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/http/HttpInputTests.java b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/http/HttpInputTests.java index af4d64aa390..ec3c305f0f3 100644 --- a/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/http/HttpInputTests.java +++ b/elasticsearch/x-pack/watcher/src/test/java/org/elasticsearch/xpack/watcher/input/http/HttpInputTests.java @@ -289,6 +289,21 @@ public class HttpInputTests extends ESTestCase { assertThat(result.payload().data(), not(hasKey("foo"))); } + public void testThatStatusCodeIsSetInResultAndPayload() throws Exception { + HttpResponse response = new HttpResponse(200); + when(httpClient.execute(any(HttpRequest.class))).thenReturn(response); + + HttpRequestTemplate.Builder request = HttpRequestTemplate.builder("localhost", 8080); + HttpInput httpInput = InputBuilders.httpInput(request.build()).build(); + ExecutableHttpInput input = new ExecutableHttpInput(httpInput, logger, httpClient, templateEngine); + + WatchExecutionContext ctx = createWatchExecutionContext(); + HttpInput.Result result = input.execute(ctx, new Payload.Simple()); + assertThat(result.statusCode, is(200)); + assertThat(result.payload().data(), hasKey("_status_code")); + assertThat(result.payload().data().get("_status_code"), is(200)); + } + private WatchExecutionContext createWatchExecutionContext() { Watch watch = new Watch("test-watch", new ScheduleTrigger(new IntervalSchedule(new IntervalSchedule.Interval(1, IntervalSchedule.Interval.Unit.MINUTES))),