@Override
protected HttpResponse invoke(HttpUriRequest nativeRequest) throws IOException {
org.apache.http.HttpResponse apacheResponse = executeRequest(nativeRequest);
Payload payload = null;
if (apacheResponse.getEntity() != null)
try {
payload = Payloads.newInputStreamPayload(consumeOnClose(apacheResponse.getEntity().getContent()));
if (apacheResponse.getEntity().getContentLength() >= 0)
payload.getContentMetadata().setContentLength(apacheResponse.getEntity().getContentLength());
if (apacheResponse.getEntity().getContentType() != null)
payload.getContentMetadata().setContentType(apacheResponse.getEntity().getContentType().getValue());
} catch (IOException e) {
logger.warn(e, "couldn't receive payload for request: %s", nativeRequest.getRequestLine());
throw e;
}
Multimap<String, String> headers = LinkedHashMultimap.create();
for (Header header : apacheResponse.getAllHeaders()) {
headers.put(header.getName(), header.getValue());
}
if (payload != null) {
contentMetadataCodec.fromHeaders(payload.getContentMetadata(), headers);
}
return HttpResponse.builder().statusCode(apacheResponse.getStatusLine().getStatusCode())
.message(apacheResponse.getStatusLine().getReasonPhrase())
.payload(payload)
.headers(filterOutContentHeaders(headers)).build();