org.schwering.irc.lib.ssl
Class SSLIRCConnection

java.lang.Object
  extended byjava.lang.Thread
      extended byorg.schwering.irc.lib.IRCConnection
          extended byorg.schwering.irc.lib.ssl.SSLIRCConnection
All Implemented Interfaces:
java.lang.Runnable

public class SSLIRCConnection
extends IRCConnection

The SSL extension of the IRCConnection class.

The IRC server you want to connect to must accept SSL connections. Otherwise you cannot connect to it with an instance of SSLIRCConnection. IRC servers which accept SSL connections are really very rare, because SSL means a high load for the server.

The following sample code tries to establish an IRC connection to an IRC server which must support SSL. Differences to the code which demonstrates the use of of the IRCConnection class are printed in bold font:


 /* 
  * The following code of a class which imports org.schwering.irc.lib.*
  * prepares an SSL IRC connection and then tries to establish the 
  * connection. The server is "irc.somenetwork.com", the ports are 
  * the default SSL port (443) and the port used on most SSL IRC servers
  * (994). No password is used (null). The nickname is "Foo" and 
  * the realname is "Mr. Foobar". The username "foobar".
  * Because of setDaemon(true), the JVM exits even if this thread is 
  * running.
  * By setting an instance of SSLDefaultTrustManager as TrustManager
  * (which is also done implicitely by the SSLIRCConnection class if no
  * TrustManager is set until the connect method is invoked), the
  * X509Certificate is accepted automatically. Of course, you can write
  * your own TrustManager. For example, you could write a class which
  * extends SSLDefaultTrustManager and overrides its checkServerTrusted
  * method. In the new checkServerTrusted method, you could ask the user
  * to accept or reject the certificate.
  * An instance of the class MyListener which must implement 
  * IRCActionListener is set as event-listener for the connection. 
  * The connection is told to parse out mIRC color codes and to enable
  * automatic PING? PONG! replies.
  */
 SSLIRCConnection conn = new SSLIRCConnection(
                               "irc.somenetwork.com", 
                               new int[] { 443, 994 },  
                               null, 
                               "Foo", 
                               "Mr. Foobar", 
                               "foo@bar.com" 
                             ); 
 
 conn.addIRCEventListener(new MyListener()); 
 conn.addTrustManager(new SSLDefaultTrustManager());
 conn.setDaemon(true);
 conn.setColors(false); 
 conn.setPong(true); 
   
 try {
   conn.connect(); // Try to connect!!! Don't forget this!!!
 } catch (IOException ioexc) {
   ioexc.printStackTrace(); 
 }
 

The serverpassword isn't needed in most cases. You can give null or "" instead as done in this example.

SSLTrustManagers can be added and removed until the connect method is invoked. If no SSLTrustManagers are set until then, an SSLDefaultTrustManager is set automatically. It accepts all X509 certificates.

Since:
1.10
Version:
2.00
Author:
Christoph Schwering <schwering@gmail.com>
See Also:
IRCConnection, SSLTrustManager

Field Summary
static java.lang.String protocol
          The SSL protocol of choice.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
SSLIRCConnection(java.lang.String host, int[] ports, java.lang.String pass, java.lang.String nick, java.lang.String username, java.lang.String realname)
          Creates a new IRC connection with secure sockets (SSL).
SSLIRCConnection(java.lang.String host, int portMin, int portMax, java.lang.String pass, java.lang.String nick, java.lang.String username, java.lang.String realname)
          Creates a new IRC connection with secure sockets (SSL).
 
Method Summary
 void addTrustManager(SSLTrustManager trustManager)
          Adds a new SSLTrustManager.
 void connect()
          Establish a connection to the server.
 SSLTrustManager[] getTrustManagers()
          Returns the set SSLTrustManagers.
 boolean removeTrustManager(SSLTrustManager trustManager)
          Removes one SSLTrustManager.
 
Methods inherited from class org.schwering.irc.lib.IRCConnection
addIRCEventListener, close, doAway, doAway, doInvite, doIson, doJoin, doJoin, doKick, doKick, doList, doList, doMode, doMode, doNames, doNames, doNick, doNotice, doPart, doPart, doPong, doPrivmsg, doQuit, doQuit, doTopic, doTopic, doUserhost, doWho, doWhois, doWhowas, getColors, getEncoding, getHost, getNick, getPassword, getPong, getPort, getPorts, getRealname, getTimeout, getUsername, isConnected, removeIRCEventListener, run, send, setColors, setEncoding, setPong, setTimeout, toString
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, yield
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

protocol

public static java.lang.String protocol
The SSL protocol of choice. Values can be "TLS", "SSLv3" or "SSL". "SSL" is the default value.

Constructor Detail

SSLIRCConnection

public SSLIRCConnection(java.lang.String host,
                        int[] ports,
                        java.lang.String pass,
                        java.lang.String nick,
                        java.lang.String username,
                        java.lang.String realname)
Creates a new IRC connection with secure sockets (SSL).
The difference to the other constructor is, that it transmits the ports in an int[]. Thus, also ports like 994, 6000 and 6697 can be selected.

The constructor prepares a new IRC connection with secure sockets which can be really started by invoking the connect method. Before invoking it, you should set the IRCEventListener, optionally the SSLTrustManager, if you don't want to use the SSLDefaultTrustManager which accepts the X509 certificate automatically, and other settings.
Note that you do not need to set a password to connect to the large public IRC networks like QuakeNet, EFNet etc. To use no password in your IRC connection, use "" or null for the password argument in the constructor.

Parameters:
host - The hostname of the server we want to connect to.
ports - The portrange to which we want to connect.
pass - The password of the IRC server. If your server isn't secured by a password (that's normal), use null or "".
nick - The nickname for the connection. Is used to register the connection.
username - The username. Is used to register the connection.
realname - The realname. Is used to register the connection.
Throws:
java.lang.IllegalArgumentException - If the host or ports is null or ports' length is 0.
See Also:
connect()

SSLIRCConnection

public SSLIRCConnection(java.lang.String host,
                        int portMin,
                        int portMax,
                        java.lang.String pass,
                        java.lang.String nick,
                        java.lang.String username,
                        java.lang.String realname)
Creates a new IRC connection with secure sockets (SSL).
The difference to the other constructor is, that it transmits the ports as two ints. Thus, only a portrange from port x to port y like from port 6000 to 6010 can be selected.

The constructor prepares a new IRC connection with secure sockets which can be really started by invoking the connect method. Before invoking it, you should set the IRCEventListener, optionally the SSLTrustManager, if you don't want to use the SSLDefaultTrustManager which accepts the X509 certificate automatically, and other settings.
Note that you do not need to set a password to connect to the large public IRC networks like QuakeNet, EFNet etc. To use no password in your IRC connection, use "" or null for the password argument in the constructor.

Parameters:
host - The hostname of the server we want to connect to.
portMin - The beginning of the port range we are going to connect to.
portMax - The ending of the port range we are going to connect to.
pass - The password of the IRC server. If your server isn't secured by a password (that's normal), use null or "".
nick - The nickname for the connection. Is used to register the connection.
username - The username. Is used to register the connection.
realname - The realname. Is used to register the connection.
Throws:
java.lang.IllegalArgumentException - If the host is null.
See Also:
connect()
Method Detail

connect

public void connect()
             throws java.io.IOException
Establish a connection to the server.
This method must be invoked to start a connection; the constructor doesn't do that!
It tries all set ports until one is open. If all ports fail it throws an IOException. If anything SSL related fails (for example conflicts with the algorithms or during the handshaking), a SSLException is thrown.
You can invoke connect only one time.

Overrides:
connect in class IRCConnection
Throws:
java.lang.NoClassDefFoundError - If SSL is not supported. This is the case if neither JSSE nor J2SE 1.4 or later is installed.
SSLNotSupportedException - If SSL is not supported. This is the case if neither JSSE nor J2SE 1.4 or later is installed. This exception is thrown if no NoClassDefFoundError is thrown.
java.io.IOException - If an I/O error occurs.
javax.net.ssl.SSLException - If anything with the secure sockets fails.
java.net.SocketException - If the connect method was already invoked.
See Also:
IRCConnection.isConnected(), IRCConnection.doQuit(), IRCConnection.doQuit(String), IRCConnection.close()

addTrustManager

public void addTrustManager(SSLTrustManager trustManager)
Adds a new SSLTrustManager.

Parameters:
trustManager - The SSLTrustManager object which is to add.
See Also:
removeTrustManager(SSLTrustManager), getTrustManagers()

removeTrustManager

public boolean removeTrustManager(SSLTrustManager trustManager)
Removes one SSLTrustManager.

Parameters:
trustManager - The SSLTrustManager object which is to remove.
Returns:
true if a SSLTrustManager was removed.
See Also:
addTrustManager(SSLTrustManager), getTrustManagers()

getTrustManagers

public SSLTrustManager[] getTrustManagers()
Returns the set SSLTrustManagers. The default SSLTrustManager is an instance of SSLDefaultTrustManager, which is set when you invoke the connect method without having set another SSLTrustManager.

Returns:
The set SSLTrustManagers.
See Also:
addTrustManager(SSLTrustManager), removeTrustManager(SSLTrustManager)