From 65d8fdf3da084911890c9d03e1d3556c913c5028 Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Wed, 21 Nov 2018 10:21:45 +0100 Subject: [PATCH] Clean up PutLicenseResponse (#35689) This commit removes the parsing code from the PutLicenseResponse server variant, and the toXContent portion from the corresponding client variant. Relates to #35547 --- .../xpack/license/PutLicenseResponse.java | 69 ------------------- .../license/PutLicenseResponseTests.java | 6 -- 2 files changed, 75 deletions(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponse.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponse.java index 206c5a3b383..1bf4a78cf46 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponse.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponse.java @@ -6,83 +6,20 @@ package org.elasticsearch.protocol.xpack.license; import org.elasticsearch.action.support.master.AcknowledgedResponse; -import org.elasticsearch.common.ParseField; import org.elasticsearch.common.Strings; -import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.ConstructingObjectParser; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentParseException; -import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.protocol.xpack.common.ProtocolUtils; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; -import java.util.List; import java.util.Map; import java.util.Objects; -import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; -import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; - public class PutLicenseResponse extends AcknowledgedResponse { - private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( - "put_license_response", true, (a, v) -> { - boolean acknowledged = (Boolean) a[0]; - LicensesStatus licensesStatus = LicensesStatus.fromString((String) a[1]); - @SuppressWarnings("unchecked") Tuple> acknowledgements = (Tuple>) a[2]; - if (acknowledgements == null) { - return new PutLicenseResponse(acknowledged, licensesStatus); - } else { - return new PutLicenseResponse(acknowledged, licensesStatus, acknowledgements.v1(), acknowledgements.v2()); - } - - }); - - static { - PARSER.declareBoolean(constructorArg(), new ParseField("acknowledged")); - PARSER.declareString(constructorArg(), new ParseField("license_status")); - PARSER.declareObject(optionalConstructorArg(), (parser, v) -> { - Map acknowledgeMessages = new HashMap<>(); - String message = null; - XContentParser.Token token; - String currentFieldName = null; - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentFieldName = parser.currentName(); - } else { - if (currentFieldName == null) { - throw new XContentParseException(parser.getTokenLocation(), "expected message header or acknowledgement"); - } - if ("message".equals(currentFieldName)) { - if (token != XContentParser.Token.VALUE_STRING) { - throw new XContentParseException(parser.getTokenLocation(), "unexpected message header type"); - } - message = parser.text(); - } else { - if (token != XContentParser.Token.START_ARRAY) { - throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement type"); - } - List acknowledgeMessagesList = new ArrayList<>(); - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - if (token != XContentParser.Token.VALUE_STRING) { - throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement text"); - } - acknowledgeMessagesList.add(parser.text()); - } - acknowledgeMessages.put(currentFieldName, acknowledgeMessagesList.toArray(new String[0])); - } - } - } - return new Tuple<>(message, acknowledgeMessages); - }, - new ParseField("acknowledge")); - } - private LicensesStatus status; private Map acknowledgeMessages; private String acknowledgeHeader; @@ -170,10 +107,6 @@ public class PutLicenseResponse extends AcknowledgedResponse { return Strings.toString(this, true, true); } - public static PutLicenseResponse fromXContent(XContentParser parser) throws IOException { - return PARSER.parse(parser, null); - } - @Override public boolean equals(Object o) { if (this == o) return true; @@ -190,6 +123,4 @@ public class PutLicenseResponse extends AcknowledgedResponse { public int hashCode() { return Objects.hash(super.hashCode(), status, ProtocolUtils.hashCode(acknowledgeMessages), acknowledgeHeader); } - - } diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java index 87ba4324ec1..4480d925bed 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/PutLicenseResponseTests.java @@ -76,11 +76,6 @@ public class PutLicenseResponseTests extends return ackMessages; } - @Override - protected PutLicenseResponse doParseInstance(XContentParser parser) throws IOException { - return PutLicenseResponse.fromXContent(parser); - } - @Override protected PutLicenseResponse createBlankInstance() { return new PutLicenseResponse(); @@ -120,5 +115,4 @@ public class PutLicenseResponseTests extends private LicensesStatus mutateStatus(LicensesStatus status) { return randomValueOtherThan(status, () -> randomFrom(LicensesStatus.values())); } - }