From b269eb7cce47dafebd8e08a484fcef689797589e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Fri, 10 Feb 2023 14:26:54 +0100 Subject: [PATCH] add open/close to HTTP API --- .../java/org/xbib/elx/http/HttpAction.java | 22 ++++----- .../cluster/state/HttpClusterStateAction.java | 24 ++++------ .../indices/alias/get/HttpGetAliasAction.java | 10 ++--- .../indices/close/HttpCloseIndexAction.java | 45 +++++++++++++++++++ .../indices/HttpIndicesExistsAction.java | 2 +- ...dexAction.java => HttpGetIndexAction.java} | 3 +- .../indices/open/HttpOpenIndexAction.java | 30 +++++++++++++ .../services/org.xbib.elx.http.HttpAction | 3 +- .../xbib/elx/http/test/IndexPruneTest.java | 1 - gradle.properties | 2 +- 10 files changed, 105 insertions(+), 37 deletions(-) create mode 100644 elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/close/HttpCloseIndexAction.java rename elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/get/{HtppGetIndexAction.java => HttpGetIndexAction.java} (92%) create mode 100644 elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/open/HttpOpenIndexAction.java diff --git a/elx-http/src/main/java/org/xbib/elx/http/HttpAction.java b/elx-http/src/main/java/org/xbib/elx/http/HttpAction.java index 31397d4..3ebafba 100644 --- a/elx-http/src/main/java/org/xbib/elx/http/HttpAction.java +++ b/elx-http/src/main/java/org/xbib/elx/http/HttpAction.java @@ -60,18 +60,19 @@ public abstract class HttpAction { + httpActionContext.setHttpResponse(fullHttpResponse); + String content = httpActionContext.getHttpResponse().getBodyAsChars(StandardCharsets.UTF_8).toString(); if (logger.isTraceEnabled()) { logger.log(Level.TRACE, "got response: " + fullHttpResponse.getStatus().codeAsText() + " headers = " + fullHttpResponse.getHeaders() + - " content = " + fullHttpResponse.getBodyAsChars(StandardCharsets.UTF_8)); + " content = " + content); } - httpActionContext.setHttpResponse(fullHttpResponse); if (fullHttpResponse.getStatus().code() == 200) { - listener.onResponse(parseToResponse(httpActionContext)); + listener.onResponse(parseToResponse(httpActionContext, content)); } else { - ElasticsearchStatusException statusException = parseToError(httpActionContext); + ElasticsearchStatusException statusException = parseToError(httpActionContext, content); if (statusException.status().equals(RestStatus.NOT_FOUND)) { - listener.onResponse(parseToResponse(httpActionContext)); + listener.onResponse(parseToResponse(httpActionContext, content)); } else { listener.onFailure(statusException); } @@ -147,7 +148,7 @@ public abstract class HttpAction httpActionContext) { + protected T parseToResponse(HttpActionContext httpActionContext, String content) { String mediaType = httpActionContext.getHttpResponse().getHeaders().get(HttpHeaderNames.CONTENT_TYPE); XContentType xContentType = XContentType.fromMediaTypeOrFormat(mediaType); if (xContentType == null) { @@ -156,23 +157,24 @@ public abstract class HttpAction { + + @Override + public CloseIndexAction getActionInstance() { + return CloseIndexAction.INSTANCE; + } + + @Override + protected HttpRequestBuilder createHttpRequest(String url, CloseIndexRequest closeIndexRequest) { + return newPostRequest(url, "/" + String.join(",", closeIndexRequest.indices()) + "/_close"); + } + + @Override + protected CheckedFunction entityParser(HttpResponse httpResponse) { + return this::fromXContent; + } + + public CloseIndexResponse fromXContent(XContentParser parser) throws IOException { + AcknowledgedResponse acknowledgedResponse = CloseIndexResponse.fromXContent(parser); + if (parser.currentToken() == null) { + parser.nextToken(); + } + boolean shardAcknowledged = true; + List list = new LinkedList<>(); + return new CloseIndexResponse(acknowledgedResponse.isAcknowledged(), shardAcknowledged, list); + } +} diff --git a/elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/exists/indices/HttpIndicesExistsAction.java b/elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/exists/indices/HttpIndicesExistsAction.java index bd35ae6..abedb17 100644 --- a/elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/exists/indices/HttpIndicesExistsAction.java +++ b/elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/exists/indices/HttpIndicesExistsAction.java @@ -33,7 +33,7 @@ public class HttpIndicesExistsAction extends HttpAction httpActionContext) { + protected ElasticsearchStatusException parseToError(HttpActionContext httpActionContext, String content) { return new ElasticsearchStatusException("not found", RestStatus.NOT_FOUND); } diff --git a/elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/get/HtppGetIndexAction.java b/elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/get/HttpGetIndexAction.java similarity index 92% rename from elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/get/HtppGetIndexAction.java rename to elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/get/HttpGetIndexAction.java index ae52b41..bb2feeb 100644 --- a/elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/get/HtppGetIndexAction.java +++ b/elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/get/HttpGetIndexAction.java @@ -13,7 +13,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.List; -public class HtppGetIndexAction extends HttpAction { +public class HttpGetIndexAction extends HttpAction { @Override public GetIndexAction getActionInstance() { @@ -25,7 +25,6 @@ public class HtppGetIndexAction extends HttpAction list = getIndexRequest.indices().length == 0 ? List.of("*") : Arrays.asList(getIndexRequest.indices()); String command = "/" + String.join(",", list); - logger.info("command = " + command); return newGetRequest(url, command); } diff --git a/elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/open/HttpOpenIndexAction.java b/elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/open/HttpOpenIndexAction.java new file mode 100644 index 0000000..1588e1f --- /dev/null +++ b/elx-http/src/main/java/org/xbib/elx/http/action/admin/indices/open/HttpOpenIndexAction.java @@ -0,0 +1,30 @@ +package org.xbib.elx.http.action.admin.indices.open; + +import org.elasticsearch.action.admin.indices.open.OpenIndexAction; +import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; +import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; +import org.elasticsearch.common.CheckedFunction; +import org.elasticsearch.common.xcontent.XContentParser; +import org.xbib.elx.http.HttpAction; +import org.xbib.net.http.client.HttpResponse; +import org.xbib.net.http.client.netty.HttpRequestBuilder; + +import java.io.IOException; + +public class HttpOpenIndexAction extends HttpAction { + + @Override + public OpenIndexAction getActionInstance() { + return OpenIndexAction.INSTANCE; + } + + @Override + protected HttpRequestBuilder createHttpRequest(String url, OpenIndexRequest openIndexRequest) { + return newPostRequest(url, "/" + String.join(",", openIndexRequest.indices()) + "/_open"); + } + + @Override + protected CheckedFunction entityParser(HttpResponse httpResponse) { + return OpenIndexResponse::fromXContent; + } +} diff --git a/elx-http/src/main/resources/META-INF/services/org.xbib.elx.http.HttpAction b/elx-http/src/main/resources/META-INF/services/org.xbib.elx.http.HttpAction index 89df3d6..f78d2b6 100644 --- a/elx-http/src/main/resources/META-INF/services/org.xbib.elx.http.HttpAction +++ b/elx-http/src/main/resources/META-INF/services/org.xbib.elx.http.HttpAction @@ -7,8 +7,9 @@ org.xbib.elx.http.action.admin.indices.alias.HttpIndicesAliasesAction org.xbib.elx.http.action.admin.indices.alias.get.HttpGetAliasAction org.xbib.elx.http.action.admin.indices.create.HttpCreateIndexAction org.xbib.elx.http.action.admin.indices.delete.HttpDeleteIndexAction +org.xbib.elx.http.action.admin.indices.close.HttpCloseIndexAction org.xbib.elx.http.action.admin.indices.exists.indices.HttpIndicesExistsAction -org.xbib.elx.http.action.admin.indices.get.HtppGetIndexAction +org.xbib.elx.http.action.admin.indices.get.HttpGetIndexAction org.xbib.elx.http.action.admin.indices.refresh.HttpRefreshIndexAction org.xbib.elx.http.action.admin.indices.settings.get.HttpGetSettingsAction org.xbib.elx.http.action.admin.indices.settings.put.HttpUpdateSettingsAction diff --git a/elx-http/src/test/java/org/xbib/elx/http/test/IndexPruneTest.java b/elx-http/src/test/java/org/xbib/elx/http/test/IndexPruneTest.java index 5f5e8a0..141922f 100644 --- a/elx-http/src/test/java/org/xbib/elx/http/test/IndexPruneTest.java +++ b/elx-http/src/test/java/org/xbib/elx/http/test/IndexPruneTest.java @@ -93,7 +93,6 @@ class IndexPruneTest { } @Test - @Disabled("internal error") void testPruneWithClose() throws IOException { try (HttpAdminClient adminClient = ClientBuilder.builder() .setAdminClientProvider(HttpAdminClientProvider.class) diff --git a/gradle.properties b/gradle.properties index 96296b5..4c8c11a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group = org.xbib name = elx -version = 7.10.2.25 +version = 7.10.2.26 org.gradle.warning.mode = ALL