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.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 43 private int maxThreads = 256; 44 private Server server; 45 private static final Log log = LogFactory.getLog(HttpsConnector.class); 46 private String host; 47 private int port; 48 private String keyPassword; 49 private String keyStore; 50 private String keyStorePassword; 51 private String keyStoreType = "JKS"; private String protocol = "TLS"; 53 private String keyManagerFactoryAlgorithm = "SunX509"; private String trustManagerFactoryAlgorithm = "SunX509"; private boolean wantClientAuth = false; 56 private boolean needClientAuth = false; 57 58 64 public HttpsSoapConnector(String host, int port, String keyPassword, String keyStorePassword, String 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 82 public HttpsSoapConnector(SslSocketConnector listener) { 83 this.listener = listener; 84 } 85 86 92 public void init(ComponentContext cc) throws JBIException { 93 super.init(cc); 94 if (keyStore == null) { 96 keyStore = System.getProperty("javax.net.ssl.keyStore", ""); 97 if (keyStore == null) { 98 throw new IllegalArgumentException ("keyStore or system property javax.net.ssl.keyStore must be set"); 99 } 100 } 101 if (keyStore.startsWith("classpath:")) { 102 try { 103 String res = keyStore.substring(10); 104 URL url = new ClassPathResource(res).getURL(); 105 keyStore = url.toString(); 106 } catch (IOException 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 ("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 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 e) { 162 log.warn(e.toString()); 163 throw new JBIException("Start failed: " + e, e); 164 } 165 } 166 167 170 public void stop() throws JBIException { 171 try { 172 if (server != null) { 173 server.stop(); 174 } 175 } catch (Exception e) { 176 throw new JBIException("Stop failed: " + e, e); 177 } 178 } 179 180 183 public void shutDown() throws JBIException { 184 server = null; 185 } 186 187 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 getHost() { 206 return host; 207 } 208 209 public void setHost(String 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 224 public String getKeyManagerFactoryAlgorithm() { 225 return keyManagerFactoryAlgorithm; 226 } 227 228 231 public void setKeyManagerFactoryAlgorithm(String algorithm) { 232 this.keyManagerFactoryAlgorithm = algorithm; 233 } 234 235 238 public String getTrustManagerFactoryAlgorithm() { 239 return trustManagerFactoryAlgorithm; 240 } 241 242 245 public void setTrustManagerFactoryAlgorithm(String algorithm) { 246 this.trustManagerFactoryAlgorithm = algorithm; 247 } 248 249 252 public String getKeyPassword() { 253 return keyPassword; 254 } 255 256 259 public void setKeyPassword(String keyPassword) { 260 this.keyPassword = keyPassword; 261 } 262 263 266 public String getKeyStore() { 267 return keyStore; 268 } 269 270 273 public void setKeyStore(String keyStore) { 274 this.keyStore = keyStore; 275 } 276 277 280 public String getKeyStorePassword() { 281 return keyStorePassword; 282 } 283 284 287 public void setKeyStorePassword(String keyStorePassword) { 288 this.keyStorePassword = keyStorePassword; 289 } 290 291 294 public String getKeyStoreType() { 295 return keyStoreType; 296 } 297 298 301 public void setKeyStoreType(String keyStoreType) { 302 this.keyStoreType = keyStoreType; 303 } 304 305 308 public boolean isNeedClientAuth() { 309 return needClientAuth; 310 } 311 312 315 public void setNeedClientAuth(boolean needClientAuth) { 316 this.needClientAuth = needClientAuth; 317 } 318 319 322 public String getProtocol() { 323 return protocol; 324 } 325 326 329 public void setProtocol(String protocol) { 330 this.protocol = protocol; 331 } 332 333 336 public boolean isWantClientAuth() { 337 return wantClientAuth; 338 } 339 340 343 public void setWantClientAuth(boolean wantClientAuth) { 344 this.wantClientAuth = wantClientAuth; 345 } 346 } 347 | Popular Tags |