From cb20de5d951889354e9d42b4d4f0d205dd39f80d Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Wed, 26 Jul 2017 16:01:09 +0200 Subject: [PATCH] Adapt to removal of XContentHelper#toString(ToXContent) (elastic/x-pack-elasticsearch#2072) Original commit: elastic/x-pack-elasticsearch@35f6ac23a1e7aa596fdddfb1e3b7612f3f2d0999 --- .../xpack/common/http/HttpRequest.java | 4 +- .../hipchat/IntegrationAccount.java | 32 +++---- .../notification/hipchat/UserAccount.java | 48 ++++------ .../hipchat/IntegrationAccountTests.java | 31 +++---- .../hipchat/UserAccountTests.java | 93 ++++++++----------- 5 files changed, 85 insertions(+), 123 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpRequest.java b/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpRequest.java index 0993ff127a4..f5b7fd52935 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpRequest.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpRequest.java @@ -13,7 +13,6 @@ 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.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.rest.RestUtils; @@ -420,8 +419,7 @@ public class HttpRequest implements ToXContentObject { } public Builder jsonBody(ToXContent xContent) { - return body(XContentHelper.toString(xContent)) - .setHeader("Content-Type", XContentType.JSON.mediaType()); + return body(Strings.toString(xContent)).setHeader("Content-Type", XContentType.JSON.mediaType()); } public Builder connectionTimeout(TimeValue timeout) { diff --git a/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/IntegrationAccount.java b/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/IntegrationAccount.java index 1af0fc20032..60f74d037d0 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/IntegrationAccount.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/IntegrationAccount.java @@ -9,11 +9,9 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xpack.common.http.HttpClient; import org.elasticsearch.xpack.common.http.HttpMethod; import org.elasticsearch.xpack.common.http.HttpProxy; @@ -25,7 +23,6 @@ import org.elasticsearch.xpack.notification.hipchat.HipChatMessage.Color; import org.elasticsearch.xpack.notification.hipchat.HipChatMessage.Format; import org.elasticsearch.xpack.watcher.actions.hipchat.HipChatAction; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -99,28 +96,25 @@ public class IntegrationAccount extends HipChatAccount { return new SentMessages(name, sentMessages); } - public HttpRequest buildRoomRequest(String room, final HipChatMessage message, HttpProxy proxy) { + private HttpRequest buildRoomRequest(String room, final HipChatMessage message, HttpProxy proxy) { HttpRequest.Builder builder = server.httpRequest() .method(HttpMethod.POST) .scheme(Scheme.HTTPS) .path("/v2/room/" + room + "/notification") .setHeader("Content-Type", "application/json") .setHeader("Authorization", "Bearer " + authToken) - .body(XContentHelper.toString(new ToXContent() { - @Override - public XContentBuilder toXContent(XContentBuilder xbuilder, Params params) throws IOException { - xbuilder.field("message", message.body); - if (message.format != null) { - xbuilder.field("message_format", message.format.value()); - } - if (message.notify != null) { - xbuilder.field("notify", message.notify); - } - if (message.color != null) { - xbuilder.field("color", String.valueOf(message.color.value())); - } - return xbuilder; + .body(Strings.toString((xbuilder, params) -> { + xbuilder.field("message", message.body); + if (message.format != null) { + xbuilder.field("message_format", message.format.value()); } + if (message.notify != null) { + xbuilder.field("notify", message.notify); + } + if (message.color != null) { + xbuilder.field("color", String.valueOf(message.color.value())); + } + return xbuilder; })); if (proxy != null) { builder.proxy(proxy); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/UserAccount.java b/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/UserAccount.java index c6a4299de08..5ac87b13b5c 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/UserAccount.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/UserAccount.java @@ -9,11 +9,9 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.xpack.common.http.HttpClient; import org.elasticsearch.xpack.common.http.HttpMethod; import org.elasticsearch.xpack.common.http.HttpProxy; @@ -119,21 +117,18 @@ public class UserAccount extends HipChatAccount { .path("/v2/room/" + room + "/notification") .setHeader("Content-Type", "application/json") .setHeader("Authorization", "Bearer " + authToken) - .body(XContentHelper.toString(new ToXContent() { - @Override - public XContentBuilder toXContent(XContentBuilder xbuilder, Params params) throws IOException { - xbuilder.field("message", message.body); - if (message.format != null) { - xbuilder.field("message_format", message.format.value()); - } - if (message.notify != null) { - xbuilder.field("notify", message.notify); - } - if (message.color != null) { - xbuilder.field("color", String.valueOf(message.color.value())); - } - return xbuilder; + .body(Strings.toString((xbuilder, params) -> { + xbuilder.field("message", message.body); + if (message.format != null) { + xbuilder.field("message_format", message.format.value()); } + if (message.notify != null) { + xbuilder.field("notify", message.notify); + } + if (message.color != null) { + xbuilder.field("color", String.valueOf(message.color.value())); + } + return xbuilder; })); if (proxy != null) { builder.proxy(proxy); @@ -148,18 +143,15 @@ public class UserAccount extends HipChatAccount { .path("/v2/user/" + user + "/message") .setHeader("Content-Type", "application/json") .setHeader("Authorization", "Bearer " + authToken) - .body(XContentHelper.toString(new ToXContent() { - @Override - public XContentBuilder toXContent(XContentBuilder xbuilder, Params params) throws IOException { - xbuilder.field("message", message.body); - if (message.format != null) { - xbuilder.field("message_format", message.format.value()); - } - if (message.notify != null) { - xbuilder.field("notify", message.notify); - } - return xbuilder; + .body(Strings.toString((xbuilder, params) -> { + xbuilder.field("message", message.body); + if (message.format != null) { + xbuilder.field("message_format", message.format.value()); } + if (message.notify != null) { + xbuilder.field("notify", message.notify); + } + return xbuilder; })); if (proxy != null) { builder.proxy(proxy); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/notification/hipchat/IntegrationAccountTests.java b/plugin/src/test/java/org/elasticsearch/xpack/notification/hipchat/IntegrationAccountTests.java index 078bebd87f6..81023b28791 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/notification/hipchat/IntegrationAccountTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/notification/hipchat/IntegrationAccountTests.java @@ -6,11 +6,9 @@ package org.elasticsearch.xpack.notification.hipchat; import org.apache.logging.log4j.Logger; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.common.http.HttpClient; import org.elasticsearch.xpack.common.http.HttpMethod; @@ -18,8 +16,6 @@ import org.elasticsearch.xpack.common.http.HttpRequest; import org.elasticsearch.xpack.common.http.HttpResponse; import org.elasticsearch.xpack.common.http.Scheme; -import java.io.IOException; - import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; @@ -135,21 +131,18 @@ public class IntegrationAccountTests extends ESTestCase { .path("/v2/room/_room/notification") .setHeader("Content-Type", "application/json") .setHeader("Authorization", "Bearer _token") - .body(XContentHelper.toString(new ToXContent() { - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.field("message", message.body); - if (message.format != null) { - builder.field("message_format", message.format.value()); - } - if (message.notify != null) { - builder.field("notify", message.notify); - } - if (message.color != null) { - builder.field("color", String.valueOf(message.color.value())); - } - return builder; + .body(Strings.toString((builder, params) -> { + builder.field("message", message.body); + if (message.format != null) { + builder.field("message_format", message.format.value()); } + if (message.notify != null) { + builder.field("notify", message.notify); + } + if (message.color != null) { + builder.field("color", String.valueOf(message.color.value())); + } + return builder; })) .build(); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/notification/hipchat/UserAccountTests.java b/plugin/src/test/java/org/elasticsearch/xpack/notification/hipchat/UserAccountTests.java index c3904e9e074..c9dcdb38422 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/notification/hipchat/UserAccountTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/notification/hipchat/UserAccountTests.java @@ -6,11 +6,9 @@ package org.elasticsearch.xpack.notification.hipchat; import org.apache.logging.log4j.Logger; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.xpack.common.http.HttpClient; import org.elasticsearch.xpack.common.http.HttpMethod; @@ -20,7 +18,6 @@ import org.elasticsearch.xpack.common.http.Scheme; import org.elasticsearch.xpack.common.text.TextTemplate; import org.elasticsearch.xpack.watcher.test.MockTextTemplateEngine; -import java.io.IOException; import java.util.HashMap; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -132,21 +129,18 @@ public class UserAccountTests extends ESTestCase { .path("/v2/room/_r1/notification") .setHeader("Content-Type", "application/json") .setHeader("Authorization", "Bearer _token") - .body(XContentHelper.toString(new ToXContent() { - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.field("message", message.body); - if (message.format != null) { - builder.field("message_format", message.format.value()); - } - if (message.notify != null) { - builder.field("notify", message.notify); - } - if (message.color != null) { - builder.field("color", String.valueOf(message.color.value())); - } - return builder; + .body(Strings.toString((builder, params) -> { + builder.field("message", message.body); + if (message.format != null) { + builder.field("message_format", message.format.value()); } + if (message.notify != null) { + builder.field("notify", message.notify); + } + if (message.color != null) { + builder.field("color", String.valueOf(message.color.value())); + } + return builder; })) .build(); @@ -162,21 +156,18 @@ public class UserAccountTests extends ESTestCase { .path("/v2/room/_r2/notification") .setHeader("Content-Type", "application/json") .setHeader("Authorization", "Bearer _token") - .body(XContentHelper.toString(new ToXContent() { - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.field("message", message.body); - if (message.format != null) { - builder.field("message_format", message.format.value()); - } - if (message.notify != null) { - builder.field("notify", message.notify); - } - if (message.color != null) { - builder.field("color", String.valueOf(message.color.value())); - } - return builder; + .body(Strings.toString((builder, params) -> { + builder.field("message", message.body); + if (message.format != null) { + builder.field("message_format", message.format.value()); } + if (message.notify != null) { + builder.field("notify", message.notify); + } + if (message.color != null) { + builder.field("color", String.valueOf(message.color.value())); + } + return builder; })) .build(); @@ -192,18 +183,15 @@ public class UserAccountTests extends ESTestCase { .path("/v2/user/_u1/message") .setHeader("Content-Type", "application/json") .setHeader("Authorization", "Bearer _token") - .body(XContentHelper.toString(new ToXContent() { - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.field("message", message.body); - if (message.format != null) { - builder.field("message_format", message.format.value()); - } - if (message.notify != null) { - builder.field("notify", message.notify); - } - return builder; + .body(Strings.toString((builder, params) -> { + builder.field("message", message.body); + if (message.format != null) { + builder.field("message_format", message.format.value()); } + if (message.notify != null) { + builder.field("notify", message.notify); + } + return builder; })) .build(); @@ -219,18 +207,15 @@ public class UserAccountTests extends ESTestCase { .path("/v2/user/_u2/message") .setHeader("Content-Type", "application/json") .setHeader("Authorization", "Bearer _token") - .body(XContentHelper.toString(new ToXContent() { - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.field("message", message.body); - if (message.format != null) { - builder.field("message_format", message.format.value()); - } - if (message.notify != null) { - builder.field("notify", message.notify); - } - return builder; + .body(Strings.toString((builder, params) -> { + builder.field("message", message.body); + if (message.format != null) { + builder.field("message_format", message.format.value()); } + if (message.notify != null) { + builder.field("notify", message.notify); + } + return builder; })) .build();