HttpCall extends Call.Base<V> { V parseResponse(AggregatedHttpResponse response, BodyConverter<V> bodyConverter) throws IOException { 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 ((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) { } 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); } } HttpCall(WebClient httpClient, RequestSupplier request, BodyConverter<V> bodyConverter,
    String name); @Override HttpCall<V> clone(); @Override String toString(); final RequestSupplier request; }
@Test void releasesAllReferencesToByteBuf() { byte[] message = "{\"Message\":\"error\"}".getBytes(UTF_8); ByteBuf encodedBuf = PooledByteBufAllocator.DEFAULT.buffer(message.length); encodedBuf.writeBytes(message); AggregatedHttpResponse response = AggregatedHttpResponse.of( ResponseHeaders.of(HttpStatus.FORBIDDEN), HttpData.wrap(encodedBuf) ); HttpCall<Object> call = http.newCall(REQUEST, NULL, "test"); assertThatThrownBy(() -> call.parseResponse(response, NULL)).hasMessage("error"); assertThat(encodedBuf.refCnt()).isEqualTo(0); }