Search in sources :

Example 1 with JsonpDeserializer

use of org.opensearch.client.json.JsonpDeserializer 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)

Aggregations

JsonParser (jakarta.json.stream.JsonParser)1 InputStream (java.io.InputStream)1 ResponseException (org.opensearch.client.ResponseException)1 JsonpDeserializer (org.opensearch.client.json.JsonpDeserializer)1 JsonEndpoint (org.opensearch.client.transport.JsonEndpoint)1 TransportException (org.opensearch.client.transport.TransportException)1 BooleanEndpoint (org.opensearch.client.transport.endpoints.BooleanEndpoint)1 BooleanResponse (org.opensearch.client.transport.endpoints.BooleanResponse)1