From de6c275dcab4cd3d5b6dcf159812ab610c9eade8 Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Wed, 23 Aug 2017 08:17:28 +0100 Subject: [PATCH] Refactor/to x content fragments2 (elastic/x-pack-elasticsearch#2329) * Moves more classes over to ToXContentObject/Fragment * Removes ToXContentToBytes * Removes ToXContent from Enums * review comment fix * slight change to use XContantHelper Original commit: elastic/x-pack-elasticsearch@0f2d3f328b3cb39f2e8f09ec6055f1376981c791 --- .../xpack/common/http/HttpContentType.java | 10 +-------- .../xpack/common/http/HttpMethod.java | 12 +++------- .../xpack/common/http/HttpRequest.java | 4 ++-- .../common/http/HttpRequestTemplate.java | 5 +++-- .../xpack/common/http/Scheme.java | 12 +++------- .../common/http/auth/basic/BasicAuth.java | 2 +- .../xpack/common/secret/Secret.java | 13 +++++------ .../xpack/ml/job/config/JobState.java | 15 +++---------- .../xpack/ml/job/config/JobTaskStatus.java | 2 +- .../xpack/notification/email/Email.java | 22 ++++++------------- .../xpack/notification/email/Profile.java | 15 ++++--------- .../attachment/EmailAttachmentParser.java | 4 ++-- .../notification/hipchat/HipChatAccount.java | 9 +------- .../notification/hipchat/HipChatMessage.java | 21 +++++------------- .../persistent/PersistentTaskParams.java | 4 ++-- .../xpack/watcher/actions/Action.java | 8 +++---- .../xpack/watcher/actions/ActionWrapper.java | 2 +- .../watcher/actions/email/EmailAction.java | 2 +- .../actions/logging/LoggingAction.java | 4 ++-- .../watcher/actions/logging/LoggingLevel.java | 11 +++------- .../watcher/client/WatchSourceBuilder.java | 20 +++++++++++++++-- .../xpack/watcher/input/http/HttpInput.java | 4 ++-- .../xpack/watcher/transform/Transform.java | 3 +-- .../trigger/schedule/support/DayOfWeek.java | 10 +-------- .../trigger/schedule/support/Month.java | 10 +-------- .../trigger/schedule/support/Times.java | 4 ++-- .../xpack/watcher/watch/Payload.java | 3 +-- .../hipchat/HipChatMessageTests.java | 2 +- .../watcher/support/WatcherUtilsTests.java | 3 ++- .../transform/chain/ChainTransformTests.java | 2 +- 30 files changed, 83 insertions(+), 155 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpContentType.java b/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpContentType.java index 1beb5ab58c0..d39b1e0a081 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpContentType.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpContentType.java @@ -5,16 +5,13 @@ */ package org.elasticsearch.xpack.common.http; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentType; -import java.io.IOException; import java.util.Locale; import static org.elasticsearch.xpack.watcher.support.Exceptions.illegalArgument; -public enum HttpContentType implements ToXContent { +public enum HttpContentType { JSON() { @Override @@ -39,11 +36,6 @@ public enum HttpContentType implements ToXContent { public abstract XContentType contentType(); - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(id()); - } - @Override public String toString() { return id(); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpMethod.java b/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpMethod.java index fb67b711538..81f5b53444a 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpMethod.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpMethod.java @@ -5,13 +5,9 @@ */ package org.elasticsearch.xpack.common.http; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; - -import java.io.IOException; import java.util.Locale; -public enum HttpMethod implements ToXContent { +public enum HttpMethod { HEAD("HEAD"), GET("GET"), @@ -47,9 +43,7 @@ public enum HttpMethod implements ToXContent { } } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(name().toLowerCase(Locale.ROOT)); + public String value() { + return name().toLowerCase(Locale.ROOT); } } 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 f5b7fd52935..4178ae16009 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 @@ -140,8 +140,8 @@ public class HttpRequest implements ToXContentObject { builder.startObject(); builder.field(Field.HOST.getPreferredName(), host); builder.field(Field.PORT.getPreferredName(), port); - builder.field(Field.SCHEME.getPreferredName(), scheme, params); - builder.field(Field.METHOD.getPreferredName(), method, params); + builder.field(Field.SCHEME.getPreferredName(), scheme.value()); + builder.field(Field.METHOD.getPreferredName(), method.value()); if (path != null) { builder.field(Field.PATH.getPreferredName(), path); } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpRequestTemplate.java b/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpRequestTemplate.java index 4ff248faeb2..d8386e6161d 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpRequestTemplate.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/common/http/HttpRequestTemplate.java @@ -6,6 +6,7 @@ package org.elasticsearch.xpack.common.http; import io.netty.handler.codec.http.HttpHeaders; + import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; @@ -162,10 +163,10 @@ public class HttpRequestTemplate implements ToXContentObject { @Override public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { builder.startObject(); - builder.field(HttpRequest.Field.SCHEME.getPreferredName(), scheme, params); + builder.field(HttpRequest.Field.SCHEME.getPreferredName(), scheme.value()); builder.field(HttpRequest.Field.HOST.getPreferredName(), host); builder.field(HttpRequest.Field.PORT.getPreferredName(), port); - builder.field(HttpRequest.Field.METHOD.getPreferredName(), method, params); + builder.field(HttpRequest.Field.METHOD.getPreferredName(), method.value()); if (path != null) { builder.field(HttpRequest.Field.PATH.getPreferredName(), path, params); } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/common/http/Scheme.java b/plugin/src/main/java/org/elasticsearch/xpack/common/http/Scheme.java index 11949873674..6d74361aad0 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/common/http/Scheme.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/common/http/Scheme.java @@ -5,13 +5,9 @@ */ package org.elasticsearch.xpack.common.http; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; - -import java.io.IOException; import java.util.Locale; -public enum Scheme implements ToXContent { +public enum Scheme { HTTP("http", 80), HTTPS("https", 443); @@ -44,9 +40,7 @@ public enum Scheme implements ToXContent { } } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(name().toLowerCase(Locale.ROOT)); + public String value() { + return name().toLowerCase(Locale.ROOT); } } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/common/http/auth/basic/BasicAuth.java b/plugin/src/main/java/org/elasticsearch/xpack/common/http/auth/basic/BasicAuth.java index d414a5bb316..5a4b66efb05 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/common/http/auth/basic/BasicAuth.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/common/http/auth/basic/BasicAuth.java @@ -64,7 +64,7 @@ public class BasicAuth implements HttpAuth { builder.startObject(); builder.field(Field.USERNAME.getPreferredName(), username); if (!WatcherParams.hideSecrets(params)) { - builder.field(Field.PASSWORD.getPreferredName(), password, params); + builder.field(Field.PASSWORD.getPreferredName(), password.value()); } return builder.endObject(); } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/common/secret/Secret.java b/plugin/src/main/java/org/elasticsearch/xpack/common/secret/Secret.java index b6e0adf57bc..1deb9931ad8 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/common/secret/Secret.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/common/secret/Secret.java @@ -5,14 +5,12 @@ */ package org.elasticsearch.xpack.common.secret; +import org.elasticsearch.xpack.security.crypto.CryptoService; + import java.io.IOException; import java.util.Arrays; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.xpack.security.crypto.CryptoService; - -public class Secret implements ToXContent { +public class Secret { protected final char[] text; @@ -27,9 +25,8 @@ public class Secret implements ToXContent { return service.decrypt(text); } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(new String(text)); + public String value() throws IOException { + return new String(text); } @Override diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/JobState.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/JobState.java index 850bf623744..7b0aba05aca 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/JobState.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/JobState.java @@ -9,8 +9,6 @@ import org.elasticsearch.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; import java.io.IOException; import java.util.Arrays; @@ -21,7 +19,7 @@ import java.util.Locale; * When a job is created it is initialised in to the state closed * i.e. it is not running. */ -public enum JobState implements ToXContent, Writeable { +public enum JobState implements Writeable { CLOSING, CLOSED, OPENED, FAILED, OPENING; @@ -43,15 +41,8 @@ public enum JobState implements ToXContent, Writeable { out.writeEnum(state); } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.value(name().toLowerCase(Locale.ROOT)); - return builder; - } - - @Override - public boolean isFragment() { - return true; + public String value() { + return name().toLowerCase(Locale.ROOT); } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/JobTaskStatus.java b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/JobTaskStatus.java index 97c4804fe4e..6eb40486152 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/JobTaskStatus.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ml/job/config/JobTaskStatus.java @@ -90,7 +90,7 @@ public class JobTaskStatus implements Task.Status { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); - builder.field(STATE.getPreferredName(), state, params); + builder.field(STATE.getPreferredName(), state.value()); builder.field(ALLOCATION_ID.getPreferredName(), allocationId); builder.endObject(); return builder; diff --git a/plugin/src/main/java/org/elasticsearch/xpack/notification/email/Email.java b/plugin/src/main/java/org/elasticsearch/xpack/notification/email/Email.java index a16d554cfc9..7db00290815 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/notification/email/Email.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/notification/email/Email.java @@ -8,7 +8,6 @@ package org.elasticsearch.xpack.notification.email; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -119,13 +118,13 @@ public class Email implements ToXContentObject { builder.startObject(); builder.field(Field.ID.getPreferredName(), id); if (from != null) { - builder.field(Field.FROM.getPreferredName(), from, params); + builder.field(Field.FROM.getPreferredName(), from.toUnicodeString()); } if (replyTo != null) { builder.field(Field.REPLY_TO.getPreferredName(), replyTo, params); } if (priority != null) { - builder.field(Field.PRIORITY.getPreferredName(), priority, params); + builder.field(Field.PRIORITY.getPreferredName(), priority.value()); } builder.field(Field.SENT_DATE.getPreferredName(), sentDate); if (to != null) { @@ -362,7 +361,7 @@ public class Email implements ToXContentObject { } - public enum Priority implements ToXContent { + public enum Priority { HIGHEST(1), HIGH(2), @@ -382,10 +381,8 @@ public class Email implements ToXContentObject { message.setHeader(HEADER, String.valueOf(value)); } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(name().toLowerCase(Locale.ROOT)); + public String value() { + return name().toLowerCase(Locale.ROOT); } public static Priority resolve(String name) { @@ -420,7 +417,7 @@ public class Email implements ToXContentObject { } } - public static class Address extends javax.mail.internet.InternetAddress implements ToXContent { + public static class Address extends javax.mail.internet.InternetAddress { public static final ParseField ADDRESS_NAME_FIELD = new ParseField("name"); public static final ParseField ADDRESS_EMAIL_FIELD = new ParseField("email"); @@ -433,11 +430,6 @@ public class Email implements ToXContentObject { super(address, personal, StandardCharsets.UTF_8.name()); } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(toUnicodeString()); - } - public static Address parse(String field, XContentParser.Token token, XContentParser parser) throws IOException { if (token == XContentParser.Token.VALUE_STRING) { String text = parser.text(); @@ -523,7 +515,7 @@ public class Email implements ToXContentObject { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startArray(); for (Address address : addresses) { - address.toXContent(builder, params); + builder.value(address.toUnicodeString()); } return builder.endArray(); } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/notification/email/Profile.java b/plugin/src/main/java/org/elasticsearch/xpack/notification/email/Profile.java index 9c31063bded..6bbde3ba881 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/notification/email/Profile.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/notification/email/Profile.java @@ -5,8 +5,9 @@ */ package org.elasticsearch.xpack.notification.email; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Locale; import javax.mail.Message; import javax.mail.MessagingException; @@ -14,15 +15,12 @@ import javax.mail.Session; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.Locale; /** * A profile of an email client, can be seen as a strategy to emulate a real world email client * (different clients potentially support different mime message structures) */ -public enum Profile implements ToXContent { +public enum Profile { STANDARD() { @@ -172,11 +170,6 @@ public enum Profile implements ToXContent { } } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(name().toLowerCase(Locale.ROOT)); - } - static MimeMessage createCommon(Email email, Session session) throws MessagingException { MimeMessage message = new MimeMessage(session); message.setHeader(MESSAGE_ID_HEADER, email.id); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/notification/email/attachment/EmailAttachmentParser.java b/plugin/src/main/java/org/elasticsearch/xpack/notification/email/attachment/EmailAttachmentParser.java index c7052b16dc5..8544398062e 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/notification/email/attachment/EmailAttachmentParser.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/notification/email/attachment/EmailAttachmentParser.java @@ -5,7 +5,7 @@ */ package org.elasticsearch.xpack.notification.email.attachment; -import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.xpack.notification.email.Attachment; import org.elasticsearch.xpack.watcher.execution.WatchExecutionContext; @@ -19,7 +19,7 @@ import java.io.IOException; */ public interface EmailAttachmentParser { - interface EmailAttachment extends ToXContent { + interface EmailAttachment extends ToXContentFragment { /** * @return A type to identify the email attachment, same as the parser identifier */ diff --git a/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/HipChatAccount.java b/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/HipChatAccount.java index baeb65b594f..6c6147ad24b 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/HipChatAccount.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/HipChatAccount.java @@ -9,8 +9,6 @@ import org.apache.logging.log4j.Logger; import org.elasticsearch.common.Nullable; 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.XContentParser; import org.elasticsearch.xpack.common.http.HttpClient; import org.elasticsearch.xpack.common.http.HttpProxy; @@ -60,7 +58,7 @@ public abstract class HipChatAccount { public abstract SentMessages send(HipChatMessage message, @Nullable HttpProxy proxy); - public enum Profile implements ToXContent { + public enum Profile { V1() { @Override @@ -87,11 +85,6 @@ public abstract class HipChatAccount { abstract HipChatAccount createAccount(String name, Settings settings, HipChatServer defaultServer, HttpClient httpClient, Logger logger); - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(name().toLowerCase(Locale.ROOT)); - } - public String value() { return name().toLowerCase(Locale.ROOT); } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/HipChatMessage.java b/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/HipChatMessage.java index d7f9bf8704b..dd80aee381d 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/HipChatMessage.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/notification/hipchat/HipChatMessage.java @@ -9,7 +9,6 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; @@ -124,10 +123,10 @@ public class HipChatMessage implements ToXContentObject { } builder.field(Field.BODY.getPreferredName(), body); if (format != null) { - builder.field(Field.FORMAT.getPreferredName(), format, params); + builder.field(Field.FORMAT.getPreferredName(), format.value()); } if (color != null) { - builder.field(Field.COLOR.getPreferredName(), color, params); + builder.field(Field.COLOR.getPreferredName(), color.value()); } if (notify != null) { builder.field(Field.NOTIFY.getPreferredName(), notify); @@ -224,7 +223,7 @@ public class HipChatMessage implements ToXContentObject { } builder.field(Field.BODY.getPreferredName(), body, params); if (format != null) { - builder.field(Field.FORMAT.getPreferredName(), format, params); + builder.field(Field.FORMAT.getPreferredName(), format.value()); } if (color != null) { builder.field(Field.COLOR.getPreferredName(), color, params); @@ -390,7 +389,7 @@ public class HipChatMessage implements ToXContentObject { } - public enum Color implements ToXContent { + public enum Color { YELLOW, GREEN, RED, PURPLE, GRAY, RANDOM; private final TextTemplate template = new TextTemplate(name()); @@ -399,11 +398,6 @@ public class HipChatMessage implements ToXContentObject { return template; } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(name().toLowerCase(Locale.ROOT)); - } - public String value() { return name().toLowerCase(Locale.ROOT); } @@ -433,7 +427,7 @@ public class HipChatMessage implements ToXContentObject { } } - public enum Format implements ToXContent { + public enum Format { TEXT, HTML; @@ -444,11 +438,6 @@ public class HipChatMessage implements ToXContentObject { return template; } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(name().toLowerCase(Locale.ROOT)); - } - public String value() { return name().toLowerCase(Locale.ROOT); } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/persistent/PersistentTaskParams.java b/plugin/src/main/java/org/elasticsearch/xpack/persistent/PersistentTaskParams.java index 478bbadfc26..912252b5c70 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/persistent/PersistentTaskParams.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/persistent/PersistentTaskParams.java @@ -6,11 +6,11 @@ package org.elasticsearch.xpack.persistent; import org.elasticsearch.common.io.stream.NamedWriteable; -import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.ToXContentObject; /** * Parameters used to start persistent task */ -public interface PersistentTaskParams extends NamedWriteable, ToXContent { +public interface PersistentTaskParams extends NamedWriteable, ToXContentObject { } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/Action.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/Action.java index 5d8e8502bdb..f50803a06de 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/Action.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/Action.java @@ -8,7 +8,6 @@ package org.elasticsearch.xpack.watcher.actions; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.logging.LoggerMessageFormat; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContentFragment; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -22,7 +21,7 @@ public interface Action extends ToXContentObject { abstract class Result implements ToXContentFragment { - public enum Status implements ToXContent { + public enum Status { SUCCESS, FAILURE, PARTIAL_FAILURE, @@ -31,9 +30,8 @@ public interface Action extends ToXContentObject { CONDITION_FAILED, SIMULATED; - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(name().toLowerCase(Locale.ROOT)); + public String value() { + return name().toLowerCase(Locale.ROOT); } } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionWrapper.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionWrapper.java index ace8d7cb947..61773a634ba 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionWrapper.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/ActionWrapper.java @@ -296,7 +296,7 @@ public class ActionWrapper implements ToXContentObject { builder.startObject(); builder.field(Field.ID.getPreferredName(), id); builder.field(Field.TYPE.getPreferredName(), action.type()); - builder.field(Field.STATUS.getPreferredName(), action.status(), params); + builder.field(Field.STATUS.getPreferredName(), action.status().value()); if (condition != null) { builder.field(Watch.Field.CONDITION.getPreferredName(), condition, params); } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailAction.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailAction.java index 992a2ae3347..f05df948d52 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailAction.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/email/EmailAction.java @@ -105,7 +105,7 @@ public class EmailAction implements Action { if (auth != null) { builder.field(Field.USER.getPreferredName(), auth.user()); if (WatcherParams.hideSecrets(params) == false) { - builder.field(Field.PASSWORD.getPreferredName(), auth.password(), params); + builder.field(Field.PASSWORD.getPreferredName(), auth.password().value()); } } if (profile != null) { diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingAction.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingAction.java index 31601197858..ae2c07d36f6 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingAction.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingAction.java @@ -10,8 +10,8 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.xpack.watcher.actions.Action; import org.elasticsearch.xpack.common.text.TextTemplate; +import org.elasticsearch.xpack.watcher.actions.Action; import java.io.IOException; import java.util.Locale; @@ -61,7 +61,7 @@ public class LoggingAction implements Action { if (category != null) { builder.field(Field.CATEGORY.getPreferredName(), category); } - builder.field(Field.LEVEL.getPreferredName(), level, params); + builder.field(Field.LEVEL.getPreferredName(), level.value()); builder.field(Field.TEXT.getPreferredName(), text, params); return builder.endObject(); } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingLevel.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingLevel.java index 282d4c52629..7d40cdc8afa 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingLevel.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/actions/logging/LoggingLevel.java @@ -7,13 +7,10 @@ package org.elasticsearch.xpack.watcher.actions.logging; import org.apache.logging.log4j.Logger; import org.elasticsearch.common.SuppressLoggerChecks; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import java.io.IOException; import java.util.Locale; -public enum LoggingLevel implements ToXContent { +public enum LoggingLevel { ERROR() { @Override @@ -53,9 +50,7 @@ public enum LoggingLevel implements ToXContent { abstract void log(Logger logger, String text); - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(name().toLowerCase(Locale.ROOT)); + public String value() { + return name().toLowerCase(Locale.ROOT); } } diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/client/WatchSourceBuilder.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/client/WatchSourceBuilder.java index 53ffe2e9582..10ac2d3549d 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/client/WatchSourceBuilder.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/client/WatchSourceBuilder.java @@ -5,12 +5,15 @@ */ package org.elasticsearch.xpack.watcher.client; -import org.elasticsearch.action.support.ToXContentToBytes; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Nullable; +import org.elasticsearch.common.bytes.BytesReference; 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.XContentType; import org.elasticsearch.xpack.watcher.actions.Action; import org.elasticsearch.xpack.watcher.actions.throttler.Throttler; import org.elasticsearch.xpack.watcher.condition.AlwaysCondition; @@ -29,7 +32,7 @@ import java.util.Map; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; -public class WatchSourceBuilder extends ToXContentToBytes implements ToXContentObject { +public class WatchSourceBuilder implements ToXContentObject { private Trigger trigger; private Input input = NoneInput.INSTANCE; @@ -165,6 +168,19 @@ public class WatchSourceBuilder extends ToXContentToBytes implements ToXContentO return builder.endObject(); } + /** + * Returns a {@link org.elasticsearch.common.bytes.BytesReference} + * containing the {@link ToXContent} output in binary format. Builds the + * request as the provided contentType + */ + public final BytesReference buildAsBytes(XContentType contentType) { + try { + return XContentHelper.toXContent(this, contentType, false); + } catch (Exception e) { + throw new ElasticsearchException("Failed to build ToXContent", e); + } + } + static class TransformedAction implements ToXContentObject { private final String id; diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/input/http/HttpInput.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/input/http/HttpInput.java index 24199d192d8..ef6c8344a0b 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/input/http/HttpInput.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/input/http/HttpInput.java @@ -10,10 +10,10 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.xpack.watcher.input.Input; import org.elasticsearch.xpack.common.http.HttpContentType; import org.elasticsearch.xpack.common.http.HttpRequest; import org.elasticsearch.xpack.common.http.HttpRequestTemplate; +import org.elasticsearch.xpack.watcher.input.Input; import org.elasticsearch.xpack.watcher.watch.Payload; import java.io.IOException; @@ -64,7 +64,7 @@ public class HttpInput implements Input { builder.field(Field.EXTRACT.getPreferredName(), extractKeys); } if (expectedResponseXContentType != null) { - builder.field(Field.RESPONSE_CONTENT_TYPE.getPreferredName(), expectedResponseXContentType, params); + builder.field(Field.RESPONSE_CONTENT_TYPE.getPreferredName(), expectedResponseXContentType.id()); } builder.endObject(); return builder; diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/transform/Transform.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/transform/Transform.java index 6f4378231af..2e2ffe8c9c2 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/transform/Transform.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/transform/Transform.java @@ -8,7 +8,6 @@ package org.elasticsearch.xpack.watcher.transform; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.xpack.watcher.watch.Payload; @@ -16,7 +15,7 @@ import org.elasticsearch.xpack.watcher.watch.Payload; import java.io.IOException; import java.util.Locale; -public interface Transform extends ToXContent { +public interface Transform extends ToXContentObject { ParseField TRANSFORM = new ParseField("transform"); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/DayOfWeek.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/DayOfWeek.java index 514e3baf74c..db0d2108def 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/DayOfWeek.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/DayOfWeek.java @@ -6,14 +6,11 @@ package org.elasticsearch.xpack.watcher.trigger.schedule.support; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import java.io.IOException; import java.util.EnumSet; import java.util.Locale; -public enum DayOfWeek implements ToXContent { +public enum DayOfWeek { SUNDAY("SUN"), MONDAY("MON"), @@ -29,11 +26,6 @@ public enum DayOfWeek implements ToXContent { this.cronKey = cronKey; } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(name().toLowerCase(Locale.ROOT)); - } - public static String cronPart(EnumSet days) { StringBuilder sb = new StringBuilder(); for (DayOfWeek day : days) { diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/Month.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/Month.java index a68b2d5fec0..d683cc3cd00 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/Month.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/Month.java @@ -6,14 +6,11 @@ package org.elasticsearch.xpack.watcher.trigger.schedule.support; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import java.io.IOException; import java.util.EnumSet; import java.util.Locale; -public enum Month implements ToXContent { +public enum Month { JANUARY("JAN"), FEBRUARY("FEB"), @@ -34,11 +31,6 @@ public enum Month implements ToXContent { this.cronKey = cronKey; } - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.value(name().toLowerCase(Locale.ROOT)); - } - public static String cronPart(EnumSet days) { StringBuilder sb = new StringBuilder(); for (Month day : days) { diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/Times.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/Times.java index e4e4957dde1..75d3b53d7eb 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/Times.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/support/Times.java @@ -6,9 +6,9 @@ package org.elasticsearch.xpack.watcher.trigger.schedule.support; import org.elasticsearch.common.ParseField; -import org.elasticsearch.common.xcontent.ToXContent; +import org.elasticsearch.common.xcontent.ToXContentObject; -public interface Times extends ToXContent { +public interface Times extends ToXContentObject { ParseField MONTH_FIELD = new ParseField("in", "month"); ParseField DAY_FIELD = new ParseField("on", "day"); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/watch/Payload.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/watch/Payload.java index d53bb24bae6..d697f7c87d8 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/watch/Payload.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/watch/Payload.java @@ -6,7 +6,6 @@ package org.elasticsearch.xpack.watcher.watch; import org.elasticsearch.common.collect.MapBuilder; -import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -18,7 +17,7 @@ import java.util.Objects; import static org.elasticsearch.xpack.watcher.support.WatcherUtils.responseToData; -public interface Payload extends ToXContent { +public interface Payload extends ToXContentObject { Simple EMPTY = new Simple(Collections.emptyMap()); diff --git a/plugin/src/test/java/org/elasticsearch/xpack/notification/hipchat/HipChatMessageTests.java b/plugin/src/test/java/org/elasticsearch/xpack/notification/hipchat/HipChatMessageTests.java index 3eb4d47452e..5708a66e938 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/notification/hipchat/HipChatMessageTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/notification/hipchat/HipChatMessageTests.java @@ -195,7 +195,7 @@ public class HipChatMessageTests extends ESTestCase { HipChatMessage.Format format = null; if (randomBoolean()) { format = randomFrom(HipChatMessage.Format.values()); - jsonBuilder.field("format", format, ToXContent.EMPTY_PARAMS); + jsonBuilder.field("format", format.value()); } Boolean notify = null; if (randomBoolean()) { diff --git a/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherUtilsTests.java b/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherUtilsTests.java index a9ae8359bec..99b1315c857 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherUtilsTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/watcher/support/WatcherUtilsTests.java @@ -14,6 +14,7 @@ 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.index.query.QueryBuilders; @@ -192,7 +193,7 @@ public class WatcherUtilsTests extends ESTestCase { SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery()).size(11); XContentBuilder searchSourceJsonBuilder = jsonBuilder(); searchSourceBuilder.toXContent(searchSourceJsonBuilder, ToXContent.EMPTY_PARAMS); - source = searchSourceBuilder.buildAsBytes(XContentType.JSON); + source = XContentHelper.toXContent(searchSourceBuilder, XContentType.JSON, false); builder.rawField("body", source); } Script template = null; diff --git a/plugin/src/test/java/org/elasticsearch/xpack/watcher/transform/chain/ChainTransformTests.java b/plugin/src/test/java/org/elasticsearch/xpack/watcher/transform/chain/ChainTransformTests.java index 5258177d844..b57745d147b 100644 --- a/plugin/src/test/java/org/elasticsearch/xpack/watcher/transform/chain/ChainTransformTests.java +++ b/plugin/src/test/java/org/elasticsearch/xpack/watcher/transform/chain/ChainTransformTests.java @@ -244,7 +244,7 @@ public class ChainTransformTests extends ESTestCase { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder.startObject().endArray(); + return builder.startObject().endObject(); } }