Search in sources :

Example 1 with TransportException

use of org.opensearch.client.transport.TransportException in project opensearch-java by opensearch-project.

the class FailingTransport method performRequestAsync.

@Override
public <RequestT, ResponseT, ErrorT> CompletableFuture<ResponseT> performRequestAsync(RequestT request, Endpoint<RequestT, ResponseT, ErrorT> endpoint, @Nullable TransportOptions options) {
    CompletableFuture<ResponseT> future = new CompletableFuture<>();
    future.completeExceptionally(new TransportException("Not implemented"));
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) TransportException(org.opensearch.client.transport.TransportException)

Example 2 with TransportException

use of org.opensearch.client.transport.TransportException 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 TransportException

use of org.opensearch.client.transport.TransportException in project opensearch-java by opensearch-project.

the class RestClientTransport method getHighLevelResponse.

private <ResponseT, ErrorT> ResponseT getHighLevelResponse(org.opensearch.client.Response clientResp, Endpoint<?, ResponseT, ErrorT> endpoint) throws IOException {
    try {
        int statusCode = clientResp.getStatusLine().getStatusCode();
        if (endpoint.isError(statusCode)) {
            JsonpDeserializer<ErrorT> 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);
                }
            } 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 {
            return decodeResponse(statusCode, clientResp.getEntity(), clientResp, endpoint);
        }
    } finally {
        EntityUtils.consume(clientResp.getEntity());
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) ResponseException(org.opensearch.client.ResponseException) InputStream(java.io.InputStream) TransportException(org.opensearch.client.transport.TransportException) BooleanEndpoint(org.opensearch.client.transport.endpoints.BooleanEndpoint) Endpoint(org.opensearch.client.transport.Endpoint) JsonEndpoint(org.opensearch.client.transport.JsonEndpoint) TransportException(org.opensearch.client.transport.TransportException) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) IOException(java.io.IOException) MissingRequiredPropertyException(org.opensearch.client.util.MissingRequiredPropertyException) ResponseException(org.opensearch.client.ResponseException) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) MissingRequiredPropertyException(org.opensearch.client.util.MissingRequiredPropertyException) JsonParser(jakarta.json.stream.JsonParser)

Aggregations

TransportException (org.opensearch.client.transport.TransportException)3 JsonParser (jakarta.json.stream.JsonParser)2 InputStream (java.io.InputStream)2 ResponseException (org.opensearch.client.ResponseException)2 JsonEndpoint (org.opensearch.client.transport.JsonEndpoint)2 BooleanEndpoint (org.opensearch.client.transport.endpoints.BooleanEndpoint)2 IOException (java.io.IOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 HttpEntity (org.apache.http.HttpEntity)1 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)1 JsonpDeserializer (org.opensearch.client.json.JsonpDeserializer)1 OpenSearchException (org.opensearch.client.opensearch._types.OpenSearchException)1 Endpoint (org.opensearch.client.transport.Endpoint)1 BooleanResponse (org.opensearch.client.transport.endpoints.BooleanResponse)1 MissingRequiredPropertyException (org.opensearch.client.util.MissingRequiredPropertyException)1