Search in sources :

Example 1 with ResponseException

use of org.opensearch.client.ResponseException in project opensearch-java by opensearch-project.

the class RequestOptionsTest method getProps.

private Properties getProps(OpenSearchClient client) throws IOException {
    ResponseException ex = assertThrows(ResponseException.class, client::info);
    assertEquals(418, ex.getResponse().getStatusLine().getStatusCode());
    Properties result = new Properties();
    result.load(ex.getResponse().getEntity().getContent());
    return result;
}
Also used : ResponseException(org.opensearch.client.ResponseException) Properties(java.util.Properties)

Example 2 with ResponseException

use of org.opensearch.client.ResponseException in project opensearch-java by opensearch-project.

the class RestClientTransport method decodeResponse.

private <ResponseT> ResponseT decodeResponse(int statusCode, @Nullable HttpEntity entity, Response clientResp, Endpoint<?, ResponseT, ?> endpoint) throws IOException {
    if (endpoint instanceof BooleanEndpoint) {
        BooleanEndpoint<?> bep = (BooleanEndpoint<?>) endpoint;
        @SuppressWarnings("unchecked") ResponseT response = (ResponseT) new BooleanResponse(bep.getResult(statusCode));
        return response;
    } else if (endpoint instanceof JsonEndpoint) {
        @SuppressWarnings("unchecked") JsonEndpoint<?, ResponseT, ?> jsonEndpoint = (JsonEndpoint<?, ResponseT, ?>) endpoint;
        // Successful response
        ResponseT response = null;
        JsonpDeserializer<ResponseT> responseParser = jsonEndpoint.responseDeserializer();
        if (responseParser != null) {
            // Expecting a body
            if (entity == null) {
                throw new TransportException("Expecting a response body, but none was sent", new ResponseException(clientResp));
            }
            InputStream content = entity.getContent();
            try (JsonParser parser = mapper.jsonProvider().createParser(content)) {
                response = responseParser.deserialize(parser, mapper);
            }
            ;
        }
        return response;
    } else {
        throw new TransportException("Unhandled endpoint type: '" + endpoint.getClass().getName() + "'");
    }
}
Also used : JsonpDeserializer(org.opensearch.client.json.JsonpDeserializer) ResponseException(org.opensearch.client.ResponseException) InputStream(java.io.InputStream) BooleanEndpoint(org.opensearch.client.transport.endpoints.BooleanEndpoint) JsonEndpoint(org.opensearch.client.transport.JsonEndpoint) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) TransportException(org.opensearch.client.transport.TransportException) JsonParser(jakarta.json.stream.JsonParser)

Example 3 with ResponseException

use of org.opensearch.client.ResponseException in project OpenSearch by opensearch-project.

the class OpenSearchNodesSnifferTests method testSniffNodes.

public void testSniffNodes() throws IOException {
    HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
    try (RestClient restClient = RestClient.builder(httpHost).build()) {
        OpenSearchNodesSniffer sniffer = new OpenSearchNodesSniffer(restClient, sniffRequestTimeout, scheme);
        try {
            List<Node> sniffedNodes = sniffer.sniff();
            if (sniffResponse.isFailure) {
                fail("sniffNodes should have failed");
            }
            assertEquals(sniffResponse.result, sniffedNodes);
        } catch (ResponseException e) {
            Response response = e.getResponse();
            if (sniffResponse.isFailure) {
                final String errorPrefix = "method [GET], host [" + httpHost + "], URI [/_nodes/http?timeout=" + sniffRequestTimeout + "ms], status line [HTTP/1.1";
                assertThat(e.getMessage(), startsWith(errorPrefix));
                assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode)));
                assertThat(response.getHost(), equalTo(httpHost));
                assertThat(response.getStatusLine().getStatusCode(), equalTo(sniffResponse.nodesInfoResponseCode));
                assertThat(response.getRequestLine().toString(), equalTo("GET /_nodes/http?timeout=" + sniffRequestTimeout + "ms HTTP/1.1"));
            } else {
                fail("sniffNodes should have succeeded: " + response.getStatusLine());
            }
        }
    }
}
Also used : Response(org.opensearch.client.Response) ResponseException(org.opensearch.client.ResponseException) HttpHost(org.apache.http.HttpHost) Node(org.opensearch.client.Node) RestClient(org.opensearch.client.RestClient) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 4 with ResponseException

use of org.opensearch.client.ResponseException in project OpenSearch by opensearch-project.

the class RequestsWithoutContentIT method testPutScriptMissingBody.

public void testPutScriptMissingBody() throws IOException {
    ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/_scripts/lang")));
    assertResponseException(responseException, "request body is required");
}
Also used : ResponseException(org.opensearch.client.ResponseException) Request(org.opensearch.client.Request)

Example 5 with ResponseException

use of org.opensearch.client.ResponseException in project OpenSearch by opensearch-project.

the class RequestsWithoutContentIT method testPutMappingsMissingBody.

public void testPutMappingsMissingBody() throws IOException {
    ResponseException responseException = expectThrows(ResponseException.class, () -> client().performRequest(new Request(randomBoolean() ? "POST" : "PUT", "/test_index/_mapping")));
    assertResponseException(responseException, "request body is required");
}
Also used : ResponseException(org.opensearch.client.ResponseException) Request(org.opensearch.client.Request)

Aggregations

ResponseException (org.opensearch.client.ResponseException)49 Request (org.opensearch.client.Request)43 Response (org.opensearch.client.Response)23 Map (java.util.Map)10 RequestOptions (org.opensearch.client.RequestOptions)8 IOException (java.io.IOException)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 HashMap (java.util.HashMap)6 InputStream (java.io.InputStream)5 ObjectPath (org.opensearch.test.rest.yaml.ObjectPath)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 PutRepositoryRequest (org.opensearch.action.admin.cluster.repositories.put.PutRepositoryRequest)4 RestClient (org.opensearch.client.RestClient)4 Settings (org.opensearch.common.settings.Settings)4 LegacyESVersion (org.opensearch.LegacyESVersion)3 Version (org.opensearch.Version)3 JsonParser (jakarta.json.stream.JsonParser)2 KeyManagementException (java.security.KeyManagementException)2 KeyStoreException (java.security.KeyStoreException)2