KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > http > processors > CommonsHttpSSLSocketFactory


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.http.processors;
18
19 import java.io.IOException JavaDoc;
20 import java.net.InetAddress JavaDoc;
21 import java.net.InetSocketAddress JavaDoc;
22 import java.net.Socket JavaDoc;
23 import java.net.SocketAddress JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26 import java.security.KeyStore JavaDoc;
27
28 import javax.jbi.JBIException;
29 import javax.net.ssl.KeyManagerFactory;
30 import javax.net.ssl.SSLContext;
31 import javax.net.ssl.SSLSocketFactory;
32 import javax.net.ssl.TrustManagerFactory;
33
34 import org.apache.commons.httpclient.ConnectTimeoutException;
35 import org.apache.commons.httpclient.params.HttpConnectionParams;
36 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
37 import org.apache.servicemix.http.SslParameters;
38 import org.apache.servicemix.jbi.security.keystore.KeystoreManager;
39 import org.mortbay.resource.Resource;
40 import org.springframework.core.io.ClassPathResource;
41
42 public class CommonsHttpSSLSocketFactory implements SecureProtocolSocketFactory {
43
44     private SSLSocketFactory factory;
45     
46     public CommonsHttpSSLSocketFactory(SslParameters ssl, KeystoreManager keystoreManager) throws Exception JavaDoc {
47         if (ssl.isManaged()) {
48             createManagedFactory(ssl, keystoreManager);
49         } else {
50             createUnmanagedFactory(ssl);
51         }
52     }
53     
54     protected void createManagedFactory(SslParameters ssl, KeystoreManager keystoreManager) throws Exception JavaDoc {
55         factory = keystoreManager.createSSLFactory(
56                         ssl.getProvider(),
57                         ssl.getProtocol(),
58                         ssl.getKeyManagerFactoryAlgorithm(),
59                         ssl.getKeyStore(),
60                         ssl.getKeyAlias(),
61                         ssl.getTrustStore());
62     }
63     
64     protected void createUnmanagedFactory(SslParameters ssl) throws Exception JavaDoc {
65         SSLContext context;
66         if (ssl.getProvider() == null) {
67             context = SSLContext.getInstance(ssl.getProtocol());
68         } else {
69             context = SSLContext.getInstance(ssl.getProtocol(), ssl.getProvider());
70         }
71         KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(ssl.getKeyManagerFactoryAlgorithm());
72         String JavaDoc keyStore = ssl.getKeyStore();
73         if (keyStore == null) {
74             keyStore = System.getProperty("javax.net.ssl.keyStore");
75             if (keyStore == null) {
76                 throw new IllegalArgumentException JavaDoc("keyStore or system property javax.net.ssl.keyStore must be set");
77             }
78         }
79         if (keyStore.startsWith("classpath:")) {
80             try {
81                 String JavaDoc res = keyStore.substring(10);
82                 URL JavaDoc url = new ClassPathResource(res).getURL();
83                 keyStore = url.toString();
84             } catch (IOException JavaDoc e) {
85                 throw new JBIException("Unable to find keyStore " + keyStore, e);
86             }
87         }
88         String JavaDoc keyStorePassword = ssl.getKeyStorePassword();
89         if (keyStorePassword == null) {
90             keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword");
91             if (keyStorePassword == null) {
92                 throw new IllegalArgumentException JavaDoc("keyStorePassword or system property javax.net.ssl.keyStorePassword must be set");
93             }
94         }
95         String JavaDoc trustStore = ssl.getTrustStore();
96         String JavaDoc trustStorePassword = null;
97         if (trustStore == null) {
98             trustStore = System.getProperty("javax.net.ssl.trustStore");
99         }
100         if (trustStore != null) {
101             if (trustStore.startsWith("classpath:")) {
102                 try {
103                     String JavaDoc res = trustStore.substring(10);
104                     URL JavaDoc url = new ClassPathResource(res).getURL();
105                     trustStore = url.toString();
106                 } catch (IOException JavaDoc e) {
107                     throw new JBIException("Unable to find trustStore " + trustStore, e);
108                 }
109             }
110             trustStorePassword = ssl.getTrustStorePassword();
111             if (trustStorePassword == null) {
112                 trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
113                 if (trustStorePassword == null) {
114                     throw new IllegalArgumentException JavaDoc("trustStorePassword or system property javax.net.ssl.trustStorePassword must be set");
115                 }
116             }
117         }
118         KeyStore JavaDoc ks = KeyStore.getInstance(ssl.getKeyStoreType());
119         ks.load(Resource.newResource(keyStore).getInputStream(), keyStorePassword.toCharArray());
120         keyManagerFactory.init(ks, ssl.getKeyPassword() != null ? ssl.getKeyPassword().toCharArray() : keyStorePassword.toCharArray());
121         if (trustStore != null) {
122             KeyStore JavaDoc ts = KeyStore.getInstance(ssl.getTrustStoreType());
123             ts.load(Resource.newResource(trustStore).getInputStream(), trustStorePassword.toCharArray());
124             TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(ssl.getTrustManagerFactoryAlgorithm());
125             trustManagerFactory.init(ts);
126             context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new java.security.SecureRandom JavaDoc());
127         } else {
128             context.init(keyManagerFactory.getKeyManagers(), null, new java.security.SecureRandom JavaDoc());
129         }
130         factory = context.getSocketFactory();
131     }
132     
133     public Socket JavaDoc createSocket(Socket JavaDoc socket, String JavaDoc host, int port, boolean autoClose) throws IOException JavaDoc, UnknownHostException JavaDoc {
134         return factory.createSocket(socket, host, port, autoClose);
135     }
136
137     public Socket JavaDoc createSocket(String JavaDoc host, int port, InetAddress JavaDoc localAddress, int localPort) throws IOException JavaDoc, UnknownHostException JavaDoc {
138         return factory.createSocket(host, port, localAddress, localPort);
139     }
140
141     public Socket JavaDoc createSocket(String JavaDoc host, int port, InetAddress JavaDoc localAddress, int localPort, HttpConnectionParams params) throws IOException JavaDoc, UnknownHostException JavaDoc, ConnectTimeoutException {
142         if (params == null) {
143             throw new IllegalArgumentException JavaDoc("Parameters may not be null");
144         }
145         int timeout = params.getConnectionTimeout();
146         if (timeout == 0) {
147             return createSocket(host, port, localAddress, localPort);
148         } else {
149             Socket JavaDoc socket = factory.createSocket();
150             SocketAddress JavaDoc localaddr = new InetSocketAddress JavaDoc(localAddress, localPort);
151             SocketAddress JavaDoc remoteaddr = new InetSocketAddress JavaDoc(host, port);
152             socket.bind(localaddr);
153             socket.connect(remoteaddr, timeout);
154             return socket;
155         }
156     }
157
158     public Socket JavaDoc createSocket(String JavaDoc host, int port) throws IOException JavaDoc, UnknownHostException JavaDoc {
159         return factory.createSocket(host, port);
160     }
161     
162 }
163
164
Popular Tags