Search in sources :

Example 11 with HttpData

use of com.linecorp.armeria.common.HttpData in project zipkin by openzipkin.

the class HttpCall method parseResponse.

V parseResponse(AggregatedHttpResponse response, BodyConverter<V> bodyConverter) throws IOException {
    // Handle the case where there is no content, as that means we have no resources to release.
    HttpStatus status = response.status();
    if (response.content().isEmpty()) {
        if (status.codeClass().equals(HttpStatusClass.SUCCESS)) {
            return null;
        } else if (status.code() == 404) {
            throw new FileNotFoundException(request.headers().path());
        } else {
            throw new RuntimeException("response for " + request.headers().path() + " failed: " + response.status());
        }
    }
    // If this is a client or server error, we look for a json message.
    if ((status.codeClass().equals(HttpStatusClass.CLIENT_ERROR) || status.codeClass().equals(HttpStatusClass.SERVER_ERROR))) {
        bodyConverter = (parser, contentString) -> {
            String message = null;
            try {
                JsonNode root = OBJECT_MAPPER.readTree(parser);
                message = root.findPath("reason").textValue();
                if (message == null)
                    message = root.at("/Message").textValue();
            } catch (RuntimeException | IOException possiblyParseException) {
            // EmptyCatch ignored
            }
            throw new RuntimeException(message != null ? message : "response for " + request.headers().path() + " failed: " + contentString.get());
        };
    }
    try (HttpData content = response.content();
        InputStream stream = content.toInputStream();
        JsonParser parser = JSON_FACTORY.createParser(stream)) {
        if (status.code() == 404)
            throw new FileNotFoundException(request.headers().path());
        return bodyConverter.convert(parser, content::toStringUtf8);
    }
}
Also used : HttpStatus(com.linecorp.armeria.common.HttpStatus) HttpData(com.linecorp.armeria.common.HttpData) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

HttpData (com.linecorp.armeria.common.HttpData)11 HttpResponse (com.linecorp.armeria.common.HttpResponse)5 RequestHeaders (com.linecorp.armeria.common.RequestHeaders)4 ByteBuf (io.netty.buffer.ByteBuf)4 ResponseHeaders (com.linecorp.armeria.common.ResponseHeaders)3 HttpHeaderNames (com.linecorp.armeria.common.HttpHeaderNames)2 HttpHeaders (com.linecorp.armeria.common.HttpHeaders)2 HttpMethod (com.linecorp.armeria.common.HttpMethod)2 HttpRequest (com.linecorp.armeria.common.HttpRequest)2 HttpStatus (com.linecorp.armeria.common.HttpStatus)2 IOException (java.io.IOException)2 ByteBuffer (java.nio.ByteBuffer)2 Map (java.util.Map)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)1 JsonSerialize (com.fasterxml.jackson.databind.annotation.JsonSerialize)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ByteString (com.google.protobuf.ByteString)1