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.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.rest.RestUtils; import org.elasticsearch.rest.RestUtils;
@ -420,8 +419,7 @@ public class HttpRequest implements ToXContentObject {
} }
public Builder jsonBody(ToXContent xContent) { public Builder jsonBody(ToXContent xContent) {
return body(XContentHelper.toString(xContent)) return body(Strings.toString(xContent)).setHeader("Content-Type", XContentType.JSON.mediaType());
.setHeader("Content-Type", XContentType.JSON.mediaType());
} }
public Builder connectionTimeout(TimeValue timeout) { public Builder connectionTimeout(TimeValue timeout) {

View File

@ -9,11 +9,9 @@ import org.apache.logging.log4j.Logger;
import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException; 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.HttpClient;
import org.elasticsearch.xpack.common.http.HttpMethod; import org.elasticsearch.xpack.common.http.HttpMethod;
import org.elasticsearch.xpack.common.http.HttpProxy; 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.notification.hipchat.HipChatMessage.Format;
import org.elasticsearch.xpack.watcher.actions.hipchat.HipChatAction; import org.elasticsearch.xpack.watcher.actions.hipchat.HipChatAction;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -99,16 +96,14 @@ public class IntegrationAccount extends HipChatAccount {
return new SentMessages(name, sentMessages); 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() HttpRequest.Builder builder = server.httpRequest()
.method(HttpMethod.POST) .method(HttpMethod.POST)
.scheme(Scheme.HTTPS) .scheme(Scheme.HTTPS)
.path("/v2/room/" + room + "/notification") .path("/v2/room/" + room + "/notification")
.setHeader("Content-Type", "application/json") .setHeader("Content-Type", "application/json")
.setHeader("Authorization", "Bearer " + authToken) .setHeader("Authorization", "Bearer " + authToken)
.body(XContentHelper.toString(new ToXContent() { .body(Strings.toString((xbuilder, params) -> {
@Override
public XContentBuilder toXContent(XContentBuilder xbuilder, Params params) throws IOException {
xbuilder.field("message", message.body); xbuilder.field("message", message.body);
if (message.format != null) { if (message.format != null) {
xbuilder.field("message_format", message.format.value()); xbuilder.field("message_format", message.format.value());
@ -120,7 +115,6 @@ public class IntegrationAccount extends HipChatAccount {
xbuilder.field("color", String.valueOf(message.color.value())); xbuilder.field("color", String.valueOf(message.color.value()));
} }
return xbuilder; return xbuilder;
}
})); }));
if (proxy != null) { if (proxy != null) {
builder.proxy(proxy); builder.proxy(proxy);

View File

@ -9,11 +9,9 @@ import org.apache.logging.log4j.Logger;
import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException; 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.HttpClient;
import org.elasticsearch.xpack.common.http.HttpMethod; import org.elasticsearch.xpack.common.http.HttpMethod;
import org.elasticsearch.xpack.common.http.HttpProxy; import org.elasticsearch.xpack.common.http.HttpProxy;
@ -119,9 +117,7 @@ public class UserAccount extends HipChatAccount {
.path("/v2/room/" + room + "/notification") .path("/v2/room/" + room + "/notification")
.setHeader("Content-Type", "application/json") .setHeader("Content-Type", "application/json")
.setHeader("Authorization", "Bearer " + authToken) .setHeader("Authorization", "Bearer " + authToken)
.body(XContentHelper.toString(new ToXContent() { .body(Strings.toString((xbuilder, params) -> {
@Override
public XContentBuilder toXContent(XContentBuilder xbuilder, Params params) throws IOException {
xbuilder.field("message", message.body); xbuilder.field("message", message.body);
if (message.format != null) { if (message.format != null) {
xbuilder.field("message_format", message.format.value()); xbuilder.field("message_format", message.format.value());
@ -133,7 +129,6 @@ public class UserAccount extends HipChatAccount {
xbuilder.field("color", String.valueOf(message.color.value())); xbuilder.field("color", String.valueOf(message.color.value()));
} }
return xbuilder; return xbuilder;
}
})); }));
if (proxy != null) { if (proxy != null) {
builder.proxy(proxy); builder.proxy(proxy);
@ -148,9 +143,7 @@ public class UserAccount extends HipChatAccount {
.path("/v2/user/" + user + "/message") .path("/v2/user/" + user + "/message")
.setHeader("Content-Type", "application/json") .setHeader("Content-Type", "application/json")
.setHeader("Authorization", "Bearer " + authToken) .setHeader("Authorization", "Bearer " + authToken)
.body(XContentHelper.toString(new ToXContent() { .body(Strings.toString((xbuilder, params) -> {
@Override
public XContentBuilder toXContent(XContentBuilder xbuilder, Params params) throws IOException {
xbuilder.field("message", message.body); xbuilder.field("message", message.body);
if (message.format != null) { if (message.format != null) {
xbuilder.field("message_format", message.format.value()); xbuilder.field("message_format", message.format.value());
@ -159,7 +152,6 @@ public class UserAccount extends HipChatAccount {
xbuilder.field("notify", message.notify); xbuilder.field("notify", message.notify);
} }
return xbuilder; return xbuilder;
}
})); }));
if (proxy != null) { if (proxy != null) {
builder.proxy(proxy); builder.proxy(proxy);

View File

@ -6,11 +6,9 @@
package org.elasticsearch.xpack.notification.hipchat; package org.elasticsearch.xpack.notification.hipchat;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException; 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.test.ESTestCase;
import org.elasticsearch.xpack.common.http.HttpClient; import org.elasticsearch.xpack.common.http.HttpClient;
import org.elasticsearch.xpack.common.http.HttpMethod; 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.HttpResponse;
import org.elasticsearch.xpack.common.http.Scheme; import org.elasticsearch.xpack.common.http.Scheme;
import java.io.IOException;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
@ -135,9 +131,7 @@ public class IntegrationAccountTests extends ESTestCase {
.path("/v2/room/_room/notification") .path("/v2/room/_room/notification")
.setHeader("Content-Type", "application/json") .setHeader("Content-Type", "application/json")
.setHeader("Authorization", "Bearer _token") .setHeader("Authorization", "Bearer _token")
.body(XContentHelper.toString(new ToXContent() { .body(Strings.toString((builder, params) -> {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("message", message.body); builder.field("message", message.body);
if (message.format != null) { if (message.format != null) {
builder.field("message_format", message.format.value()); builder.field("message_format", message.format.value());
@ -149,7 +143,6 @@ public class IntegrationAccountTests extends ESTestCase {
builder.field("color", String.valueOf(message.color.value())); builder.field("color", String.valueOf(message.color.value()));
} }
return builder; return builder;
}
})) }))
.build(); .build();

View File

@ -6,11 +6,9 @@
package org.elasticsearch.xpack.notification.hipchat; package org.elasticsearch.xpack.notification.hipchat;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException; 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.test.ESTestCase;
import org.elasticsearch.xpack.common.http.HttpClient; import org.elasticsearch.xpack.common.http.HttpClient;
import org.elasticsearch.xpack.common.http.HttpMethod; 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.common.text.TextTemplate;
import org.elasticsearch.xpack.watcher.test.MockTextTemplateEngine; import org.elasticsearch.xpack.watcher.test.MockTextTemplateEngine;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@ -132,9 +129,7 @@ public class UserAccountTests extends ESTestCase {
.path("/v2/room/_r1/notification") .path("/v2/room/_r1/notification")
.setHeader("Content-Type", "application/json") .setHeader("Content-Type", "application/json")
.setHeader("Authorization", "Bearer _token") .setHeader("Authorization", "Bearer _token")
.body(XContentHelper.toString(new ToXContent() { .body(Strings.toString((builder, params) -> {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("message", message.body); builder.field("message", message.body);
if (message.format != null) { if (message.format != null) {
builder.field("message_format", message.format.value()); builder.field("message_format", message.format.value());
@ -146,7 +141,6 @@ public class UserAccountTests extends ESTestCase {
builder.field("color", String.valueOf(message.color.value())); builder.field("color", String.valueOf(message.color.value()));
} }
return builder; return builder;
}
})) }))
.build(); .build();
@ -162,9 +156,7 @@ public class UserAccountTests extends ESTestCase {
.path("/v2/room/_r2/notification") .path("/v2/room/_r2/notification")
.setHeader("Content-Type", "application/json") .setHeader("Content-Type", "application/json")
.setHeader("Authorization", "Bearer _token") .setHeader("Authorization", "Bearer _token")
.body(XContentHelper.toString(new ToXContent() { .body(Strings.toString((builder, params) -> {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("message", message.body); builder.field("message", message.body);
if (message.format != null) { if (message.format != null) {
builder.field("message_format", message.format.value()); builder.field("message_format", message.format.value());
@ -176,7 +168,6 @@ public class UserAccountTests extends ESTestCase {
builder.field("color", String.valueOf(message.color.value())); builder.field("color", String.valueOf(message.color.value()));
} }
return builder; return builder;
}
})) }))
.build(); .build();
@ -192,9 +183,7 @@ public class UserAccountTests extends ESTestCase {
.path("/v2/user/_u1/message") .path("/v2/user/_u1/message")
.setHeader("Content-Type", "application/json") .setHeader("Content-Type", "application/json")
.setHeader("Authorization", "Bearer _token") .setHeader("Authorization", "Bearer _token")
.body(XContentHelper.toString(new ToXContent() { .body(Strings.toString((builder, params) -> {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("message", message.body); builder.field("message", message.body);
if (message.format != null) { if (message.format != null) {
builder.field("message_format", message.format.value()); builder.field("message_format", message.format.value());
@ -203,7 +192,6 @@ public class UserAccountTests extends ESTestCase {
builder.field("notify", message.notify); builder.field("notify", message.notify);
} }
return builder; return builder;
}
})) }))
.build(); .build();
@ -219,9 +207,7 @@ public class UserAccountTests extends ESTestCase {
.path("/v2/user/_u2/message") .path("/v2/user/_u2/message")
.setHeader("Content-Type", "application/json") .setHeader("Content-Type", "application/json")
.setHeader("Authorization", "Bearer _token") .setHeader("Authorization", "Bearer _token")
.body(XContentHelper.toString(new ToXContent() { .body(Strings.toString((builder, params) -> {
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("message", message.body); builder.field("message", message.body);
if (message.format != null) { if (message.format != null) {
builder.field("message_format", message.format.value()); builder.field("message_format", message.format.value());
@ -230,7 +216,6 @@ public class UserAccountTests extends ESTestCase {
builder.field("notify", message.notify); builder.field("notify", message.notify);
} }
return builder; return builder;
}
})) }))
.build(); .build();