KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > http > HttpsSoapConnector


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.http;
18
19 import java.io.IOException JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import javax.jbi.JBIException;
23 import javax.jbi.component.ComponentContext;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.mortbay.jetty.Connector;
27 import org.mortbay.jetty.Server;
28 import org.mortbay.jetty.security.SslSocketConnector;
29 import org.mortbay.jetty.handler.ContextHandler;
30 import org.mortbay.jetty.servlet.ServletHandler;
31 import org.mortbay.jetty.servlet.ServletHolder;
32 import org.mortbay.jetty.servlet.ServletMapping;
33 import org.mortbay.thread.BoundedThreadPool;
34 import org.springframework.core.io.ClassPathResource;
35
36 public class HttpsSoapConnector extends HttpSoapInOutBinding {
37     private SslSocketConnector listener = new SslSocketConnector();
38     
39     /**
40      * The maximum number of threads for the Jetty SocketListener. It's set
41      * to 256 by default to match the default value in Jetty.
42      */

43     private int maxThreads = 256;
44     private Server server;
45     private static final Log log = LogFactory.getLog(HttpsConnector.class);
46     private String JavaDoc host;
47     private int port;
48     private String JavaDoc keyPassword;
49     private String JavaDoc keyStore;
50     private String JavaDoc keyStorePassword;
51     private String JavaDoc keyStoreType = "JKS"; // type of the key store
52
private String JavaDoc protocol = "TLS";
53     private String JavaDoc keyManagerFactoryAlgorithm = "SunX509"; // cert algorithm
54
private String JavaDoc trustManagerFactoryAlgorithm = "SunX509"; // cert algorithm
55
private boolean wantClientAuth = false;
56     private boolean needClientAuth = false;
57
58     /**
59      * Constructor
60      *
61      * @param host
62      * @param port
63      */

64     public HttpsSoapConnector(String JavaDoc host, int port, String JavaDoc keyPassword, String JavaDoc keyStorePassword, String JavaDoc keyStore, boolean needClientAuth, boolean wantClientAuth) {
65         this.host = host;
66         this.port = port;
67         this.keyPassword = keyPassword;
68         this.keyStorePassword = keyStorePassword;
69         this.keyStore = keyStore;
70         this.wantClientAuth = wantClientAuth;
71         this.needClientAuth = needClientAuth;
72     }
73
74     public HttpsSoapConnector() {
75     }
76
77     /**
78      * Constructor
79      *
80      * @param listener
81      */

82     public HttpsSoapConnector(SslSocketConnector listener) {
83         this.listener = listener;
84     }
85
86     /**
87      * Called when the Component is initialized
88      *
89      * @param cc
90      * @throws JBIException
91      */

92     public void init(ComponentContext cc) throws JBIException {
93         super.init(cc);
94         //should set all ports etc here - from the naming context I guess ?
95
if (keyStore == null) {
96             keyStore = System.getProperty("javax.net.ssl.keyStore", "");
97             if (keyStore == null) {
98                 throw new IllegalArgumentException JavaDoc("keyStore or system property javax.net.ssl.keyStore must be set");
99             }
100         }
101         if (keyStore.startsWith("classpath:")) {
102             try {
103                 String JavaDoc res = keyStore.substring(10);
104                 URL JavaDoc url = new ClassPathResource(res).getURL();
105                 keyStore = url.toString();
106             } catch (IOException JavaDoc e) {
107                 throw new JBIException("Unable to find keystore " + keyStore, e);
108             }
109         }
110         if (keyStorePassword == null) {
111             keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword");
112             if (keyStorePassword == null) {
113                 throw new IllegalArgumentException JavaDoc("keyStorePassword or system property javax.net.ssl.keyStorePassword must be set");
114             }
115         }
116         if (listener == null) {
117             listener = new SslSocketConnector();
118         }
119         listener.setHost(host);
120         listener.setPort(port);
121         listener.setConfidentialPort(port);
122         listener.setPassword(keyStorePassword);
123         listener.setKeyPassword(keyPassword != null ? keyPassword : keyStorePassword);
124         listener.setKeystore(keyStore);
125         listener.setWantClientAuth(wantClientAuth);
126         listener.setNeedClientAuth(needClientAuth);
127         listener.setProtocol(protocol);
128         listener.setSslKeyManagerFactoryAlgorithm(keyManagerFactoryAlgorithm);
129         listener.setSslTrustManagerFactoryAlgorithm(trustManagerFactoryAlgorithm);
130         listener.setKeystoreType(keyStoreType);
131         server = new Server();
132         BoundedThreadPool btp = new BoundedThreadPool();
133         btp.setMaxThreads(getMaxThreads());
134         server.setThreadPool(btp);
135     }
136
137     /**
138      * start the Component
139      *
140      * @throws JBIException
141      */

142     public void start() throws JBIException {
143         server.setConnectors(new Connector[] { listener });
144         ContextHandler context = new ContextHandler();
145         context.setContextPath("/");
146         ServletHolder holder = new ServletHolder();
147         holder.setName("jbiServlet");
148         holder.setClassName(BindingServlet.class.getName());
149         ServletHandler handler = new ServletHandler();
150         handler.setServlets(new ServletHolder[] { holder });
151         ServletMapping mapping = new ServletMapping();
152         mapping.setServletName("jbiServlet");
153         mapping.setPathSpec("/*");
154         handler.setServletMappings(new ServletMapping[] { mapping });
155         context.setHandler(handler);
156         server.setHandler(context);
157         context.setAttribute("binding", this);
158         try {
159             server.start();
160         }
161         catch (Exception JavaDoc e) {
162             log.warn(e.toString());
163             throw new JBIException("Start failed: " + e, e);
164         }
165     }
166
167     /**
168      * stop
169      */

170     public void stop() throws JBIException {
171         try {
172             if (server != null) {
173                 server.stop();
174             }
175         } catch (Exception JavaDoc e) {
176             throw new JBIException("Stop failed: " + e, e);
177         }
178     }
179
180     /**
181      * shutdown
182      */

183     public void shutDown() throws JBIException {
184         server = null;
185     }
186
187     // Properties
188
//-------------------------------------------------------------------------
189
public int getPort() {
190         return port;
191     }
192
193     public void setPort(int port) {
194         this.port = port;
195     }
196
197     public Server getServer() {
198         return server;
199     }
200
201     public void setServer(Server server) {
202         this.server = server;
203     }
204
205     public String JavaDoc getHost() {
206         return host;
207     }
208
209     public void setHost(String JavaDoc host) {
210         this.host = host;
211     }
212
213     public int getMaxThreads() {
214         return maxThreads;
215     }
216
217     public void setMaxThreads(int maxThreads) {
218         this.maxThreads = maxThreads;
219     }
220
221     /**
222      * @return Returns the algorithm.
223      */

224     public String JavaDoc getKeyManagerFactoryAlgorithm() {
225         return keyManagerFactoryAlgorithm;
226     }
227
228     /**
229      * @param algorithm The algorithm to set.
230      */

231     public void setKeyManagerFactoryAlgorithm(String JavaDoc algorithm) {
232         this.keyManagerFactoryAlgorithm = algorithm;
233     }
234
235     /**
236      * @return Returns the algorithm.
237      */

238     public String JavaDoc getTrustManagerFactoryAlgorithm() {
239         return trustManagerFactoryAlgorithm;
240     }
241
242     /**
243      * @param algorithm The algorithm to set.
244      */

245     public void setTrustManagerFactoryAlgorithm(String JavaDoc algorithm) {
246         this.trustManagerFactoryAlgorithm = algorithm;
247     }
248
249     /**
250      * @return Returns the keyPassword.
251      */

252     public String JavaDoc getKeyPassword() {
253         return keyPassword;
254     }
255
256     /**
257      * @param keyPassword The keyPassword to set.
258      */

259     public void setKeyPassword(String JavaDoc keyPassword) {
260         this.keyPassword = keyPassword;
261     }
262
263     /**
264      * @return Returns the keyStore.
265      */

266     public String JavaDoc getKeyStore() {
267         return keyStore;
268     }
269
270     /**
271      * @param keyStore The keyStore to set.
272      */

273     public void setKeyStore(String JavaDoc keyStore) {
274         this.keyStore = keyStore;
275     }
276
277     /**
278      * @return Returns the keyStorePassword.
279      */

280     public String JavaDoc getKeyStorePassword() {
281         return keyStorePassword;
282     }
283
284     /**
285      * @param keyStorePassword The keyStorePassword to set.
286      */

287     public void setKeyStorePassword(String JavaDoc keyStorePassword) {
288         this.keyStorePassword = keyStorePassword;
289     }
290
291     /**
292      * @return Returns the keyStoreType.
293      */

294     public String JavaDoc getKeyStoreType() {
295         return keyStoreType;
296     }
297
298     /**
299      * @param keyStoreType The keyStoreType to set.
300      */

301     public void setKeyStoreType(String JavaDoc keyStoreType) {
302         this.keyStoreType = keyStoreType;
303     }
304
305     /**
306      * @return Returns the needClientAuth.
307      */

308     public boolean isNeedClientAuth() {
309         return needClientAuth;
310     }
311
312     /**
313      * @param needClientAuth The needClientAuth to set.
314      */

315     public void setNeedClientAuth(boolean needClientAuth) {
316         this.needClientAuth = needClientAuth;
317     }
318
319     /**
320      * @return Returns the protocol.
321      */

322     public String JavaDoc getProtocol() {
323         return protocol;
324     }
325
326     /**
327      * @param protocol The protocol to set.
328      */

329     public void setProtocol(String JavaDoc protocol) {
330         this.protocol = protocol;
331     }
332
333     /**
334      * @return Returns the wantClientAuth.
335      */

336     public boolean isWantClientAuth() {
337         return wantClientAuth;
338     }
339
340     /**
341      * @param wantClientAuth The wantClientAuth to set.
342      */

343     public void setWantClientAuth(boolean wantClientAuth) {
344         this.wantClientAuth = wantClientAuth;
345     }
346 }
347
Popular Tags