From 9c09b88e9f52a38b1b0832c92b11e46968eda0a1 Mon Sep 17 00:00:00 2001 From: jaymode Date: Fri, 30 Dec 2016 08:34:39 -0500 Subject: [PATCH] watcher: do not continue execution when get result doesn't exist When attempting to a get a watch that does not exist, the listener is called once inside an if statement and the execution of the method continues as if the watch existed. This causes failures to happen including a NPE. This commit wraps the execution in a if-else to prevent this. Original commit: elastic/x-pack-elasticsearch@27f09852e7598c40bb941945a7dce79f32aaf063 --- .../actions/get/TransportGetWatchAction.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/get/TransportGetWatchAction.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/get/TransportGetWatchAction.java index 719db950922..93b09f82f05 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/get/TransportGetWatchAction.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/get/TransportGetWatchAction.java @@ -70,24 +70,24 @@ public class TransportGetWatchAction extends WatcherTransportAction { - if (getResponse.isExists() == false) { + if (getResponse.isExists()) { + try (XContentBuilder builder = jsonBuilder()) { + // When we return the watch via the Get Watch REST API, we want to return the watch as was specified in the put api, + // we don't include the status in the watch source itself, but as a separate top level field, so that + // it indicates the the status is managed by watcher itself. + DateTime now = new DateTime(clock.millis(), UTC); + Watch watch = parser.parseWithSecrets(request.getId(), true, getResponse.getSourceAsBytesRef(), now); + watch.toXContent(builder, WatcherParams.builder() + .hideSecrets(true) + .put(Watch.INCLUDE_STATUS_KEY, false) + .build()); + watch.version(getResponse.getVersion()); + watch.status().version(getResponse.getVersion()); + listener.onResponse(new GetWatchResponse(watch.id(), watch.status(), builder.bytes(), XContentType.JSON)); + } + } else { listener.onResponse(new GetWatchResponse(request.getId())); } - - try (XContentBuilder builder = jsonBuilder()) { - // When we return the watch via the Get Watch REST API, we want to return the watch as was specified in the put api, - // we don't include the status in the watch source itself, but as a separate top level field, so that - // it indicates the the status is managed by watcher itself. - DateTime now = new DateTime(clock.millis(), UTC); - Watch watch = parser.parseWithSecrets(request.getId(), true, getResponse.getSourceAsBytesRef(), now); - watch.toXContent(builder, WatcherParams.builder() - .hideSecrets(true) - .put(Watch.INCLUDE_STATUS_KEY, false) - .build()); - watch.version(getResponse.getVersion()); - watch.status().version(getResponse.getVersion()); - listener.onResponse(new GetWatchResponse(watch.id(), watch.status(), builder.bytes(), XContentType.JSON)); - } }, listener::onFailure)); }