View Javadoc

1   /*
2    * Copyright 2002-2013 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.IOException;
20  import java.net.URI;
21  import java.net.URISyntaxException;
22  import java.util.Map;
23  
24  import org.apache.http.HttpEntityEnclosingRequest;
25  import org.apache.http.HttpException;
26  import org.apache.http.HttpHost;
27  import org.apache.http.HttpRequest;
28  import org.apache.http.HttpRequestInterceptor;
29  import org.apache.http.auth.AuthScope;
30  import org.apache.http.auth.Credentials;
31  import org.apache.http.auth.UsernamePasswordCredentials;
32  import org.apache.http.client.HttpClient;
33  import org.apache.http.client.methods.HttpPost;
34  import org.apache.http.conn.ClientConnectionManager;
35  import org.apache.http.conn.routing.HttpRoute;
36  import org.apache.http.impl.client.DefaultHttpClient;
37  import org.apache.http.impl.conn.PoolingClientConnectionManager;
38  import org.apache.http.params.HttpConnectionParams;
39  import org.apache.http.protocol.HTTP;
40  import org.apache.http.protocol.HttpContext;
41  
42  import org.springframework.beans.factory.DisposableBean;
43  import org.springframework.beans.factory.InitializingBean;
44  import org.springframework.util.Assert;
45  import org.springframework.ws.transport.WebServiceConnection;
46  
47  /**
48   * {@code WebServiceMessageSender} implementation that uses <a href="http://hc.apache.org/httpcomponents-client">Apache
49   * HttpClient</a> to execute POST requests.
50   * <p/>
51   * Allows to use a pre-configured HttpClient instance, potentially with authentication, HTTP connection pooling, etc.
52   * Authentication can also be set by injecting a {@link Credentials} instance (such as the {@link
53   * UsernamePasswordCredentials}).
54   *
55   * @author Alan Stewart
56   * @author Barry Pitman
57   * @author Arjen Poutsma
58   * @see HttpClient
59   * @since 2.1.0
60   */
61  public class HttpComponentsMessageSender extends AbstractHttpWebServiceMessageSender
62          implements InitializingBean, DisposableBean {
63  
64      private static final int DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS = (60 * 1000);
65  
66      private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = (60 * 1000);
67  
68      private HttpClient httpClient;
69  
70      private Credentials credentials;
71  
72      private AuthScope authScope = AuthScope.ANY;
73  
74      /**
75       * Create a new instance of the {@code HttpClientMessageSender} with a default {@link HttpClient} that uses a
76       * default {@link PoolingClientConnectionManager}.
77       */
78      public HttpComponentsMessageSender() {
79          DefaultHttpClient defaultClient = new DefaultHttpClient(new PoolingClientConnectionManager());
80          defaultClient.addRequestInterceptor(new RemoveSoapHeadersInterceptor(), 0);
81  
82          this.httpClient = defaultClient;
83          setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS);
84          setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
85      }
86  
87      /**
88       * Create a new instance of the {@code HttpClientMessageSender} with the given
89       * {@link HttpClient} instance.
90       * <p>
91       * This constructor does not change the given {@code HttpClient} in any way. As such,
92       * it does not set timeouts, nor does it
93       * {@linkplain DefaultHttpClient#addRequestInterceptor(org.apache.http.HttpRequestInterceptor) add}
94       * the {@link RemoveSoapHeadersInterceptor}.
95       *
96       * @param httpClient the HttpClient instance to use for this sender
97       */
98      public HttpComponentsMessageSender(HttpClient httpClient) {
99          Assert.notNull(httpClient, "httpClient must not be null");
100         this.httpClient = httpClient;
101     }
102 
103     /**
104      * Sets the credentials to be used. If not set, no authentication is done.
105      *
106      * @see UsernamePasswordCredentials
107      * @see org.apache.http.auth.NTCredentials
108      */
109     public void setCredentials(Credentials credentials) {
110         this.credentials = credentials;
111     }
112 
113     /**
114      * Returns the <code>HttpClient</code> used by this message sender.
115      */
116     public HttpClient getHttpClient() {
117         return httpClient;
118     }
119 
120     /**
121      * Set the {@code HttpClient} used by this message sender.
122      */
123     public void setHttpClient(HttpClient httpClient) {
124         this.httpClient = httpClient;
125     }
126 
127     /**
128      * Sets the timeout until a connection is established. A value of 0 means <em>never</em> timeout.
129      *
130      * @param timeout the timeout value in milliseconds
131      * @see org.apache.http.params.HttpConnectionParams#setConnectionTimeout(org.apache.http.params.HttpParams, int)
132      */
133     public void setConnectionTimeout(int timeout) {
134         if (timeout < 0) {
135             throw new IllegalArgumentException("timeout must be a non-negative value");
136         }
137         HttpConnectionParams.setConnectionTimeout(getHttpClient().getParams(), timeout);
138     }
139 
140     /**
141      * Set the socket read timeout for the underlying HttpClient. A value of 0 means <em>never</em> timeout.
142      *
143      * @param timeout the timeout value in milliseconds
144      * @see org.apache.http.params.HttpConnectionParams#setSoTimeout(org.apache.http.params.HttpParams, int)
145      */
146     public void setReadTimeout(int timeout) {
147         if (timeout < 0) {
148             throw new IllegalArgumentException("timeout must be a non-negative value");
149         }
150         HttpConnectionParams.setSoTimeout(getHttpClient().getParams(), timeout);
151     }
152 
153     /**
154      * Sets the maximum number of connections allowed for the underlying HttpClient.
155      *
156      * @param maxTotalConnections the maximum number of connections allowed
157      * @see PoolingClientConnectionManager#setMaxTotal(int)
158      */
159     public void setMaxTotalConnections(int maxTotalConnections) {
160         if (maxTotalConnections <= 0) {
161             throw new IllegalArgumentException("maxTotalConnections must be a positive value");
162         }
163         ClientConnectionManager connectionManager = getHttpClient().getConnectionManager();
164         if (!(connectionManager instanceof PoolingClientConnectionManager)) {
165             throw new IllegalArgumentException("maxTotalConnections is not supported on " +
166                     connectionManager.getClass().getName() + ". Use " + PoolingClientConnectionManager.class.getName() +
167                     " instead");
168         }
169         ((PoolingClientConnectionManager) connectionManager).setMaxTotal(maxTotalConnections);
170     }
171 
172     /**
173      * Sets the maximum number of connections per host for the underlying HttpClient. The maximum number of connections
174      * per host can be set in a form accepted by the {@code java.util.Properties} class, like as follows:
175      * <p/>
176      * <pre>
177      * https://www.example.com=1
178      * http://www.example.com:8080=7
179      * http://www.springframework.org=10
180      * </pre>
181      * <p/>
182      * The host can be specified as a URI (with scheme and port).
183      *
184      * @param maxConnectionsPerHost a properties object specifying the maximum number of connection
185      * @see PoolingClientConnectionManager#setMaxPerRoute(HttpRoute, int)
186      */
187     public void setMaxConnectionsPerHost(Map<String, String> maxConnectionsPerHost) throws URISyntaxException {
188         ClientConnectionManager connectionManager = getHttpClient().getConnectionManager();
189         if (!(connectionManager instanceof PoolingClientConnectionManager)) {
190             throw new IllegalArgumentException("maxConnectionsPerHost is not supported on " +
191                     connectionManager.getClass().getName() + ". Use " + PoolingClientConnectionManager.class.getName() +
192                     " instead");
193         }
194 	    PoolingClientConnectionManager poolingConnectionManager =
195 			    (PoolingClientConnectionManager) connectionManager;
196 
197 	    for (Map.Entry<String, String> entry : maxConnectionsPerHost.entrySet()) {
198             URI uri = new URI(entry.getKey());
199             HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
200 		    HttpRoute route = new HttpRoute(host);
201 
202             int max = Integer.parseInt(entry.getValue());
203 
204 		    poolingConnectionManager.setMaxPerRoute(route, max);
205         }
206     }
207 
208     /**
209      * Sets the authentication scope to be used. Only used when the <code>credentials</code> property has been set.
210      * <p/>
211      * By default, the {@link AuthScope#ANY} is used.
212      *
213      * @see #setCredentials(Credentials)
214      */
215     public void setAuthScope(AuthScope authScope) {
216         this.authScope = authScope;
217     }
218 
219     public void afterPropertiesSet() throws Exception {
220         if (credentials != null && getHttpClient() instanceof DefaultHttpClient) {
221             ((DefaultHttpClient) getHttpClient()).getCredentialsProvider().setCredentials(authScope, credentials);
222         }
223     }
224 
225     public WebServiceConnection createConnection(URI uri) throws IOException {
226         HttpPost httpPost = new HttpPost(uri);
227         if (isAcceptGzipEncoding()) {
228             httpPost.addHeader(HttpTransportConstants.HEADER_ACCEPT_ENCODING,
229                     HttpTransportConstants.CONTENT_ENCODING_GZIP);
230         }
231         HttpContext httpContext = createContext(uri);
232         return new HttpComponentsConnection(getHttpClient(), httpPost, httpContext);
233     }
234 
235     /**
236      * Template method that allows for creation of a {@link HttpContext} for the given uri. Default implementation
237      * returns {@code null}.
238      *
239      * @param uri the URI to create the context for
240      * @return the context, or {@code null}
241      */
242     protected HttpContext createContext(URI uri) {
243         return null;
244     }
245 
246     public void destroy() throws Exception {
247         getHttpClient().getConnectionManager().shutdown();
248     }
249 
250     /**
251      * HttpClient {@link org.apache.http.HttpRequestInterceptor} implementation that removes {@code Content-Length} and
252      * {@code Transfer-Encoding} headers from the request. Necessary, because some SAAJ and other SOAP implementations set these
253      * headers themselves, and HttpClient throws an exception if they have been set.
254      */
255     public static class RemoveSoapHeadersInterceptor implements HttpRequestInterceptor {
256 
257         public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
258             if (request instanceof HttpEntityEnclosingRequest) {
259                 if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
260                     request.removeHeaders(HTTP.TRANSFER_ENCODING);
261                 }
262                 if (request.containsHeader(HTTP.CONTENT_LEN)) {
263                     request.removeHeaders(HTTP.CONTENT_LEN);
264                 }
265             }
266         }
267     }
268 }