Search in sources :

Example 1 with JsonpDeserializer

use of co.elastic.clients.json.JsonpDeserializer in project elasticsearch-java by elastic.

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", endpoint.id(), 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() + "'", endpoint.id());
    }
}
Also used : JsonpDeserializer(co.elastic.clients.json.JsonpDeserializer) ResponseException(org.elasticsearch.client.ResponseException) InputStream(java.io.InputStream) BooleanEndpoint(co.elastic.clients.transport.endpoints.BooleanEndpoint) JsonEndpoint(co.elastic.clients.transport.JsonEndpoint) BooleanResponse(co.elastic.clients.transport.endpoints.BooleanResponse) TransportException(co.elastic.clients.transport.TransportException) JsonParser(jakarta.json.stream.JsonParser)

Aggregations

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