diff --git a/pom.xml b/pom.xml index 886073e..684e187 100644 --- a/pom.xml +++ b/pom.xml @@ -51,7 +51,7 @@ org.testcontainers testcontainers - 1.4.2 + 1.10.0 test @@ -66,6 +66,12 @@ 3.5.4 test + + commons-io + commons-io + 2.6 + test + diff --git a/src/main/java/com/gentics/elasticsearch/client/ClientUtility.java b/src/main/java/com/gentics/elasticsearch/client/ClientUtility.java index dcf3e79..ea95310 100644 --- a/src/main/java/com/gentics/elasticsearch/client/ClientUtility.java +++ b/src/main/java/com/gentics/elasticsearch/client/ClientUtility.java @@ -7,6 +7,33 @@ */ public final class ClientUtility { + /** + * Check if the json string contains a json array as root element + * @param jsonStr the json string + * @return true if the json string contains an array as root element. + */ + public static Boolean isJsonArray(String jsonStr) { + jsonStr = jsonStr.trim(); + return jsonStr.startsWith("[") && jsonStr.endsWith("]"); + } + + /** + * Check if the json string contains a json array as root element and + * will wrap the array in a json object with "arrayData" as key. + * If the root element is no json array the json string will be + * returned unchanged. + * + * @param jsonStr the json string + * @return a json array string wrapped in an object or the jsonString itself + */ + public static String wrapJsonArrays(String jsonStr) { + if (isJsonArray(jsonStr)) { + return "{ \"arrayData\": " + jsonStr + "}"; + } + return jsonStr; + } + + /** * Convert the given list into an array. * diff --git a/src/main/java/com/gentics/elasticsearch/client/methods/InfoMethods.java b/src/main/java/com/gentics/elasticsearch/client/methods/InfoMethods.java index 9d48826..deca71a 100644 --- a/src/main/java/com/gentics/elasticsearch/client/methods/InfoMethods.java +++ b/src/main/java/com/gentics/elasticsearch/client/methods/InfoMethods.java @@ -15,6 +15,11 @@ default RequestBuilder info() throws HttpErrorException { return getBuilder(""); } + default RequestBuilder plugins() { + // we need to explicitly set the accept header here otherwise elasticsearch will return a plain text table + return getBuilder("_cat/plugins").addHttpHeader("Accept", "application/json"); + } + default RequestBuilder clusterHealth() throws HttpErrorException { return getBuilder("_cluster/health"); } diff --git a/src/main/java/com/gentics/elasticsearch/client/okhttp/ElasticsearchOkClient.java b/src/main/java/com/gentics/elasticsearch/client/okhttp/ElasticsearchOkClient.java index f0dea6d..aacc766 100644 --- a/src/main/java/com/gentics/elasticsearch/client/okhttp/ElasticsearchOkClient.java +++ b/src/main/java/com/gentics/elasticsearch/client/okhttp/ElasticsearchOkClient.java @@ -5,6 +5,7 @@ import java.util.concurrent.TimeUnit; import com.gentics.elasticsearch.client.AbstractElasticsearchClient; +import com.gentics.elasticsearch.client.ClientUtility; import com.gentics.elasticsearch.client.HttpErrorException; import io.reactivex.Single; @@ -132,7 +133,7 @@ public T executeSync(Request request) throws HttpErrorException { throw new HttpErrorException("Request failed {" + response.message() + "}", response.code(), bodyStr); } Objects.requireNonNull(parser, "No body parser was configured."); - return parser.apply(bodyStr); + return parser.apply(ClientUtility.wrapJsonArrays(bodyStr)); } catch (IOException e1) { throw new HttpErrorException("Error while excuting request", e1); } @@ -175,7 +176,7 @@ public void onResponse(Call call, Response response) { return; } Objects.requireNonNull(parser, "No body parser was configured."); - sub.onSuccess(parser.apply(bodyStr)); + sub.onSuccess(parser.apply(ClientUtility.wrapJsonArrays(bodyStr))); } } }); diff --git a/src/main/java/com/gentics/elasticsearch/client/okhttp/RequestBuilder.java b/src/main/java/com/gentics/elasticsearch/client/okhttp/RequestBuilder.java index 360e544..1cd8f10 100644 --- a/src/main/java/com/gentics/elasticsearch/client/okhttp/RequestBuilder.java +++ b/src/main/java/com/gentics/elasticsearch/client/okhttp/RequestBuilder.java @@ -1,14 +1,13 @@ package com.gentics.elasticsearch.client.okhttp; import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; import com.gentics.elasticsearch.client.HttpErrorException; import io.reactivex.Single; -import okhttp3.HttpUrl; -import okhttp3.MediaType; -import okhttp3.Request; -import okhttp3.RequestBody; +import okhttp3.*; import okhttp3.Request.Builder; public class RequestBuilder { @@ -25,6 +24,8 @@ public class RequestBuilder { private String method; + private Map headers = new HashMap<>(); + @SuppressWarnings("unchecked") public RequestBuilder(String method, String path, ElasticsearchOkClient client, T... json) { urlBuilder = new HttpUrl.Builder() @@ -68,6 +69,9 @@ public RequestBuilder(String method, String path, ElasticsearchOkClient clien private Request build() { Builder builder = new Request.Builder().url(urlBuilder.build()); builder.method(method, body); + if (!headers.isEmpty()) { + builder.headers(Headers.of(headers)); + } return builder.build(); } @@ -101,4 +105,16 @@ public RequestBuilder addQueryParameter(String key, String value) { urlBuilder.addQueryParameter(key, value); return this; } + + /** + * Add a http header to the request. + * + * @param name the header name + * @param value the header value + * @return + */ + public RequestBuilder addHttpHeader(String name, String value) { + headers.put(name, value); + return this; + } } diff --git a/src/test/java/com/gentics/elasticsearch/client/methods/InfoMethodsTest.java b/src/test/java/com/gentics/elasticsearch/client/methods/InfoMethodsTest.java new file mode 100644 index 0000000..4de35b9 --- /dev/null +++ b/src/test/java/com/gentics/elasticsearch/client/methods/InfoMethodsTest.java @@ -0,0 +1,39 @@ +package com.gentics.elasticsearch.client.methods; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.gentics.elasticsearch.AbstractDockerTest; +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; + +import org.junit.Test; + +import java.util.Optional; +import java.util.stream.IntStream; + +public class InfoMethodsTest extends AbstractDockerTest { + + public void assertContainsIngestPlugin(JsonObject obj) { + assertNotNull(obj); + assertTrue(obj.containsKey("arrayData")); + JsonArray plugins = obj.getJsonArray("arrayData"); + Optional ingestPlugin = IntStream.range(0, plugins.size()) + .mapToObj(plugins::getJsonObject) + .filter(o -> o.containsKey("component")) + .map(o -> o.getString("component")) + .filter("ingest-attachment"::equals) + .findFirst(); + assertTrue(ingestPlugin.isPresent()); + } + + @Test + public void testGetPluginsSync() throws Exception { + assertContainsIngestPlugin(client.plugins().sync()); + } + + @Test + public void testGetPluginsAsync() throws Exception { + assertContainsIngestPlugin(client.plugins().async().blockingGet()); + } +}