Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<PutLicenseResponse, Void> PARSER = new ConstructingObjectParser<>(
"put_license_response", true, (a, v) -> {
boolean acknowledged = (Boolean) a[0];
LicensesStatus licensesStatus = LicensesStatus.fromString((String) a[1]);
@SuppressWarnings("unchecked") Tuple<String, Map<String, String[]>> acknowledgements = (Tuple<String, Map<String, String[]>>) 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<String, String[]> 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<String> 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<String, String[]> acknowledgeMessages;
private String acknowledgeHeader;
Expand Down Expand Up @@ -170,10 +107,6 @@ public String toString() {
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;
Expand All @@ -190,6 +123,4 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(super.hashCode(), status, ProtocolUtils.hashCode(acknowledgeMessages), acknowledgeHeader);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ private static Map<String, String[]> randomAckMessages() {
return ackMessages;
}

@Override
protected PutLicenseResponse doParseInstance(XContentParser parser) throws IOException {
return PutLicenseResponse.fromXContent(parser);
}

@Override
protected PutLicenseResponse createBlankInstance() {
return new PutLicenseResponse();
Expand Down Expand Up @@ -120,5 +115,4 @@ protected PutLicenseResponse mutateInstance(PutLicenseResponse response) {
private LicensesStatus mutateStatus(LicensesStatus status) {
return randomValueOtherThan(status, () -> randomFrom(LicensesStatus.values()));
}

}