KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > jmail > base > JMailSSLSocketFactory


1 package org.lucane.applications.jmail.base;
2
3 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4  * This file is part of JMail *
5  * Copyright (C) 2002-2003 Yvan Norsa <norsay@wanadoo.fr> *
6  * *
7  * JMail is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * any later version. *
11  * *
12  * JMail is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License along *
18  * with JMail; if not, write to the Free Software Foundation, Inc., *
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20  * *
21  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

22
23 import java.io.*;
24 import java.net.*;
25 import java.security.*;
26 import javax.net.*;
27 import javax.net.ssl.*;
28
29 /** This class is done thanks to Java Tip #115 ( http://www.javaworld.com/javatips/jw-javatip115.html ) */
30 public class JMailSSLSocketFactory extends SSLSocketFactory
31 {
32     SSLSocketFactory socketfactory;
33     
34     public JMailSSLSocketFactory()
35     {
36     try
37         {
38         SSLContext sslcontext = SSLContext.getInstance("TLS");
39
40         sslcontext.init(null, new TrustManager[]
41                               {
42                       new JMailTrustManager()
43                       }, new SecureRandom());
44
45         socketfactory = (SSLSocketFactory)sslcontext.getSocketFactory();
46         }
47
48     catch(Exception JavaDoc ex)
49     {
50         ex.printStackTrace();
51     }
52     }
53     
54     public static SocketFactory getDefault()
55     {
56     return((SocketFactory)new JMailSSLSocketFactory());
57     }
58
59     public Socket createSocket(Socket s, String JavaDoc hostaddress, int hostport, boolean autoClose) throws IOException
60     {
61     return(socketfactory.createSocket(s, hostaddress, hostport, autoClose));
62     }
63     
64     public Socket createSocket(InetAddress hostaddress, int hostport) throws IOException
65     {
66     return(socketfactory.createSocket(hostaddress, hostport));
67     }
68
69     public Socket createSocket(InetAddress hostaddress, int hostport, InetAddress clientaddress, int clientport) throws IOException
70     {
71     return(socketfactory.createSocket(hostaddress, hostport, clientaddress, clientport));
72     }
73
74     public Socket createSocket(String JavaDoc hostaddress, int hostport) throws IOException
75     {
76     return(socketfactory.createSocket(hostaddress, hostport));
77     }
78
79     public Socket createSocket(String JavaDoc hostaddress, int hostport, InetAddress clientaddress, int clientport) throws IOException
80     {
81     return(socketfactory.createSocket(hostaddress, hostport, clientaddress, clientport));
82     }
83     
84     public String JavaDoc[] getDefaultCipherSuites()
85     {
86     return(socketfactory.getDefaultCipherSuites());
87     }
88
89     public String JavaDoc[] getSupportedCipherSuites()
90     {
91     return(socketfactory.getSupportedCipherSuites());
92     }
93 }
94
Popular Tags