Skip to content

Commit a19c411

Browse files
committed
SWS-563 - Provide support for Apache HttpClient 4.0
1 parent 8df20ea commit a19c411

File tree

7 files changed

+549
-4
lines changed

7 files changed

+549
-4
lines changed

core/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@
136136
<artifactId>mail</artifactId>
137137
</dependency>
138138
<!-- Transport dependencies -->
139+
<dependency>
140+
<groupId>org.apache.httpcomponents</groupId>
141+
<artifactId>httpclient</artifactId>
142+
<optional>true</optional>
143+
</dependency>
139144
<dependency>
140145
<groupId>commons-httpclient</groupId>
141146
<artifactId>commons-httpclient</artifactId>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2005-2010 the original author or authors.
2+
* Copyright 2005-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -42,7 +42,9 @@
4242
*
4343
* @author Arjen Poutsma
4444
* @since 1.0.0
45+
* @deprecated In favor of {@link HttpComponentsConnection}
4546
*/
47+
@Deprecated
4648
public class CommonsHttpConnection extends AbstractHttpSenderConnection {
4749

4850
private final HttpClient httpClient;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2005-2010 the original author or authors.
2+
* Copyright 2005-2012 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -51,7 +51,9 @@
5151
* @see HttpClient
5252
* @see #setCredentials(Credentials)
5353
* @since 1.0.0
54+
* @deprecated In favor of {@link HttpComponentsMessageSender}
5455
*/
56+
@Deprecated
5557
public class CommonsHttpMessageSender extends AbstractHttpWebServiceMessageSender
5658
implements InitializingBean, DisposableBean {
5759

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright 2005-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ws.transport.http;
18+
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.OutputStream;
23+
import java.net.URI;
24+
import java.net.URISyntaxException;
25+
import java.util.Arrays;
26+
import java.util.Iterator;
27+
28+
import org.springframework.util.Assert;
29+
import org.springframework.ws.WebServiceMessage;
30+
import org.springframework.ws.transport.WebServiceConnection;
31+
32+
import org.apache.http.Header;
33+
import org.apache.http.HttpEntity;
34+
import org.apache.http.HttpResponse;
35+
import org.apache.http.client.HttpClient;
36+
import org.apache.http.client.methods.HttpPost;
37+
import org.apache.http.entity.ByteArrayEntity;
38+
import org.apache.http.util.EntityUtils;
39+
40+
/**
41+
* Implementation of {@link WebServiceConnection} that is based on Apache HttpClient. Exposes a {@link HttpPost} and
42+
* {@link HttpResponse}.
43+
*
44+
* @author Alan Stewart
45+
* @author Barry Pitman
46+
* @author Arjen Poutsma
47+
* @since 2.1.0
48+
*/
49+
public class HttpComponentsConnection extends AbstractHttpSenderConnection {
50+
51+
private final HttpClient httpClient;
52+
53+
private final HttpPost httpPost;
54+
55+
private HttpResponse httpResponse;
56+
57+
private ByteArrayOutputStream requestBuffer;
58+
59+
protected HttpComponentsConnection(HttpClient httpClient, HttpPost httpPost) {
60+
Assert.notNull(httpClient, "httpClient must not be null");
61+
Assert.notNull(httpPost, "httpPost must not be null");
62+
this.httpClient = httpClient;
63+
this.httpPost = httpPost;
64+
}
65+
66+
public HttpPost getHttpPost() {
67+
return httpPost;
68+
}
69+
70+
public HttpResponse getHttpResponse() {
71+
return httpResponse;
72+
}
73+
74+
@Override
75+
public void onClose() throws IOException {
76+
if (httpResponse != null && httpResponse.getEntity() != null) {
77+
EntityUtils.consume(httpResponse.getEntity());
78+
}
79+
}
80+
81+
/*
82+
* URI
83+
*/
84+
public URI getUri() throws URISyntaxException {
85+
return new URI(httpPost.getURI().toString());
86+
}
87+
88+
/*
89+
* Sending request
90+
*/
91+
92+
@Override
93+
protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
94+
requestBuffer = new ByteArrayOutputStream();
95+
}
96+
97+
@Override
98+
protected void addRequestHeader(String name, String value) throws IOException {
99+
httpPost.addHeader(name, value);
100+
}
101+
102+
@Override
103+
protected OutputStream getRequestOutputStream() throws IOException {
104+
return requestBuffer;
105+
}
106+
107+
@Override
108+
protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
109+
httpPost.setEntity(new ByteArrayEntity(requestBuffer.toByteArray()));
110+
requestBuffer = null;
111+
httpResponse = httpClient.execute(httpPost);
112+
}
113+
114+
/*
115+
* Receiving response
116+
*/
117+
118+
@Override
119+
protected int getResponseCode() throws IOException {
120+
return httpResponse.getStatusLine().getStatusCode();
121+
}
122+
123+
@Override
124+
protected String getResponseMessage() throws IOException {
125+
return httpResponse.getStatusLine().getReasonPhrase();
126+
}
127+
128+
@Override
129+
protected long getResponseContentLength() throws IOException {
130+
HttpEntity entity = httpResponse.getEntity();
131+
if (entity != null) {
132+
return entity.getContentLength();
133+
}
134+
return 0;
135+
}
136+
137+
@Override
138+
protected InputStream getRawResponseInputStream() throws IOException {
139+
HttpEntity entity = httpResponse.getEntity();
140+
if (entity != null) {
141+
return entity.getContent();
142+
}
143+
throw new IllegalStateException("Response has no enclosing response entity, cannot create input stream");
144+
}
145+
146+
@Override
147+
protected Iterator<String> getResponseHeaderNames() throws IOException {
148+
Header[] headers = httpResponse.getAllHeaders();
149+
String[] names = new String[headers.length];
150+
for (int i = 0; i < headers.length; i++) {
151+
names[i] = headers[i].getName();
152+
}
153+
return Arrays.asList(names).iterator();
154+
}
155+
156+
@Override
157+
protected Iterator<String> getResponseHeaders(String name) throws IOException {
158+
Header[] headers = httpResponse.getHeaders(name);
159+
String[] values = new String[headers.length];
160+
for (int i = 0; i < headers.length; i++) {
161+
values[i] = headers[i].getValue();
162+
}
163+
return Arrays.asList(values).iterator();
164+
}
165+
}

0 commit comments

Comments
 (0)