public ApacheHCUtils(ContentMetadataCodec contentMetadataCodec) {
this.contentMetadataCodec = contentMetadataCodec;
}
public HttpUriRequest convertToApacheRequest(HttpRequest request) {
HttpUriRequest apacheRequest;
if (request.getMethod().equals(HttpMethod.HEAD)) {
apacheRequest = new HttpHead(request.getEndpoint());
} else if (request.getMethod().equals(HttpMethod.GET)) {
apacheRequest = new HttpGet(request.getEndpoint());
} else if (request.getMethod().equals(HttpMethod.DELETE)) {
apacheRequest = new HttpDelete(request.getEndpoint());
} else if (request.getMethod().equals(HttpMethod.PUT)) {
apacheRequest = new HttpPut(request.getEndpoint());
apacheRequest.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
} else if (request.getMethod().equals(HttpMethod.POST)) {
apacheRequest = new HttpPost(request.getEndpoint());
} else {
final String method = request.getMethod();
if (request.getPayload() != null)
apacheRequest = new HttpEntityEnclosingRequestBase() {
@Override
public String getMethod() {
return method;
}
};
else
apacheRequest = new HttpRequestBase() {
@Override
public String getMethod() {
return method;
}
};
HttpRequestBase.class.cast(apacheRequest).setURI(request.getEndpoint());
}
Payload payload = request.getPayload();
// Since we may remove headers, ensure they are added to the apache
// request after this block
if (apacheRequest instanceof HttpEntityEnclosingRequest) {
if (payload != null) {
addEntityForContent(HttpEntityEnclosingRequest.class.cast(apacheRequest), payload);
}
} else {
apacheRequest.addHeader(HttpHeaders.CONTENT_LENGTH, "0");
}
for (Map.Entry<String, String> entry : request.getHeaders().entries()) {
String header = entry.getKey();
// apache automatically tries to add content length header
if (!header.equals(HttpHeaders.CONTENT_LENGTH))
apacheRequest.addHeader(header, entry.getValue());
}
apacheRequest.addHeader(HttpHeaders.USER_AGENT, USER_AGENT);
return apacheRequest;
}