KickJava   Java API By Example, From Geeks To Geeks.

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


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.Server;
27 import org.mortbay.jetty.Connector;
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 /**
37  * An embedded Servlet engine to implement a HTTP connector
38  *
39  * @version $Revision: 373823 $
40  */

41 public class HttpsConnector extends HttpInOutBinding {
42     private SslSocketConnector listener = new SslSocketConnector();
43     
44     /**
45      * The maximum number of threads for the Jetty SocketListener. It's set
46      * to 256 by default to match the default value in Jetty.
47      */

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

69     public HttpsConnector(String JavaDoc host, int port, String JavaDoc keyPassword, String JavaDoc keyStorePassword, String JavaDoc keyStore, boolean needClientAuth, boolean wantClientAuth) {
70         this.host = host;
71         this.port = port;
72         this.keyPassword = keyPassword;
73         this.keyStorePassword = keyStorePassword;
74         this.keyStore = keyStore;
75         this.wantClientAuth = wantClientAuth;
76         this.needClientAuth = needClientAuth;
77     }
78
79     public HttpsConnector() {
80     }
81
82     /**
83      * Constructor
84      *
85      * @param listener
86      */

87     public HttpsConnector(SslSocketConnector listener) {
88         this.listener = listener;
89     }
90
91     /**
92      * Called when the Component is initialized
93      *
94      * @param cc
95      * @throws JBIException
96      */

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

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

175     public void stop() throws JBIException {
176         try {
177             if (server != null) {
178                 server.stop();
179             }
180         }
181         catch (Exception JavaDoc e) {
182             log.warn(e.toString());
183             throw new JBIException("Stop failed: " + e, e);
184         }
185     }
186
187     /**
188      * shutdown
189      */

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

232     public String JavaDoc getKeyManagerFactoryAlgorithm() {
233         return keyManagerFactoryAlgorithm;
234     }
235
236     /**
237      * @param algorithm The algorithm to set.
238      */

239     public void setKeyManagerFactoryAlgorithm(String JavaDoc algorithm) {
240         this.keyManagerFactoryAlgorithm = algorithm;
241     }
242
243     /**
244      * @return Returns the algorithm.
245      */

246     public String JavaDoc getTrustManagerFactoryAlgorithm() {
247         return trustManagerFactoryAlgorithm;
248     }
249
250     /**
251      * @param algorithm The algorithm to set.
252      */

253     public void setTrustManagerFactoryAlgorithm(String JavaDoc algorithm) {
254         this.trustManagerFactoryAlgorithm = algorithm;
255     }
256
257     /**
258      * @return Returns the keyPassword.
259      */

260     public String JavaDoc getKeyPassword() {
261         return keyPassword;
262     }
263
264     /**
265      * @param keyPassword The keyPassword to set.
266      */

267     public void setKeyPassword(String JavaDoc keyPassword) {
268         this.keyPassword = keyPassword;
269     }
270
271     /**
272      * @return Returns the keyStore.
273      */

274     public String JavaDoc getKeyStore() {
275         return keyStore;
276     }
277
278     /**
279      * @param keyStore The keyStore to set.
280      */

281     public void setKeyStore(String JavaDoc keyStore) {
282         this.keyStore = keyStore;
283     }
284
285     /**
286      * @return Returns the keyStorePassword.
287      */

288     public String JavaDoc getKeyStorePassword() {
289         return keyStorePassword;
290     }
291
292     /**
293      * @param keyStorePassword The keyStorePassword to set.
294      */

295     public void setKeyStorePassword(String JavaDoc keyStorePassword) {
296         this.keyStorePassword = keyStorePassword;
297     }
298
299     /**
300      * @return Returns the keyStoreType.
301      */

302     public String JavaDoc getKeyStoreType() {
303         return keyStoreType;
304     }
305
306     /**
307      * @param keyStoreType The keyStoreType to set.
308      */

309     public void setKeyStoreType(String JavaDoc keyStoreType) {
310         this.keyStoreType = keyStoreType;
311     }
312
313     /**
314      * @return Returns the needClientAuth.
315      */

316     public boolean isNeedClientAuth() {
317         return needClientAuth;
318     }
319
320     /**
321      * @param needClientAuth The needClientAuth to set.
322      */

323     public void setNeedClientAuth(boolean needClientAuth) {
324         this.needClientAuth = needClientAuth;
325     }
326
327     /**
328      * @return Returns the protocol.
329      */

330     public String JavaDoc getProtocol() {
331         return protocol;
332     }
333
334     /**
335      * @param protocol The protocol to set.
336      */

337     public void setProtocol(String JavaDoc protocol) {
338         this.protocol = protocol;
339     }
340
341     /**
342      * @return Returns the wantClientAuth.
343      */

344     public boolean isWantClientAuth() {
345         return wantClientAuth;
346     }
347
348     /**
349      * @param wantClientAuth The wantClientAuth to set.
350      */

351     public void setWantClientAuth(boolean wantClientAuth) {
352         this.wantClientAuth = wantClientAuth;
353     }
354
355 }
356
Popular Tags