KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > SSLXMPPConnection


1 /**
2  * $RCSfile$
3  * $Revision: 2734 $
4  * $Date: 2005-08-26 23:34:19 -0300 (Fri, 26 Aug 2005) $
5  *
6  * Copyright 2003-2004 Jive Software.
7  *
8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */

20
21 package org.jivesoftware.smack;
22
23 import javax.net.SocketFactory;
24 import javax.net.ssl.SSLContext;
25 import javax.net.ssl.SSLSocketFactory;
26 import javax.net.ssl.TrustManager;
27 import java.io.IOException JavaDoc;
28 import java.net.InetAddress JavaDoc;
29 import java.net.Socket JavaDoc;
30 import java.security.KeyManagementException JavaDoc;
31 import java.security.NoSuchAlgorithmException JavaDoc;
32
33 /**
34  * Creates an SSL connection to a XMPP server.
35  *
36  * @author Matt Tucker
37  */

38 public class SSLXMPPConnection extends XMPPConnection {
39
40     private static SocketFactory socketFactory = new DummySSLSocketFactory();
41
42     /**
43      * Creates a new SSL connection to the specified host on the default
44      * SSL port (5223). The IP address of the server is assumed to match the
45      * service name.
46      *
47      * @param host the XMPP host.
48      * @throws XMPPException if an error occurs while trying to establish the connection.
49      * Two possible errors can occur which will be wrapped by an XMPPException --
50      * UnknownHostException (XMPP error code 504), and IOException (XMPP error code
51      * 502). The error codes and wrapped exceptions can be used to present more
52      * appropiate error messages to end-users.
53      */

54     public SSLXMPPConnection(String JavaDoc host) throws XMPPException {
55         this(host, 5223);
56     }
57
58     /**
59      * Creates a new SSL connection to the specified host on the specified port. The IP address
60      * of the server is assumed to match the service name.
61      *
62      * @param host the XMPP host.
63      * @param port the port to use for the connection (default XMPP SSL port is 5223).
64      * @throws XMPPException if an error occurs while trying to establish the connection.
65      * Two possible errors can occur which will be wrapped by an XMPPException --
66      * UnknownHostException (XMPP error code 504), and IOException (XMPP error code
67      * 502). The error codes and wrapped exceptions can be used to present more
68      * appropiate error messages to end-users.
69      */

70     public SSLXMPPConnection(String JavaDoc host, int port) throws XMPPException {
71         this(host, port, host);
72     }
73
74     /**
75      * Creates a new SSL connection to the specified XMPP server on the given host and port.
76      *
77      * @param host the host name, or null for the loopback address.
78      * @param port the port on the server that should be used (default XMPP SSL port is 5223).
79      * @param serviceName the name of the XMPP server to connect to; e.g. <tt>jivesoftware.com</tt>.
80      * @throws XMPPException if an error occurs while trying to establish the connection.
81      * Two possible errors can occur which will be wrapped by an XMPPException --
82      * UnknownHostException (XMPP error code 504), and IOException (XMPP error code
83      * 502). The error codes and wrapped exceptions can be used to present more
84      * appropiate error messages to end-users.
85      */

86     public SSLXMPPConnection(String JavaDoc host, int port, String JavaDoc serviceName) throws XMPPException {
87         super(host, port, serviceName, socketFactory);
88     }
89
90     public boolean isSecureConnection() {
91         return true;
92     }
93
94     /**
95      * An SSL socket factory that will let any certifacte past, even if it's expired or
96      * not singed by a root CA.
97      */

98     private static class DummySSLSocketFactory extends SSLSocketFactory {
99
100         private SSLSocketFactory factory;
101
102         public DummySSLSocketFactory() {
103
104             try {
105                 SSLContext sslcontent = SSLContext.getInstance("TLS");
106                 sslcontent.init(null, // KeyManager not required
107
new TrustManager[] { new OpenTrustManager() },
108                             new java.security.SecureRandom JavaDoc());
109                 factory = sslcontent.getSocketFactory();
110             }
111             catch (NoSuchAlgorithmException JavaDoc e) {
112                 e.printStackTrace();
113             }
114             catch (KeyManagementException JavaDoc e) {
115                 e.printStackTrace();
116             }
117         }
118
119         public static SocketFactory getDefault() {
120             return new DummySSLSocketFactory();
121         }
122
123         public Socket JavaDoc createSocket(Socket JavaDoc socket, String JavaDoc s, int i, boolean flag)
124                 throws IOException JavaDoc
125         {
126             return factory.createSocket(socket, s, i, flag);
127         }
128
129         public Socket JavaDoc createSocket(InetAddress JavaDoc inaddr, int i, InetAddress JavaDoc inaddr2, int j)
130                 throws IOException JavaDoc
131         {
132             return factory.createSocket(inaddr, i, inaddr2, j);
133         }
134
135         public Socket JavaDoc createSocket(InetAddress JavaDoc inaddr, int i) throws IOException JavaDoc {
136             return factory.createSocket(inaddr, i);
137         }
138
139         public Socket JavaDoc createSocket(String JavaDoc s, int i, InetAddress JavaDoc inaddr, int j) throws IOException JavaDoc {
140             return factory.createSocket(s, i, inaddr, j);
141         }
142
143         public Socket JavaDoc createSocket(String JavaDoc s, int i) throws IOException JavaDoc {
144             return factory.createSocket(s, i);
145         }
146
147         public String JavaDoc[] getDefaultCipherSuites() {
148             return factory.getSupportedCipherSuites();
149         }
150
151         public String JavaDoc[] getSupportedCipherSuites() {
152             return factory.getSupportedCipherSuites();
153         }
154     }
155 }
156
Popular Tags