SWS-563 - Provide support for Apache HttpClient 4.0

This commit is contained in:
Arjen Poutsma
2012-05-09 08:53:34 +00:00
parent 7c9ae0ccc7
commit d567c3a3fb
2 changed files with 29 additions and 13 deletions

View File

@@ -35,6 +35,7 @@ import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
/**
@@ -52,15 +53,18 @@ public class HttpComponentsConnection extends AbstractHttpSenderConnection {
private final HttpPost httpPost;
private final HttpContext httpContext;
private HttpResponse httpResponse;
private ByteArrayOutputStream requestBuffer;
protected HttpComponentsConnection(HttpClient httpClient, HttpPost httpPost) {
protected HttpComponentsConnection(HttpClient httpClient, HttpPost httpPost, HttpContext httpContext) {
Assert.notNull(httpClient, "httpClient must not be null");
Assert.notNull(httpPost, "httpPost must not be null");
this.httpClient = httpClient;
this.httpPost = httpPost;
this.httpContext = httpContext;
}
public HttpPost getHttpPost() {
@@ -108,7 +112,12 @@ public class HttpComponentsConnection extends AbstractHttpSenderConnection {
protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
httpPost.setEntity(new ByteArrayEntity(requestBuffer.toByteArray()));
requestBuffer = null;
httpResponse = httpClient.execute(httpPost);
if (httpContext != null) {
httpResponse = httpClient.execute(httpPost, httpContext);
}
else {
httpResponse = httpClient.execute(httpPost);
}
}
/*

View File

@@ -42,7 +42,6 @@ import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
@@ -78,14 +77,10 @@ public class HttpComponentsMessageSender extends AbstractHttpWebServiceMessageSe
* default {@link SingleClientConnManager}.
*/
public HttpComponentsMessageSender() {
httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager()) {
@Override
protected BasicHttpProcessor createHttpProcessor() {
BasicHttpProcessor processor = super.createHttpProcessor();
processor.addInterceptor(new ProtocolExceptionOverrideInterceptor(), 0);
return processor;
}
};
DefaultHttpClient defaultClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
defaultClient.addRequestInterceptor(new RemoveSoapHeadersInterceptor(), 0);
this.httpClient = defaultClient;
setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS);
setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
}
@@ -226,7 +221,19 @@ public class HttpComponentsMessageSender extends AbstractHttpWebServiceMessageSe
httpPost.addHeader(HttpTransportConstants.HEADER_ACCEPT_ENCODING,
HttpTransportConstants.CONTENT_ENCODING_GZIP);
}
return new HttpComponentsConnection(getHttpClient(), httpPost);
HttpContext httpContext = createContext(uri);
return new HttpComponentsConnection(getHttpClient(), httpPost, httpContext);
}
/**
* Template method that allows for creation of a {@link HttpContext} for the given uri. Default implementation
* returns {@code null}.
*
* @param uri the URI to create the context for
* @return the context, or {@code null}
*/
protected HttpContext createContext(URI uri) {
return null;
}
public void destroy() throws Exception {
@@ -238,7 +245,7 @@ public class HttpComponentsMessageSender extends AbstractHttpWebServiceMessageSe
* {@code Transfer-Encoding} headers from the request. Necessary, because SAAJ and other SOAP implementations set these
* headers themselves, and HttpClient throws an exception if they have been set.
*/
private static class ProtocolExceptionOverrideInterceptor implements HttpRequestInterceptor {
private static class RemoveSoapHeadersInterceptor implements HttpRequestInterceptor {
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
if (request instanceof HttpEntityEnclosingRequest) {