Search in sources :

Example 1 with HttpStatus

use of com.linecorp.armeria.common.HttpStatus 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

JsonParser (com.fasterxml.jackson.core.JsonParser)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 HttpData (com.linecorp.armeria.common.HttpData)1 HttpStatus (com.linecorp.armeria.common.HttpStatus)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1