Skip to content

Commit

Permalink
SPR-8367 - 3.1.0.M2 update of the RestTemplate for Apache HTTP Compon…
Browse files Browse the repository at this point in the history
…ents will default to sending 2 requests on authentication and doesn't support HttpContext parameters without significant extention/rewrite
  • Loading branch information
poutsma committed Dec 7, 2011
1 parent 29e9690 commit f9144ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
Expand Up @@ -21,16 +21,17 @@
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;

import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.protocol.HTTP;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.apache.http.protocol.HttpContext;

/**
* {@link org.springframework.http.client.ClientHttpRequest} implementation that uses
Expand All @@ -49,10 +50,13 @@ final class HttpComponentsClientHttpRequest extends AbstractBufferingClientHttpR

private final HttpUriRequest httpRequest;

private final HttpContext httpContext;


public HttpComponentsClientHttpRequest(HttpClient httpClient, HttpUriRequest httpRequest) {
public HttpComponentsClientHttpRequest(HttpClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) {
this.httpClient = httpClient;
this.httpRequest = httpRequest;
this.httpContext = httpContext;
}


Expand Down Expand Up @@ -81,7 +85,7 @@ protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] buffere
HttpEntity requestEntity = new ByteArrayEntity(bufferedOutput);
entityEnclosingRequest.setEntity(requestEntity);
}
HttpResponse httpResponse = this.httpClient.execute(this.httpRequest);
HttpResponse httpResponse = this.httpClient.execute(this.httpRequest, this.httpContext);
return new HttpComponentsClientHttpResponse(httpResponse);
}

Expand Down
Expand Up @@ -19,6 +19,10 @@
import java.io.IOException;
import java.net.URI;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -35,10 +39,7 @@
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.CoreConnectionPNames;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.http.HttpMethod;
import org.springframework.util.Assert;
import org.apache.http.protocol.HttpContext;

/**
* {@link org.springframework.http.client.ClientHttpRequestFactory} implementation that uses
Expand Down Expand Up @@ -126,11 +127,10 @@ public void setReadTimeout(int timeout) {
getHttpClient().getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
}


public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
HttpUriRequest httpRequest = createHttpUriRequest(httpMethod, uri);
postProcessHttpRequest(httpRequest);
return new HttpComponentsClientHttpRequest(getHttpClient(), httpRequest);
return new HttpComponentsClientHttpRequest(getHttpClient(), httpRequest, createHttpContext(httpMethod, uri));
}

/**
Expand Down Expand Up @@ -169,6 +169,17 @@ protected HttpUriRequest createHttpUriRequest(HttpMethod httpMethod, URI uri) {
protected void postProcessHttpRequest(HttpUriRequest request) {
}

/**
* Template methods that creates a {@link HttpContext} for the given HTTP method and URI.
* <p>The default implementation returns {@code null}.
* @param httpMethod the HTTP method
* @param uri the URI
* @return the http context
*/
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
return null;
}

/**
* Shutdown hook that closes the underlying
* {@link org.apache.http.conn.ClientConnectionManager ClientConnectionManager}'s
Expand Down

0 comments on commit f9144ea

Please sign in to comment.