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.Connector;
027: import org.mortbay.jetty.Server;
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: public class HttpsSoapConnector extends HttpSoapInOutBinding {
037: private SslSocketConnector listener = new SslSocketConnector();
038:
039: /**
040: * The maximum number of threads for the Jetty SocketListener. It's set
041: * to 256 by default to match the default value in Jetty.
042: */
043: private int maxThreads = 256;
044: private Server server;
045: private static final Log log = LogFactory
046: .getLog(HttpsConnector.class);
047: private String host;
048: private int port;
049: private String keyPassword;
050: private String keyStore;
051: private String keyStorePassword;
052: private String keyStoreType = "JKS"; // type of the key store
053: private String protocol = "TLS";
054: private String keyManagerFactoryAlgorithm = "SunX509"; // cert algorithm
055: private String trustManagerFactoryAlgorithm = "SunX509"; // cert algorithm
056: private boolean wantClientAuth = false;
057: private boolean needClientAuth = false;
058:
059: /**
060: * Constructor
061: *
062: * @param host
063: * @param port
064: */
065: public HttpsSoapConnector(String host, int port,
066: String keyPassword, String keyStorePassword,
067: String keyStore, boolean needClientAuth,
068: boolean wantClientAuth) {
069: this .host = host;
070: this .port = port;
071: this .keyPassword = keyPassword;
072: this .keyStorePassword = keyStorePassword;
073: this .keyStore = keyStore;
074: this .wantClientAuth = wantClientAuth;
075: this .needClientAuth = needClientAuth;
076: }
077:
078: public HttpsSoapConnector() {
079: }
080:
081: /**
082: * Constructor
083: *
084: * @param listener
085: */
086: public HttpsSoapConnector(SslSocketConnector listener) {
087: this .listener = listener;
088: }
089:
090: /**
091: * Called when the Component is initialized
092: *
093: * @param cc
094: * @throws JBIException
095: */
096: public void init(ComponentContext cc) throws JBIException {
097: super .init(cc);
098: //should set all ports etc here - from the naming context I guess ?
099: if (keyStore == null) {
100: keyStore = System.getProperty("javax.net.ssl.keyStore", "");
101: if (keyStore == null) {
102: throw new IllegalArgumentException(
103: "keyStore or system property javax.net.ssl.keyStore must be set");
104: }
105: }
106: if (keyStore.startsWith("classpath:")) {
107: try {
108: String res = keyStore.substring(10);
109: URL url = new ClassPathResource(res).getURL();
110: keyStore = url.toString();
111: } catch (IOException e) {
112: throw new JBIException("Unable to find keystore "
113: + keyStore, e);
114: }
115: }
116: if (keyStorePassword == null) {
117: keyStorePassword = System
118: .getProperty("javax.net.ssl.keyStorePassword");
119: if (keyStorePassword == null) {
120: throw new IllegalArgumentException(
121: "keyStorePassword or system property javax.net.ssl.keyStorePassword must be set");
122: }
123: }
124: if (listener == null) {
125: listener = new SslSocketConnector();
126: }
127: listener.setHost(host);
128: listener.setPort(port);
129: listener.setConfidentialPort(port);
130: listener.setPassword(keyStorePassword);
131: listener.setKeyPassword(keyPassword != null ? keyPassword
132: : keyStorePassword);
133: listener.setKeystore(keyStore);
134: listener.setWantClientAuth(wantClientAuth);
135: listener.setNeedClientAuth(needClientAuth);
136: listener.setProtocol(protocol);
137: listener
138: .setSslKeyManagerFactoryAlgorithm(keyManagerFactoryAlgorithm);
139: listener
140: .setSslTrustManagerFactoryAlgorithm(trustManagerFactoryAlgorithm);
141: listener.setKeystoreType(keyStoreType);
142: server = new Server();
143: BoundedThreadPool btp = new BoundedThreadPool();
144: btp.setMaxThreads(getMaxThreads());
145: server.setThreadPool(btp);
146: }
147:
148: /**
149: * start the Component
150: *
151: * @throws JBIException
152: */
153: public void start() throws JBIException {
154: server.setConnectors(new Connector[] { listener });
155: ContextHandler context = new ContextHandler();
156: context.setContextPath("/");
157: ServletHolder holder = new ServletHolder();
158: holder.setName("jbiServlet");
159: holder.setClassName(BindingServlet.class.getName());
160: ServletHandler handler = new ServletHandler();
161: handler.setServlets(new ServletHolder[] { holder });
162: ServletMapping mapping = new ServletMapping();
163: mapping.setServletName("jbiServlet");
164: mapping.setPathSpec("/*");
165: handler.setServletMappings(new ServletMapping[] { mapping });
166: context.setHandler(handler);
167: server.setHandler(context);
168: context.setAttribute("binding", this );
169: try {
170: server.start();
171: } catch (Exception e) {
172: log.warn(e.toString());
173: throw new JBIException("Start failed: " + e, e);
174: }
175: }
176:
177: /**
178: * stop
179: */
180: public void stop() throws JBIException {
181: try {
182: if (server != null) {
183: server.stop();
184: }
185: } catch (Exception e) {
186: throw new JBIException("Stop failed: " + e, e);
187: }
188: }
189:
190: /**
191: * shutdown
192: */
193: public void shutDown() throws JBIException {
194: server = null;
195: }
196:
197: // Properties
198: //-------------------------------------------------------------------------
199: public int getPort() {
200: return port;
201: }
202:
203: public void setPort(int port) {
204: this .port = port;
205: }
206:
207: public Server getServer() {
208: return server;
209: }
210:
211: public void setServer(Server server) {
212: this .server = server;
213: }
214:
215: public String getHost() {
216: return host;
217: }
218:
219: public void setHost(String host) {
220: this .host = host;
221: }
222:
223: public int getMaxThreads() {
224: return maxThreads;
225: }
226:
227: public void setMaxThreads(int maxThreads) {
228: this .maxThreads = maxThreads;
229: }
230:
231: /**
232: * @return Returns the algorithm.
233: */
234: public String getKeyManagerFactoryAlgorithm() {
235: return keyManagerFactoryAlgorithm;
236: }
237:
238: /**
239: * @param algorithm The algorithm to set.
240: */
241: public void setKeyManagerFactoryAlgorithm(String algorithm) {
242: this .keyManagerFactoryAlgorithm = algorithm;
243: }
244:
245: /**
246: * @return Returns the algorithm.
247: */
248: public String getTrustManagerFactoryAlgorithm() {
249: return trustManagerFactoryAlgorithm;
250: }
251:
252: /**
253: * @param algorithm The algorithm to set.
254: */
255: public void setTrustManagerFactoryAlgorithm(String algorithm) {
256: this .trustManagerFactoryAlgorithm = algorithm;
257: }
258:
259: /**
260: * @return Returns the keyPassword.
261: */
262: public String getKeyPassword() {
263: return keyPassword;
264: }
265:
266: /**
267: * @param keyPassword The keyPassword to set.
268: */
269: public void setKeyPassword(String keyPassword) {
270: this .keyPassword = keyPassword;
271: }
272:
273: /**
274: * @return Returns the keyStore.
275: */
276: public String getKeyStore() {
277: return keyStore;
278: }
279:
280: /**
281: * @param keyStore The keyStore to set.
282: */
283: public void setKeyStore(String keyStore) {
284: this .keyStore = keyStore;
285: }
286:
287: /**
288: * @return Returns the keyStorePassword.
289: */
290: public String getKeyStorePassword() {
291: return keyStorePassword;
292: }
293:
294: /**
295: * @param keyStorePassword The keyStorePassword to set.
296: */
297: public void setKeyStorePassword(String keyStorePassword) {
298: this .keyStorePassword = keyStorePassword;
299: }
300:
301: /**
302: * @return Returns the keyStoreType.
303: */
304: public String getKeyStoreType() {
305: return keyStoreType;
306: }
307:
308: /**
309: * @param keyStoreType The keyStoreType to set.
310: */
311: public void setKeyStoreType(String keyStoreType) {
312: this .keyStoreType = keyStoreType;
313: }
314:
315: /**
316: * @return Returns the needClientAuth.
317: */
318: public boolean isNeedClientAuth() {
319: return needClientAuth;
320: }
321:
322: /**
323: * @param needClientAuth The needClientAuth to set.
324: */
325: public void setNeedClientAuth(boolean needClientAuth) {
326: this .needClientAuth = needClientAuth;
327: }
328:
329: /**
330: * @return Returns the protocol.
331: */
332: public String getProtocol() {
333: return protocol;
334: }
335:
336: /**
337: * @param protocol The protocol to set.
338: */
339: public void setProtocol(String protocol) {
340: this .protocol = protocol;
341: }
342:
343: /**
344: * @return Returns the wantClientAuth.
345: */
346: public boolean isWantClientAuth() {
347: return wantClientAuth;
348: }
349:
350: /**
351: * @param wantClientAuth The wantClientAuth to set.
352: */
353: public void setWantClientAuth(boolean wantClientAuth) {
354: this.wantClientAuth = wantClientAuth;
355: }
356: }
|