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-09-18 00:01:24 +02:00
|
|
|
import org.jivesoftware.smack.util.DNSUtil;
|
2005-12-08 22:07:01 +01:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
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;
|
2006-01-16 18:34:56 +01:00
|
|
|
import java.lang.reflect.Method;
|
2005-08-27 04:33:08 +02:00
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.UnknownHostException;
|
2006-07-18 07:14:33 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
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
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
private final static List<ConnectionEstablishedListener> connectionEstablishedListeners =
|
|
|
|
new ArrayList<ConnectionEstablishedListener>();
|
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.
|
|
|
|
*/
|
2006-07-18 07:14:33 +02:00
|
|
|
Map<String, WeakReference<Chat>> chats = new ConcurrentHashMap<String, WeakReference<Chat>>();
|
2005-09-05 22:00:45 +02:00
|
|
|
|
2006-01-16 18:34:56 +01:00
|
|
|
/**
|
|
|
|
* Collection of available stream compression methods offered by the server.
|
|
|
|
*/
|
|
|
|
private Collection compressionMethods;
|
|
|
|
/**
|
|
|
|
* Flag that indicates if stream compression is actually in use.
|
|
|
|
*/
|
|
|
|
private boolean usingCompression;
|
2006-01-17 21:05:31 +01:00
|
|
|
/**
|
|
|
|
* Holds the initial configuration used while creating the connection.
|
|
|
|
*/
|
|
|
|
private ConnectionConfiguration configuration;
|
2006-01-16 18:34:56 +01: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 {
|
2006-01-17 21:05:31 +01:00
|
|
|
// Perform DNS lookup to get host and port to use
|
2005-09-18 00:01:24 +02:00
|
|
|
DNSUtil.HostAddress address = DNSUtil.resolveXMPPDomain(serviceName);
|
2006-01-17 21:05:31 +01:00
|
|
|
// Create the configuration for this new connection
|
|
|
|
ConnectionConfiguration config =
|
|
|
|
new ConnectionConfiguration(address.getHost(), address.getPort(), serviceName);
|
|
|
|
config.setTLSEnabled(true);
|
|
|
|
config.setCompressionEnabled(false);
|
|
|
|
config.setSASLAuthenticationEnabled(true);
|
|
|
|
config.setDebuggerEnabled(DEBUG_ENABLED);
|
|
|
|
// Set the new connection configuration
|
|
|
|
connectUsingConfiguration(config, null);
|
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.
|
2006-07-18 08:47:38 +02:00
|
|
|
*
|
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 {
|
2006-01-17 21:05:31 +01:00
|
|
|
// Create the configuration for this new connection
|
|
|
|
ConnectionConfiguration config = new ConnectionConfiguration(host, port);
|
|
|
|
config.setTLSEnabled(true);
|
|
|
|
config.setCompressionEnabled(false);
|
|
|
|
config.setSASLAuthenticationEnabled(true);
|
|
|
|
config.setDebuggerEnabled(DEBUG_ENABLED);
|
|
|
|
// Set the new connection configuration
|
|
|
|
connectUsingConfiguration(config, null);
|
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 {
|
2006-01-17 21:05:31 +01:00
|
|
|
// Create the configuration for this new connection
|
|
|
|
ConnectionConfiguration config = new ConnectionConfiguration(host, port, serviceName);
|
|
|
|
config.setTLSEnabled(true);
|
|
|
|
config.setCompressionEnabled(false);
|
|
|
|
config.setSASLAuthenticationEnabled(true);
|
|
|
|
config.setDebuggerEnabled(DEBUG_ENABLED);
|
|
|
|
// Set the new connection configuration
|
|
|
|
connectUsingConfiguration(config, null);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2006-01-17 21:05:31 +01:00
|
|
|
// Create the configuration for this new connection
|
|
|
|
ConnectionConfiguration config = new ConnectionConfiguration(host, port, serviceName);
|
|
|
|
config.setTLSEnabled(true);
|
|
|
|
config.setCompressionEnabled(false);
|
|
|
|
config.setSASLAuthenticationEnabled(true);
|
|
|
|
config.setDebuggerEnabled(DEBUG_ENABLED);
|
|
|
|
// Set the new connection configuration
|
|
|
|
connectUsingConfiguration(config, socketFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
public XMPPConnection(ConnectionConfiguration config) throws XMPPException {
|
|
|
|
// Set the new connection configuration
|
|
|
|
connectUsingConfiguration(config, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public XMPPConnection(ConnectionConfiguration config, SocketFactory socketFactory)
|
|
|
|
throws XMPPException {
|
|
|
|
// Set the new connection configuration
|
|
|
|
connectUsingConfiguration(config, socketFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void connectUsingConfiguration(ConnectionConfiguration config,
|
|
|
|
SocketFactory socketFactory) throws XMPPException {
|
|
|
|
this.host = config.getHost();
|
|
|
|
this.port = config.getPort();
|
2004-07-06 00:22:02 +02:00
|
|
|
try {
|
2006-01-17 21:05:31 +01:00
|
|
|
if (socketFactory == null) {
|
|
|
|
this.socket = new Socket(host, port);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.socket = socketFactory.createSocket(host, port);
|
|
|
|
}
|
2004-07-06 00:22:02 +02:00
|
|
|
}
|
|
|
|
catch (UnknownHostException uhe) {
|
2006-07-19 21:24:00 +02:00
|
|
|
String errorMessage = "Could not connect to " + host + ":" + port + ".";
|
|
|
|
throw new XMPPException(errorMessage, new XMPPError(
|
|
|
|
XMPPError.Condition.remote_server_timeout, errorMessage),
|
|
|
|
uhe);
|
2004-07-06 00:22:02 +02:00
|
|
|
}
|
|
|
|
catch (IOException ioe) {
|
2006-07-19 21:24:00 +02:00
|
|
|
String errorMessage = "XMPPError connecting to " + host + ":"
|
|
|
|
+ port + ".";
|
|
|
|
throw new XMPPException(errorMessage, new XMPPError(
|
|
|
|
XMPPError.Condition.remote_server_error, errorMessage), ioe);
|
2004-07-06 00:22:02 +02:00
|
|
|
}
|
2006-01-17 21:05:31 +01:00
|
|
|
this.serviceName = config.getServiceName();
|
2006-01-17 22:28:46 +01:00
|
|
|
try {
|
|
|
|
// Keep a copy to be sure that once the configuration has been passed to the
|
|
|
|
// constructor it cannot be modified
|
|
|
|
this.configuration = (ConnectionConfiguration) config.clone();
|
2006-01-17 21:05:31 +01:00
|
|
|
}
|
2006-07-18 07:14:33 +02:00
|
|
|
catch (CloneNotSupportedException e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2006-01-17 22:28:46 +01:00
|
|
|
init();
|
2004-07-06 00:22:02 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
String response;
|
2006-01-17 21:05:31 +01:00
|
|
|
if (configuration.isSASLAuthenticationEnabled() &&
|
|
|
|
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
|
|
|
|
2006-01-17 22:28:46 +01:00
|
|
|
// If compression is enabled then request the server to use stream compression
|
|
|
|
if (configuration.isCompressionEnabled()) {
|
|
|
|
useCompression();
|
|
|
|
}
|
|
|
|
|
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) {
|
2006-07-17 10:39:08 +02:00
|
|
|
packetWriter.sendPacket(new Presence(Presence.Type.available));
|
2005-06-02 06:52:40 +02:00
|
|
|
}
|
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
|
2006-01-17 21:05:31 +01:00
|
|
|
if (configuration.isDebuggerEnabled() && 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.");
|
|
|
|
}
|
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
String response;
|
2006-01-17 21:05:31 +01:00
|
|
|
if (configuration.isSASLAuthenticationEnabled() &&
|
|
|
|
saslAuthentication.hasAnonymousAuthentication()) {
|
2005-09-06 00:06:40 +02:00
|
|
|
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
|
|
|
|
2006-01-17 22:28:46 +01:00
|
|
|
// If compression is enabled then request the server to use stream compression
|
|
|
|
if (configuration.isCompressionEnabled()) {
|
|
|
|
useCompression();
|
|
|
|
}
|
|
|
|
|
2003-08-12 21:30:51 +02:00
|
|
|
// Anonymous users can't have a roster.
|
|
|
|
roster = null;
|
|
|
|
|
|
|
|
// Set presence to online.
|
2006-07-17 10:39:08 +02:00
|
|
|
packetWriter.sendPacket(new Presence(Presence.Type.available));
|
2003-08-12 21:30:51 +02:00
|
|
|
|
|
|
|
// 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
|
2006-01-17 21:05:31 +01:00
|
|
|
if (configuration.isDebuggerEnabled() && 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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.
|
2006-07-17 10:39:08 +02: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
|
2006-02-09 00:11:21 +01:00
|
|
|
* which packets will be delivered to the listener. Note that the thread
|
|
|
|
* that writes packets will be used to invoke the listeners. Therefore, each
|
|
|
|
* packet listener should complete all operations quickly or use a different
|
|
|
|
* thread for processing.
|
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.
|
2006-01-17 21:05:31 +01:00
|
|
|
if (configuration.isDebuggerEnabled()) {
|
2004-09-24 04:15:55 +02:00
|
|
|
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;
|
|
|
|
|
2006-06-12 18:32:18 +02:00
|
|
|
// Start keep alive process (after TLS was negotiated - if available)
|
|
|
|
packetWriter.startKeepAliveProcess();
|
|
|
|
|
2004-09-24 04:15:55 +02:00
|
|
|
// 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.
|
2005-12-08 22:07:01 +01:00
|
|
|
if (message.getThread() == null &&
|
|
|
|
message.getType() != Message.Type.GROUP_CHAT &&
|
|
|
|
message.getType() != Message.Type.HEADLINE) {
|
2006-07-18 07:14:33 +02:00
|
|
|
WeakReference<Chat> chatRef =
|
|
|
|
chats.get(StringUtils.parseBareAddress(message.getFrom()));
|
2005-09-05 22:00:45 +02:00
|
|
|
if (chatRef != null) {
|
|
|
|
// Do some extra clean-up if the reference was cleared.
|
2006-07-18 07:14:33 +02:00
|
|
|
Chat chat = chatRef.get();
|
|
|
|
if (chat == null) {
|
2005-09-05 22:00:45 +02:00
|
|
|
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 {
|
2006-01-16 18:34:56 +01:00
|
|
|
if (!usingCompression) {
|
|
|
|
reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
|
|
|
|
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
2006-07-18 07:14:33 +02:00
|
|
|
Class<?> zoClass = Class.forName("com.jcraft.jzlib.ZOutputStream");
|
2006-01-16 18:34:56 +01:00
|
|
|
//ZOutputStream out = new ZOutputStream(socket.getOutputStream(), JZlib.Z_BEST_COMPRESSION);
|
2006-07-18 07:14:33 +02:00
|
|
|
Constructor<?> constructor =
|
2006-07-18 08:47:38 +02:00
|
|
|
zoClass.getConstructor(OutputStream.class, Integer.TYPE);
|
|
|
|
Object out = constructor.newInstance(socket.getOutputStream(), 9);
|
2006-01-16 18:34:56 +01:00
|
|
|
//out.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
|
2006-07-18 08:47:38 +02:00
|
|
|
Method method = zoClass.getMethod("setFlushMode", Integer.TYPE);
|
|
|
|
method.invoke(out, 1);
|
2006-01-16 18:34:56 +01:00
|
|
|
writer = new BufferedWriter(new OutputStreamWriter((OutputStream) out, "UTF-8"));
|
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
Class<?> ziClass = Class.forName("com.jcraft.jzlib.ZInputStream");
|
2006-01-16 18:34:56 +01:00
|
|
|
//ZInputStream in = new ZInputStream(socket.getInputStream());
|
2006-07-18 08:47:38 +02:00
|
|
|
constructor = ziClass.getConstructor(InputStream.class);
|
|
|
|
Object in = constructor.newInstance(socket.getInputStream());
|
2006-01-16 18:34:56 +01:00
|
|
|
//in.setFlushMode(JZlib.Z_PARTIAL_FLUSH);
|
2006-07-18 08:47:38 +02:00
|
|
|
method = ziClass.getMethod("setFlushMode", Integer.TYPE);
|
|
|
|
method.invoke(in, 1);
|
2006-01-16 18:34:56 +01:00
|
|
|
reader = new BufferedReader(new InputStreamReader((InputStream) in, "UTF-8"));
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
|
|
|
|
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
|
|
|
|
}
|
|
|
|
}
|
2005-08-27 04:33:08 +02:00
|
|
|
}
|
|
|
|
catch (IOException ioe) {
|
|
|
|
throw new XMPPException(
|
2006-07-19 21:24:00 +02:00
|
|
|
"XMPPError establishing connection with server.",
|
|
|
|
new XMPPError(XMPPError.Condition.remote_server_error,
|
|
|
|
"XMPPError establishing connection with server."),
|
|
|
|
ioe);
|
2005-08-27 04:33:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If debugging is enabled, we open a window and write out all network traffic.
|
2006-01-17 21:05:31 +01:00
|
|
|
if (configuration.isDebuggerEnabled()) {
|
2005-08-27 04:33:08 +02:00
|
|
|
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) {
|
2006-07-18 08:47:38 +02:00
|
|
|
// Ignore.
|
2005-08-27 04:33:08 +02:00
|
|
|
}
|
2006-07-18 07:14:33 +02:00
|
|
|
Class<?> debuggerClass = null;
|
2005-08-27 04:33:08 +02:00
|
|
|
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 {
|
2006-07-18 07:14:33 +02:00
|
|
|
Constructor<?> constructor =
|
2006-07-18 08:47:38 +02:00
|
|
|
debuggerClass.getConstructor(XMPPConnection.class, Writer.class, Reader.class);
|
|
|
|
debugger = (SmackDebugger) constructor.newInstance(this, writer, reader);
|
2005-08-27 04:33:08 +02:00
|
|
|
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) {
|
2006-07-18 07:14:33 +02:00
|
|
|
ConnectionEstablishedListener[] listeners;
|
2003-12-19 18:54:38 +01:00
|
|
|
synchronized (connectionEstablishedListeners) {
|
|
|
|
listeners = new ConnectionEstablishedListener[connectionEstablishedListeners.size()];
|
|
|
|
connectionEstablishedListeners.toArray(listeners);
|
|
|
|
}
|
2006-07-18 07:14:33 +02:00
|
|
|
for (ConnectionEstablishedListener listener : listeners) {
|
|
|
|
listener.connectionEstablished(connection);
|
2003-12-19 18:54:38 +01:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2006-08-03 00:17:11 +02:00
|
|
|
/**
|
|
|
|
* Returns the configuration used while connecting to the server.
|
|
|
|
*
|
|
|
|
* @return the configuration used while connecting to the server.
|
|
|
|
*/
|
|
|
|
ConnectionConfiguration getConfiguration() {
|
|
|
|
return configuration;
|
|
|
|
}
|
|
|
|
|
2005-08-27 04:33:08 +02:00
|
|
|
/**
|
|
|
|
* Notification message saying that the server supports TLS so confirm the server that we
|
|
|
|
* want to secure the connection.
|
|
|
|
*/
|
|
|
|
void startTLSReceived() {
|
2006-01-17 21:05:31 +01:00
|
|
|
if (!configuration.isTLSEnabled()) {
|
|
|
|
// Do not secure the connection using TLS since TLS was disabled
|
|
|
|
return;
|
|
|
|
}
|
2005-08-27 04:33:08 +02:00
|
|
|
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");
|
2006-01-17 21:05:31 +01:00
|
|
|
// Verify certificate presented by the server
|
2005-09-06 00:06:40 +02:00
|
|
|
context.init(null, // KeyManager not required
|
2006-01-17 21:05:31 +01:00
|
|
|
new javax.net.ssl.TrustManager[]{new ServerTrustManager(serviceName, configuration)},
|
2005-09-06 00:06:40 +02:00
|
|
|
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
|
|
|
}
|
2006-01-16 18:34:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the available stream compression methods offered by the server.
|
|
|
|
*
|
|
|
|
* @param methods compression methods offered by the server.
|
|
|
|
*/
|
|
|
|
void setAvailableCompressionMethods(Collection methods) {
|
|
|
|
compressionMethods = methods;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the specified compression method was offered by the server.
|
|
|
|
*
|
|
|
|
* @param method the method to check.
|
|
|
|
* @return true if the specified compression method was offered by the server.
|
|
|
|
*/
|
|
|
|
private boolean hasAvailableCompressionMethod(String method) {
|
|
|
|
return compressionMethods != null && compressionMethods.contains(method);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if network traffic is being compressed. When using stream compression network
|
|
|
|
* traffic can be reduced up to 90%. Therefore, stream compression is ideal when using a slow
|
|
|
|
* speed network connection. However, the server will need to use more CPU time in order to
|
|
|
|
* un/compress network data so under high load the server performance might be affected.<p>
|
|
|
|
*
|
2006-07-18 08:47:38 +02:00
|
|
|
* Note: to use stream compression the smackx.jar file has to be present in the classpath.
|
2006-01-16 18:34:56 +01:00
|
|
|
*
|
|
|
|
* @return true if network traffic is being compressed.
|
|
|
|
*/
|
|
|
|
public boolean isUsingCompression() {
|
|
|
|
return usingCompression;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts using stream compression that will compress network traffic. Traffic can be
|
|
|
|
* reduced up to 90%. Therefore, stream compression is ideal when using a slow speed network
|
|
|
|
* connection. However, the server and the client will need to use more CPU time in order to
|
|
|
|
* un/compress network data so under high load the server performance might be affected.<p>
|
|
|
|
*
|
|
|
|
* Stream compression has to have been previously offered by the server. Currently only the
|
|
|
|
* zlib method is supported by the client. Stream compression negotiation has to be done
|
|
|
|
* before authentication took place.<p>
|
|
|
|
*
|
2006-07-18 08:47:38 +02:00
|
|
|
* Note: to use stream compression the smackx.jar file has to be present in the classpath.
|
2006-01-16 18:34:56 +01:00
|
|
|
*
|
|
|
|
* @return true if stream compression negotiation was successful.
|
|
|
|
*/
|
2006-01-17 21:05:31 +01:00
|
|
|
private boolean useCompression() {
|
2006-01-16 18:34:56 +01:00
|
|
|
// If stream compression was offered by the server and we want to use
|
|
|
|
// compression then send compression request to the server
|
|
|
|
if (authenticated) {
|
|
|
|
throw new IllegalStateException("Compression should be negotiated before authentication.");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Class.forName("com.jcraft.jzlib.ZOutputStream");
|
|
|
|
}
|
|
|
|
catch (ClassNotFoundException e) {
|
|
|
|
throw new IllegalStateException("Cannot use compression. Add smackx.jar to the classpath");
|
|
|
|
}
|
|
|
|
if (hasAvailableCompressionMethod("zlib")) {
|
|
|
|
requestStreamCompression();
|
|
|
|
// Wait until compression is being used or a timeout happened
|
|
|
|
synchronized (this) {
|
|
|
|
try {
|
|
|
|
this.wait(SmackConfiguration.getPacketReplyTimeout() * 5);
|
|
|
|
}
|
2006-07-18 08:47:38 +02:00
|
|
|
catch (InterruptedException e) {
|
|
|
|
// Ignore.
|
|
|
|
}
|
2006-01-16 18:34:56 +01:00
|
|
|
}
|
|
|
|
return usingCompression;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request the server that we want to start using stream compression. When using TLS
|
|
|
|
* then negotiation of stream compression can only happen after TLS was negotiated. If TLS
|
|
|
|
* compression is being used the stream compression should not be used.
|
|
|
|
*/
|
|
|
|
private void requestStreamCompression() {
|
|
|
|
try {
|
|
|
|
writer.write("<compress xmlns='http://jabber.org/protocol/compress'>");
|
|
|
|
writer.write("<method>zlib</method></compress>");
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
packetReader.notifyConnectionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start using stream compression since the server has acknowledged stream compression.
|
|
|
|
*/
|
|
|
|
void startStreamCompression() throws Exception {
|
|
|
|
// Secure the plain connection
|
|
|
|
usingCompression = true;
|
|
|
|
// Initialize the reader and writer with the new secured version
|
|
|
|
initReaderAndWriter();
|
|
|
|
|
|
|
|
// Set the new writer to use
|
|
|
|
packetWriter.setWriter(writer);
|
|
|
|
// Send a new opening stream to the server
|
|
|
|
packetWriter.openStream();
|
|
|
|
// Notify that compression is being used
|
|
|
|
synchronized (this) {
|
|
|
|
this.notify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void streamCompressionDenied() {
|
|
|
|
// Notify that compression has been denied
|
|
|
|
synchronized (this) {
|
|
|
|
this.notify();
|
|
|
|
}
|
|
|
|
}
|
2004-11-05 06:25:52 +01:00
|
|
|
}
|