2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 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-06 00:06:40 +02:00
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
|
|
|
import org.jivesoftware.smack.packet.Presence;
|
|
|
|
import org.jivesoftware.smack.packet.XMPPError;
|
2005-12-08 22:07:01 +01:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
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-06 00:06:40 +02:00
|
|
|
import java.lang.reflect.Constructor;
|
2006-01-16 18:34:56 +01:00
|
|
|
import java.lang.reflect.Method;
|
2007-03-20 23:09:15 +01:00
|
|
|
import java.net.InetAddress;
|
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.Collection;
|
2006-09-16 00:42:06 +02:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.CopyOnWriteArraySet;
|
2007-02-04 21:52:45 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
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>
|
2007-01-05 00:00:58 +01:00
|
|
|
* // Create a connection to the igniterealtime.org XMPP server.
|
|
|
|
* XMPPConnection con = new XMPPConnection("igniterealtime.org");
|
2006-09-14 21:21:38 +02:00
|
|
|
* // Connect to the server
|
|
|
|
* con.connect();
|
2003-01-13 17:58:47 +01:00
|
|
|
* // 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.
|
2007-02-04 19:50:38 +01:00
|
|
|
* Chat chat = connection.getChatManager().createChat("jdoe@igniterealtime.org"</font>, new MessageListener() {
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2007-02-04 19:50:38 +01:00
|
|
|
* public void processMessage(Chat chat, Message message) {
|
|
|
|
* // Print out any messages we get back to standard out.
|
|
|
|
* System.out.println(<font color="green">"Received message: "</font> + message);
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
* chat.sendMessage(<font color="green">"Howdy!"</font>);
|
2006-09-14 21:21:38 +02:00
|
|
|
* // Disconnect from the server
|
|
|
|
* con.disconnect();
|
2003-01-13 17:58:47 +01:00
|
|
|
* </pre>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2006-09-14 21:21:38 +02:00
|
|
|
* XMPPConnections can be reused between connections. This means that an XMPPConnection
|
|
|
|
* may be connected, disconnected and then connected again. Listeners of the XMPPConnection
|
2006-09-15 23:51:08 +02:00
|
|
|
* will be retained accross connections.<p>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2006-09-14 21:21:38 +02:00
|
|
|
* If a connected XMPPConnection gets disconnected abruptly then it will try to reconnect
|
2006-09-15 23:51:08 +02:00
|
|
|
* again. To stop the reconnection process, use {@link #disconnect()}. Once stopped
|
2006-09-14 21:21:38 +02:00
|
|
|
* you can use {@link #connect()} to manually connect to the server.
|
|
|
|
*
|
2003-01-13 17:58:47 +01:00
|
|
|
* @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>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <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.
|
2003-01-13 17:58:47 +01:00
|
|
|
* </ul>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2003-01-13 17:58:47 +01:00
|
|
|
* 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-09-16 01:05:01 +02:00
|
|
|
private final static Set<ConnectionCreationListener> connectionEstablishedListeners =
|
|
|
|
new CopyOnWriteArraySet<ConnectionCreationListener>();
|
2004-04-18 20:11:26 +02:00
|
|
|
|
2007-02-04 21:52:45 +01:00
|
|
|
// Counter to uniquely identify connections that are created. This is distinct from the
|
|
|
|
// connection ID, which is a value sent by the server once a connection is made.
|
|
|
|
private static AtomicInteger connectionCounter = new AtomicInteger(0);
|
|
|
|
|
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
|
|
|
}
|
2007-03-20 23:09:15 +01: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
|
|
|
|
2007-02-04 21:52:45 +01:00
|
|
|
int connectionCounterValue = connectionCounter.getAndIncrement();
|
2007-01-05 00:00:58 +01:00
|
|
|
String connectionID = null;
|
2003-08-12 21:30:51 +02:00
|
|
|
private String user = null;
|
2003-01-13 17:58:47 +01:00
|
|
|
private boolean connected = false;
|
2006-09-14 21:21:38 +02:00
|
|
|
/**
|
|
|
|
* Flag that indicates if the user is currently authenticated with the server.
|
|
|
|
*/
|
2003-03-09 00:09:48 +01:00
|
|
|
private boolean authenticated = false;
|
2006-09-14 21:21:38 +02:00
|
|
|
/**
|
|
|
|
* Flag that indicates if the user was authenticated with the server when the connection
|
|
|
|
* to the server was closed (abruptly or not).
|
|
|
|
*/
|
|
|
|
private boolean wasAuthenticated = 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;
|
2007-02-19 23:24:07 +01: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;
|
|
|
|
|
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-11-22 23:55:37 +01:00
|
|
|
private ChatManager chatManager;
|
2006-09-16 00:42:06 +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
|
2007-01-05 00:00:58 +01:00
|
|
|
* performed to determine the IP address and port corresponding to the
|
|
|
|
* service name; if that lookup fails, it's assumed that server resides at
|
|
|
|
* <tt>serviceName</tt> with the default port of 5222. Encrypted connections (TLS)
|
|
|
|
* will be used if available, stream compression is disabled, and standard SASL
|
|
|
|
* mechanisms will be used for authentication.<p>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2007-01-05 00:00:58 +01:00
|
|
|
* This is the simplest constructor for connecting to an XMPP server. Alternatively,
|
|
|
|
* you can get fine-grained control over connection settings using the
|
|
|
|
* {@link #XMPPConnection(ConnectionConfiguration)} constructor.<p>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2007-01-05 00:00:58 +01:00
|
|
|
* Note that XMPPConnection constructors do not establish a connection to the server
|
|
|
|
* and you must call {@link #connect()}.
|
2006-09-14 21:21:38 +02:00
|
|
|
*
|
2007-01-05 00:00:58 +01:00
|
|
|
* @param serviceName the name of the XMPP server to connect to; e.g. <tt>example.com</tt>.
|
2005-08-27 04:33:08 +02:00
|
|
|
*/
|
2007-01-05 00:00:58 +01:00
|
|
|
public XMPPConnection(String serviceName) {
|
2006-01-17 21:05:31 +01:00
|
|
|
// Create the configuration for this new connection
|
2007-01-05 00:00:58 +01:00
|
|
|
ConnectionConfiguration config = new ConnectionConfiguration(serviceName);
|
2006-01-17 21:05:31 +01:00
|
|
|
config.setCompressionEnabled(false);
|
|
|
|
config.setSASLAuthenticationEnabled(true);
|
|
|
|
config.setDebuggerEnabled(DEBUG_ENABLED);
|
2007-01-05 00:00:58 +01:00
|
|
|
this.configuration = config;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2004-07-06 00:22:02 +02:00
|
|
|
/**
|
2007-01-05 00:00:58 +01:00
|
|
|
* Creates a new XMPP connection using the specified connection configuration.<p>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2007-01-05 00:00:58 +01:00
|
|
|
* Manually specifying connection configuration information is suitable for
|
|
|
|
* advanced users of the API. In many cases, using the
|
|
|
|
* {@link #XMPPConnection(String)} constructor is a better approach.<p>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2007-01-05 00:00:58 +01:00
|
|
|
* Note that XMPPConnection constructors do not establish a connection to the server
|
|
|
|
* and you must call {@link #connect()}.
|
2004-07-06 00:22:02 +02:00
|
|
|
*
|
2007-01-05 00:00:58 +01:00
|
|
|
* @param config the connection configuration.
|
2004-07-06 00:22:02 +02:00
|
|
|
*/
|
2006-09-14 21:21:38 +02:00
|
|
|
public XMPPConnection(ConnectionConfiguration config) {
|
2007-01-05 00:00:58 +01:00
|
|
|
this.configuration = config;
|
2005-07-18 23:28:25 +02:00
|
|
|
}
|
|
|
|
|
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
|
2007-01-05 00:00:58 +01:00
|
|
|
* will be null. This value will be <tt>null</tt> if not connected to the server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2007-01-05 00:00:58 +01:00
|
|
|
* @return the ID of this connection returned from the XMPP server or <tt>null</tt> if
|
2007-03-20 23:09:15 +01:00
|
|
|
* not connected to the server.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public String getConnectionID() {
|
2007-01-05 00:00:58 +01:00
|
|
|
if (!isConnected()) {
|
|
|
|
return null;
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
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
|
2007-02-12 01:56:47 +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-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.
|
2007-03-20 23:09:15 +01:00
|
|
|
* @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
|
2007-03-20 23:09:15 +01:00
|
|
|
* to the serrver.
|
2003-01-16 02:01:27 +01:00
|
|
|
*/
|
|
|
|
public synchronized void login(String username, String password, String resource)
|
2007-03-20 23:09:15 +01:00
|
|
|
throws XMPPException {
|
2005-06-02 06:52:40 +02:00
|
|
|
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
|
2006-09-14 21:21:38 +02:00
|
|
|
* response from the server, or if an error occurs, a XMPPException will be thrown.<p>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2006-09-14 21:21:38 +02:00
|
|
|
* Before logging in (i.e. authenticate) to the server the connection must be connected.
|
|
|
|
* For compatibility and easiness of use the connection will automatically connect to the
|
|
|
|
* server if not already connected.
|
2005-06-02 06:52:40 +02:00
|
|
|
*
|
2007-03-20 23:09:15 +01:00
|
|
|
* @param username the username.
|
|
|
|
* @param password the password.
|
|
|
|
* @param resource the resource.
|
2005-06-02 06:52:40 +02:00
|
|
|
* @param sendPresence if <tt>true</tt> an available presence will be sent automatically
|
2007-03-20 23:09:15 +01:00
|
|
|
* after login is completed.
|
|
|
|
* @throws XMPPException if an error occurs.
|
2005-06-02 06:52:40 +02:00
|
|
|
* @throws IllegalStateException if not connected to the server, or already logged in
|
2007-03-20 23:09:15 +01:00
|
|
|
* to the serrver.
|
2005-06-02 06:52:40 +02:00
|
|
|
*/
|
|
|
|
public synchronized void login(String username, String password, String resource,
|
2007-03-20 23:09:15 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2006-09-14 21:21:38 +02:00
|
|
|
// Create the roster if it is not a reconnection.
|
|
|
|
if (this.roster == null) {
|
|
|
|
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
|
|
|
}
|
2006-09-16 00:42:06 +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
|
|
|
|
2006-09-14 21:21:38 +02:00
|
|
|
// Stores the autentication for future reconnection
|
2006-11-10 20:06:33 +01:00
|
|
|
this.getConfiguration().setLoginInfo(username, password, resource, sendPresence);
|
2006-09-16 00:42:06 +02: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
|
|
|
*
|
2007-03-20 23:09:15 +01:00
|
|
|
* @throws XMPPException if an error occurs or anonymous logins are not supported by the server.
|
2003-08-12 21:30:51 +02:00
|
|
|
* @throws IllegalStateException if not connected to the server, or already logged in
|
2007-03-20 23:09:15 +01:00
|
|
|
* to the serrver.
|
2003-08-12 21:30:51 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2006-11-22 23:55:37 +01:00
|
|
|
* Returns a chat manager instance for this connection. The ChatManager manages all incoming and
|
|
|
|
* outgoing chats on the current connection.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2006-11-22 23:55:37 +01:00
|
|
|
* @return a chat manager instance for this connection.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2006-11-22 23:55:37 +01:00
|
|
|
public synchronized ChatManager getChatManager() {
|
2007-03-20 23:09:15 +01:00
|
|
|
if (this.chatManager == null) {
|
2006-11-22 23:55:37 +01:00
|
|
|
this.chatManager = new ChatManager(this);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2006-11-22 23:55:37 +01:00
|
|
|
return this.chatManager;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2006-09-14 21:21:38 +02:00
|
|
|
* the XMPP server. The shutdown logic will be used during a planned disconnection or when
|
|
|
|
* dealing with an unexpected disconnection. Unlike {@link #disconnect()} the connection's
|
2007-01-08 00:03:16 +01:00
|
|
|
* packet reader, packet writer, and {@link Roster} will not be removed; thus
|
2006-09-14 21:21:38 +02:00
|
|
|
* connection's state is kept.
|
2007-02-13 03:23:28 +01:00
|
|
|
*
|
|
|
|
* @param unavailablePresence the presence packet to send during shutdown.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2007-02-13 03:23:28 +01:00
|
|
|
protected void shutdown(Presence unavailablePresence) {
|
2003-01-13 17:58:47 +01:00
|
|
|
// Set presence to offline.
|
2007-02-13 03:23:28 +01:00
|
|
|
packetWriter.sendPacket(unavailablePresence);
|
2007-02-19 09:38:19 +01:00
|
|
|
|
|
|
|
this.setWasAuthenticated(authenticated);
|
|
|
|
authenticated = false;
|
|
|
|
connected = false;
|
|
|
|
|
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.
|
2007-03-20 23:09:15 +01:00
|
|
|
if (reader != null) {
|
|
|
|
try {
|
|
|
|
reader.close();
|
|
|
|
}
|
|
|
|
catch (Throwable ignore) { /* ignore */ }
|
2005-09-05 22:00:45 +02:00
|
|
|
reader = null;
|
|
|
|
}
|
2007-03-20 23:09:15 +01:00
|
|
|
if (writer != null) {
|
|
|
|
try {
|
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
catch (Throwable ignore) { /* ignore */ }
|
2005-09-05 22:00:45 +02:00
|
|
|
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
|
|
|
}
|
2006-09-16 00:42:06 +02:00
|
|
|
|
2006-09-14 21:21:38 +02:00
|
|
|
saslAuthentication.init();
|
|
|
|
}
|
2006-09-16 00:42:06 +02:00
|
|
|
|
2006-09-14 21:21:38 +02:00
|
|
|
/**
|
|
|
|
* Closes the connection by setting presence to unavailable then closing the stream to
|
|
|
|
* the XMPP server. The XMPPConnection can still be used for connecting to the server
|
2007-02-19 06:48:57 +01:00
|
|
|
* again.<p>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2007-02-19 06:48:57 +01:00
|
|
|
* This method cleans up all resources used by the connection. Therefore, the roster,
|
|
|
|
* listeners and other stateful objects cannot be re-used by simply calling connect()
|
|
|
|
* on this connection again. This is unlike the behavior during unexpected disconnects
|
|
|
|
* (and subsequent connections). In that case, all state is preserved to allow for
|
|
|
|
* more seamless error recovery.
|
2006-09-14 21:21:38 +02:00
|
|
|
*/
|
|
|
|
public void disconnect() {
|
2007-02-13 03:23:28 +01:00
|
|
|
disconnect(new Presence(Presence.Type.unavailable));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the connection. A custom unavailable presence is sent to the server, followed
|
|
|
|
* by closing the stream. The XMPPConnection can still be used for connecting to the server
|
|
|
|
* again. A custom unavilable presence is useful for communicating offline presence
|
|
|
|
* information such as "On vacation". Typically, just the status text of the presence
|
|
|
|
* packet is set with online information, but most XMPP servers will deliver the full
|
2007-02-19 06:48:57 +01:00
|
|
|
* presence packet with whatever data is set.<p>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2007-02-19 06:48:57 +01:00
|
|
|
* This method cleans up all resources used by the connection. Therefore, the roster,
|
|
|
|
* listeners and other stateful objects cannot be re-used by simply calling connect()
|
|
|
|
* on this connection again. This is unlike the behavior during unexpected disconnects
|
|
|
|
* (and subsequent connections). In that case, all state is preserved to allow for
|
|
|
|
* more seamless error recovery.
|
2007-02-13 03:23:28 +01:00
|
|
|
*
|
2007-03-20 23:09:15 +01:00
|
|
|
* @param unavailablePresence the presence packet to send during shutdown.
|
2007-02-13 03:23:28 +01:00
|
|
|
*/
|
|
|
|
public void disconnect(Presence unavailablePresence) {
|
2007-02-21 01:57:31 +01:00
|
|
|
// If not connected, ignore this request.
|
|
|
|
if (packetReader == null || packetWriter == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-02-19 09:35:05 +01:00
|
|
|
shutdown(unavailablePresence);
|
2006-09-16 00:42:06 +02:00
|
|
|
|
2007-02-20 19:48:26 +01:00
|
|
|
if (roster != null) {
|
|
|
|
roster.cleanup();
|
|
|
|
roster = null;
|
|
|
|
}
|
2006-09-16 00:42:06 +02:00
|
|
|
|
2007-02-19 09:35:05 +01:00
|
|
|
wasAuthenticated = false;
|
2006-09-16 00:42:06 +02:00
|
|
|
|
2007-02-19 06:48:57 +01:00
|
|
|
packetWriter.cleanup();
|
2006-09-14 21:21:38 +02:00
|
|
|
packetWriter = null;
|
2007-02-19 06:48:57 +01:00
|
|
|
packetReader.cleanup();
|
2006-09-14 21:21:38 +02:00
|
|
|
packetReader = null;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2007-03-01 19:44:08 +01:00
|
|
|
* which packets will be delivered to the listener. If the same packet listener
|
|
|
|
* is added again with a different filter, only the new filter will be used.
|
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.
|
2007-03-20 23:09:15 +01:00
|
|
|
* @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.
|
2007-03-20 23:09:15 +01:00
|
|
|
* @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.
|
2007-03-20 23:09:15 +01:00
|
|
|
* @param packetFilter the packet filter to use.
|
2005-11-02 00:49:07 +01:00
|
|
|
*/
|
|
|
|
public void addPacketWriterInterceptor(PacketInterceptor packetInterceptor,
|
2007-03-20 23:09:15 +01:00
|
|
|
PacketFilter packetFilter) {
|
2005-11-02 00:49:07 +01:00
|
|
|
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
|
2006-11-10 20:06:33 +01:00
|
|
|
* the connection closes or fails. The connection needs to already be connected
|
|
|
|
* or otherwise an IllegalStateException will be thrown.
|
2003-07-09 18:05:43 +02:00
|
|
|
*
|
|
|
|
* @param connectionListener a connection listener.
|
|
|
|
*/
|
|
|
|
public void addConnectionListener(ConnectionListener connectionListener) {
|
2006-11-10 20:06:33 +01:00
|
|
|
if (!isConnected()) {
|
|
|
|
throw new IllegalStateException("Not connected to server.");
|
|
|
|
}
|
2003-07-09 18:05:43 +02:00
|
|
|
if (connectionListener == null) {
|
|
|
|
return;
|
|
|
|
}
|
2007-02-16 01:12:17 +01:00
|
|
|
if (!packetReader.connectionListeners.contains(connectionListener)) {
|
|
|
|
packetReader.connectionListeners.add(connectionListener);
|
2003-07-09 18:05:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a connection listener from this connection.
|
|
|
|
*
|
|
|
|
* @param connectionListener a connection listener.
|
|
|
|
*/
|
|
|
|
public void removeConnectionListener(ConnectionListener connectionListener) {
|
2007-02-16 01:12:17 +01:00
|
|
|
packetReader.connectionListeners.remove(connectionListener);
|
2003-07-09 18:05:43 +02:00
|
|
|
}
|
|
|
|
|
2003-12-19 18:54:38 +01:00
|
|
|
/**
|
2006-09-16 01:05:01 +02:00
|
|
|
* Adds a new listener that will be notified when new XMPPConnections are created. Note
|
|
|
|
* that newly created connections will not be actually connected to the server.
|
2003-12-19 18:54:38 +01:00
|
|
|
*
|
2006-09-16 01:05:01 +02:00
|
|
|
* @param connectionCreationListener a listener interested on new connections.
|
2003-12-19 18:54:38 +01:00
|
|
|
*/
|
2006-09-16 01:05:01 +02:00
|
|
|
public static void addConnectionCreationListener(
|
|
|
|
ConnectionCreationListener connectionCreationListener) {
|
|
|
|
connectionEstablishedListeners.add(connectionCreationListener);
|
2003-12-19 18:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-09-16 01:05:01 +02:00
|
|
|
* Removes a listener that was interested in connection creation events.
|
2003-12-19 18:54:38 +01:00
|
|
|
*
|
2006-09-16 01:05:01 +02:00
|
|
|
* @param connectionCreationListener a listener interested on new connections.
|
2003-12-19 18:54:38 +01:00
|
|
|
*/
|
2006-09-16 01:05:01 +02:00
|
|
|
public static void removeConnectionCreationListener(
|
|
|
|
ConnectionCreationListener connectionCreationListener) {
|
|
|
|
connectionEstablishedListeners.remove(connectionCreationListener);
|
2006-09-15 23:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void connectUsingConfiguration(ConnectionConfiguration config) throws XMPPException {
|
|
|
|
this.host = config.getHost();
|
|
|
|
this.port = config.getPort();
|
|
|
|
try {
|
|
|
|
if (config.getSocketFactory() == null) {
|
|
|
|
this.socket = new Socket(host, port);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.socket = config.getSocketFactory().createSocket(host, port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (UnknownHostException uhe) {
|
|
|
|
String errorMessage = "Could not connect to " + host + ":" + port + ".";
|
|
|
|
throw new XMPPException(errorMessage, new XMPPError(
|
|
|
|
XMPPError.Condition.remote_server_timeout, errorMessage),
|
|
|
|
uhe);
|
|
|
|
}
|
|
|
|
catch (IOException ioe) {
|
|
|
|
String errorMessage = "XMPPError connecting to " + host + ":"
|
|
|
|
+ port + ".";
|
|
|
|
throw new XMPPException(errorMessage, new XMPPError(
|
|
|
|
XMPPError.Condition.remote_server_error, errorMessage), ioe);
|
2003-12-19 18:54:38 +01:00
|
|
|
}
|
2006-09-15 23:51:08 +02:00
|
|
|
this.serviceName = config.getServiceName();
|
|
|
|
initConnection();
|
2003-12-19 18:54:38 +01:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2006-09-14 21:21:38 +02:00
|
|
|
private void initConnection() throws XMPPException {
|
2006-11-10 20:06:33 +01:00
|
|
|
boolean isFirstInitialization = packetReader == null || packetWriter == null;
|
|
|
|
if (!isFirstInitialization) {
|
|
|
|
usingCompression = false;
|
|
|
|
}
|
|
|
|
|
2005-08-27 04:33:08 +02:00
|
|
|
// Set the reader and writer instance variables
|
|
|
|
initReaderAndWriter();
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2006-09-14 21:21:38 +02:00
|
|
|
try {
|
|
|
|
if (isFirstInitialization) {
|
|
|
|
packetWriter = new PacketWriter(this);
|
|
|
|
packetReader = new PacketReader(this);
|
|
|
|
|
|
|
|
// If debugging is enabled, we should start the thread that will listen for
|
|
|
|
// all packets and then log them.
|
|
|
|
if (configuration.isDebuggerEnabled()) {
|
|
|
|
packetReader.addPacketListener(debugger.getReaderListener(), null);
|
|
|
|
if (debugger.getWriterListener() != null) {
|
|
|
|
packetWriter.addPacketListener(debugger.getWriterListener(), null);
|
|
|
|
}
|
2004-09-24 04:15:55 +02:00
|
|
|
}
|
2003-11-15 21:23:20 +01:00
|
|
|
}
|
2006-09-14 21:21:38 +02:00
|
|
|
else {
|
|
|
|
packetWriter.init();
|
|
|
|
packetReader.init();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
2006-09-14 21:21:38 +02:00
|
|
|
|
|
|
|
if (isFirstInitialization) {
|
2006-09-15 23:51:08 +02:00
|
|
|
// Notify listeners that a new connection has been established
|
2006-09-16 01:05:01 +02:00
|
|
|
for (ConnectionCreationListener listener : connectionEstablishedListeners) {
|
|
|
|
listener.connectionCreated(this);
|
2006-09-15 23:51:08 +02:00
|
|
|
}
|
2006-09-14 21:21:38 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
packetReader.notifyReconnection();
|
|
|
|
}
|
|
|
|
|
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) {
|
2006-09-14 21:21:38 +02:00
|
|
|
try {
|
|
|
|
packetWriter.shutdown();
|
|
|
|
}
|
|
|
|
catch (Throwable ignore) { /* ignore */ }
|
2005-08-27 04:33:08 +02:00
|
|
|
packetWriter = null;
|
|
|
|
}
|
|
|
|
if (packetReader != null) {
|
2006-09-14 21:21:38 +02:00
|
|
|
try {
|
|
|
|
packetReader.shutdown();
|
|
|
|
}
|
|
|
|
catch (Throwable ignore) { /* ignore */ }
|
2005-08-27 04:33:08 +02:00
|
|
|
packetReader = null;
|
|
|
|
}
|
|
|
|
if (reader != null) {
|
2006-09-14 21:21:38 +02:00
|
|
|
try {
|
|
|
|
reader.close();
|
|
|
|
}
|
|
|
|
catch (Throwable ignore) { /* ignore */ }
|
2005-08-27 04:33:08 +02:00
|
|
|
reader = null;
|
|
|
|
}
|
|
|
|
if (writer != null) {
|
2006-09-14 21:21:38 +02:00
|
|
|
try {
|
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
catch (Throwable ignore) { /* ignore */}
|
2005-08-27 04:33:08 +02:00
|
|
|
writer = null;
|
|
|
|
}
|
|
|
|
if (socket != null) {
|
2006-09-14 21:21:38 +02:00
|
|
|
try {
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
catch (Exception e) { /* ignore */ }
|
2005-08-27 04:33:08 +02:00
|
|
|
socket = null;
|
|
|
|
}
|
2006-09-14 21:21:38 +02:00
|
|
|
this.setWasAuthenticated(authenticated);
|
2005-08-27 04:33:08 +02:00
|
|
|
authenticated = false;
|
|
|
|
connected = false;
|
|
|
|
|
2006-09-14 21:21:38 +02:00
|
|
|
throw ex; // Everything stoppped. Now throw the exception.
|
2005-08-27 04:33:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initReaderAndWriter() throws XMPPException {
|
|
|
|
try {
|
2006-01-16 18:34:56 +01:00
|
|
|
if (!usingCompression) {
|
2006-09-14 21:21:38 +02:00
|
|
|
reader =
|
|
|
|
new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
|
|
|
|
writer = new BufferedWriter(
|
|
|
|
new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
|
2006-01-16 18:34:56 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
2006-07-18 07:14:33 +02:00
|
|
|
Class<?> zoClass = Class.forName("com.jcraft.jzlib.ZOutputStream");
|
|
|
|
Constructor<?> constructor =
|
2006-07-18 08:47:38 +02:00
|
|
|
zoClass.getConstructor(OutputStream.class, Integer.TYPE);
|
|
|
|
Object out = constructor.newInstance(socket.getOutputStream(), 9);
|
|
|
|
Method method = zoClass.getMethod("setFlushMode", Integer.TYPE);
|
2007-03-20 23:09:15 +01:00
|
|
|
method.invoke(out, 2);
|
2006-09-14 21:21:38 +02:00
|
|
|
writer =
|
|
|
|
new BufferedWriter(new OutputStreamWriter((OutputStream) out, "UTF-8"));
|
2006-01-16 18:34:56 +01:00
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
Class<?> ziClass = Class.forName("com.jcraft.jzlib.ZInputStream");
|
2006-07-18 08:47:38 +02:00
|
|
|
constructor = ziClass.getConstructor(InputStream.class);
|
|
|
|
Object in = constructor.newInstance(socket.getInputStream());
|
|
|
|
method = ziClass.getMethod("setFlushMode", Integer.TYPE);
|
2007-03-20 23:09:15 +01:00
|
|
|
method.invoke(in, 2);
|
2006-01-16 18:34:56 +01:00
|
|
|
reader = new BufferedReader(new InputStreamReader((InputStream) in, "UTF-8"));
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2006-09-14 21:21:38 +02:00
|
|
|
reader = new BufferedReader(
|
|
|
|
new InputStreamReader(socket.getInputStream(), "UTF-8"));
|
|
|
|
writer = new BufferedWriter(
|
|
|
|
new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
|
2006-01-16 18:34:56 +01:00
|
|
|
}
|
|
|
|
}
|
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 {
|
2006-09-14 21:21:38 +02:00
|
|
|
debuggerClass =
|
|
|
|
Class.forName("org.jivesoftware.smack.debugger.LiteDebugger");
|
2005-08-27 04:33:08 +02:00
|
|
|
}
|
|
|
|
catch (Exception ex2) {
|
|
|
|
ex2.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create a new debugger instance. If an exception occurs then disable the debugging
|
|
|
|
// option
|
|
|
|
try {
|
2006-09-14 21:21:38 +02:00
|
|
|
Constructor<?> constructor = debuggerClass
|
|
|
|
.getConstructor(XMPPConnection.class, Writer.class, Reader.class);
|
2006-07-18 08:47:38 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
2006-09-14 21:21:38 +02:00
|
|
|
* Returns the configuration used to connect to the server.
|
2006-08-03 00:17:11 +02:00
|
|
|
*
|
2006-09-14 21:21:38 +02:00
|
|
|
* @return the configuration used to connect to the server.
|
2006-08-03 00:17:11 +02:00
|
|
|
*/
|
2006-09-14 21:21:38 +02:00
|
|
|
protected ConnectionConfiguration getConfiguration() {
|
2006-08-03 00:17:11 +02:00
|
|
|
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.
|
2007-01-11 20:01:24 +01:00
|
|
|
*
|
|
|
|
* @param required true when the server indicates that TLS is required.
|
2005-08-27 04:33:08 +02:00
|
|
|
*/
|
2007-01-11 20:01:24 +01:00
|
|
|
void startTLSReceived(boolean required) {
|
|
|
|
if (required && configuration.getSecurityMode() ==
|
2007-03-20 23:09:15 +01:00
|
|
|
ConnectionConfiguration.SecurityMode.disabled) {
|
2007-01-11 20:01:24 +01:00
|
|
|
packetReader.notifyConnectionError(new IllegalStateException(
|
|
|
|
"TLS required by server but not allowed by connection configuration"));
|
|
|
|
return;
|
|
|
|
}
|
2007-03-20 23:09:15 +01:00
|
|
|
|
2007-01-11 20:01:24 +01:00
|
|
|
if (configuration.getSecurityMode() == ConnectionConfiguration.SecurityMode.disabled) {
|
2006-01-17 21:05:31 +01:00
|
|
|
// 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.
|
2006-09-15 23:51:08 +02:00
|
|
|
*
|
|
|
|
* @throws Exception if an exception occurs.
|
2005-08-27 04:33:08 +02:00
|
|
|
*/
|
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>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <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>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2006-01-16 18:34:56 +01:00
|
|
|
* 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>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <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.
|
2006-09-15 23:51:08 +02:00
|
|
|
*
|
|
|
|
* @throws Exception if there is an exception starting stream compression.
|
2006-01-16 18:34:56 +01:00
|
|
|
*/
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-15 23:51:08 +02:00
|
|
|
/**
|
|
|
|
* Notifies the XMPP connection that stream compression was denied so that
|
|
|
|
* the connection process can proceed.
|
|
|
|
*/
|
2006-01-16 18:34:56 +01:00
|
|
|
void streamCompressionDenied() {
|
|
|
|
synchronized (this) {
|
|
|
|
this.notify();
|
|
|
|
}
|
|
|
|
}
|
2007-03-20 23:09:15 +01:00
|
|
|
|
2006-09-16 00:42:06 +02:00
|
|
|
/**
|
2006-09-14 21:21:38 +02:00
|
|
|
* Establishes a connection to the XMPP server and performs an automatic login
|
|
|
|
* only if the previous connection state was logged (authenticated). It basically
|
|
|
|
* creates and maintains a socket connection to the server.<p>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2006-09-14 21:21:38 +02:00
|
|
|
* Listeners will be preserved from a previous connection if the reconnection
|
|
|
|
* occurs after an abrupt termination.
|
2006-09-16 00:42:06 +02:00
|
|
|
*
|
2006-09-14 21:21:38 +02:00
|
|
|
* @throws XMPPException if an error occurs while trying to establish the connection.
|
2007-03-20 23:09:15 +01:00
|
|
|
* 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.
|
2006-09-14 21:21:38 +02:00
|
|
|
*/
|
|
|
|
public void connect() throws XMPPException {
|
|
|
|
// Stablishes the connection, readers and writers
|
|
|
|
connectUsingConfiguration(configuration);
|
|
|
|
// Automatically makes the login if the user was previouslly connected successfully
|
|
|
|
// to the server and the connection was terminated abruptly
|
|
|
|
if (connected && wasAuthenticated) {
|
|
|
|
// Make the login
|
|
|
|
try {
|
|
|
|
if (isAnonymous()) {
|
|
|
|
// Make the anonymous login
|
|
|
|
loginAnonymously();
|
2007-03-20 23:09:15 +01:00
|
|
|
}
|
|
|
|
else {
|
2006-11-10 20:06:33 +01:00
|
|
|
login(getConfiguration().getUsername(), getConfiguration().getPassword(),
|
|
|
|
getConfiguration().getResource(), getConfiguration().isSendPresence());
|
2006-09-14 21:21:38 +02:00
|
|
|
}
|
2007-03-20 23:09:15 +01:00
|
|
|
}
|
|
|
|
catch (XMPPException e) {
|
2006-09-14 21:21:38 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-09-16 00:42:06 +02:00
|
|
|
|
|
|
|
/**
|
2006-09-15 23:51:08 +02:00
|
|
|
* Sets whether the connection has already logged in the server.
|
2006-09-16 00:42:06 +02:00
|
|
|
*
|
2006-09-15 23:51:08 +02:00
|
|
|
* @param wasAuthenticated true if the connection has already been authenticated.
|
2006-09-14 21:21:38 +02:00
|
|
|
*/
|
|
|
|
private void setWasAuthenticated(boolean wasAuthenticated) {
|
|
|
|
if (!this.wasAuthenticated) {
|
|
|
|
this.wasAuthenticated = wasAuthenticated;
|
|
|
|
}
|
|
|
|
}
|
2004-11-05 06:25:52 +01:00
|
|
|
}
|