View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
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.opensaml.ws.soap.client;
18  
19  import org.apache.commons.httpclient.HttpClient;
20  import org.apache.commons.httpclient.HttpVersion;
21  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
22  import org.apache.commons.httpclient.params.HttpClientParams;
23  import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
24  import org.opensaml.ws.message.decoder.MessageDecoder;
25  import org.opensaml.ws.message.encoder.MessageEncoder;
26  import org.opensaml.ws.transport.http.HTTPTransport.HTTP_VERSION;
27  
28  /**
29   * HTTP-based SOAP transport factory.
30   */
31  public class HTTPSOAPTransportFactory implements ClientTransportFactory {
32  
33      /** Socket timeout in milliseconds, defaults to 30,000. */
34      private int socketTimeout = 30000;
35  
36      /** Connection timeout in milliseconds, defaults to 60,000. */
37      private int connectionTimeout = 60000;
38  
39      /** HTTP version used when connecting, defaults to HTTP 1.1. */
40      private HTTP_VERSION httpVersion = HTTP_VERSION.HTTP1_1;
41  
42      /** Encoder used to encode messages onto the outgoing transport. */
43      private MessageEncoder messageEncoder;
44  
45      /** Decoder used to decode message from inbound transport. */
46      private MessageDecoder messageDecoder;
47  
48      /** Client used by transports. */
49      private HttpClient httpClient;
50  
51      /**
52       * Constructor.
53       * 
54       * @param encoder encoder used to encode messages onto the outgoing transport
55       * @param decoder decoder used to decode messages from inbound transport
56       */
57      public HTTPSOAPTransportFactory(MessageEncoder encoder, MessageDecoder decoder) {
58          messageEncoder = encoder;
59          messageDecoder = decoder;
60          initializeHttpClient();
61      }
62  
63      /**
64       * Gets the connection timeout in milliseconds.
65       * 
66       * @return connection timeout in milliseconds
67       */
68      public int getConnectionTimeout() {
69          return connectionTimeout;
70      }
71  
72      /**
73       * Sets the connection timeout in milliseconds.
74       * 
75       * @param timeout connection timeout in milliseconds
76       */
77      public void setConnectionTimeout(int timeout) {
78          connectionTimeout = timeout;
79      }
80  
81      /**
82       * Gets the HTTP version used when connecting to peers.
83       * 
84       * @return HTTP version used when connecting to peers
85       */
86      public HTTP_VERSION getHttpVersion() {
87          return httpVersion;
88      }
89  
90      /**
91       * Sets the HTTP version used when connecting to peers.
92       * 
93       * @param version HTTP version used when connecting to peers
94       */
95      public void setHttpVersion(HTTP_VERSION version) {
96          this.httpVersion = version;
97      }
98  
99      /**
100      * Gets the socket connection timeout in milliseconds.
101      * 
102      * @return socket connection timeout in milliseconds
103      */
104     public int getSocketTimeout() {
105         return socketTimeout;
106     }
107 
108     /**
109      * Sets the socket connection timeout in milliseconds.
110      * 
111      * @param timeout socket connection timeout in milliseconds
112      */
113     public void setSocketTimeout(int timeout) {
114         this.socketTimeout = timeout;
115     }
116 
117     /**
118      * Gets the decoder used to decode messages from the inbound transport.
119      * 
120      * @return decoder used to decode messages from the inbound transport
121      */
122     public MessageDecoder getMessageDecoder() {
123         return messageDecoder;
124     }
125 
126     /**
127      * Gets the encoder used to encode messages to the outbound transport.
128      * 
129      * @return encoder used to encode messages to the outbound transport
130      */
131     public MessageEncoder getMessageEncoder() {
132         return messageEncoder;
133     }
134 
135     /** {@inheritDoc} */
136     public ClientTransport createTransport() {
137         return new HTTPSOAPTransport(httpClient, messageEncoder, messageDecoder);
138     }
139 
140     /**
141      * Initializes the {@link HttpClient} that will be used by the created {@link HTTPSOAPTransport} built by this
142      * factory.
143      */
144     protected void initializeHttpClient() {
145         HttpConnectionManagerParams connectionParams = new HttpConnectionManagerParams();
146         connectionParams.setConnectionTimeout(connectionTimeout);
147 
148         MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
149         connectionManager.setParams(connectionParams);
150 
151         HttpClientParams clientParams = new HttpClientParams();
152         clientParams.setSoTimeout(socketTimeout);
153         if (httpVersion == HTTP_VERSION.HTTP1_0) {
154             clientParams.setVersion(HttpVersion.HTTP_1_0);
155         } else {
156             clientParams.setVersion(HttpVersion.HTTP_1_1);
157         }
158 
159         httpClient = new HttpClient(clientParams, connectionManager);
160     }
161 }