Adapt to removal of XContentHelper#toString(ToXContent) (elastic/x-pack-elasticsearch#2072)

Original commit: elastic/x-pack-elasticsearch@35f6ac23a1
This commit is contained in:
Luca Cavanna 2017-07-26 16:01:09 +02:00 committed by GitHub
parent 4da7ae21bc
commit cb20de5d95
5 changed files with 85 additions and 123 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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);

View File

@ -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();

View File

@ -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();