1 17 package org.apache.servicemix.components.http; 18 19 import java.io.IOException ; 20 import java.net.URL ; 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 41 public class HttpsConnector extends HttpInOutBinding { 42 private SslSocketConnector listener = new SslSocketConnector(); 43 44 48 private int maxThreads = 256; 49 private static final Log log = LogFactory.getLog(HttpsConnector.class); 50 private Server server; 51 private String host; 52 private int port; 53 private String keyPassword; 54 private String keyStore; 55 private String keyStorePassword; 56 private String keyStoreType = "JKS"; private String protocol = "TLS"; 58 private String keyManagerFactoryAlgorithm = "SunX509"; private String trustManagerFactoryAlgorithm = "SunX509"; private boolean wantClientAuth = false; 61 private boolean needClientAuth = false; 62 63 69 public HttpsConnector(String host, int port, String keyPassword, String keyStorePassword, String 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 87 public HttpsConnector(SslSocketConnector listener) { 88 this.listener = listener; 89 } 90 91 97 public void init(ComponentContext cc) throws JBIException { 98 super.init(cc); 99 if (keyStore == null) { 101 keyStore = System.getProperty("javax.net.ssl.keyStore", ""); 102 if (keyStore == null) { 103 throw new IllegalArgumentException ("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 " + keyStore, e); 113 } 114 } 115 if (keyStorePassword == null) { 116 keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword"); 117 if (keyStorePassword == null) { 118 throw new IllegalArgumentException ("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 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 e) { 167 log.warn(e.toString()); 168 throw new JBIException("Start failed: " + e, e); 169 } 170 } 171 172 175 public void stop() throws JBIException { 176 try { 177 if (server != null) { 178 server.stop(); 179 } 180 } 181 catch (Exception e) { 182 log.warn(e.toString()); 183 throw new JBIException("Stop failed: " + e, e); 184 } 185 } 186 187 190 public void shutDown() throws JBIException { 191 server = null; 192 } 193 194 195 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 getHost() { 214 return host; 215 } 216 217 public void setHost(String 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 232 public String getKeyManagerFactoryAlgorithm() { 233 return keyManagerFactoryAlgorithm; 234 } 235 236 239 public void setKeyManagerFactoryAlgorithm(String algorithm) { 240 this.keyManagerFactoryAlgorithm = algorithm; 241 } 242 243 246 public String getTrustManagerFactoryAlgorithm() { 247 return trustManagerFactoryAlgorithm; 248 } 249 250 253 public void setTrustManagerFactoryAlgorithm(String algorithm) { 254 this.trustManagerFactoryAlgorithm = algorithm; 255 } 256 257 260 public String getKeyPassword() { 261 return keyPassword; 262 } 263 264 267 public void setKeyPassword(String keyPassword) { 268 this.keyPassword = keyPassword; 269 } 270 271 274 public String getKeyStore() { 275 return keyStore; 276 } 277 278 281 public void setKeyStore(String keyStore) { 282 this.keyStore = keyStore; 283 } 284 285 288 public String getKeyStorePassword() { 289 return keyStorePassword; 290 } 291 292 295 public void setKeyStorePassword(String keyStorePassword) { 296 this.keyStorePassword = keyStorePassword; 297 } 298 299 302 public String getKeyStoreType() { 303 return keyStoreType; 304 } 305 306 309 public void setKeyStoreType(String keyStoreType) { 310 this.keyStoreType = keyStoreType; 311 } 312 313 316 public boolean isNeedClientAuth() { 317 return needClientAuth; 318 } 319 320 323 public void setNeedClientAuth(boolean needClientAuth) { 324 this.needClientAuth = needClientAuth; 325 } 326 327 330 public String getProtocol() { 331 return protocol; 332 } 333 334 337 public void setProtocol(String protocol) { 338 this.protocol = protocol; 339 } 340 341 344 public boolean isWantClientAuth() { 345 return wantClientAuth; 346 } 347 348 351 public void setWantClientAuth(boolean wantClientAuth) { 352 this.wantClientAuth = wantClientAuth; 353 } 354 355 } 356 | Popular Tags |