final HTTPRequestType requestType,
final HTTPVersion version,
final ProxyManager proxyManager,
final XMLPipelineContext xmlPipelineContext) throws HTTPException {
final HttpState state = new HttpState();
state.setCookiePolicy(CookiePolicy.COMPATIBILITY);
HttpClientFactory factory = HttpClientFactory.getDefaultInstance();
HttpClientBuilder builder = factory.createClientBuilder();
builder.setState(state);
builder.setConnectionTimeout(timeout);
builder.setRoundTripTimeout(timeout);
// create the HTTPClient instance.
final HttpClient httpClient = builder.buildHttpClient();
// return an HTTPRequestExecutor that uses HttpClient to perform
// the request
return new HTTPRequestExecutor() {
/**
* The request method to use.
*/
HttpMethod method = null;
/**
* The list of request parameters
*/
List requestParameters = null;
/**
* A list containing the request headers.
*/
List requestHeaders = null;
// javadoc inherited
public HTTPResponseAccessor execute() throws HTTPException {
// create the request method this will be POST or GET depending
// on the requestType argument. This will copy the request
// parameters to the appropraite place.
method = createMethod(url, requestType, requestParameters);
transferRequestHeaders(method, requestHeaders);
// set the http version for the request
((HttpMethodBase) method).setHttp11(
version == HTTPVersion.HTTP_1_1);
if (proxyManager != null) {
try {
URL realUrl = new URL(url);
// Get the proxy config to use for the host.
Proxy proxy = proxyManager.getProxyForHost(
realUrl.getHost());
if (proxy != null) {
method.getHostConfiguration().
setProxy(proxy.getHost(), proxy.getPort());
if (proxy.useAuthorization()) {
method.setDoAuthentication(true);
// mock up a authentication challenge so we can get
// the response (which can be sent before the
// challenge if we want to save time and effort)
method.setRequestHeader(
HTTPAuthorizationScheme.PROXY_AUTHORIZATION_HEADER,
HTTPAuthorizationScheme.BASIC.
createResponseForChallenge(HTTPAuthorizationScheme.MOCK_CHALLENGE_BASIC,
proxy));
}
}
} catch (MalformedURLException mue) {
LOGGER.error(mue);
}
}
try {
HttpStatusCode statusCode = httpClient.executeMethod(method);
if (xmlPipelineContext != null) {
final DependencyContext dependencyContext =
xmlPipelineContext.getDependencyContext();
if (dependencyContext != null &&
dependencyContext.isTrackingDependencies()) {
dependencyContext.addDependency(
UncacheableDependency.getInstance());
}
}
return new HTTPClientResponseAccessor(method, statusCode);
} catch (IOException e) {
throw new HTTPException(e);
}
}
// javadoc inherited
public void release() {
if (method != null) {
method.releaseConnection();
}
}
// javadoc inherited
public void addRequestParameter(RequestParameter parameter) {
// if no list of request parameters exists then create one and
// add the request parameter
if (parameter != null) {
if (requestParameters == null) {
requestParameters = new ArrayList();
}
requestParameters.add(parameter);
}
}
// javadoc inherited
public void addRequestHeader(Header header) {
if (header != null) {
if (requestHeaders == null) {
requestHeaders = new ArrayList();
}
requestHeaders.add(header);
}
}
// javadoc inherited
public void addRequestCookie(Cookie cookie) {
// For each cookie we need to create an equivalent HttpClient
// cookie
org.apache.commons.httpclient.Cookie httpClientCookie =
cookieToHTTPClientCookie(cookie);
// Now add the cookie header to the HttpState. The state object
// is needed as cookies set directly as headers on the request
// are ignored.
state.addCookie(httpClientCookie);
}
/**
* Method that transfers the list of request headers to the
* HttpMethod supplied.