Skip to content

Commit e1317da

Browse files
committed
Clean up StartBasicResponse (#35688)
This commit removes the parsing code from the PostStartBasicResponse server variant. It also makes the server response implement StatusToXContent which allows us to save a couple of lines of code in the corredponding REST action. Relates to #35547
1 parent 085b9b6 commit e1317da

File tree

4 files changed

+13
-112
lines changed

4 files changed

+13
-112
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/license/StartBasicResponse.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
2424
import org.elasticsearch.common.xcontent.XContentParseException;
2525
import org.elasticsearch.common.xcontent.XContentParser;
26-
import org.elasticsearch.rest.RestStatus;
2726

2827
import java.io.IOException;
2928
import java.util.ArrayList;
@@ -91,26 +90,16 @@ public class StartBasicResponse {
9190
private String acknowledgeMessage;
9291

9392
public enum Status {
94-
GENERATED_BASIC(true, null, RestStatus.OK),
95-
ALREADY_USING_BASIC(false, "Operation failed: Current license is basic.", RestStatus.FORBIDDEN),
96-
NEED_ACKNOWLEDGEMENT(false, "Operation failed: Needs acknowledgement.", RestStatus.OK);
93+
GENERATED_BASIC(true, null),
94+
ALREADY_USING_BASIC(false, "Operation failed: Current license is basic."),
95+
NEED_ACKNOWLEDGEMENT(false, "Operation failed: Needs acknowledgement.");
9796

9897
private final boolean isBasicStarted;
9998
private final String errorMessage;
100-
private final RestStatus restStatus;
10199

102-
Status(boolean isBasicStarted, String errorMessage, RestStatus restStatus) {
100+
Status(boolean isBasicStarted, String errorMessage) {
103101
this.isBasicStarted = isBasicStarted;
104102
this.errorMessage = errorMessage;
105-
this.restStatus = restStatus;
106-
}
107-
108-
String getErrorMessage() {
109-
return errorMessage;
110-
}
111-
112-
boolean isBasicStarted() {
113-
return isBasicStarted;
114103
}
115104

116105
static StartBasicResponse.Status fromErrorMessage(final String errorMessage) {
@@ -126,14 +115,11 @@ static StartBasicResponse.Status fromErrorMessage(final String errorMessage) {
126115

127116
private StartBasicResponse.Status status;
128117

129-
public StartBasicResponse() {
130-
}
131-
132-
StartBasicResponse(StartBasicResponse.Status status) {
118+
private StartBasicResponse(StartBasicResponse.Status status) {
133119
this(status, Collections.emptyMap(), null);
134120
}
135121

136-
StartBasicResponse(StartBasicResponse.Status status,
122+
private StartBasicResponse(StartBasicResponse.Status status,
137123
Map<String, String[]> acknowledgeMessages, String acknowledgeMessage) {
138124
this.status = status;
139125
this.acknowledgeMessages = acknowledgeMessages;
@@ -167,5 +153,4 @@ public Map<String, String[]> getAcknowledgeMessages() {
167153
public static StartBasicResponse fromXContent(XContentParser parser) throws IOException {
168154
return PARSER.parse(parser, null);
169155
}
170-
171156
}

x-pack/plugin/core/src/main/java/org/elasticsearch/license/PostStartBasicResponse.java

Lines changed: 5 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,25 @@
77

88
import org.elasticsearch.action.support.master.AcknowledgedResponse;
99
import org.elasticsearch.common.ParseField;
10-
import org.elasticsearch.common.collect.Tuple;
1110
import org.elasticsearch.common.io.stream.StreamInput;
1211
import org.elasticsearch.common.io.stream.StreamOutput;
13-
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
12+
import org.elasticsearch.common.xcontent.StatusToXContentObject;
1413
import org.elasticsearch.common.xcontent.XContentBuilder;
15-
import org.elasticsearch.common.xcontent.XContentParseException;
16-
import org.elasticsearch.common.xcontent.XContentParser;
1714
import org.elasticsearch.protocol.xpack.common.ProtocolUtils;
1815
import org.elasticsearch.rest.RestStatus;
1916

2017
import java.io.IOException;
21-
import java.util.ArrayList;
2218
import java.util.Collections;
2319
import java.util.HashMap;
24-
import java.util.List;
2520
import java.util.Map;
2621
import java.util.Objects;
2722

28-
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
29-
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
30-
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
31-
32-
public class PostStartBasicResponse extends AcknowledgedResponse {
33-
34-
private static final ConstructingObjectParser<PostStartBasicResponse, Void> PARSER = new ConstructingObjectParser<>(
35-
"start_basic_response", true, (a, v) -> {
36-
boolean basicWasStarted = (Boolean) a[0];
37-
String errorMessage = (String) a[1];
38-
39-
if (basicWasStarted) {
40-
return new PostStartBasicResponse(Status.GENERATED_BASIC);
41-
}
42-
Status status = Status.fromErrorMessage(errorMessage);
43-
@SuppressWarnings("unchecked") Tuple<String, Map<String, String[]>> acknowledgements = (Tuple<String, Map<String, String[]>>) a[2];
44-
return new PostStartBasicResponse(status, acknowledgements.v2(), acknowledgements.v1());
45-
});
23+
public class PostStartBasicResponse extends AcknowledgedResponse implements StatusToXContentObject {
4624

4725
private static final ParseField BASIC_WAS_STARTED_FIELD = new ParseField("basic_was_started");
4826
private static final ParseField ERROR_MESSAGE_FIELD = new ParseField("error_message");
49-
private static final ParseField ACKNOWLEDGE_FIELD = new ParseField("acknowledge");
5027
private static final ParseField MESSAGE_FIELD = new ParseField("message");
5128

52-
static {
53-
PARSER.declareBoolean(constructorArg(), BASIC_WAS_STARTED_FIELD);
54-
PARSER.declareString(optionalConstructorArg(), ERROR_MESSAGE_FIELD);
55-
PARSER.declareObject(optionalConstructorArg(), (parser, v) -> {
56-
Map<String, String[]> acknowledgeMessages = new HashMap<>();
57-
String message = null;
58-
XContentParser.Token token;
59-
String currentFieldName = null;
60-
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
61-
if (token == XContentParser.Token.FIELD_NAME) {
62-
currentFieldName = parser.currentName();
63-
} else {
64-
if (currentFieldName == null) {
65-
throw new XContentParseException(parser.getTokenLocation(), "expected message header or acknowledgement");
66-
}
67-
if (MESSAGE_FIELD.getPreferredName().equals(currentFieldName)) {
68-
ensureExpectedToken(XContentParser.Token.VALUE_STRING, token, parser::getTokenLocation);
69-
message = parser.text();
70-
} else {
71-
if (token != XContentParser.Token.START_ARRAY) {
72-
throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement type");
73-
}
74-
List<String> acknowledgeMessagesList = new ArrayList<>();
75-
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
76-
ensureExpectedToken(XContentParser.Token.VALUE_STRING, token, parser::getTokenLocation);
77-
acknowledgeMessagesList.add(parser.text());
78-
}
79-
acknowledgeMessages.put(currentFieldName, acknowledgeMessagesList.toArray(new String[0]));
80-
}
81-
}
82-
}
83-
return new Tuple<>(message, acknowledgeMessages);
84-
}, ACKNOWLEDGE_FIELD);
85-
}
86-
8729
private Map<String, String[]> acknowledgeMessages;
8830
private String acknowledgeMessage;
8931

@@ -113,16 +55,6 @@ String getErrorMessage() {
11355
RestStatus getRestStatus() {
11456
return restStatus;
11557
}
116-
117-
static Status fromErrorMessage(final String errorMessage) {
118-
final Status[] values = Status.values();
119-
for (Status status : values) {
120-
if (Objects.equals(status.errorMessage, errorMessage)) {
121-
return status;
122-
}
123-
}
124-
throw new IllegalArgumentException("No status for error message ['" + errorMessage + "']");
125-
}
12658
}
12759

12860
private Status status;
@@ -201,8 +133,9 @@ protected void addCustomFields(XContentBuilder builder, Params params) throws IO
201133
}
202134
}
203135

204-
public static PostStartBasicResponse fromXContent(XContentParser parser) throws IOException {
205-
return PARSER.parse(parser, null);
136+
@Override
137+
public RestStatus status() {
138+
return status.restStatus;
206139
}
207140

208141
@Override
@@ -221,5 +154,4 @@ public boolean equals(Object o) {
221154
public int hashCode() {
222155
return Objects.hash(super.hashCode(), status, ProtocolUtils.hashCode(acknowledgeMessages), acknowledgeMessage);
223156
}
224-
225157
}

x-pack/plugin/core/src/main/java/org/elasticsearch/license/RestPostStartBasicLicense.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,9 @@
66
package org.elasticsearch.license;
77

88
import org.elasticsearch.common.settings.Settings;
9-
import org.elasticsearch.common.xcontent.ToXContent;
10-
import org.elasticsearch.common.xcontent.XContentBuilder;
11-
import org.elasticsearch.rest.BytesRestResponse;
129
import org.elasticsearch.rest.RestController;
1310
import org.elasticsearch.rest.RestRequest;
14-
import org.elasticsearch.rest.RestResponse;
15-
import org.elasticsearch.rest.action.RestBuilderListener;
11+
import org.elasticsearch.rest.action.RestStatusToXContentListener;
1612
import org.elasticsearch.xpack.core.XPackClient;
1713
import org.elasticsearch.xpack.core.rest.XPackRestHandler;
1814

@@ -33,14 +29,7 @@ protected RestChannelConsumer doPrepareRequest(RestRequest request, XPackClient
3329
startBasicRequest.acknowledge(request.paramAsBoolean("acknowledge", false));
3430
startBasicRequest.timeout(request.paramAsTime("timeout", startBasicRequest.timeout()));
3531
startBasicRequest.masterNodeTimeout(request.paramAsTime("master_timeout", startBasicRequest.masterNodeTimeout()));
36-
return channel -> client.licensing().postStartBasic(startBasicRequest, new RestBuilderListener<PostStartBasicResponse>(channel) {
37-
@Override
38-
public RestResponse buildResponse(PostStartBasicResponse response, XContentBuilder builder) throws Exception {
39-
PostStartBasicResponse.Status status = response.getStatus();
40-
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
41-
return new BytesRestResponse(status.getRestStatus(), builder);
42-
}
43-
});
32+
return channel -> client.licensing().postStartBasic(startBasicRequest, new RestStatusToXContentListener<>(channel));
4433
}
4534

4635
@Override

x-pack/plugin/core/src/test/java/org/elasticsearch/protocol/xpack/license/StartBasicResponseTests.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ public PostStartBasicResponse convertHlrcToInternal(org.elasticsearch.client.lic
3030
instance.getAcknowledgeMessages(), instance.getAcknowledgeMessage());
3131
}
3232

33-
@Override
34-
protected PostStartBasicResponse doParseInstance(XContentParser parser) throws IOException {
35-
return PostStartBasicResponse.fromXContent(parser);
36-
}
37-
3833
@Override
3934
protected PostStartBasicResponse createBlankInstance() {
4035
return new PostStartBasicResponse();

0 commit comments

Comments
 (0)