Skip to content

Commit d567c3a

Browse files
committed
SWS-563 - Provide support for Apache HttpClient 4.0
1 parent 7c9ae0c commit d567c3a

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

core/src/main/java/org/springframework/ws/transport/http/HttpComponentsConnection.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.http.client.HttpClient;
3636
import org.apache.http.client.methods.HttpPost;
3737
import org.apache.http.entity.ByteArrayEntity;
38+
import org.apache.http.protocol.HttpContext;
3839
import org.apache.http.util.EntityUtils;
3940

4041
/**
@@ -52,15 +53,18 @@ public class HttpComponentsConnection extends AbstractHttpSenderConnection {
5253

5354
private final HttpPost httpPost;
5455

56+
private final HttpContext httpContext;
57+
5558
private HttpResponse httpResponse;
5659

5760
private ByteArrayOutputStream requestBuffer;
5861

59-
protected HttpComponentsConnection(HttpClient httpClient, HttpPost httpPost) {
62+
protected HttpComponentsConnection(HttpClient httpClient, HttpPost httpPost, HttpContext httpContext) {
6063
Assert.notNull(httpClient, "httpClient must not be null");
6164
Assert.notNull(httpPost, "httpPost must not be null");
6265
this.httpClient = httpClient;
6366
this.httpPost = httpPost;
67+
this.httpContext = httpContext;
6468
}
6569

6670
public HttpPost getHttpPost() {
@@ -108,7 +112,12 @@ protected OutputStream getRequestOutputStream() throws IOException {
108112
protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
109113
httpPost.setEntity(new ByteArrayEntity(requestBuffer.toByteArray()));
110114
requestBuffer = null;
111-
httpResponse = httpClient.execute(httpPost);
115+
if (httpContext != null) {
116+
httpResponse = httpClient.execute(httpPost, httpContext);
117+
}
118+
else {
119+
httpResponse = httpClient.execute(httpPost);
120+
}
112121
}
113122

114123
/*

core/src/main/java/org/springframework/ws/transport/http/HttpComponentsMessageSender.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.apache.http.impl.conn.SingleClientConnManager;
4343
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
4444
import org.apache.http.params.HttpConnectionParams;
45-
import org.apache.http.protocol.BasicHttpProcessor;
4645
import org.apache.http.protocol.HTTP;
4746
import org.apache.http.protocol.HttpContext;
4847

@@ -78,14 +77,10 @@ public class HttpComponentsMessageSender extends AbstractHttpWebServiceMessageSe
7877
* default {@link SingleClientConnManager}.
7978
*/
8079
public HttpComponentsMessageSender() {
81-
httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager()) {
82-
@Override
83-
protected BasicHttpProcessor createHttpProcessor() {
84-
BasicHttpProcessor processor = super.createHttpProcessor();
85-
processor.addInterceptor(new ProtocolExceptionOverrideInterceptor(), 0);
86-
return processor;
87-
}
88-
};
80+
DefaultHttpClient defaultClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
81+
defaultClient.addRequestInterceptor(new RemoveSoapHeadersInterceptor(), 0);
82+
83+
this.httpClient = defaultClient;
8984
setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS);
9085
setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
9186
}
@@ -226,7 +221,19 @@ public WebServiceConnection createConnection(URI uri) throws IOException {
226221
httpPost.addHeader(HttpTransportConstants.HEADER_ACCEPT_ENCODING,
227222
HttpTransportConstants.CONTENT_ENCODING_GZIP);
228223
}
229-
return new HttpComponentsConnection(getHttpClient(), httpPost);
224+
HttpContext httpContext = createContext(uri);
225+
return new HttpComponentsConnection(getHttpClient(), httpPost, httpContext);
226+
}
227+
228+
/**
229+
* Template method that allows for creation of a {@link HttpContext} for the given uri. Default implementation
230+
* returns {@code null}.
231+
*
232+
* @param uri the URI to create the context for
233+
* @return the context, or {@code null}
234+
*/
235+
protected HttpContext createContext(URI uri) {
236+
return null;
230237
}
231238

232239
public void destroy() throws Exception {
@@ -238,7 +245,7 @@ public void destroy() throws Exception {
238245
* {@code Transfer-Encoding} headers from the request. Necessary, because SAAJ and other SOAP implementations set these
239246
* headers themselves, and HttpClient throws an exception if they have been set.
240247
*/
241-
private static class ProtocolExceptionOverrideInterceptor implements HttpRequestInterceptor {
248+
private static class RemoveSoapHeadersInterceptor implements HttpRequestInterceptor {
242249

243250
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
244251
if (request instanceof HttpEntityEnclosingRequest) {

0 commit comments

Comments
 (0)