|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.client.license; |
| 20 | + |
| 21 | +import org.elasticsearch.common.ParseField; |
| 22 | +import org.elasticsearch.common.collect.Tuple; |
| 23 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 24 | +import org.elasticsearch.common.xcontent.XContentParseException; |
| 25 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 26 | +import org.elasticsearch.rest.RestStatus; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Objects; |
| 35 | + |
| 36 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; |
| 37 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; |
| 38 | +import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; |
| 39 | + |
| 40 | +public class StartBasicResponse { |
| 41 | + |
| 42 | + private static final ConstructingObjectParser<StartBasicResponse, Void> PARSER = new ConstructingObjectParser<>( |
| 43 | + "start_basic_response", true, (a, v) -> { |
| 44 | + boolean basicWasStarted = (Boolean) a[0]; |
| 45 | + String errorMessage = (String) a[1]; |
| 46 | + |
| 47 | + if (basicWasStarted) { |
| 48 | + return new StartBasicResponse(StartBasicResponse.Status.GENERATED_BASIC); |
| 49 | + } |
| 50 | + StartBasicResponse.Status status = StartBasicResponse.Status.fromErrorMessage(errorMessage); |
| 51 | + @SuppressWarnings("unchecked") Tuple<String, Map<String, String[]>> acknowledgements = (Tuple<String, Map<String, String[]>>) a[2]; |
| 52 | + return new StartBasicResponse(status, acknowledgements.v2(), acknowledgements.v1()); |
| 53 | + }); |
| 54 | + |
| 55 | + static { |
| 56 | + PARSER.declareBoolean(constructorArg(), new ParseField("basic_was_started")); |
| 57 | + PARSER.declareString(optionalConstructorArg(), new ParseField("error_message")); |
| 58 | + PARSER.declareObject(optionalConstructorArg(), (parser, v) -> { |
| 59 | + Map<String, String[]> acknowledgeMessages = new HashMap<>(); |
| 60 | + String message = null; |
| 61 | + XContentParser.Token token; |
| 62 | + String currentFieldName = null; |
| 63 | + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { |
| 64 | + if (token == XContentParser.Token.FIELD_NAME) { |
| 65 | + currentFieldName = parser.currentName(); |
| 66 | + } else { |
| 67 | + if (currentFieldName == null) { |
| 68 | + throw new XContentParseException(parser.getTokenLocation(), "expected message header or acknowledgement"); |
| 69 | + } |
| 70 | + if (new ParseField("message").getPreferredName().equals(currentFieldName)) { |
| 71 | + ensureExpectedToken(XContentParser.Token.VALUE_STRING, token, parser::getTokenLocation); |
| 72 | + message = parser.text(); |
| 73 | + } else { |
| 74 | + if (token != XContentParser.Token.START_ARRAY) { |
| 75 | + throw new XContentParseException(parser.getTokenLocation(), "unexpected acknowledgement type"); |
| 76 | + } |
| 77 | + List<String> acknowledgeMessagesList = new ArrayList<>(); |
| 78 | + while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { |
| 79 | + ensureExpectedToken(XContentParser.Token.VALUE_STRING, token, parser::getTokenLocation); |
| 80 | + acknowledgeMessagesList.add(parser.text()); |
| 81 | + } |
| 82 | + acknowledgeMessages.put(currentFieldName, acknowledgeMessagesList.toArray(new String[0])); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + return new Tuple<>(message, acknowledgeMessages); |
| 87 | + }, |
| 88 | + new ParseField("acknowledge")); |
| 89 | + } |
| 90 | + |
| 91 | + private Map<String, String[]> acknowledgeMessages; |
| 92 | + private String acknowledgeMessage; |
| 93 | + |
| 94 | + enum Status { |
| 95 | + GENERATED_BASIC(true, null, RestStatus.OK), |
| 96 | + ALREADY_USING_BASIC(false, "Operation failed: Current license is basic.", RestStatus.FORBIDDEN), |
| 97 | + NEED_ACKNOWLEDGEMENT(false, "Operation failed: Needs acknowledgement.", RestStatus.OK); |
| 98 | + |
| 99 | + private final boolean isBasicStarted; |
| 100 | + private final String errorMessage; |
| 101 | + private final RestStatus restStatus; |
| 102 | + |
| 103 | + Status(boolean isBasicStarted, String errorMessage, RestStatus restStatus) { |
| 104 | + this.isBasicStarted = isBasicStarted; |
| 105 | + this.errorMessage = errorMessage; |
| 106 | + this.restStatus = restStatus; |
| 107 | + } |
| 108 | + |
| 109 | + String getErrorMessage() { |
| 110 | + return errorMessage; |
| 111 | + } |
| 112 | + |
| 113 | + boolean isBasicStarted() { |
| 114 | + return isBasicStarted; |
| 115 | + } |
| 116 | + |
| 117 | + static StartBasicResponse.Status fromErrorMessage(final String errorMessage) { |
| 118 | + final StartBasicResponse.Status[] values = StartBasicResponse.Status.values(); |
| 119 | + for (StartBasicResponse.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 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private StartBasicResponse.Status status; |
| 129 | + |
| 130 | + public StartBasicResponse() { |
| 131 | + } |
| 132 | + |
| 133 | + StartBasicResponse(StartBasicResponse.Status status) { |
| 134 | + this(status, Collections.emptyMap(), null); |
| 135 | + } |
| 136 | + |
| 137 | + StartBasicResponse(StartBasicResponse.Status status, |
| 138 | + Map<String, String[]> acknowledgeMessages, String acknowledgeMessage) { |
| 139 | + this.status = status; |
| 140 | + this.acknowledgeMessages = acknowledgeMessages; |
| 141 | + this.acknowledgeMessage = acknowledgeMessage; |
| 142 | + } |
| 143 | + |
| 144 | + public boolean isAcknowledged() { |
| 145 | + return status != StartBasicResponse.Status.NEED_ACKNOWLEDGEMENT; |
| 146 | + } |
| 147 | + |
| 148 | + public boolean isBasicStarted() { |
| 149 | + return status.isBasicStarted; |
| 150 | + } |
| 151 | + |
| 152 | + public String getErrorMessage() { |
| 153 | + return status.errorMessage; |
| 154 | + } |
| 155 | + |
| 156 | + public String getAcknowledgeMessage() { |
| 157 | + return acknowledgeMessage; |
| 158 | + } |
| 159 | + |
| 160 | + public Map<String, String[]> getAcknowledgeMessages() { |
| 161 | + return acknowledgeMessages; |
| 162 | + } |
| 163 | + |
| 164 | + public static StartBasicResponse fromXContent(XContentParser parser) throws IOException { |
| 165 | + return PARSER.parse(parser, null); |
| 166 | + } |
| 167 | + |
| 168 | +} |
0 commit comments