2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* Copyright 2003-2004 Jive Software.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
2005-08-27 04:33:08 +02:00
|
|
|
import org.jivesoftware.smack.debugger.SmackDebugger;
|
|
|
|
import org.jivesoftware.smack.filter.PacketFilter;
|
2005-09-05 22:00:45 +02:00
|
|
|
import org.jivesoftware.smack.filter.PacketTypeFilter;
|
2005-09-06 00:06:40 +02:00
|
|
|
import org.jivesoftware.smack.packet.Message;
|
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
|
|
|
import org.jivesoftware.smack.packet.Presence;
|
|
|
|
import org.jivesoftware.smack.packet.XMPPError;
|
2005-08-27 04:33:08 +02:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2005-09-18 00:01:24 +02:00
|
|
|
import org.jivesoftware.smack.util.DNSUtil;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2004-07-06 00:22:02 +02:00
|
|
|
import javax.net.SocketFactory;
|
2005-08-27 04:33:08 +02:00
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
import javax.net.ssl.SSLSocket;
|
2003-01-13 17:58:47 +01:00
|
|
|
import java.io.*;
|
2005-09-05 22:00:45 +02:00
|
|
|
import java.lang.ref.WeakReference;
|
2005-09-06 00:06:40 +02:00
|
|
|
import java.lang.reflect.Constructor;
|
2005-08-27 04:33:08 +02:00
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.UnknownHostException;
|
2005-10-24 21:37:37 +02:00
|
|
|
import java.util.*;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
/**
|
2003-04-18 06:38:27 +02:00
|
|
|
* Creates a connection to a XMPP server. A simple use of this API might
|
2003-01-13 17:58:47 +01:00
|
|
|
* look like the following:
|
|
|
|
* <pre>
|
|
|
|
* // Create a connection to the jivesoftware.com XMPP server.
|
|
|
|
* XMPPConnection con = new XMPPConnection("jivesoftware.com");
|
|
|
|
* // Most servers require you to login before performing other tasks.
|
|
|
|
* con.login("jsmith", "mypass");
|
|
|
|
* // Start a new conversation with John Doe and send him a message.
|
2003-03-09 00:09:48 +01:00
|
|
|
* Chat chat = con.createChat("jdoe@jabber.org");
|
2003-01-13 17:58:47 +01:00
|
|
|
* chat.sendMessage("Hey, how's it going?");
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
|
|
|
public class XMPPConnection {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Value that indicates whether debugging is enabled. When enabled, a debug
|
|
|
|
* window will apear for each new connection that will contain the following
|
|
|
|
* information:<ul>
|
|
|
|
* <li> Client Traffic -- raw XML traffic generated by Smack and sent to the server.
|
|
|
|
* <li> Server Traffic -- raw XML traffic sent by the server to the client.
|
|
|
|
* <li> Interpreted Packets -- shows XML packets from the server as parsed by Smack.
|
|
|
|
* </ul>
|
|
|
|
*
|
|
|
|
* Debugging can be enabled by setting this field to true, or by setting the Java system
|
|
|
|
* property <tt>smack.debugEnabled</tt> to true. The system property can be set on the
|
|
|
|
* command line such as "java SomeApp -Dsmack.debugEnabled=true".
|
|
|
|
*/
|
2003-04-29 07:32:34 +02:00
|
|
|
public static boolean DEBUG_ENABLED = false;
|
2003-12-19 19:30:58 +01:00
|
|
|
|
|
|
|
private static List connectionEstablishedListeners = new ArrayList();
|
2004-04-18 20:11:26 +02:00
|
|
|
|
2003-04-29 07:32:34 +02:00
|
|
|
static {
|
2003-05-05 07:02:31 +02:00
|
|
|
// Use try block since we may not have permission to get a system
|
|
|
|
// property (for example, when an applet).
|
2003-04-29 07:32:34 +02:00
|
|
|
try {
|
|
|
|
DEBUG_ENABLED = Boolean.getBoolean("smack.debugEnabled");
|
|
|
|
}
|
2003-11-15 21:23:20 +01:00
|
|
|
catch (Exception e) {
|
2005-09-05 22:00:45 +02:00
|
|
|
// Ignore.
|
2003-11-15 21:23:20 +01:00
|
|
|
}
|
2004-04-18 20:11:26 +02:00
|
|
|
// Ensure the SmackConfiguration class is loaded by calling a method in it.
|
|
|
|
SmackConfiguration.getVersion();
|
2003-04-29 07:32:34 +02:00
|
|
|
}
|
2003-11-15 21:23:20 +01:00
|
|
|
private SmackDebugger debugger = null;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2005-08-27 04:33:08 +02:00
|
|
|
/**
|
|
|
|
* IP address or host name of the server. This information is only used when
|
|
|
|
* creating new socket connections to the server. If this information is not
|
|
|
|
* configured then it will be assumed that the host name matches the service name.
|
|
|
|
*/
|
2003-09-18 23:37:52 +02:00
|
|
|
String host;
|
|
|
|
int port;
|
|
|
|
Socket socket;
|
2005-09-05 22:00:45 +02:00
|
|
|
|
2005-08-27 04:33:08 +02:00
|
|
|
/**
|
|
|
|
* Hostname of the XMPP server. Usually servers use the same service name as the name
|
|
|
|
* of the server. However, there are some servers like google where host would be
|
|
|
|
* talk.google.com and the serviceName would be gmail.com.
|
|
|
|
*/
|
|
|
|
String serviceName;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
String connectionID;
|
2003-08-12 21:30:51 +02:00
|
|
|
private String user = null;
|
2003-01-13 17:58:47 +01:00
|
|
|
private boolean connected = false;
|
2003-03-09 00:09:48 +01:00
|
|
|
private boolean authenticated = false;
|
2003-08-12 21:30:51 +02:00
|
|
|
private boolean anonymous = false;
|
2005-08-27 04:33:08 +02:00
|
|
|
private boolean usingTLS = false;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2003-09-19 00:35:42 +02:00
|
|
|
PacketWriter packetWriter;
|
|
|
|
PacketReader packetReader;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2003-04-07 07:40:28 +02:00
|
|
|
Roster roster = null;
|
2003-03-09 00:09:48 +01:00
|
|
|
private AccountManager accountManager = null;
|
2005-08-27 04:33:08 +02:00
|
|
|
private SASLAuthentication saslAuthentication = new SASLAuthentication(this);
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
Writer writer;
|
|
|
|
Reader reader;
|
|
|
|
|
2005-09-05 22:00:45 +02:00
|
|
|
/**
|
|
|
|
* A map between JIDs and the most recently created Chat object with that JID.
|
|
|
|
* Reference to the Chat is stored via a WeakReference so that the map
|
|
|
|
* does not interfere with garbage collection. The map of chats must be stored
|
|
|
|
* with each connection.
|
|
|
|
*/
|
2005-10-24 21:37:37 +02:00
|
|
|
Map chats = Collections.synchronizedMap(new HashMap());
|
2005-09-05 22:00:45 +02:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
2005-09-18 00:01:24 +02:00
|
|
|
* Creates a new connection to the specified XMPP server. A DNS SRV lookup will be
|
|
|
|
* performed to try to determine the IP address and port corresponding to the
|
|
|
|
* serviceName; if that lookup fails, it's assumed that server resides at serviceName
|
|
|
|
* with the default port of 5222. This is the preferred constructor for connecting
|
|
|
|
* to an XMPP server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2005-08-27 04:33:08 +02:00
|
|
|
* @param serviceName the name of the XMPP server to connect to; e.g. <tt>jivesoftware.com</tt>.
|
2003-08-19 18:32:13 +02:00
|
|
|
* @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.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2005-08-27 04:33:08 +02:00
|
|
|
public XMPPConnection(String serviceName) throws XMPPException {
|
2005-09-18 00:01:24 +02:00
|
|
|
DNSUtil.HostAddress address = DNSUtil.resolveXMPPDomain(serviceName);
|
|
|
|
|
|
|
|
this.host = address.getHost();
|
|
|
|
this.port = address.getPort();
|
|
|
|
try {
|
|
|
|
this.socket = new Socket(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);
|
|
|
|
}
|
|
|
|
this.serviceName = serviceName;
|
|
|
|
init();
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-11-19 19:13:50 +01:00
|
|
|
* Creates a new connection to the XMPP server at the specifiec host and port.
|
|
|
|
*
|
2003-04-18 06:38:27 +02:00
|
|
|
* @param host the name of the XMPP server to connect to; e.g. <tt>jivesoftware.com</tt>.
|
2003-01-13 17:58:47 +01:00
|
|
|
* @param port the port on the server that should be used; e.g. <tt>5222</tt>.
|
2003-08-19 18:32:13 +02:00
|
|
|
* @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.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public XMPPConnection(String host, int port) throws XMPPException {
|
2005-09-18 00:01:24 +02:00
|
|
|
this.host = host;
|
|
|
|
this.port = port;
|
|
|
|
try {
|
|
|
|
this.socket = new Socket(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);
|
|
|
|
}
|
|
|
|
this.serviceName = host;
|
|
|
|
init();
|
2005-08-27 04:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new connection to the specified XMPP server on the given host and port.
|
|
|
|
*
|
|
|
|
* @param host the host name, or null for the loopback address.
|
|
|
|
* @param port the port on the server that should be used; e.g. <tt>5222</tt>.
|
|
|
|
* @param serviceName the name of the XMPP server to connect to; e.g. <tt>jivesoftware.com</tt>.
|
|
|
|
* @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, String serviceName) throws XMPPException {
|
2003-01-13 17:58:47 +01:00
|
|
|
this.host = host;
|
|
|
|
this.port = port;
|
|
|
|
try {
|
|
|
|
this.socket = new Socket(host, port);
|
|
|
|
}
|
|
|
|
catch (UnknownHostException uhe) {
|
2003-11-15 21:23:20 +01:00
|
|
|
throw new XMPPException(
|
|
|
|
"Could not connect to " + host + ":" + port + ".",
|
|
|
|
new XMPPError(504),
|
|
|
|
uhe);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
catch (IOException ioe) {
|
2003-11-15 21:23:20 +01:00
|
|
|
throw new XMPPException(
|
|
|
|
"XMPPError connecting to " + host + ":" + port + ".",
|
|
|
|
new XMPPError(502),
|
|
|
|
ioe);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2005-08-27 04:33:08 +02:00
|
|
|
this.serviceName = serviceName;
|
2003-01-13 17:58:47 +01:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2004-07-06 00:22:02 +02:00
|
|
|
/**
|
2005-07-18 23:28:25 +02:00
|
|
|
* Creates a new connection to the specified XMPP server on the given port using the
|
|
|
|
* specified SocketFactory.<p>
|
2004-07-06 00:22:02 +02:00
|
|
|
*
|
2005-07-18 23:28:25 +02:00
|
|
|
* 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.
|
2004-07-06 00:22:02 +02:00
|
|
|
*
|
2005-08-27 04:33:08 +02:00
|
|
|
* @param host the host name, or null for the loopback address.
|
2004-07-06 00:22:02 +02:00
|
|
|
* @param port the port on the server that should be used; e.g. <tt>5222</tt>.
|
2005-08-27 04:33:08 +02:00
|
|
|
* @param serviceName the name of the XMPP server to connect to; e.g. <tt>jivesoftware.com</tt>.
|
|
|
|
* @param socketFactory a SocketFactory that will be used to create the socket to the XMPP
|
|
|
|
* server.
|
2004-07-06 00:22:02 +02:00
|
|
|
* @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.
|
|
|
|
*/
|
2005-08-27 04:33:08 +02:00
|
|
|
public XMPPConnection(String host, int port, String serviceName, SocketFactory socketFactory)
|
2005-09-18 00:01:24 +02:00
|
|
|
throws XMPPException
|
|
|
|
{
|
2004-07-06 00:22:02 +02:00
|
|
|
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);
|
|
|
|
}
|
2005-08-27 04:33:08 +02:00
|
|
|
this.serviceName = serviceName;
|
2004-07-06 00:22:02 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2005-07-18 23:28:25 +02:00
|
|
|
/**
|
|
|
|
* Package-private default constructor. This constructor is only intended
|
|
|
|
* for unit testing. Normal classes extending XMPPConnection should override
|
|
|
|
* one of the other constructors.
|
|
|
|
*/
|
|
|
|
XMPPConnection() {
|
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Returns the connection ID for this connection, which is the value set by the server
|
2003-04-18 06:38:27 +02:00
|
|
|
* when opening a XMPP stream. If the server does not set a connection ID, this value
|
2003-01-13 17:58:47 +01:00
|
|
|
* will be null.
|
|
|
|
*
|
2003-04-18 06:38:27 +02:00
|
|
|
* @return the ID of this connection returned from the XMPP server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public String getConnectionID() {
|
|
|
|
return connectionID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-08-27 04:33:08 +02:00
|
|
|
* Returns the name of the service provided by the XMPP server for this connection. After
|
|
|
|
* authenticating with the server the returned value may be different.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2005-08-27 04:33:08 +02:00
|
|
|
* @return the name of the service provided by the XMPP server.
|
|
|
|
*/
|
|
|
|
public String getServiceName() {
|
|
|
|
return serviceName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the host name of the server where the XMPP server is running. This would be the
|
|
|
|
* IP address of the server or a name that may be resolved by a DNS server.
|
|
|
|
*
|
|
|
|
* @return the host name of the server where the XMPP server is running.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public String getHost() {
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the port number of the XMPP server for this connection. The default port
|
|
|
|
* for normal connections is 5222. The default port for SSL connections is 5223.
|
|
|
|
*
|
2003-04-18 06:38:27 +02:00
|
|
|
* @return the port number of the XMPP server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public int getPort() {
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2003-03-10 00:06:59 +01:00
|
|
|
/**
|
2003-08-12 21:30:51 +02:00
|
|
|
* Returns the full XMPP address of the user that is logged in to the connection or
|
|
|
|
* <tt>null</tt> if not logged in yet. An XMPP address is in the form
|
|
|
|
* username@server/resource.
|
2003-03-10 00:06:59 +01:00
|
|
|
*
|
2003-08-12 21:30:51 +02:00
|
|
|
* @return the full XMPP address of the user logged in.
|
2003-03-10 00:06:59 +01:00
|
|
|
*/
|
2003-08-12 21:30:51 +02:00
|
|
|
public String getUser() {
|
2003-03-10 00:06:59 +01:00
|
|
|
if (!isAuthenticated()) {
|
|
|
|
return null;
|
|
|
|
}
|
2003-08-12 21:30:51 +02:00
|
|
|
return user;
|
2003-03-10 00:06:59 +01:00
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
2003-01-17 08:11:33 +01:00
|
|
|
* Logs in to the server using the strongest authentication mode supported by
|
2003-01-15 15:54:11 +01:00
|
|
|
* the server, then set our presence to available. If more than five seconds
|
2005-06-02 06:52:40 +02:00
|
|
|
* (default timeout) elapses in each step of the authentication process without
|
|
|
|
* a response from the server, or if an error occurs, a XMPPException will be thrown.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @param username the username.
|
|
|
|
* @param password the password.
|
|
|
|
* @throws XMPPException if an error occurs.
|
|
|
|
*/
|
2003-01-16 02:01:27 +01:00
|
|
|
public void login(String username, String password) throws XMPPException {
|
|
|
|
login(username, password, "Smack");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-01-17 08:11:33 +01:00
|
|
|
* Logs in to the server using the strongest authentication mode supported by
|
2004-11-15 09:08:23 +01:00
|
|
|
* the server, then sets presence to available. If more than five seconds
|
2005-06-02 06:52:40 +02:00
|
|
|
* (default timeout) elapses in each step of the authentication process without
|
|
|
|
* a response from the server, or if an error occurs, a XMPPException will be thrown.
|
2003-01-16 02:01:27 +01:00
|
|
|
*
|
|
|
|
* @param username the username.
|
|
|
|
* @param password the password.
|
|
|
|
* @param resource the resource.
|
|
|
|
* @throws XMPPException if an error occurs.
|
2003-03-09 00:09:48 +01:00
|
|
|
* @throws IllegalStateException if not connected to the server, or already logged in
|
|
|
|
* to the serrver.
|
2003-01-16 02:01:27 +01:00
|
|
|
*/
|
|
|
|
public synchronized void login(String username, String password, String resource)
|
2005-06-02 06:52:40 +02:00
|
|
|
throws XMPPException
|
|
|
|
{
|
|
|
|
login(username, password, resource, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs in to the server using the strongest authentication mode supported by
|
2005-09-06 00:06:40 +02:00
|
|
|
* the server. If the server supports SASL authentication then the user will be
|
|
|
|
* authenticated using SASL if not Non-SASL authentication will be tried. An available
|
|
|
|
* presence may optionally be sent. If <tt>sendPresence</tt>
|
2005-06-02 06:52:40 +02:00
|
|
|
* is false, a presence packet must be sent manually later. If more than five seconds
|
|
|
|
* (default timeout) elapses in each step of the authentication process without a
|
|
|
|
* response from the server, or if an error occurs, a XMPPException will be thrown.
|
|
|
|
*
|
|
|
|
* @param username the username.
|
|
|
|
* @param password the password.
|
|
|
|
* @param resource the resource.
|
|
|
|
* @param sendPresence if <tt>true</tt> an available presence will be sent automatically
|
|
|
|
* after login is completed.
|
|
|
|
* @throws XMPPException if an error occurs.
|
|
|
|
* @throws IllegalStateException if not connected to the server, or already logged in
|
|
|
|
* to the serrver.
|
|
|
|
*/
|
|
|
|
public synchronized void login(String username, String password, String resource,
|
|
|
|
boolean sendPresence) throws XMPPException
|
|
|
|
{
|
2003-01-13 17:58:47 +01:00
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
2003-03-09 00:09:48 +01:00
|
|
|
if (authenticated) {
|
|
|
|
throw new IllegalStateException("Already logged in to server.");
|
|
|
|
}
|
2004-11-15 09:08:23 +01:00
|
|
|
// Do partial version of nameprep on the username.
|
|
|
|
username = username.toLowerCase().trim();
|
2003-01-16 02:01:27 +01:00
|
|
|
|
2005-08-27 04:33:08 +02:00
|
|
|
String response = null;
|
2005-09-06 00:06:40 +02:00
|
|
|
if (saslAuthentication.hasNonAnonymousAuthentication()) {
|
2005-08-27 04:33:08 +02:00
|
|
|
// Authenticate using SASL
|
|
|
|
response = saslAuthentication.authenticate(username, password, resource);
|
2003-01-15 15:54:11 +01:00
|
|
|
}
|
|
|
|
else {
|
2005-08-27 04:33:08 +02:00
|
|
|
// Authenticate using Non-SASL
|
|
|
|
response = new NonSASLAuthentication(this).authenticate(username, password, resource);
|
2003-01-15 15:54:11 +01:00
|
|
|
}
|
|
|
|
|
2003-08-12 21:30:51 +02:00
|
|
|
// Set the user.
|
2005-08-27 04:33:08 +02:00
|
|
|
if (response != null) {
|
|
|
|
this.user = response;
|
|
|
|
// Update the serviceName with the one returned by the server
|
|
|
|
this.serviceName = StringUtils.parseServer(response);
|
2003-08-12 21:30:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2005-08-27 04:33:08 +02:00
|
|
|
this.user = username + "@" + this.serviceName;
|
2003-08-20 22:31:47 +02:00
|
|
|
if (resource != null) {
|
|
|
|
this.user += "/" + resource;
|
2003-08-12 21:30:51 +02:00
|
|
|
}
|
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2004-01-18 13:58:36 +01:00
|
|
|
// Create the roster.
|
|
|
|
this.roster = new Roster(this);
|
2003-02-10 06:01:01 +01:00
|
|
|
roster.reload();
|
2003-03-07 20:50:24 +01:00
|
|
|
|
2003-07-27 01:07:43 +02:00
|
|
|
// Set presence to online.
|
2005-06-02 06:52:40 +02:00
|
|
|
if (sendPresence) {
|
|
|
|
packetWriter.sendPacket(new Presence(Presence.Type.AVAILABLE));
|
|
|
|
}
|
2003-07-27 01:07:43 +02:00
|
|
|
|
2003-03-09 00:09:48 +01:00
|
|
|
// Indicate that we're now authenticated.
|
|
|
|
authenticated = true;
|
2003-08-12 21:30:51 +02:00
|
|
|
anonymous = false;
|
2003-03-09 00:09:48 +01:00
|
|
|
|
2003-03-07 20:50:24 +01:00
|
|
|
// If debugging is enabled, change the the debug window title to include the
|
|
|
|
// name we are now logged-in as.
|
2005-09-06 00:06:40 +02:00
|
|
|
// If DEBUG_ENABLED was set to true AFTER the connection was created the debugger
|
2004-01-24 19:39:39 +01:00
|
|
|
// will be null
|
|
|
|
if (DEBUG_ENABLED && debugger != null) {
|
2003-11-15 21:23:20 +01:00
|
|
|
debugger.userHasLogged(user);
|
2003-03-07 20:50:24 +01:00
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
}
|
|
|
|
|
2003-08-12 21:30:51 +02:00
|
|
|
/**
|
|
|
|
* Logs in to the server anonymously. Very few servers are configured to support anonymous
|
|
|
|
* authentication, so it's fairly likely logging in anonymously will fail. If anonymous login
|
2005-08-27 04:33:08 +02:00
|
|
|
* does succeed, your XMPP address will likely be in the form "server/123ABC" (where "123ABC"
|
|
|
|
* is a random value generated by the server).
|
2003-08-12 21:30:51 +02:00
|
|
|
*
|
|
|
|
* @throws XMPPException if an error occurs or anonymous logins are not supported by the server.
|
|
|
|
* @throws IllegalStateException if not connected to the server, or already logged in
|
|
|
|
* to the serrver.
|
|
|
|
*/
|
|
|
|
public synchronized void loginAnonymously() throws XMPPException {
|
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
|
|
|
if (authenticated) {
|
|
|
|
throw new IllegalStateException("Already logged in to server.");
|
|
|
|
}
|
|
|
|
|
2005-09-06 00:06:40 +02:00
|
|
|
String response = null;
|
|
|
|
if (saslAuthentication.hasAnonymousAuthentication()) {
|
|
|
|
response = saslAuthentication.authenticateAnonymously();
|
2003-08-12 21:30:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2005-09-06 00:06:40 +02:00
|
|
|
// Authenticate using Non-SASL
|
|
|
|
response = new NonSASLAuthentication(this).authenticateAnonymously();
|
2003-08-12 21:30:51 +02:00
|
|
|
}
|
2005-09-06 00:06:40 +02:00
|
|
|
|
|
|
|
// Set the user value.
|
|
|
|
this.user = response;
|
|
|
|
// Update the serviceName with the one returned by the server
|
|
|
|
this.serviceName = StringUtils.parseServer(response);
|
2003-08-12 21:30:51 +02:00
|
|
|
|
|
|
|
// Anonymous users can't have a roster.
|
|
|
|
roster = null;
|
|
|
|
|
|
|
|
// Set presence to online.
|
|
|
|
packetWriter.sendPacket(new Presence(Presence.Type.AVAILABLE));
|
|
|
|
|
|
|
|
// Indicate that we're now authenticated.
|
|
|
|
authenticated = true;
|
|
|
|
anonymous = true;
|
|
|
|
|
|
|
|
// If debugging is enabled, change the the debug window title to include the
|
|
|
|
// name we are now logged-in as.
|
2005-09-06 00:06:40 +02:00
|
|
|
// If DEBUG_ENABLED was set to true AFTER the connection was created the debugger
|
2004-01-24 19:39:39 +01:00
|
|
|
// will be null
|
|
|
|
if (DEBUG_ENABLED && debugger != null) {
|
2003-11-15 21:23:20 +01:00
|
|
|
debugger.userHasLogged(user);
|
2003-08-12 21:30:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-09 00:09:48 +01:00
|
|
|
/**
|
|
|
|
* Returns the roster for the user logged into the server. If the user has not yet
|
2004-01-18 13:58:36 +01:00
|
|
|
* logged into the server (or if the user is logged in anonymously), this method will return
|
2003-08-12 21:30:51 +02:00
|
|
|
* <tt>null</tt>.
|
2003-03-09 00:09:48 +01:00
|
|
|
*
|
2004-01-18 13:58:36 +01:00
|
|
|
* @return the user's roster, or <tt>null</tt> if the user has not logged in yet.
|
2003-03-09 00:09:48 +01:00
|
|
|
*/
|
2003-02-10 06:01:01 +01:00
|
|
|
public Roster getRoster() {
|
2003-04-07 07:40:28 +02:00
|
|
|
if (roster == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2004-01-18 13:58:36 +01:00
|
|
|
// If this is the first time the user has asked for the roster after calling
|
2005-03-30 03:53:51 +02:00
|
|
|
// login, we want to wait for the server to send back the user's roster. This
|
|
|
|
// behavior shields API users from having to worry about the fact that roster
|
|
|
|
// operations are asynchronous, although they'll still have to listen for
|
|
|
|
// changes to the roster. Note: because of this waiting logic, internal
|
2004-01-18 13:58:36 +01:00
|
|
|
// Smack code should be wary about calling the getRoster method, and may need to
|
|
|
|
// access the roster object directly.
|
2005-03-30 03:53:51 +02:00
|
|
|
if (!roster.rosterInitialized) {
|
2004-01-18 13:58:36 +01:00
|
|
|
try {
|
2005-03-30 03:53:51 +02:00
|
|
|
synchronized (roster) {
|
|
|
|
long waitTime = SmackConfiguration.getPacketReplyTimeout();
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
while (!roster.rosterInitialized) {
|
|
|
|
if (waitTime <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
roster.wait(waitTime);
|
2005-06-16 01:04:52 +02:00
|
|
|
long now = System.currentTimeMillis();
|
|
|
|
waitTime -= now - start;
|
|
|
|
start = now;
|
2005-03-30 03:53:51 +02:00
|
|
|
}
|
|
|
|
}
|
2004-01-18 13:58:36 +01:00
|
|
|
}
|
2005-09-05 22:00:45 +02:00
|
|
|
catch (InterruptedException ie) {
|
|
|
|
// Ignore.
|
|
|
|
}
|
2004-01-18 13:58:36 +01:00
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
return roster;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2003-03-09 00:09:48 +01:00
|
|
|
/**
|
2003-03-10 00:06:59 +01:00
|
|
|
* Returns an account manager instance for this connection.
|
|
|
|
*
|
|
|
|
* @return an account manager for this connection.
|
2003-03-09 00:09:48 +01:00
|
|
|
*/
|
|
|
|
public synchronized AccountManager getAccountManager() {
|
|
|
|
if (accountManager == null) {
|
|
|
|
accountManager = new AccountManager(this);
|
|
|
|
}
|
|
|
|
return accountManager;
|
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Creates a new chat with the specified participant. The participant should
|
2003-04-18 06:38:27 +02:00
|
|
|
* be a valid XMPP user such as <tt>jdoe@jivesoftware.com</tt> or
|
2003-01-13 17:58:47 +01:00
|
|
|
* <tt>jdoe@jivesoftware.com/work</tt>.
|
|
|
|
*
|
|
|
|
* @param participant the person to start the conversation with.
|
|
|
|
* @return a new Chat object.
|
|
|
|
*/
|
|
|
|
public Chat createChat(String participant) {
|
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
|
|
|
return new Chat(this, participant);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new group chat connected to the specified room. The room name
|
2003-09-23 22:14:31 +02:00
|
|
|
* should be full address, such as <tt>room@chat.example.com</tt>.
|
|
|
|
* <p>
|
|
|
|
* Most XMPP servers use a sub-domain for the chat service (eg chat.example.com
|
|
|
|
* for the XMPP server example.com). You must ensure that the room address you're
|
|
|
|
* trying to connect to includes the proper chat sub-domain.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2003-09-23 22:14:31 +02:00
|
|
|
* @param room the fully qualifed name of the room.
|
2003-01-13 17:58:47 +01:00
|
|
|
* @return a new GroupChat object.
|
|
|
|
*/
|
|
|
|
public GroupChat createGroupChat(String room) {
|
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
|
|
|
return new GroupChat(this, room);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-03-09 00:09:48 +01:00
|
|
|
* Returns true if currently connected to the XMPP server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @return true if connected.
|
|
|
|
*/
|
|
|
|
public boolean isConnected() {
|
|
|
|
return connected;
|
|
|
|
}
|
|
|
|
|
2003-09-18 22:39:14 +02:00
|
|
|
/**
|
2005-09-06 00:06:40 +02:00
|
|
|
* Returns true if the connection is a secured one, such as an SSL connection or
|
|
|
|
* if TLS was negotiated successfully.
|
2003-09-18 22:39:14 +02:00
|
|
|
*
|
|
|
|
* @return true if a secure connection to the server.
|
|
|
|
*/
|
|
|
|
public boolean isSecureConnection() {
|
2005-09-06 00:06:40 +02:00
|
|
|
return isUsingTLS();
|
2003-09-18 22:39:14 +02:00
|
|
|
}
|
|
|
|
|
2003-03-09 00:09:48 +01:00
|
|
|
/**
|
|
|
|
* Returns true if currently authenticated by successfully calling the login method.
|
|
|
|
*
|
|
|
|
* @return true if authenticated.
|
|
|
|
*/
|
|
|
|
public boolean isAuthenticated() {
|
|
|
|
return authenticated;
|
|
|
|
}
|
|
|
|
|
2003-08-12 21:30:51 +02:00
|
|
|
/**
|
|
|
|
* Returns true if currently authenticated anonymously.
|
|
|
|
*
|
|
|
|
* @return true if authenticated anonymously.
|
|
|
|
*/
|
|
|
|
public boolean isAnonymous() {
|
|
|
|
return anonymous;
|
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
2003-01-17 08:11:33 +01:00
|
|
|
* Closes the connection by setting presence to unavailable then closing the stream to
|
|
|
|
* the XMPP server. Once a connection has been closed, it cannot be re-opened.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public void close() {
|
|
|
|
// Set presence to offline.
|
2003-01-16 16:41:57 +01:00
|
|
|
packetWriter.sendPacket(new Presence(Presence.Type.UNAVAILABLE));
|
2003-01-13 17:58:47 +01:00
|
|
|
packetReader.shutdown();
|
2003-07-16 04:49:04 +02:00
|
|
|
packetWriter.shutdown();
|
|
|
|
// Wait 150 ms for processes to clean-up, then shutdown.
|
2003-01-27 16:56:21 +01:00
|
|
|
try {
|
2003-07-16 04:49:04 +02:00
|
|
|
Thread.sleep(150);
|
2003-01-27 16:56:21 +01:00
|
|
|
}
|
2003-11-15 21:23:20 +01:00
|
|
|
catch (Exception e) {
|
2005-09-05 22:00:45 +02:00
|
|
|
// Ignore.
|
2003-11-15 21:23:20 +01:00
|
|
|
}
|
2004-09-24 04:15:55 +02:00
|
|
|
|
2005-09-05 22:00:45 +02:00
|
|
|
// Close down the readers and writers.
|
|
|
|
if (reader != null)
|
|
|
|
{
|
|
|
|
try { reader.close(); } catch (Throwable ignore) { /* ignore */ }
|
|
|
|
reader = null;
|
|
|
|
}
|
|
|
|
if (writer != null)
|
|
|
|
{
|
|
|
|
try { writer.close(); } catch (Throwable ignore) { /* ignore */ }
|
|
|
|
writer = null;
|
|
|
|
}
|
2004-09-24 04:15:55 +02:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
try {
|
|
|
|
socket.close();
|
|
|
|
}
|
2003-11-15 21:23:20 +01:00
|
|
|
catch (Exception e) {
|
2005-09-05 22:00:45 +02:00
|
|
|
// Ignore.
|
2003-11-15 21:23:20 +01:00
|
|
|
}
|
2003-03-09 00:09:48 +01:00
|
|
|
authenticated = false;
|
2003-01-13 17:58:47 +01:00
|
|
|
connected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-01-17 08:11:33 +01:00
|
|
|
* Sends the specified packet to the server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2003-01-17 08:11:33 +01:00
|
|
|
* @param packet the packet to send.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2003-01-17 08:11:33 +01:00
|
|
|
public void sendPacket(Packet packet) {
|
2003-01-13 17:58:47 +01:00
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
2003-10-20 14:46:48 +02:00
|
|
|
if (packet == null) {
|
|
|
|
throw new NullPointerException("Packet is null.");
|
|
|
|
}
|
2003-01-17 08:11:33 +01:00
|
|
|
packetWriter.sendPacket(packet);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-01-17 08:11:33 +01:00
|
|
|
* Registers a packet listener with this connection. A packet filter determines
|
|
|
|
* which packets will be delivered to the listener.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2003-01-17 08:11:33 +01:00
|
|
|
* @param packetListener the packet listener to notify of new packets.
|
|
|
|
* @param packetFilter the packet filter to use.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2003-01-17 08:11:33 +01:00
|
|
|
public void addPacketListener(PacketListener packetListener, PacketFilter packetFilter) {
|
2003-01-13 17:58:47 +01:00
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
2003-01-17 08:11:33 +01:00
|
|
|
packetReader.addPacketListener(packetListener, packetFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a packet listener from this connection.
|
|
|
|
*
|
|
|
|
* @param packetListener the packet listener to remove.
|
|
|
|
*/
|
|
|
|
public void removePacketListener(PacketListener packetListener) {
|
|
|
|
packetReader.removePacketListener(packetListener);
|
|
|
|
}
|
|
|
|
|
2003-11-15 21:23:20 +01:00
|
|
|
/**
|
2003-11-18 18:06:47 +01:00
|
|
|
* Registers a packet listener with this connection. The listener will be
|
|
|
|
* notified of every packet that this connection sends. A packet filter determines
|
|
|
|
* which packets will be delivered to the listener.
|
2003-11-15 21:23:20 +01:00
|
|
|
*
|
2003-11-18 18:06:47 +01:00
|
|
|
* @param packetListener the packet listener to notify of sent packets.
|
|
|
|
* @param packetFilter the packet filter to use.
|
2003-11-15 21:23:20 +01:00
|
|
|
*/
|
2003-11-18 18:06:47 +01:00
|
|
|
public void addPacketWriterListener(PacketListener packetListener, PacketFilter packetFilter) {
|
2003-11-15 21:23:20 +01:00
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
2003-11-18 18:06:47 +01:00
|
|
|
packetWriter.addPacketListener(packetListener, packetFilter);
|
2003-11-15 21:23:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-11-18 18:06:47 +01:00
|
|
|
* Removes a packet listener from this connection.
|
2003-11-15 21:23:20 +01:00
|
|
|
*
|
2003-11-18 18:06:47 +01:00
|
|
|
* @param packetListener the packet listener to remove.
|
2003-11-15 21:23:20 +01:00
|
|
|
*/
|
2003-11-18 18:06:47 +01:00
|
|
|
public void removePacketWriterListener(PacketListener packetListener) {
|
|
|
|
packetWriter.removePacketListener(packetListener);
|
2003-11-15 21:23:20 +01:00
|
|
|
}
|
|
|
|
|
2005-11-02 00:49:07 +01:00
|
|
|
/**
|
|
|
|
* Registers a packet interceptor with this connection. The interceptor will be
|
|
|
|
* invoked every time a packet is about to be sent by this connection. Interceptors
|
|
|
|
* may modify the packet to be sent. A packet filter determines which packets
|
|
|
|
* will be delivered to the interceptor.
|
|
|
|
*
|
|
|
|
* @param packetInterceptor the packet interceptor to notify of packets about to be sent.
|
|
|
|
* @param packetFilter the packet filter to use.
|
|
|
|
*/
|
|
|
|
public void addPacketWriterInterceptor(PacketInterceptor packetInterceptor,
|
|
|
|
PacketFilter packetFilter) {
|
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
|
|
|
packetWriter.addPacketInterceptor(packetInterceptor, packetFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a packet interceptor.
|
|
|
|
*
|
|
|
|
* @param packetInterceptor the packet interceptor to remove.
|
|
|
|
*/
|
|
|
|
public void removePacketWriterInterceptor(PacketInterceptor packetInterceptor) {
|
|
|
|
packetWriter.removePacketInterceptor(packetInterceptor);
|
|
|
|
}
|
|
|
|
|
2003-01-17 08:11:33 +01:00
|
|
|
/**
|
|
|
|
* Creates a new packet collector for this connection. A packet filter determines
|
|
|
|
* which packets will be accumulated by the collector.
|
|
|
|
*
|
2003-01-17 19:26:52 +01:00
|
|
|
* @param packetFilter the packet filter to use.
|
|
|
|
* @return a new packet collector.
|
2003-01-17 08:11:33 +01:00
|
|
|
*/
|
|
|
|
public PacketCollector createPacketCollector(PacketFilter packetFilter) {
|
|
|
|
return packetReader.createPacketCollector(packetFilter);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2003-07-09 18:05:43 +02:00
|
|
|
/**
|
|
|
|
* Adds a connection listener to this connection that will be notified when
|
|
|
|
* the connection closes or fails.
|
|
|
|
*
|
|
|
|
* @param connectionListener a connection listener.
|
|
|
|
*/
|
|
|
|
public void addConnectionListener(ConnectionListener connectionListener) {
|
|
|
|
if (connectionListener == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
synchronized (packetReader.connectionListeners) {
|
|
|
|
if (!packetReader.connectionListeners.contains(connectionListener)) {
|
|
|
|
packetReader.connectionListeners.add(connectionListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a connection listener from this connection.
|
|
|
|
*
|
|
|
|
* @param connectionListener a connection listener.
|
|
|
|
*/
|
|
|
|
public void removeConnectionListener(ConnectionListener connectionListener) {
|
|
|
|
synchronized (packetReader.connectionListeners) {
|
|
|
|
packetReader.connectionListeners.remove(connectionListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-19 18:54:38 +01:00
|
|
|
/**
|
2005-09-06 00:06:40 +02:00
|
|
|
* Adds a connection established listener that will be notified when a new connection
|
2003-12-19 18:54:38 +01:00
|
|
|
* is established.
|
|
|
|
*
|
|
|
|
* @param connectionEstablishedListener a listener interested on connection established events.
|
|
|
|
*/
|
|
|
|
public static void addConnectionListener(ConnectionEstablishedListener connectionEstablishedListener) {
|
|
|
|
synchronized (connectionEstablishedListeners) {
|
|
|
|
if (!connectionEstablishedListeners.contains(connectionEstablishedListener)) {
|
|
|
|
connectionEstablishedListeners.add(connectionEstablishedListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a listener on new established connections.
|
|
|
|
*
|
|
|
|
* @param connectionEstablishedListener a listener interested on connection established events.
|
|
|
|
*/
|
|
|
|
public static void removeConnectionListener(ConnectionEstablishedListener connectionEstablishedListener) {
|
|
|
|
synchronized (connectionEstablishedListeners) {
|
|
|
|
connectionEstablishedListeners.remove(connectionEstablishedListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* Initializes the connection by creating a packet reader and writer and opening a
|
2003-04-18 06:38:27 +02:00
|
|
|
* XMPP stream to the server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
|
|
|
* @throws XMPPException if establishing a connection to the server fails.
|
|
|
|
*/
|
2004-07-06 00:22:02 +02:00
|
|
|
private void init() throws XMPPException {
|
2005-08-27 04:33:08 +02:00
|
|
|
// Set the reader and writer instance variables
|
|
|
|
initReaderAndWriter();
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2005-08-27 04:33:08 +02:00
|
|
|
try
|
|
|
|
{
|
2004-09-24 04:15:55 +02:00
|
|
|
packetWriter = new PacketWriter(this);
|
|
|
|
packetReader = new PacketReader(this);
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2004-09-24 04:15:55 +02:00
|
|
|
// If debugging is enabled, we should start the thread that will listen for
|
|
|
|
// all packets and then log them.
|
|
|
|
if (DEBUG_ENABLED) {
|
|
|
|
packetReader.addPacketListener(debugger.getReaderListener(), null);
|
|
|
|
if (debugger.getWriterListener() != null) {
|
|
|
|
packetWriter.addPacketListener(debugger.getWriterListener(), null);
|
|
|
|
}
|
2003-11-15 21:23:20 +01:00
|
|
|
}
|
2004-09-24 04:15:55 +02:00
|
|
|
// Start the packet writer. This will open a XMPP stream to the server
|
|
|
|
packetWriter.startup();
|
|
|
|
// Start the packet reader. The startup() method will block until we
|
|
|
|
// get an opening stream packet back from server.
|
|
|
|
packetReader.startup();
|
|
|
|
|
|
|
|
// Make note of the fact that we're now connected.
|
|
|
|
connected = true;
|
|
|
|
|
|
|
|
// Notify that a new connection has been established
|
|
|
|
connectionEstablished(this);
|
2005-09-05 22:00:45 +02:00
|
|
|
|
|
|
|
// Add a listener for all message packets so that we can deliver errant
|
|
|
|
// messages to the best Chat instance available.
|
|
|
|
addPacketListener(new PacketListener() {
|
|
|
|
public void processPacket(Packet packet) {
|
|
|
|
Message message = (Message)packet;
|
|
|
|
// Ignore any messages with a thread ID, as they will likely
|
|
|
|
// already be associated with a Chat. This will miss messages
|
|
|
|
// with new thread ID values, but we can only assume that a
|
|
|
|
// listener is registered to deal with this case.
|
|
|
|
if (message.getThread() == null) {
|
|
|
|
WeakReference chatRef = (WeakReference)chats.get(
|
|
|
|
StringUtils.parseBareAddress(message.getFrom()));
|
|
|
|
if (chatRef != null) {
|
|
|
|
// Do some extra clean-up if the reference was cleared.
|
|
|
|
Chat chat;
|
|
|
|
if ((chat = (Chat)chatRef.get()) == null) {
|
|
|
|
chats.remove(message.getFrom());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
chat.deliver(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, new PacketTypeFilter(Message.class));
|
2004-09-24 04:15:55 +02:00
|
|
|
}
|
2005-09-05 22:00:45 +02:00
|
|
|
catch (XMPPException ex) {
|
2005-08-27 04:33:08 +02:00
|
|
|
// An exception occurred in setting up the connection. Make sure we shut down the
|
|
|
|
// readers and writers and close the socket.
|
|
|
|
|
|
|
|
if (packetWriter != null) {
|
2005-09-05 22:00:45 +02:00
|
|
|
try { packetWriter.shutdown(); } catch (Throwable ignore) { /* ignore */ }
|
2005-08-27 04:33:08 +02:00
|
|
|
packetWriter = null;
|
|
|
|
}
|
|
|
|
if (packetReader != null) {
|
2005-09-05 22:00:45 +02:00
|
|
|
try { packetReader.shutdown(); } catch (Throwable ignore) { /* ignore */ }
|
2005-08-27 04:33:08 +02:00
|
|
|
packetReader = null;
|
|
|
|
}
|
|
|
|
if (reader != null) {
|
2005-09-05 22:00:45 +02:00
|
|
|
try { reader.close(); } catch (Throwable ignore) { /* ignore */ }
|
2005-08-27 04:33:08 +02:00
|
|
|
reader = null;
|
|
|
|
}
|
|
|
|
if (writer != null) {
|
2005-09-05 22:00:45 +02:00
|
|
|
try { writer.close(); } catch (Throwable ignore) { /* ignore */}
|
2005-08-27 04:33:08 +02:00
|
|
|
writer = null;
|
|
|
|
}
|
|
|
|
if (socket != null) {
|
2005-09-05 22:00:45 +02:00
|
|
|
try { socket.close(); } catch (Exception e) { /* ignore */ }
|
2005-08-27 04:33:08 +02:00
|
|
|
socket = null;
|
|
|
|
}
|
|
|
|
authenticated = false;
|
|
|
|
connected = false;
|
|
|
|
|
|
|
|
throw ex; // Everything stoppped. Now throw the exception.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initReaderAndWriter() throws XMPPException {
|
|
|
|
try {
|
|
|
|
reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
|
|
|
|
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
|
|
|
|
}
|
|
|
|
catch (IOException ioe) {
|
|
|
|
throw new XMPPException(
|
|
|
|
"XMPPError establishing connection with server.",
|
|
|
|
new XMPPError(502),
|
|
|
|
ioe);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If debugging is enabled, we open a window and write out all network traffic.
|
|
|
|
if (DEBUG_ENABLED) {
|
|
|
|
if (debugger == null) {
|
|
|
|
// Detect the debugger class to use.
|
|
|
|
String className = null;
|
|
|
|
// Use try block since we may not have permission to get a system
|
|
|
|
// property (for example, when an applet).
|
|
|
|
try {
|
|
|
|
className = System.getProperty("smack.debuggerClass");
|
|
|
|
}
|
|
|
|
catch (Throwable t) {
|
|
|
|
}
|
|
|
|
Class debuggerClass = null;
|
|
|
|
if (className != null) {
|
|
|
|
try {
|
|
|
|
debuggerClass = Class.forName(className);
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (debuggerClass == null) {
|
|
|
|
try {
|
|
|
|
debuggerClass =
|
|
|
|
Class.forName("org.jivesoftware.smackx.debugger.EnhancedDebugger");
|
|
|
|
}
|
|
|
|
catch (Exception ex) {
|
|
|
|
try {
|
|
|
|
debuggerClass = Class.forName("org.jivesoftware.smack.debugger.LiteDebugger");
|
|
|
|
}
|
|
|
|
catch (Exception ex2) {
|
|
|
|
ex2.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create a new debugger instance. If an exception occurs then disable the debugging
|
|
|
|
// option
|
|
|
|
try {
|
|
|
|
Constructor constructor =
|
|
|
|
debuggerClass.getConstructor(
|
|
|
|
new Class[] { XMPPConnection.class, Writer.class, Reader.class });
|
|
|
|
debugger = (SmackDebugger) constructor
|
|
|
|
.newInstance(new Object[]{this, writer, reader});
|
|
|
|
reader = debugger.getReader();
|
|
|
|
writer = debugger.getWriter();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
DEBUG_ENABLED = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Obtain new reader and writer from the existing debugger
|
|
|
|
reader = debugger.newConnectionReader(reader);
|
|
|
|
writer = debugger.newConnectionWriter(writer);
|
|
|
|
}
|
|
|
|
}
|
2003-12-19 18:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fires listeners on connection established events.
|
|
|
|
*/
|
|
|
|
private static void connectionEstablished(XMPPConnection connection) {
|
|
|
|
ConnectionEstablishedListener[] listeners = null;
|
|
|
|
synchronized (connectionEstablishedListeners) {
|
|
|
|
listeners = new ConnectionEstablishedListener[connectionEstablishedListeners.size()];
|
|
|
|
connectionEstablishedListeners.toArray(listeners);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < listeners.length; i++) {
|
|
|
|
listeners[i].connectionEstablished(connection);
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2005-08-27 04:33:08 +02:00
|
|
|
|
|
|
|
/***********************************************
|
|
|
|
* TLS code below
|
|
|
|
**********************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the connection to the server has successfully negotiated TLS. Once TLS
|
|
|
|
* has been negotiatied the connection has been secured.
|
|
|
|
*
|
|
|
|
* @return true if the connection to the server has successfully negotiated TLS.
|
|
|
|
*/
|
|
|
|
public boolean isUsingTLS() {
|
|
|
|
return usingTLS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the SASLAuthentication manager that is responsible for authenticating with
|
|
|
|
* the server.
|
|
|
|
*
|
|
|
|
* @return the SASLAuthentication manager that is responsible for authenticating with
|
|
|
|
* the server.
|
|
|
|
*/
|
|
|
|
public SASLAuthentication getSASLAuthentication() {
|
|
|
|
return saslAuthentication;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification message saying that the server supports TLS so confirm the server that we
|
|
|
|
* want to secure the connection.
|
|
|
|
*/
|
|
|
|
void startTLSReceived() {
|
|
|
|
try {
|
|
|
|
writer.write("<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>");
|
|
|
|
writer.flush();
|
2005-09-05 22:00:45 +02:00
|
|
|
}
|
|
|
|
catch (IOException e) {
|
2005-08-27 04:33:08 +02:00
|
|
|
packetReader.notifyConnectionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The server has indicated that TLS negotiation can start. We now need to secure the
|
|
|
|
* existing plain connection and perform a handshake. This method won't return until the
|
|
|
|
* connection has finished the handshake or an error occured while securing the connection.
|
|
|
|
*/
|
2005-09-06 00:06:40 +02:00
|
|
|
void proceedTLSReceived() throws Exception {
|
|
|
|
SSLContext context = SSLContext.getInstance("TLS");
|
|
|
|
// Accept any certificate presented by the server
|
|
|
|
context.init(null, // KeyManager not required
|
|
|
|
new javax.net.ssl.TrustManager[]{new OpenTrustManager()},
|
|
|
|
new java.security.SecureRandom());
|
|
|
|
Socket plain = socket;
|
|
|
|
// Secure the plain connection
|
|
|
|
socket = context.getSocketFactory().createSocket(plain,
|
|
|
|
plain.getInetAddress().getHostName(), plain.getPort(), true);
|
|
|
|
socket.setSoTimeout(0);
|
|
|
|
socket.setKeepAlive(true);
|
|
|
|
// Initialize the reader and writer with the new secured version
|
|
|
|
initReaderAndWriter();
|
|
|
|
// Proceed to do the handshake
|
|
|
|
((SSLSocket) socket).startHandshake();
|
|
|
|
|
|
|
|
// Set that TLS was successful
|
|
|
|
usingTLS = true;
|
|
|
|
|
|
|
|
// Set the new writer to use
|
|
|
|
packetWriter.setWriter(writer);
|
|
|
|
// Send a new opening stream to the server
|
|
|
|
packetWriter.openStream();
|
2005-08-27 04:33:08 +02:00
|
|
|
}
|
2004-11-05 06:25:52 +01:00
|
|
|
}
|