diff --git a/CHANGELOG.md b/CHANGELOG.md index da3d7a4509..5f71e41fdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,7 +36,7 @@ This section is for maintaining a changelog for all breaking changes for the cli ### Added - Add missed fields to MultisearchBody: seqNoPrimaryTerm, storedFields, explain, fields, indicesBoost ([#914](https://github.com/opensearch-project/opensearch-java/pull/914)) -- Add OpenSearchGenericClient with support for raw HTTP request/responses ([#910](https://github.com/opensearch-project/opensearch-java/pull/910)) +- Add OpenSearchGenericClient with support for raw HTTP request/responses ([#910](https://github.com/opensearch-project/opensearch-java/pull/910), [#929](https://github.com/opensearch-project/opensearch-java/pull/929)) - Add missed fields to MultisearchBody: collapse, version, timeout ([#916](https://github.com/opensearch-project/opensearch-java/pull/916) - Add missed fields to MultisearchBody: ext, rescore and to SearchRequest: ext ([#918](https://github.com/opensearch-project/opensearch-java/pull/918) diff --git a/guides/generic.md b/guides/generic.md index f9ee7ff5fa..3e0937dd2d 100644 --- a/guides/generic.md +++ b/guides/generic.md @@ -13,7 +13,13 @@ There are rare circumstances when the typed OpenSearch client APIs are too const The following sample code gets the `OpenSearchGenericClient` from the `OpenSearchClient` instance. ```java -final OpenSearchGenericClient generic = javaClient().generic(); +final OpenSearchGenericClient generic = javaClient().generic(ClientOptions.DEFAULT); +``` + +The generic client with default options (`ClientOptions.DEFAULT`) returns the responses as those were received from the server. The generic client could be instructed to raise an `OpenSearchClientException` exception instead if the HTTP status code is not indicating the successful response, for example: + +```java +final OpenSearchGenericClient generic = javaClient().generic(ClientOptions.throwOnHttpErrors()); ``` ## Sending Simple Request @@ -21,7 +27,7 @@ The following sample code sends a simple request that does not require any paylo ```java // compare with what the low level client outputs -try (Response response = javaClient().generic().execute(Requests.builder().endpoint("/").method("GET").build())) { +try (Response response = javaClient().generic(ClientOptions.DEFAULT).execute(Requests.builder().endpoint("/").method("GET").build())) { // ... } ``` @@ -29,7 +35,7 @@ try (Response response = javaClient().generic().execute(Requests.builder().endpo Please notice that the `Response` instance should be closed explicitly in order to free up any allocated resource (like response input streams or buffers), the [`try-with-resource`](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) pattern is encouraged. ```java -try (Response response = javaClient().generic().execute(...)) { +try (Response response = javaClient().generic(ClientOptions.DEFAULT).execute(...)) { // ... } ``` @@ -38,7 +44,7 @@ The generic client never interprets status codes and provides the direct access ```java // compare with what the low level client outputs -try (Response response = javaClient().generic().execute(...)) { +try (Response response = javaClient().generic(ClientOptions.DEFAULT).execute(...)) { if (response.getStatus() != 200) { // Request was not successful } @@ -49,7 +55,7 @@ try (Response response = javaClient().generic().execute(...)) { The following sample code a simple request with JSON body. ```java - try (Response response = javaClient().generic() + try (Response response = javaClient().generic(ClientOptions.DEFAULT) .execute( Requests.builder() .endpoint("/" + index + "/_search") @@ -83,7 +89,7 @@ final CreateIndexRequest request = CreateIndexRequest.of( .settings(settings -> settings.sort(s -> s.field("name").order(SegmentSortOrder.Asc))) ); -try (Response response = javaClient().generic() +try (Response response = javaClient().generic(ClientOptions.DEFAULT) .execute( Requests.builder() .endpoint("/" + index).method("PUT") @@ -100,7 +106,7 @@ try (Response response = javaClient().generic() Dealing with strings or POJOs could be daunting sometimes, using structured JSON APIs is a middle ground of both approaches, as per following sample code that uses (`jakarta.json.Json`)[https://jakarta.ee/specifications/jsonp]. ```java -try (Response response = javaClient().generic() +try (Response response = javaClient().generic(ClientOptions.DEFAULT) .execute( Requests.builder() .endpoint("/" + index) diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/OpenSearchClient.java b/java-client/src/main/java/org/opensearch/client/opensearch/OpenSearchClient.java index ee83bcc20d..c38c36236b 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/OpenSearchClient.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/OpenSearchClient.java @@ -156,8 +156,8 @@ public OpenSearchClient withTransportOptions(@Nullable TransportOptions transpor } // ----- Child clients - public OpenSearchGenericClient generic() { - return new OpenSearchGenericClient(this.transport, this.transportOptions); + public OpenSearchGenericClient generic(OpenSearchGenericClient.ClientOptions clientOptions) { + return new OpenSearchGenericClient(this.transport, this.transportOptions, clientOptions); } public OpenSearchCatClient cat() { diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/generic/Body.java b/java-client/src/main/java/org/opensearch/client/opensearch/generic/Body.java index 9957c93507..e43bf0551e 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/generic/Body.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/generic/Body.java @@ -67,6 +67,14 @@ public interface Body extends AutoCloseable { * @return body as {@link String} */ default String bodyAsString() { + return new String(bodyAsBytes(), StandardCharsets.UTF_8); + } + + /** + * Gets the body as {@link byte[]} + * @return body as {@link byte[]} + */ + default byte[] bodyAsBytes() { try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { try (final InputStream in = body()) { final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; @@ -77,7 +85,7 @@ default String bodyAsString() { } out.flush(); - return new String(out.toByteArray(), StandardCharsets.UTF_8); + return out.toByteArray(); } catch (final IOException ex) { throw new UncheckedIOException(ex); } diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/generic/GenericResponse.java b/java-client/src/main/java/org/opensearch/client/opensearch/generic/GenericResponse.java index 5a7a27cef8..6d394cbda5 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/generic/GenericResponse.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/generic/GenericResponse.java @@ -27,6 +27,10 @@ final class GenericResponse implements Response { private final Collection> headers; private final Body body; + GenericResponse(String uri, String protocol, String method, int status, String reason, Collection> headers) { + this(uri, protocol, method, status, reason, headers, null); + } + GenericResponse( String uri, String protocol, diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/generic/OpenSearchClientException.java b/java-client/src/main/java/org/opensearch/client/opensearch/generic/OpenSearchClientException.java new file mode 100644 index 0000000000..cfdcf3a539 --- /dev/null +++ b/java-client/src/main/java/org/opensearch/client/opensearch/generic/OpenSearchClientException.java @@ -0,0 +1,41 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.client.opensearch.generic; + +/** + * Exception thrown by API client methods when OpenSearch could not accept or + * process a request. + *

+ * The {@link #response()} contains the the raw response as returned by the API + * endpoint that was called. + */ +public class OpenSearchClientException extends RuntimeException { + + private final Response response; + + public OpenSearchClientException(Response response) { + super("Request failed: [" + response.getStatus() + "] " + response.getReason()); + this.response = response; + } + + /** + * The error response sent by OpenSearch + */ + public Response response() { + return this.response; + } + + /** + * Status code returned by OpenSearch. Shortcut for + * {@code response().status()}. + */ + public int status() { + return this.response.getStatus(); + } +} diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/generic/OpenSearchGenericClient.java b/java-client/src/main/java/org/opensearch/client/opensearch/generic/OpenSearchGenericClient.java index 0d759af935..1b82ee0b27 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/generic/OpenSearchGenericClient.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/generic/OpenSearchGenericClient.java @@ -10,10 +10,12 @@ import java.io.IOException; import java.io.InputStream; +import java.io.UncheckedIOException; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.CompletableFuture; +import java.util.function.Predicate; import java.util.stream.Collectors; import javax.annotation.Nullable; import org.opensearch.client.ApiClient; @@ -24,14 +26,37 @@ * Client for the generic HTTP requests. */ public class OpenSearchGenericClient extends ApiClient { + /** + * Generic client options + */ + public static final class ClientOptions { + public static final ClientOptions DEFAULT = new ClientOptions(); + + private final Predicate error; + + private ClientOptions() { + this(statusCode -> false); + } + + private ClientOptions(final Predicate error) { + this.error = error; + } + + public static ClientOptions throwOnHttpErrors() { + return new ClientOptions(statusCode -> statusCode >= 400); + } + } + /** * Generic endpoint instance */ private static final class GenericEndpoint implements org.opensearch.client.transport.GenericEndpoint { private final Request request; + private final Predicate error; - public GenericEndpoint(Request request) { + public GenericEndpoint(Request request, Predicate error) { this.request = request; + this.error = error; } @Override @@ -67,24 +92,62 @@ public GenericResponse responseDeserializer( int status, String reason, List> headers, - String contentType, - InputStream body + @Nullable String contentType, + @Nullable InputStream body ) { - return new GenericResponse(uri, protocol, method, status, reason, headers, Body.from(body, contentType)); + if (isError(status)) { + // Fully consume the response body since the it will be propagated as an exception with possible no chance to be closed + try (Body b = Body.from(body, contentType)) { + if (b != null) { + return new GenericResponse( + uri, + protocol, + method, + status, + reason, + headers, + Body.from(b.bodyAsBytes(), b.contentType()) + ); + } else { + return new GenericResponse(uri, protocol, method, status, reason, headers); + } + } catch (final IOException ex) { + throw new UncheckedIOException(ex); + } + } else { + return new GenericResponse(uri, protocol, method, status, reason, headers, Body.from(body, contentType)); + } + } + + @Override + public boolean isError(int statusCode) { + return error.test(statusCode); + } + + @Override + public T exceptionConverter(Response error) { + throw new OpenSearchClientException(error); } } + private final ClientOptions clientOptions; + public OpenSearchGenericClient(OpenSearchTransport transport) { - super(transport, null); + this(transport, null, ClientOptions.DEFAULT); } - public OpenSearchGenericClient(OpenSearchTransport transport, @Nullable TransportOptions transportOptions) { + public OpenSearchGenericClient( + OpenSearchTransport transport, + @Nullable TransportOptions transportOptions, + ClientOptions clientOptions + ) { super(transport, transportOptions); + this.clientOptions = clientOptions; } @Override public OpenSearchGenericClient withTransportOptions(@Nullable TransportOptions transportOptions) { - return new OpenSearchGenericClient(this.transport, transportOptions); + return new OpenSearchGenericClient(this.transport, transportOptions, this.clientOptions); } /** @@ -94,7 +157,7 @@ public OpenSearchGenericClient withTransportOptions(@Nullable TransportOptions t * @throws IOException I/O exception */ public Response execute(Request request) throws IOException { - return transport.performRequest(request, new GenericEndpoint(request), this.transportOptions); + return transport.performRequest(request, new GenericEndpoint(request, clientOptions.error), this.transportOptions); } /** @@ -103,6 +166,6 @@ public Response execute(Request request) throws IOException { * @return generic HTTP response future */ public CompletableFuture executeAsync(Request request) { - return transport.performRequestAsync(request, new GenericEndpoint(request), this.transportOptions); + return transport.performRequestAsync(request, new GenericEndpoint(request, clientOptions.error), this.transportOptions); } } diff --git a/java-client/src/main/java/org/opensearch/client/transport/Endpoint.java b/java-client/src/main/java/org/opensearch/client/transport/Endpoint.java index 0e74bf2519..6f46210f1a 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/Endpoint.java +++ b/java-client/src/main/java/org/opensearch/client/transport/Endpoint.java @@ -37,6 +37,8 @@ import javax.annotation.Nullable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.NdJsonpSerializable; +import org.opensearch.client.opensearch._types.ErrorResponse; +import org.opensearch.client.opensearch._types.OpenSearchException; /** * An endpoint links requests and responses to HTTP protocol encoding. It also defines the error response @@ -90,4 +92,13 @@ default Map headers(RequestT request) { @Nullable JsonpDeserializer errorDeserializer(int statusCode); + /** + * Converts error response to exception instance of type {@code T} + * @param exception type + * @param error error response + * @return exception instance + */ + default T exceptionConverter(ErrorT error) { + throw new OpenSearchException((ErrorResponse) error); + } } diff --git a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Transport.java b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Transport.java index 74a72e804d..9b7a5d37f6 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Transport.java +++ b/java-client/src/main/java/org/opensearch/client/transport/httpclient5/ApacheHttpClient5Transport.java @@ -76,8 +76,6 @@ import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.NdJsonpSerializable; -import org.opensearch.client.opensearch._types.ErrorResponse; -import org.opensearch.client.opensearch._types.OpenSearchException; import org.opensearch.client.transport.Endpoint; import org.opensearch.client.transport.GenericEndpoint; import org.opensearch.client.transport.GenericSerializable; @@ -487,34 +485,63 @@ private ResponseT prepareResponse(Response clientResp, Endpo int statusCode = clientResp.getStatusLine().getStatusCode(); if (endpoint.isError(statusCode)) { - JsonpDeserializer errorDeserializer = endpoint.errorDeserializer(statusCode); - if (errorDeserializer == null) { - throw new TransportException("Request failed with status code '" + statusCode + "'", new ResponseException(clientResp)); - } - HttpEntity entity = clientResp.getEntity(); if (entity == null) { throw new TransportException("Expecting a response body, but none was sent", new ResponseException(clientResp)); } - // We may have to replay it. - entity = new BufferedHttpEntity(entity); - - try { - InputStream content = entity.getContent(); - try (JsonParser parser = mapper.jsonProvider().createParser(content)) { - ErrorT error = errorDeserializer.deserialize(parser, mapper); - // TODO: have the endpoint provide the exception constructor - throw new OpenSearchException((ErrorResponse) error); + if (endpoint instanceof GenericEndpoint) { + @SuppressWarnings("unchecked") + final GenericEndpoint rawEndpoint = (GenericEndpoint) endpoint; + + final RequestLine requestLine = clientResp.getRequestLine(); + final StatusLine statusLine = clientResp.getStatusLine(); + + // We may have to replay it. + entity = new BufferedHttpEntity(entity); + + try (InputStream content = entity.getContent()) { + final ResponseT error = rawEndpoint.responseDeserializer( + requestLine.getUri(), + requestLine.getMethod(), + requestLine.getProtocolVersion().format(), + statusLine.getStatusCode(), + statusLine.getReasonPhrase(), + Arrays.stream(clientResp.getHeaders()) + .map(h -> new AbstractMap.SimpleEntry(h.getName(), h.getValue())) + .collect(Collectors.toList()), + entity.getContentType(), + content + ); + throw rawEndpoint.exceptionConverter(error); + } + } else { + JsonpDeserializer errorDeserializer = endpoint.errorDeserializer(statusCode); + if (errorDeserializer == null) { + throw new TransportException( + "Request failed with status code '" + statusCode + "'", + new ResponseException(clientResp) + ); } - } catch (MissingRequiredPropertyException errorEx) { - // Could not decode exception, try the response type + + // We may have to replay it. + entity = new BufferedHttpEntity(entity); + try { - ResponseT response = decodeResponse(statusCode, entity, clientResp, endpoint); - return response; - } catch (Exception respEx) { - // No better luck: throw the original error decoding exception - throw new TransportException("Failed to decode error response", new ResponseException(clientResp)); + InputStream content = entity.getContent(); + try (JsonParser parser = mapper.jsonProvider().createParser(content)) { + ErrorT error = errorDeserializer.deserialize(parser, mapper); + throw endpoint.exceptionConverter(error); + } + } catch (MissingRequiredPropertyException errorEx) { + // Could not decode exception, try the response type + try { + ResponseT response = decodeResponse(statusCode, entity, clientResp, endpoint); + return response; + } catch (Exception respEx) { + // No better luck: throw the original error decoding exception + throw new TransportException("Failed to decode error response", new ResponseException(clientResp)); + } } } } else { diff --git a/java-client/src/main/java/org/opensearch/client/transport/rest_client/RestClientTransport.java b/java-client/src/main/java/org/opensearch/client/transport/rest_client/RestClientTransport.java index 7e56409ef6..9b6a62b1b4 100644 --- a/java-client/src/main/java/org/opensearch/client/transport/rest_client/RestClientTransport.java +++ b/java-client/src/main/java/org/opensearch/client/transport/rest_client/RestClientTransport.java @@ -61,8 +61,6 @@ import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.NdJsonpSerializable; -import org.opensearch.client.opensearch._types.ErrorResponse; -import org.opensearch.client.opensearch._types.OpenSearchException; import org.opensearch.client.transport.Endpoint; import org.opensearch.client.transport.GenericEndpoint; import org.opensearch.client.transport.GenericSerializable; @@ -264,34 +262,64 @@ private ResponseT getHighLevelResponse( } else if (statusCode == HttpStatus.SC_UNAUTHORIZED) { throw new TransportException("Unauthorized access", new ResponseException(clientResp)); } else if (endpoint.isError(statusCode)) { - JsonpDeserializer errorDeserializer = endpoint.errorDeserializer(statusCode); - if (errorDeserializer == null) { - throw new TransportException("Request failed with status code '" + statusCode + "'", new ResponseException(clientResp)); - } - HttpEntity entity = clientResp.getEntity(); if (entity == null) { throw new TransportException("Expecting a response body, but none was sent", new ResponseException(clientResp)); } - // We may have to replay it. - entity = new BufferedHttpEntity(entity); - - try { - InputStream content = entity.getContent(); - try (JsonParser parser = mapper.jsonProvider().createParser(content)) { - ErrorT error = errorDeserializer.deserialize(parser, mapper); - // TODO: have the endpoint provide the exception constructor - throw new OpenSearchException((ErrorResponse) error); + if (endpoint instanceof GenericEndpoint) { + @SuppressWarnings("unchecked") + final GenericEndpoint rawEndpoint = (GenericEndpoint) endpoint; + + final RequestLine requestLine = clientResp.getRequestLine(); + final StatusLine statusLine = clientResp.getStatusLine(); + + // We may have to replay it. + entity = new BufferedHttpEntity(entity); + + try (InputStream content = entity.getContent()) { + final ResponseT error = rawEndpoint.responseDeserializer( + requestLine.getUri(), + requestLine.getMethod(), + requestLine.getProtocolVersion().format(), + statusLine.getStatusCode(), + statusLine.getReasonPhrase(), + Arrays.stream(clientResp.getHeaders()) + .map(h -> new AbstractMap.SimpleEntry(h.getName(), h.getValue())) + .collect(Collectors.toList()), + entity.getContentType(), + content + ); + + throw rawEndpoint.exceptionConverter(error); } - } catch (MissingRequiredPropertyException errorEx) { - // Could not decode exception, try the response type + } else { + JsonpDeserializer errorDeserializer = endpoint.errorDeserializer(statusCode); + if (errorDeserializer == null) { + throw new TransportException( + "Request failed with status code '" + statusCode + "'", + new ResponseException(clientResp) + ); + } + + // We may have to replay it. + entity = new BufferedHttpEntity(entity); + try { - ResponseT response = decodeResponse(statusCode, entity, clientResp, endpoint); - return response; - } catch (Exception respEx) { - // No better luck: throw the original error decoding exception - throw new TransportException("Failed to decode error response", new ResponseException(clientResp)); + InputStream content = entity.getContent(); + try (JsonParser parser = mapper.jsonProvider().createParser(content)) { + ErrorT error = errorDeserializer.deserialize(parser, mapper); + throw endpoint.exceptionConverter(error); + } + } catch (MissingRequiredPropertyException errorEx) { + // Could not decode exception, try the response type + try { + ResponseT response = decodeResponse(statusCode, entity, clientResp, endpoint); + return response; + } catch (Exception respEx) { + // No better luck: throw the original error decoding exception + throw new TransportException("Failed to decode error response", new ResponseException(clientResp)); + } } } } else { diff --git a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractGenericClientIT.java b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractGenericClientIT.java index bf3900bfb8..f6119b2ef2 100644 --- a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractGenericClientIT.java +++ b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractGenericClientIT.java @@ -20,6 +20,8 @@ import org.opensearch.client.opensearch._types.mapping.Property; import org.opensearch.client.opensearch.core.SearchResponse; import org.opensearch.client.opensearch.generic.Bodies; +import org.opensearch.client.opensearch.generic.OpenSearchClientException; +import org.opensearch.client.opensearch.generic.OpenSearchGenericClient.ClientOptions; import org.opensearch.client.opensearch.generic.Requests; import org.opensearch.client.opensearch.generic.Response; import org.opensearch.client.opensearch.indices.CreateIndexRequest; @@ -34,7 +36,7 @@ public void shouldReturnSearchResults() throws Exception { createIndex(index); try ( - Response response = javaClient().generic() + Response response = javaClient().generic(ClientOptions.DEFAULT) .execute( Requests.builder() .endpoint("/" + index + "/_search") @@ -90,6 +92,36 @@ public void shouldReturnSearchResults() throws Exception { } } + @Test + public void shouldReturn404() throws IOException { + final String index = "non_existing_doc"; + createIndex(index); + + try ( + Response response = javaClient().generic(ClientOptions.DEFAULT) + .execute(Requests.builder().endpoint("/" + index + "/_doc/10").method("GET").build()) + ) { + assertThat(response.getStatus(), equalTo(404)); + assertThat(response.getBody().isPresent(), equalTo(true)); + } + } + + @Test + public void shouldThrow() throws IOException { + final String index = "non_existing_doc"; + createIndex(index); + + final OpenSearchClientException ex = assertThrows(OpenSearchClientException.class, () -> { + try ( + Response response = javaClient().generic(ClientOptions.throwOnHttpErrors()) + .execute(Requests.builder().endpoint("/" + index + "/_doc/10").method("GET").build()) + ) {} + }); + + assertThat(ex.status(), equalTo(404)); + assertThat(ex.response().getBody().isPresent(), equalTo(true)); + } + private void createTestDocuments(String index) throws IOException { createTestDocument(index, "1", createItem("hummer", "huge", "yes", 2)); createTestDocument(index, "2", createItem("jammer", "huge", "yes", 1)); @@ -103,7 +135,7 @@ private void createTestDocuments(String index) throws IOException { private void createTestDocument(String index, String id, ShopItem document) throws IOException { try ( - Response response = javaClient().generic() + Response response = javaClient().generic(ClientOptions.DEFAULT) .execute( Requests.builder() .endpoint("/" + index + "/_doc/" + id) @@ -129,7 +161,7 @@ private void createIndexUntyped(String index) throws IOException { final JsonpMapper jsonpMapper = javaClient()._transport().jsonpMapper(); try ( - Response response = javaClient().generic() + Response response = javaClient().generic(ClientOptions.DEFAULT) .execute( Requests.builder() .endpoint("/" + index) @@ -148,11 +180,9 @@ private void createIndexUntyped(String index) throws IOException { .add( "properties", Json.createObjectBuilder() - .add("name", Json.createObjectBuilder().add("type", "keyword")) - .add("doc_values", true) + .add("name", Json.createObjectBuilder().add("type", "keyword").add("doc_values", true)) - .add("size", Json.createObjectBuilder().add("type", "keyword")) - .add("doc_values", true) + .add("size", Json.createObjectBuilder().add("type", "keyword").add("doc_values", true)) ) ) ) @@ -185,7 +215,7 @@ private void createIndexTyped(String index) throws IOException { ); try ( - Response response = javaClient().generic() + Response response = javaClient().generic(ClientOptions.DEFAULT) .execute(Requests.builder().endpoint("/" + index).method("PUT").json(request, jsonpMapper).build()) ) { assertThat(response.getStatus(), equalTo(200)); @@ -201,7 +231,7 @@ private void createIndexTyped(String index) throws IOException { private void refreshIndex(String index) throws IOException { try ( - Response response = javaClient().generic() + Response response = javaClient().generic(ClientOptions.DEFAULT) .execute(Requests.builder().endpoint("/" + index + "/_refresh").method("POST").build()) ) { assertThat(response.getStatus(), equalTo(200)); diff --git a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractPingAndInfoIT.java b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractPingAndInfoIT.java index d951799c33..1335509bbd 100644 --- a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractPingAndInfoIT.java +++ b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractPingAndInfoIT.java @@ -17,6 +17,7 @@ import org.opensearch.client.opensearch.OpenSearchClient; import org.opensearch.client.opensearch.core.InfoResponse; import org.opensearch.client.opensearch.generic.Bodies; +import org.opensearch.client.opensearch.generic.OpenSearchGenericClient.ClientOptions; import org.opensearch.client.opensearch.generic.Requests; import org.opensearch.client.opensearch.generic.Response; import org.opensearch.client.transport.endpoints.BooleanResponse; @@ -32,7 +33,9 @@ public void testInfo() throws IOException { InfoResponse info = openSearchClient.info(); // compare with what the low level client outputs - try (Response response = javaClient().generic().execute(Requests.builder().endpoint("/").method("GET").build())) { + try ( + Response response = javaClient().generic(ClientOptions.DEFAULT).execute(Requests.builder().endpoint("/").method("GET").build()) + ) { assertThat(response.getStatus(), equalTo(200)); assertThat(response.getProtocol(), equalTo("HTTP/1.1")); assertThat(response.getBody().isEmpty(), is(false));