View Javadoc

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