KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > http > HttpsConnector


1 /*
2  * $Id: HttpsConnector.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.http;
12
13 import java.io.FileNotFoundException JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.security.KeyStore JavaDoc;
17 import java.security.Provider JavaDoc;
18 import java.security.Security JavaDoc;
19
20 import javax.net.ssl.KeyManagerFactory;
21 import javax.net.ssl.TrustManagerFactory;
22
23 import org.mule.config.i18n.Message;
24 import org.mule.config.i18n.Messages;
25 import org.mule.umo.lifecycle.InitialisationException;
26 import org.mule.umo.security.provider.AutoDiscoverySecurityProviderFactory;
27 import org.mule.umo.security.provider.SecurityProviderFactory;
28 import org.mule.umo.security.provider.SecurityProviderInfo;
29 import org.mule.util.FileUtils;
30 import org.mule.util.IOUtils;
31
32 /**
33  * <code>HttpsConnector</code> provides Https connectivity
34  *
35  * @version $Revision: 3798 $
36  */

37 public class HttpsConnector extends HttpConnector
38 {
39     public static final String JavaDoc DEFAULT_KEYSTORE = ".keystore";
40     public static final String JavaDoc DEFAULT_KEYSTORE_TYPE = KeyStore.getDefaultType();
41     public static final String JavaDoc DEFAULT_SSL_TYPE = "SSLv3";
42
43     private SecurityProviderFactory spFactory = new AutoDiscoverySecurityProviderFactory();
44     private SecurityProviderInfo spInfo = spFactory.getSecurityProviderInfo();
45
46     private String JavaDoc keyStore = DEFAULT_KEYSTORE;
47     private String JavaDoc keyPassword = null;
48     private String JavaDoc storePassword = null;
49     private String JavaDoc keystoreType = DEFAULT_KEYSTORE_TYPE;
50     private String JavaDoc keyManagerAlgorithm = spInfo.getKeyManagerAlgorithm();
51     private String JavaDoc sslType = DEFAULT_SSL_TYPE;
52     private Provider JavaDoc provider = spFactory.getProvider();
53     private String JavaDoc protocolHandler = spInfo.getProtocolHandler();
54     private String JavaDoc clientKeyStore = null;
55     private String JavaDoc clientKeyStorePassword = null;
56     private String JavaDoc trustStore = null;
57     private String JavaDoc trustStorePassword = null;
58     private String JavaDoc trustStoreType = DEFAULT_KEYSTORE_TYPE;
59     // default to key manager algorithm, overridable
60
private String JavaDoc trustManagerAlgorithm = spInfo.getKeyManagerAlgorithm();
61     private TrustManagerFactory trustManagerFactory;
62     private boolean explicitTrustStoreOnly = false;
63
64     private KeyManagerFactory keyManagerFactory = null;
65     private boolean requireClientAuthentication = false;
66
67     public void doInitialise() throws InitialisationException
68     {
69         if (getProvider() == null)
70         {
71             throw new NullPointerException JavaDoc("The security provider cannot be null");
72         }
73         if (getKeyStore() == null)
74         {
75             throw new NullPointerException JavaDoc("The KeyStore location cannot be null");
76         }
77         if (getKeyPassword() == null)
78         {
79             throw new NullPointerException JavaDoc("The Key password cannot be null");
80         }
81         if (getStorePassword() == null)
82         {
83             throw new NullPointerException JavaDoc("The KeyStore password cannot be null");
84         }
85         if (getKeyManagerAlgorithm() == null)
86         {
87             throw new NullPointerException JavaDoc("The Key Manager Algorithm cannot be null");
88         }
89
90         KeyStore JavaDoc keystore;
91         try
92         {
93             Security.addProvider(getProvider());
94             // Create keyStore
95
keystore = KeyStore.getInstance(keystoreType);
96             InputStream JavaDoc is = IOUtils.getResourceAsStream(getKeyStore(), getClass());
97             if (is == null)
98             {
99                 throw new FileNotFoundException JavaDoc(new Message(Messages.CANT_LOAD_X_FROM_CLASSPATH_FILE,
100                     "Keystore: " + getKeyStore()).getMessage());
101             }
102             keystore.load(is, getStorePassword().toCharArray());
103         }
104         catch (Exception JavaDoc e)
105         {
106             throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, "KeyStore: "
107                                                                                   + getKeyStore()), e, this);
108         }
109         try
110         {
111             // Get key manager
112
keyManagerFactory = KeyManagerFactory.getInstance(getKeyManagerAlgorithm());
113             // Initialize the KeyManagerFactory to work with our keyStore
114
keyManagerFactory.init(keystore, getKeyPassword().toCharArray());
115         }
116         catch (Exception JavaDoc e)
117         {
118             throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, "Key Manager"), e, this);
119         }
120
121         if (getTrustStore() != null)
122         {
123             KeyStore JavaDoc truststore;
124             try
125             {
126                 truststore = KeyStore.getInstance(trustStoreType);
127                 InputStream JavaDoc is = IOUtils.getResourceAsStream(getTrustStore(), getClass());
128                 if (is == null)
129                 {
130                     throw new FileNotFoundException JavaDoc(
131                         "Failed to load truststore from classpath or local file: " + getTrustStore());
132                 }
133                 truststore.load(is, getTrustStorePassword().toCharArray());
134             }
135             catch (Exception JavaDoc e)
136             {
137                 throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, "TrustStore: "
138                                                                                       + getTrustStore()), e,
139                     this);
140             }
141
142             try
143             {
144                 trustManagerFactory = TrustManagerFactory.getInstance(getTrustManagerAlgorithm());
145                 trustManagerFactory.init(truststore);
146             }
147             catch (Exception JavaDoc e)
148             {
149                 throw new InitialisationException(new Message(Messages.FAILED_LOAD_X,
150                     "Trust Manager (" + getTrustManagerAlgorithm() + ")"), e, this);
151             }
152         }
153
154         super.doInitialise();
155
156         if (protocolHandler != null)
157         {
158             System.setProperty("java.protocol.handler.pkgs", protocolHandler);
159         }
160         if (clientKeyStore != null)
161         {
162             try
163             {
164                 String JavaDoc clientPath = FileUtils.getResourcePath(clientKeyStore, getClass());
165                 if (clientPath == null)
166                 {
167                     throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, "ClientKeyStore: "
168                                                                                           + clientKeyStore),
169                         this);
170                 }
171                 System.setProperty("javax.net.ssl.keyStore", clientPath);
172                 System.setProperty("javax.net.ssl.keyStorePassword", clientKeyStorePassword);
173
174                 logger.info("Set Client Key store: javax.net.ssl.keyStore=" + clientPath);
175             }
176             catch (IOException JavaDoc e)
177             {
178                 throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, "ClientKeyStore: "
179                                                                                       + clientKeyStore), this);
180             }
181         }
182
183         if (trustStore != null)
184         {
185             System.setProperty("javax.net.ssl.trustStore", getTrustStore());
186             if (getTrustStorePassword() != null)
187             {
188                 System.setProperty("javax.net.ssl.trustStorePassword", getTrustStorePassword());
189             }
190             logger.debug("Set Trust store: javax.net.ssl.trustStore=" + getTrustStore());
191         }
192         else if (!isExplicitTrustStoreOnly())
193         {
194             logger.info("Defaulting trust store to client Key Store");
195             trustStore = getClientKeyStore();
196             trustStorePassword = getClientKeyStorePassword();
197             if (trustStore != null)
198             {
199                 System.setProperty("javax.net.ssl.trustStore", getTrustStore());
200                 System.setProperty("javax.net.ssl.trustStorePassword", getTrustStorePassword());
201                 logger.debug("Set Trust store: javax.net.ssl.trustStore=" + getTrustStore());
202             }
203         }
204
205     }
206
207     public String JavaDoc getProtocol()
208     {
209         return "https";
210     }
211
212     public String JavaDoc getKeyStore()
213     {
214         return keyStore;
215     }
216
217     public void setKeyStore(String JavaDoc keyStore)
218     {
219         this.keyStore = keyStore;
220     }
221
222     public String JavaDoc getKeyPassword()
223     {
224         return keyPassword;
225     }
226
227     public void setKeyPassword(String JavaDoc keyPassword)
228     {
229         this.keyPassword = keyPassword;
230     }
231
232     public String JavaDoc getStorePassword()
233     {
234         return storePassword;
235     }
236
237     public void setStorePassword(String JavaDoc storePassword)
238     {
239         this.storePassword = storePassword;
240     }
241
242     public String JavaDoc getTrustStoreType()
243     {
244         return trustStoreType;
245     }
246
247     public void setTrustStoreType(String JavaDoc trustStoreType)
248     {
249         this.trustStoreType = trustStoreType;
250     }
251
252     public TrustManagerFactory getTrustManagerFactory()
253     {
254         return trustManagerFactory;
255     }
256
257     public void setTrustManagerFactory(TrustManagerFactory trustManagerFactory)
258     {
259         this.trustManagerFactory = trustManagerFactory;
260     }
261
262     public String JavaDoc getTrustManagerAlgorithm()
263     {
264         return trustManagerAlgorithm;
265     }
266
267     public void setTrustManagerAlgorithm(String JavaDoc trustManagerAlgorithm)
268     {
269         this.trustManagerAlgorithm = trustManagerAlgorithm;
270     }
271
272     public String JavaDoc getKeystoreType()
273     {
274         return keystoreType;
275     }
276
277     public void setKeystoreType(String JavaDoc keystoreType)
278     {
279         this.keystoreType = keystoreType;
280     }
281
282     public String JavaDoc getKeyManagerAlgorithm()
283     {
284         return keyManagerAlgorithm;
285     }
286
287     public void setKeyManagerAlgorithm(String JavaDoc keyManagerAlgorithm)
288     {
289         this.keyManagerAlgorithm = keyManagerAlgorithm;
290     }
291
292     public String JavaDoc getSslType()
293     {
294         return sslType;
295     }
296
297     public void setSslType(String JavaDoc sslType)
298     {
299         this.sslType = sslType;
300     }
301
302     public boolean isRequireClientAuthentication()
303     {
304         return requireClientAuthentication;
305     }
306
307     public void setRequireClientAuthentication(boolean requireClientAuthentication)
308     {
309         this.requireClientAuthentication = requireClientAuthentication;
310     }
311
312     public KeyManagerFactory getKeyManagerFactory()
313     {
314         return keyManagerFactory;
315     }
316
317     public Provider JavaDoc getProvider()
318     {
319         return provider;
320     }
321
322     public void setProvider(Provider JavaDoc provider)
323     {
324         this.provider = provider;
325     }
326
327     public String JavaDoc getProtocolHandler()
328     {
329         return protocolHandler;
330     }
331
332     public void setProtocolHandler(String JavaDoc protocolHandler)
333     {
334         this.protocolHandler = protocolHandler;
335     }
336
337     public String JavaDoc getClientKeyStore()
338     {
339         return clientKeyStore;
340     }
341
342     public void setClientKeyStore(String JavaDoc clientKeyStore) throws IOException JavaDoc
343     {
344         this.clientKeyStore = clientKeyStore;
345         if (this.clientKeyStore != null)
346         {
347             this.clientKeyStore = FileUtils.getResourcePath(clientKeyStore, getClass());
348             logger.debug("Normalised clientKeyStore path to: " + getClientKeyStore());
349         }
350     }
351
352     public String JavaDoc getClientKeyStorePassword()
353     {
354         return clientKeyStorePassword;
355     }
356
357     public void setClientKeyStorePassword(String JavaDoc clientKeyStorePassword)
358     {
359         this.clientKeyStorePassword = clientKeyStorePassword;
360     }
361
362     public String JavaDoc getTrustStore()
363     {
364         return trustStore;
365     }
366
367     public void setTrustStore(String JavaDoc trustStore) throws IOException JavaDoc
368     {
369         this.trustStore = trustStore;
370         if (this.trustStore != null)
371         {
372             this.trustStore = FileUtils.getResourcePath(trustStore, getClass());
373             logger.debug("Normalised trustStore path to: " + getTrustStore());
374         }
375     }
376
377     public String JavaDoc getTrustStorePassword()
378     {
379         return trustStorePassword;
380     }
381
382     public void setTrustStorePassword(String JavaDoc trustStorePassword)
383     {
384         this.trustStorePassword = trustStorePassword;
385     }
386
387     public boolean isExplicitTrustStoreOnly()
388     {
389         return explicitTrustStoreOnly;
390     }
391
392     public void setExplicitTrustStoreOnly(boolean explicitTrustStoreOnly)
393     {
394         this.explicitTrustStoreOnly = explicitTrustStoreOnly;
395     }
396
397     public SecurityProviderFactory getSecurityProviderFactory()
398     {
399         return spFactory;
400     }
401
402     public void setSecurityProviderFactory(SecurityProviderFactory spFactory)
403     {
404         this.spFactory = spFactory;
405     }
406 }
407
Popular Tags