|
@@ -19,11 +19,25 @@
|
|
|
|
|
|
package org.elasticsearch.client;
|
|
|
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
import org.elasticsearch.action.ActionListener;
|
|
|
+import org.elasticsearch.common.Strings;
|
|
|
+import org.elasticsearch.common.io.Streams;
|
|
|
+import org.elasticsearch.common.xcontent.DeprecationHandler;
|
|
|
+import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
|
|
+import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
+import org.elasticsearch.common.xcontent.XContentFactory;
|
|
|
+import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
+import org.elasticsearch.common.xcontent.XContentType;
|
|
|
+import org.elasticsearch.protocol.xpack.license.GetLicenseRequest;
|
|
|
+import org.elasticsearch.protocol.xpack.license.GetLicenseResponse;
|
|
|
import org.elasticsearch.protocol.xpack.license.PutLicenseRequest;
|
|
|
import org.elasticsearch.protocol.xpack.license.PutLicenseResponse;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
import static java.util.Collections.emptySet;
|
|
|
|
|
@@ -54,7 +68,7 @@ public class LicenseClient {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Asynchronously updates license for the cluster cluster.
|
|
|
+ * Asynchronously updates license for the cluster.
|
|
|
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
|
|
|
* @param listener the listener to be notified upon request completion
|
|
|
*/
|
|
@@ -63,4 +77,59 @@ public class LicenseClient {
|
|
|
PutLicenseResponse::fromXContent, listener, emptySet());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the current license for the cluster.
|
|
|
+ * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
|
|
|
+ * @return the response
|
|
|
+ * @throws IOException in case there is a problem sending the request or parsing back the response
|
|
|
+ */
|
|
|
+ public GetLicenseResponse getLicense(GetLicenseRequest request, RequestOptions options) throws IOException {
|
|
|
+ return restHighLevelClient.performRequest(request, RequestConverters::getLicense, options,
|
|
|
+ response -> new GetLicenseResponse(convertResponseToJson(response)), emptySet());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Asynchronously returns the current license for the cluster cluster.
|
|
|
+ * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
|
|
|
+ * @param listener the listener to be notified upon request completion
|
|
|
+ */
|
|
|
+ public void getLicenseAsync(GetLicenseRequest request, RequestOptions options, ActionListener<GetLicenseResponse> listener) {
|
|
|
+ restHighLevelClient.performRequestAsync(request, RequestConverters::getLicense, options,
|
|
|
+ response -> new GetLicenseResponse(convertResponseToJson(response)), listener, emptySet());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Converts an entire response into a json sting
|
|
|
+ *
|
|
|
+ * This is useful for responses that we don't parse on the client side, but instead work as string
|
|
|
+ * such as in case of the license JSON
|
|
|
+ */
|
|
|
+ static String convertResponseToJson(Response response) throws IOException {
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+ if (entity == null) {
|
|
|
+ throw new IllegalStateException("Response body expected but not returned");
|
|
|
+ }
|
|
|
+ if (entity.getContentType() == null) {
|
|
|
+ throw new IllegalStateException("Elasticsearch didn't return the [Content-Type] header, unable to parse response body");
|
|
|
+ }
|
|
|
+ XContentType xContentType = XContentType.fromMediaTypeOrFormat(entity.getContentType().getValue());
|
|
|
+ if (xContentType == null) {
|
|
|
+ throw new IllegalStateException("Unsupported Content-Type: " + entity.getContentType().getValue());
|
|
|
+ }
|
|
|
+ if (xContentType == XContentType.JSON) {
|
|
|
+ // No changes is required
|
|
|
+ return Streams.copyToString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8));
|
|
|
+ } else {
|
|
|
+ // Need to convert into JSON
|
|
|
+ try (InputStream stream = response.getEntity().getContent();
|
|
|
+ XContentParser parser = XContentFactory.xContent(xContentType).createParser(NamedXContentRegistry.EMPTY,
|
|
|
+ DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream)) {
|
|
|
+ parser.nextToken();
|
|
|
+ XContentBuilder builder = XContentFactory.jsonBuilder();
|
|
|
+ builder.copyCurrentStructure(parser);
|
|
|
+ return Strings.toString(builder);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|