1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.transport.http;
18
19 import java.io.IOException;
20 import java.net.URI;
21 import java.util.Map;
22
23 import org.springframework.beans.factory.DisposableBean;
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.util.Assert;
26 import org.springframework.ws.transport.WebServiceConnection;
27
28 import org.apache.commons.httpclient.Credentials;
29 import org.apache.commons.httpclient.HostConfiguration;
30 import org.apache.commons.httpclient.HttpClient;
31 import org.apache.commons.httpclient.HttpConnectionManager;
32 import org.apache.commons.httpclient.HttpURL;
33 import org.apache.commons.httpclient.HttpsURL;
34 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
35 import org.apache.commons.httpclient.NTCredentials;
36 import org.apache.commons.httpclient.URIException;
37 import org.apache.commons.httpclient.UsernamePasswordCredentials;
38 import org.apache.commons.httpclient.auth.AuthScope;
39 import org.apache.commons.httpclient.methods.PostMethod;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 @Deprecated
57 public class CommonsHttpMessageSender extends AbstractHttpWebServiceMessageSender
58 implements InitializingBean, DisposableBean {
59
60 private static final int DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS = (60 * 1000);
61
62 private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = (60 * 1000);
63
64 private HttpClient httpClient;
65
66 private Credentials credentials;
67
68 private AuthScope authScope;
69
70
71
72
73
74 public CommonsHttpMessageSender() {
75 httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
76 setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS);
77 setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
78 }
79
80
81
82
83
84
85 public CommonsHttpMessageSender(HttpClient httpClient) {
86 Assert.notNull(httpClient, "httpClient must not be null");
87 this.httpClient = httpClient;
88 }
89
90
91 public HttpClient getHttpClient() {
92 return httpClient;
93 }
94
95
96 public void setHttpClient(HttpClient httpClient) {
97 this.httpClient = httpClient;
98 }
99
100
101 public Credentials getCredentials() {
102 return credentials;
103 }
104
105
106
107
108
109
110
111 public void setCredentials(Credentials credentials) {
112 this.credentials = credentials;
113 }
114
115
116
117
118
119
120
121 public void setConnectionTimeout(int timeout) {
122 if (timeout < 0) {
123 throw new IllegalArgumentException("timeout must be a non-negative value");
124 }
125 getHttpClient().getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
126 }
127
128
129
130
131
132
133
134 public void setReadTimeout(int timeout) {
135 if (timeout < 0) {
136 throw new IllegalArgumentException("timeout must be a non-negative value");
137 }
138 getHttpClient().getHttpConnectionManager().getParams().setSoTimeout(timeout);
139 }
140
141
142
143
144
145
146
147 public void setMaxTotalConnections(int maxTotalConnections) {
148 if (maxTotalConnections <= 0) {
149 throw new IllegalArgumentException("maxTotalConnections must be a positive value");
150 }
151 getHttpClient().getHttpConnectionManager().getParams().setMaxTotalConnections(maxTotalConnections);
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 public void setMaxConnectionsPerHost(Map<String, String> maxConnectionsPerHost) throws URIException {
171 for (String host : maxConnectionsPerHost.keySet()) {
172 HostConfiguration hostConfiguration = new HostConfiguration();
173 if ("*".equals(host)) {
174 hostConfiguration = HostConfiguration.ANY_HOST_CONFIGURATION;
175 }
176 else if (host.startsWith("http://")) {
177 HttpURL httpURL = new HttpURL(host);
178 hostConfiguration.setHost(httpURL);
179 }
180 else if (host.startsWith("https://")) {
181 HttpsURL httpsURL = new HttpsURL(host);
182 hostConfiguration.setHost(httpsURL);
183 }
184 else {
185 hostConfiguration.setHost(host);
186 }
187 int maxHostConnections = Integer.parseInt(maxConnectionsPerHost.get(host));
188 getHttpClient().getHttpConnectionManager().getParams()
189 .setMaxConnectionsPerHost(hostConfiguration, maxHostConnections);
190 }
191 }
192
193
194
195
196
197
198 public AuthScope getAuthScope() {
199 return authScope != null ? authScope : AuthScope.ANY;
200 }
201
202
203
204
205
206
207
208
209 public void setAuthScope(AuthScope authScope) {
210 this.authScope = authScope;
211 }
212
213 public void afterPropertiesSet() throws Exception {
214 if (getCredentials() != null) {
215 getHttpClient().getState().setCredentials(getAuthScope(), getCredentials());
216 getHttpClient().getParams().setAuthenticationPreemptive(true);
217 }
218 }
219
220 public void destroy() throws Exception {
221 HttpConnectionManager connectionManager = getHttpClient().getHttpConnectionManager();
222 if (connectionManager instanceof MultiThreadedHttpConnectionManager) {
223 ((MultiThreadedHttpConnectionManager) connectionManager).shutdown();
224 }
225 }
226
227 public WebServiceConnection createConnection(URI uri) throws IOException {
228 PostMethod postMethod = new PostMethod(uri.toString());
229 if (isAcceptGzipEncoding()) {
230 postMethod.addRequestHeader(HttpTransportConstants.HEADER_ACCEPT_ENCODING,
231 HttpTransportConstants.CONTENT_ENCODING_GZIP);
232 }
233 return new CommonsHttpConnection(getHttpClient(), postMethod);
234 }
235
236 }
237