Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUP-7072: add method to retrieve plugin list working on aws es service #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.4.2</version>
<version>1.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -66,6 +66,12 @@
<version>3.5.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<scope>test</scope>
</dependency>

<!-- Logging -->
<dependency>
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/gentics/elasticsearch/client/ClientUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ default RequestBuilder<T> info() throws HttpErrorException {
return getBuilder("");
}

default RequestBuilder<T> 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<T> clusterHealth() throws HttpErrorException {
return getBuilder("_cluster/health");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> {
Expand All @@ -25,6 +24,8 @@ public class RequestBuilder<T> {

private String method;

private Map<String, String> headers = new HashMap<>();

@SuppressWarnings("unchecked")
public RequestBuilder(String method, String path, ElasticsearchOkClient<T> client, T... json) {
urlBuilder = new HttpUrl.Builder()
Expand Down Expand Up @@ -68,6 +69,9 @@ public RequestBuilder(String method, String path, ElasticsearchOkClient<T> 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();
}

Expand Down Expand Up @@ -101,4 +105,16 @@ public RequestBuilder<T> 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<T> addHttpHeader(String name, String value) {
headers.put(name, value);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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());
}
}