Class SSLIRCConnection
- java.lang.Object
-
- java.lang.Thread
-
- org.schwering.irc.lib.IRCConnection
-
- org.schwering.irc.lib.ssl.SSLIRCConnection
-
- All Implemented Interfaces:
java.lang.Runnable
public class SSLIRCConnection extends IRCConnection
The SSL extension of theIRCConnection
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.SSLTrustManager
s can be added and removed until theconnect
method is invoked. If noSSLTrustManager
s are set until then, anSSLDefaultTrustManager
is set automatically. It accepts all X509 certificates.- Since:
- 1.10
- See Also:
IRCConnection
,SSLTrustManager
-
-
Constructor Summary
Constructors Constructor Description 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addTrustManager(SSLTrustManager trustManager)
Adds a newSSLTrustManager
.void
connect()
Establish a connection to the server.SSLTrustManager[]
getTrustManagers()
Returns the setSSLTrustManager
s.boolean
removeTrustManager(SSLTrustManager trustManager)
Removes oneSSLTrustManager
.-
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, prepare, removeIRCEventListener, run, send, setColors, setEncoding, setPong, setTimeout, toString
-
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, yield
-
-
-
-
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 anint[]
. 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 theconnect
method. Before invoking it, you should set theIRCEventListener
, optionally theSSLTrustManager
, if you don't want to use theSSLDefaultTrustManager
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""
ornull
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), usenull
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 thehost
orports
isnull
orports
' length is0
.- 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 twoint
s. Thus, only a portrange from portx
to porty
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 theconnect
method. Before invoking it, you should set theIRCEventListener
, optionally theSSLTrustManager
, if you don't want to use theSSLDefaultTrustManager
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""
ornull
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), usenull
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 thehost
isnull
.- 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 anIOException
. If anything SSL related fails (for example conflicts with the algorithms or during the handshaking), aSSLException
is thrown.
You can invokeconnect
only one time.- Overrides:
connect
in classIRCConnection
- 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 theconnect
method was already invoked.- See Also:
IRCConnection.isConnected()
,IRCConnection.doQuit()
,IRCConnection.doQuit(String)
,IRCConnection.close()
-
addTrustManager
public void addTrustManager(SSLTrustManager trustManager)
Adds a newSSLTrustManager
.- Parameters:
trustManager
- TheSSLTrustManager
object which is to add.- See Also:
removeTrustManager(SSLTrustManager)
,getTrustManagers()
-
removeTrustManager
public boolean removeTrustManager(SSLTrustManager trustManager)
Removes oneSSLTrustManager
.- Parameters:
trustManager
- TheSSLTrustManager
object which is to remove.- Returns:
true
if aSSLTrustManager
was removed.- See Also:
addTrustManager(SSLTrustManager)
,getTrustManagers()
-
getTrustManagers
public SSLTrustManager[] getTrustManagers()
Returns the setSSLTrustManager
s. The defaultSSLTrustManager
is an instance ofSSLDefaultTrustManager
, which is set when you invoke theconnect
method without having set anotherSSLTrustManager
.- Returns:
- The set
SSLTrustManager
s. - See Also:
addTrustManager(SSLTrustManager)
,removeTrustManager(SSLTrustManager)
-
-