for (HttpRequestFilter filter : request.getFilters()) {
request = filter.filter(request);
}
RequestBuilder builder = new RequestBuilder(request.getMethod());
builder.setUrl(request.getEndpoint().toASCIIString());
Payload payload = request.getPayload();
if (payload != null) {
boolean chunked = "chunked".equals(request.getFirstHeaderOrNull("Transfer-Encoding"));
if (request.getPayload().getContentMetadata().getContentMD5() != null)
builder.addHeader("Content-MD5", CryptoStreams.base64(request.getPayload().getContentMetadata()
.getContentMD5()));
if (request.getPayload().getContentMetadata().getContentType() != null)
builder.addHeader(HttpHeaders.CONTENT_TYPE, request.getPayload().getContentMetadata().getContentType());
if (request.getPayload().getContentMetadata().getContentLanguage() != null)
builder.addHeader(HttpHeaders.CONTENT_LANGUAGE, request.getPayload().getContentMetadata()
.getContentLanguage());
if (request.getPayload().getContentMetadata().getContentEncoding() != null)
builder.addHeader(HttpHeaders.CONTENT_ENCODING, request.getPayload().getContentMetadata()
.getContentEncoding());
if (request.getPayload().getContentMetadata().getContentDisposition() != null)
builder.addHeader("Content-Disposition", request.getPayload().getContentMetadata()
.getContentDisposition());
if (!chunked) {
Long length = checkNotNull(request.getPayload().getContentMetadata().getContentLength(),
"payload.getContentLength");
builder.addHeader(HttpHeaders.CONTENT_LENGTH, length.toString());
}
setPayload(builder, payload);
} else {
builder.addHeader(HttpHeaders.CONTENT_LENGTH, "0");
}
builder.addHeader(HttpHeaders.USER_AGENT, USER_AGENT);
for (Map<String, String> entry : request.getHeaders().entries()) {
builder.addHeader(entry.getKey(), entry.getValue());
}
return builder.build();
}