Allow custom SocketFactory to be set (SMACK-140).

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2330 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Matt Tucker 2004-07-05 22:22:02 +00:00 committed by mtucker
parent 0b49cbc374
commit 757c7450d9
2 changed files with 42 additions and 28 deletions

View File

@ -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() {

View File

@ -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. <tt>jivesoftware.com</tt>.
* @param port the port on the server that should be used; e.g. <tt>5222</tt>.
@ -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.
*
* <p>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. <tt>jivesoftware.com</tt>.
* @param port the port on the server that should be used; e.g. <tt>5222</tt>.
* @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"));