diff --git a/source/org/jivesoftware/smack/SSLXMPPConnection.java b/source/org/jivesoftware/smack/SSLXMPPConnection.java index 33c8d883f..ba47e61e4 100644 --- a/source/org/jivesoftware/smack/SSLXMPPConnection.java +++ b/source/org/jivesoftware/smack/SSLXMPPConnection.java @@ -60,15 +60,11 @@ import java.net.*; import java.security.NoSuchAlgorithmException; import java.security.KeyManagementException; import javax.net.SocketFactory; -import javax.net.ssl.SSLSocketFactory; import com.sun.net.ssl.X509TrustManager; import java.security.cert.X509Certificate; import java.security.cert.CertificateExpiredException; import java.security.cert.CertificateNotYetValidException; -import org.jivesoftware.smack.packet.XMPPError; - - /** * Creates an SSL connection to a XMPP server. * @@ -76,6 +72,8 @@ import org.jivesoftware.smack.packet.XMPPError; */ public class SSLXMPPConnection extends XMPPConnection { + private static SocketFactory socketFactory = new DummySSLSocketFactory(); + /** * Creates a new SSL connection to the specified host on the default * SSL port (5223). @@ -103,21 +101,7 @@ public class SSLXMPPConnection extends XMPPConnection { * appropiate error messages to end-users. */ public SSLXMPPConnection(String host, int port) throws XMPPException { - this.host = host; - this.port = port; - try { - SSLSocketFactory sslFactory = new DummySSLSocketFactory(); - this.socket = sslFactory.createSocket(host, port); - } - catch (UnknownHostException uhe) { - throw new XMPPException("Could not connect to " + host + ":" + port + ".", - new XMPPError(504), uhe); - } - catch (IOException ioe) { - throw new XMPPException("XMPPError connecting to " + host + ":" + port + ".", - new XMPPError(502), ioe); - } - super.init(); + super(host, port, socketFactory); } public boolean isSecureConnection() { diff --git a/source/org/jivesoftware/smack/XMPPConnection.java b/source/org/jivesoftware/smack/XMPPConnection.java index 5e50da130..c7875f946 100644 --- a/source/org/jivesoftware/smack/XMPPConnection.java +++ b/source/org/jivesoftware/smack/XMPPConnection.java @@ -56,6 +56,7 @@ import org.jivesoftware.smack.packet.*; import org.jivesoftware.smack.debugger.*; import org.jivesoftware.smack.filter.*; +import javax.net.SocketFactory; import java.lang.reflect.Constructor; import java.net.*; import java.util.*; @@ -127,13 +128,6 @@ public class XMPPConnection { Writer writer; Reader reader; - /** - * Constructor for use by classes extending this one. - */ - XMPPConnection() { - - } - /** * Creates a new connection to the specified XMPP server. The default port of 5222 will * be used. @@ -150,7 +144,7 @@ public class XMPPConnection { } /** - * Creates a new connection to the to the specified XMPP server on the given port. + * Creates a new connection to the specified XMPP server on the given port. * * @param host the name of the XMPP server to connect to; e.g. jivesoftware.com. * @param port the port on the server that should be used; e.g. 5222. @@ -181,6 +175,42 @@ public class XMPPConnection { init(); } + /** + * Creates a new connection to the specified XMPP server on the given port using the specified SocketFactory. + * + *

A custom SocketFactory allows fine-grained control of the actual connection to the XMPP server. A typical + * use for a custom SocketFactory is when connecting through a SOCKS proxy. + * + * @param host the name of the XMPP server to connect to; e.g. jivesoftware.com. + * @param port the port on the server that should be used; e.g. 5222. + * @param socketFactory a SocketFactory that will be used to create the socket to the XMPP server. + * @throws XMPPException if an error occurs while trying to establish the connection. + * Two possible errors can occur which will be wrapped by an XMPPException -- + * UnknownHostException (XMPP error code 504), and IOException (XMPP error code + * 502). The error codes and wrapped exceptions can be used to present more + * appropiate error messages to end-users. + */ + public XMPPConnection(String host, int port, SocketFactory socketFactory) throws XMPPException { + this.host = host; + this.port = port; + try { + this.socket = socketFactory.createSocket(host, port); + } + catch (UnknownHostException uhe) { + throw new XMPPException( + "Could not connect to " + host + ":" + port + ".", + new XMPPError(504), + uhe); + } + catch (IOException ioe) { + throw new XMPPException( + "XMPPError connecting to " + host + ":" + port + ".", + new XMPPError(502), + ioe); + } + init(); + } + /** * Returns the connection ID for this connection, which is the value set by the server * when opening a XMPP stream. If the server does not set a connection ID, this value @@ -673,7 +703,7 @@ public class XMPPConnection { * * @throws XMPPException if establishing a connection to the server fails. */ - void init() throws XMPPException { + private void init() throws XMPPException { try { reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));