001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.components.http;
018:
019: import java.io.IOException;
020: import java.net.URL;
021:
022: import javax.jbi.JBIException;
023: import javax.jbi.component.ComponentContext;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.mortbay.jetty.Server;
027: import org.mortbay.jetty.Connector;
028: import org.mortbay.jetty.security.SslSocketConnector;
029: import org.mortbay.jetty.handler.ContextHandler;
030: import org.mortbay.jetty.servlet.ServletHandler;
031: import org.mortbay.jetty.servlet.ServletHolder;
032: import org.mortbay.jetty.servlet.ServletMapping;
033: import org.mortbay.thread.BoundedThreadPool;
034: import org.springframework.core.io.ClassPathResource;
035:
036: /**
037: * An embedded Servlet engine to implement a HTTP connector
038: *
039: * @version $Revision: 373823 $
040: */
041: public class HttpsConnector extends HttpInOutBinding {
042: private SslSocketConnector listener = new SslSocketConnector();
043:
044: /**
045: * The maximum number of threads for the Jetty SocketListener. It's set
046: * to 256 by default to match the default value in Jetty.
047: */
048: private int maxThreads = 256;
049: private static final Log log = LogFactory
050: .getLog(HttpsConnector.class);
051: private Server server;
052: private String host;
053: private int port;
054: private String keyPassword;
055: private String keyStore;
056: private String keyStorePassword;
057: private String keyStoreType = "JKS"; // type of the key store
058: private String protocol = "TLS";
059: private String keyManagerFactoryAlgorithm = "SunX509"; // cert algorithm
060: private String trustManagerFactoryAlgorithm = "SunX509"; // cert algorithm
061: private boolean wantClientAuth = false;
062: private boolean needClientAuth = false;
063:
064: /**
065: * Constructor
066: *
067: * @param host
068: * @param port
069: */
070: public HttpsConnector(String host, int port, String keyPassword,
071: String keyStorePassword, String keyStore,
072: boolean needClientAuth, boolean wantClientAuth) {
073: this .host = host;
074: this .port = port;
075: this .keyPassword = keyPassword;
076: this .keyStorePassword = keyStorePassword;
077: this .keyStore = keyStore;
078: this .wantClientAuth = wantClientAuth;
079: this .needClientAuth = needClientAuth;
080: }
081:
082: public HttpsConnector() {
083: }
084:
085: /**
086: * Constructor
087: *
088: * @param listener
089: */
090: public HttpsConnector(SslSocketConnector listener) {
091: this .listener = listener;
092: }
093:
094: /**
095: * Called when the Component is initialized
096: *
097: * @param cc
098: * @throws JBIException
099: */
100: public void init(ComponentContext cc) throws JBIException {
101: super .init(cc);
102: //should set all ports etc here - from the naming context I guess ?
103: if (keyStore == null) {
104: keyStore = System.getProperty("javax.net.ssl.keyStore", "");
105: if (keyStore == null) {
106: throw new IllegalArgumentException(
107: "keyStore or system property javax.net.ssl.keyStore must be set");
108: }
109: }
110: if (keyStore.startsWith("classpath:")) {
111: try {
112: String res = keyStore.substring(10);
113: URL url = new ClassPathResource(res).getURL();
114: keyStore = url.toString();
115: } catch (IOException e) {
116: throw new JBIException("Unable to find keystore "
117: + keyStore, e);
118: }
119: }
120: if (keyStorePassword == null) {
121: keyStorePassword = System
122: .getProperty("javax.net.ssl.keyStorePassword");
123: if (keyStorePassword == null) {
124: throw new IllegalArgumentException(
125: "keyStorePassword or system property javax.net.ssl.keyStorePassword must be set");
126: }
127: }
128: if (listener == null) {
129: listener = new SslSocketConnector();
130: }
131: listener.setHost(host);
132: listener.setPort(port);
133: listener.setConfidentialPort(port);
134: listener.setPassword(keyStorePassword);
135: listener.setKeyPassword(keyPassword != null ? keyPassword
136: : keyStorePassword);
137: listener.setKeystore(keyStore);
138: listener.setWantClientAuth(wantClientAuth);
139: listener.setNeedClientAuth(needClientAuth);
140: listener.setProtocol(protocol);
141: listener
142: .setSslKeyManagerFactoryAlgorithm(keyManagerFactoryAlgorithm);
143: listener
144: .setSslTrustManagerFactoryAlgorithm(trustManagerFactoryAlgorithm);
145: listener.setKeystoreType(keyStoreType);
146: server = new Server();
147: BoundedThreadPool btp = new BoundedThreadPool();
148: btp.setMaxThreads(getMaxThreads());
149: server.setThreadPool(btp);
150: }
151:
152: /**
153: * start the Component
154: *
155: * @throws JBIException
156: */
157: public void start() throws JBIException {
158: server.setConnectors(new Connector[] { listener });
159: ContextHandler context = new ContextHandler();
160: context.setContextPath("/");
161: ServletHolder holder = new ServletHolder();
162: holder.setName("jbiServlet");
163: holder.setClassName(BindingServlet.class.getName());
164: ServletHandler handler = new ServletHandler();
165: handler.setServlets(new ServletHolder[] { holder });
166: ServletMapping mapping = new ServletMapping();
167: mapping.setServletName("jbiServlet");
168: mapping.setPathSpec("/*");
169: handler.setServletMappings(new ServletMapping[] { mapping });
170: context.setHandler(handler);
171: server.setHandler(context);
172: context.setAttribute("binding", this );
173: try {
174: server.start();
175: } catch (Exception e) {
176: log.warn(e.toString());
177: throw new JBIException("Start failed: " + e, e);
178: }
179: }
180:
181: /**
182: * stop
183: */
184: public void stop() throws JBIException {
185: try {
186: if (server != null) {
187: server.stop();
188: }
189: } catch (Exception e) {
190: log.warn(e.toString());
191: throw new JBIException("Stop failed: " + e, e);
192: }
193: }
194:
195: /**
196: * shutdown
197: */
198: public void shutDown() throws JBIException {
199: server = null;
200: }
201:
202: // Properties
203: //-------------------------------------------------------------------------
204: public int getPort() {
205: return port;
206: }
207:
208: public void setPort(int port) {
209: this .port = port;
210: }
211:
212: public Server getServer() {
213: return server;
214: }
215:
216: public void setServer(Server server) {
217: this .server = server;
218: }
219:
220: public String getHost() {
221: return host;
222: }
223:
224: public void setHost(String host) {
225: this .host = host;
226: }
227:
228: public int getMaxThreads() {
229: return maxThreads;
230: }
231:
232: public void setMaxThreads(int maxThreads) {
233: this .maxThreads = maxThreads;
234: }
235:
236: /**
237: * @return Returns the algorithm.
238: */
239: public String getKeyManagerFactoryAlgorithm() {
240: return keyManagerFactoryAlgorithm;
241: }
242:
243: /**
244: * @param algorithm The algorithm to set.
245: */
246: public void setKeyManagerFactoryAlgorithm(String algorithm) {
247: this .keyManagerFactoryAlgorithm = algorithm;
248: }
249:
250: /**
251: * @return Returns the algorithm.
252: */
253: public String getTrustManagerFactoryAlgorithm() {
254: return trustManagerFactoryAlgorithm;
255: }
256:
257: /**
258: * @param algorithm The algorithm to set.
259: */
260: public void setTrustManagerFactoryAlgorithm(String algorithm) {
261: this .trustManagerFactoryAlgorithm = algorithm;
262: }
263:
264: /**
265: * @return Returns the keyPassword.
266: */
267: public String getKeyPassword() {
268: return keyPassword;
269: }
270:
271: /**
272: * @param keyPassword The keyPassword to set.
273: */
274: public void setKeyPassword(String keyPassword) {
275: this .keyPassword = keyPassword;
276: }
277:
278: /**
279: * @return Returns the keyStore.
280: */
281: public String getKeyStore() {
282: return keyStore;
283: }
284:
285: /**
286: * @param keyStore The keyStore to set.
287: */
288: public void setKeyStore(String keyStore) {
289: this .keyStore = keyStore;
290: }
291:
292: /**
293: * @return Returns the keyStorePassword.
294: */
295: public String getKeyStorePassword() {
296: return keyStorePassword;
297: }
298:
299: /**
300: * @param keyStorePassword The keyStorePassword to set.
301: */
302: public void setKeyStorePassword(String keyStorePassword) {
303: this .keyStorePassword = keyStorePassword;
304: }
305:
306: /**
307: * @return Returns the keyStoreType.
308: */
309: public String getKeyStoreType() {
310: return keyStoreType;
311: }
312:
313: /**
314: * @param keyStoreType The keyStoreType to set.
315: */
316: public void setKeyStoreType(String keyStoreType) {
317: this .keyStoreType = keyStoreType;
318: }
319:
320: /**
321: * @return Returns the needClientAuth.
322: */
323: public boolean isNeedClientAuth() {
324: return needClientAuth;
325: }
326:
327: /**
328: * @param needClientAuth The needClientAuth to set.
329: */
330: public void setNeedClientAuth(boolean needClientAuth) {
331: this .needClientAuth = needClientAuth;
332: }
333:
334: /**
335: * @return Returns the protocol.
336: */
337: public String getProtocol() {
338: return protocol;
339: }
340:
341: /**
342: * @param protocol The protocol to set.
343: */
344: public void setProtocol(String protocol) {
345: this .protocol = protocol;
346: }
347:
348: /**
349: * @return Returns the wantClientAuth.
350: */
351: public boolean isWantClientAuth() {
352: return wantClientAuth;
353: }
354:
355: /**
356: * @param wantClientAuth The wantClientAuth to set.
357: */
358: public void setWantClientAuth(boolean wantClientAuth) {
359: this.wantClientAuth = wantClientAuth;
360: }
361:
362: }
|