1 17 package org.apache.servicemix.components.http; 18 19 import java.io.IOException ; 20 import java.net.InetAddress ; 21 import java.net.InetSocketAddress ; 22 import java.net.Socket ; 23 import java.net.SocketAddress ; 24 import java.net.URI ; 25 import java.net.URL ; 26 import java.net.UnknownHostException ; 27 import java.security.KeyStore ; 28 29 import javax.jbi.JBIException; 30 import javax.jbi.messaging.MessageExchange; 31 import javax.jbi.messaging.MessagingException; 32 import javax.jbi.messaging.NormalizedMessage; 33 import javax.net.ssl.KeyManagerFactory; 34 import javax.net.ssl.SSLContext; 35 import javax.net.ssl.SSLSocketFactory; 36 import javax.net.ssl.TrustManagerFactory; 37 38 import org.apache.commons.httpclient.ConnectTimeoutException; 39 import org.apache.commons.httpclient.HostConfiguration; 40 import org.apache.commons.httpclient.HttpClient; 41 import org.apache.commons.httpclient.HttpHost; 42 import org.apache.commons.httpclient.HttpStatus; 43 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 44 import org.apache.commons.httpclient.methods.PostMethod; 45 import org.apache.commons.httpclient.params.HttpConnectionParams; 46 import org.apache.commons.httpclient.protocol.Protocol; 47 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; 48 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory; 49 import org.apache.servicemix.MessageExchangeListener; 50 import org.apache.servicemix.components.util.TransformComponentSupport; 51 import org.mortbay.resource.Resource; 52 import org.springframework.core.io.ClassPathResource; 53 54 59 public class HttpsInvoker extends TransformComponentSupport implements MessageExchangeListener { 60 61 protected HttpClientMarshaler marshaler = new HttpClientMarshaler(); 62 protected MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); 63 protected HttpClient httpClient = new HttpClient(connectionManager); 64 protected HostConfiguration hostConfiguration = new HostConfiguration(); 65 protected String url; 66 protected boolean defaultInOut = true; 67 68 private String keyPassword; 69 private String keyStore; 70 private String keyStorePassword; 71 private String keyStoreType = "JKS"; private String trustStore; 73 private String trustStorePassword; 74 private String trustStoreType = "JKS"; 75 private String protocol = "TLS"; 76 private String algorithm = "SunX509"; 78 private class CommonsHttpSSLSocketFactory implements SecureProtocolSocketFactory { 79 80 private SSLSocketFactory factory; 81 82 public CommonsHttpSSLSocketFactory() throws Exception { 83 SSLContext context = SSLContext.getInstance(protocol); 84 KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(algorithm); 85 if (keyStore == null) { 86 keyStore = System.getProperty("javax.net.ssl.keyStore"); 87 if (keyStore == null) { 88 throw new IllegalArgumentException ("keyStore or system property javax.net.ssl.keyStore must be set"); 89 } 90 } 91 if (keyStore.startsWith("classpath:")) { 92 try { 93 String res = keyStore.substring(10); 94 URL url = new ClassPathResource(res).getURL(); 95 keyStore = url.toString(); 96 } catch (IOException e) { 97 throw new JBIException("Unable to find keyStore " + keyStore, e); 98 } 99 } 100 if (keyStorePassword == null) { 101 keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword"); 102 if (keyStorePassword == null) { 103 throw new IllegalArgumentException ("keyStorePassword or system property javax.net.ssl.keyStorePassword must be set"); 104 } 105 } 106 if (trustStore == null) { 107 trustStore = System.getProperty("javax.net.ssl.trustStore"); 108 } 109 if (trustStore != null && trustStore.startsWith("classpath:")) { 110 try { 111 String res = trustStore.substring(10); 112 URL url = new ClassPathResource(res).getURL(); 113 trustStore = url.toString(); 114 } catch (IOException e) { 115 throw new JBIException("Unable to find trustStore " + trustStore, e); 116 } 117 } 118 if (trustStorePassword == null) { 119 trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword"); 120 if (keyStorePassword == null) { 121 throw new IllegalArgumentException ("trustStorePassword or system property javax.net.ssl.trustStorePassword must be set"); 122 } 123 } 124 KeyStore ks = KeyStore.getInstance(keyStoreType); 125 ks.load(Resource.newResource(keyStore).getInputStream(), keyStorePassword.toCharArray()); 126 keyManagerFactory.init(ks, keyPassword != null ? keyPassword.toCharArray() : keyStorePassword.toCharArray()); 127 if (trustStore != null) { 128 KeyStore ts = KeyStore.getInstance(trustStoreType); 129 ts.load(Resource.newResource(trustStore).getInputStream(), trustStorePassword.toCharArray()); 130 TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(algorithm); 131 trustManagerFactory.init(ts); 132 context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new java.security.SecureRandom ()); 133 } else { 134 context.init(keyManagerFactory.getKeyManagers(), null, new java.security.SecureRandom ()); 135 } 136 factory = context.getSocketFactory(); 137 } 138 139 public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException , UnknownHostException { 140 return factory.createSocket(socket, host, port, autoClose); 141 } 142 143 public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException , UnknownHostException { 144 return factory.createSocket(host, port, localAddress, localPort); 145 } 146 147 public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException , UnknownHostException , ConnectTimeoutException { 148 if (params == null) { 149 throw new IllegalArgumentException ("Parameters may not be null"); 150 } 151 int timeout = params.getConnectionTimeout(); 152 if (timeout == 0) { 153 return createSocket(host, port, localAddress, localPort); 154 } else { 155 Socket socket = factory.createSocket(); 156 SocketAddress localaddr = new InetSocketAddress (localAddress, localPort); 157 SocketAddress remoteaddr = new InetSocketAddress (host, port); 158 socket.bind(localaddr); 159 socket.connect(remoteaddr, timeout); 160 return socket; 161 } 162 } 163 164 public Socket createSocket(String host, int port) throws IOException , UnknownHostException { 165 return factory.createSocket(host, port); 166 } 167 168 } 169 170 protected void init() throws JBIException { 171 super.init(); 172 try { 173 URI uri = new URI (url); 174 ProtocolSocketFactory sf = new CommonsHttpSSLSocketFactory(); 175 Protocol protocol = new Protocol("https", sf, 443); 176 HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), protocol); 177 hostConfiguration.setHost(host); 178 } catch (Exception e) { 179 throw new JBIException("Unable to initialize HttpsInvoker", e); 180 } 181 } 182 183 public void stop() throws JBIException { 184 super.stop(); 185 connectionManager.shutdown(); 186 } 187 188 protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException { 189 String url; 190 try { 193 java.net.URI uri = new URI (this.url); 194 uri = uri.relativize(new URI (hostConfiguration.getHostURL())); 195 url = uri.toString(); 196 } catch (Exception e1) { 197 url = this.url; 198 } 199 PostMethod method = new PostMethod(url); 200 try { 201 marshaler.fromNMS(method, exchange, in); 202 if (method.getRequestHeader("Content-Type") == null) { 203 method.setRequestHeader("Content-Type", "text/html; charset=UTF-8"); 204 } 205 int response = httpClient.executeMethod(hostConfiguration, method); 206 207 if (response != HttpStatus.SC_OK && response != HttpStatus.SC_ACCEPTED) { 208 throw new InvalidStatusResponseException(response); 209 } 210 211 if (defaultInOut) { 213 marshaler.toNMS(out, method); 214 } 215 return defaultInOut; 216 } 217 catch (Exception e) { 218 throw new MessagingException("Error executing http request", e); 219 } 220 finally { 221 method.releaseConnection(); 222 } 223 } 224 225 public HttpClient getHttpClient() { 226 return httpClient; 227 } 228 229 public void setHttpClient(HttpClient httpClient) { 230 this.httpClient = httpClient; 231 } 232 233 public String getUrl() { 234 return url; 235 } 236 237 public void setUrl(String url) { 238 this.url = url; 239 } 240 241 public boolean isDefaultInOut() { 242 return defaultInOut; 243 } 244 245 public void setDefaultInOut(boolean defaultInOut) { 246 this.defaultInOut = defaultInOut; 247 } 248 249 public HttpClientMarshaler getMarshaler() { 250 return marshaler; 251 } 252 253 public void setMarshaler(HttpClientMarshaler marshaler) { 254 this.marshaler = marshaler; 255 } 256 257 260 public String getAlgorithm() { 261 return algorithm; 262 } 263 264 267 public void setAlgorithm(String algorithm) { 268 this.algorithm = algorithm; 269 } 270 271 274 public String getKeyPassword() { 275 return keyPassword; 276 } 277 278 281 public void setKeyPassword(String keyPassword) { 282 this.keyPassword = keyPassword; 283 } 284 285 288 public String getKeyStore() { 289 return keyStore; 290 } 291 292 295 public void setKeyStore(String keyStore) { 296 this.keyStore = keyStore; 297 } 298 299 302 public String getKeyStorePassword() { 303 return keyStorePassword; 304 } 305 306 309 public void setKeyStorePassword(String keyStorePassword) { 310 this.keyStorePassword = keyStorePassword; 311 } 312 313 316 public String getKeyStoreType() { 317 return keyStoreType; 318 } 319 320 323 public void setKeyStoreType(String keyStoreType) { 324 this.keyStoreType = keyStoreType; 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 String getTrustStore() { 345 return trustStore; 346 } 347 348 351 public void setTrustStore(String trustStore) { 352 this.trustStore = trustStore; 353 } 354 355 358 public String getTrustStorePassword() { 359 return trustStorePassword; 360 } 361 362 365 public void setTrustStorePassword(String trustStorePassword) { 366 this.trustStorePassword = trustStorePassword; 367 } 368 369 372 public String getTrustStoreType() { 373 return trustStoreType; 374 } 375 376 379 public void setTrustStoreType(String trustStoreType) { 380 this.trustStoreType = trustStoreType; 381 } 382 383 } 384 | Popular Tags |