KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > MailEngine > SSLConnection


1 package SnowMailClient.MailEngine;
2
3 import SnowMailClient.Language.Language;
4 import java.util.*;
5 import javax.net.ssl.*;
6 import java.net.*;
7
8 public final class SSLConnection
9 {
10   private SSLConnection() {}
11
12   /** just create an ssl connection to the host at the given port
13   */

14   public static Socket createSSLConnection(String JavaDoc host, int port) throws Exception JavaDoc
15   {
16      boolean debug = false;
17
18      Socket s0 = null;
19      try
20      {
21          s0 = new Socket(host, port);
22          s0.setSoTimeout(1000*60);
23      }
24      catch(Exception JavaDoc e)
25      {
26        throw new Exception JavaDoc(Language.translate("Cannot connect to %1 on port %2", host, ""+port), e);
27      }
28
29      try
30      {
31          SSLSocketFactory ssf = VerifiedSSLSocketFactory.getInstance().getSocketFactory();
32          //(SSLSocketFactory) SSLSocketFactory.getDefault();
33
SSLSocket sconnection = (SSLSocket) ssf.createSocket(s0, host, port, true);
34          sconnection.setSoTimeout(1000*60);
35
36          sconnection.setEnabledProtocols(sconnection.getEnabledProtocols());
37          sconnection.setEnabledCipherSuites(sconnection.getEnabledCipherSuites());
38
39          sconnection.setUseClientMode(true);
40          sconnection.setNeedClientAuth(false);
41          sconnection.setWantClientAuth(false);
42          sconnection.setEnableSessionCreation(true);
43
44 // FAIL if certificate not present!!
45
sconnection.startHandshake();
46
47          SSLSession sSLSession = sconnection.getSession();
48
49          if(debug && sSLSession!=null)
50          {
51            String JavaDoc cipherSuite = sSLSession.getCipherSuite();
52            System.out.println("Cipher suite used = "+cipherSuite);
53
54            String JavaDoc proto = sSLSession.getProtocol();
55            System.out.println("Protocol = "+proto);
56          }
57
58          return sconnection;
59      }
60      catch(Exception JavaDoc e)
61      {
62         e.printStackTrace();
63          throw new Exception JavaDoc(Language.translate("Cannot initiale SSL connection to %1 on port %2", host, ""+port), e);
64      }
65
66   } // Constructor
67

68
69
70
71
72 } // SSLConnection
Popular Tags