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.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
|
|
|
|
2008-08-12 19:42:44 +02:00
|
|
|
import javax.net.ssl.KeyManager;
|
|
|
|
import javax.net.ssl.KeyManagerFactory;
|
2005-08-27 04:33:08 +02:00
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
import javax.net.ssl.SSLSocket;
|
2008-08-12 19:42:44 +02:00
|
|
|
import javax.security.auth.callback.Callback;
|
|
|
|
import javax.security.auth.callback.CallbackHandler;
|
|
|
|
import javax.security.auth.callback.PasswordCallback;
|
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;
|
2005-08-27 04:33:08 +02:00
|
|
|
import java.net.Socket;
|
|
|
|
import java.net.UnknownHostException;
|
2008-08-12 19:42:44 +02:00
|
|
|
import java.security.KeyStore;
|
|
|
|
import java.security.Provider;
|
|
|
|
import java.security.Security;
|
2006-07-18 07:14:33 +02:00
|
|
|
import java.util.Collection;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
/**
|
2010-02-09 12:55:56 +01:00
|
|
|
* Creates a socket connection to a XMPP server. This is the default connection
|
|
|
|
* to a Jabber server and is specified in the XMPP Core (RFC 3920).
|
|
|
|
*
|
|
|
|
* @see Connection
|
2003-01-13 17:58:47 +01:00
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
public class XMPPConnection extends Connection {
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2005-08-27 04:33:08 +02:00
|
|
|
/**
|
2010-02-09 12:55:56 +01:00
|
|
|
* The socket which is used for this connection.
|
2005-08-27 04:33:08 +02:00
|
|
|
*/
|
2003-09-18 23:37:52 +02:00
|
|
|
Socket socket;
|
2005-09-05 22:00:45 +02:00
|
|
|
|
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-01-13 17:58:47 +01:00
|
|
|
|
2006-01-16 18:34:56 +01:00
|
|
|
/**
|
|
|
|
* Collection of available stream compression methods offered by the server.
|
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
private Collection<String> compressionMethods;
|
2006-01-16 18:34:56 +01:00
|
|
|
/**
|
|
|
|
* Flag that indicates if stream compression is actually in use.
|
|
|
|
*/
|
|
|
|
private boolean usingCompression;
|
2006-09-16 00:42:06 +02:00
|
|
|
|
2007-11-30 20:40:31 +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
|
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
|
2007-11-30 20:40:31 +01:00
|
|
|
* and you must call {@link #connect()}.<p>
|
|
|
|
* <p/>
|
|
|
|
* The CallbackHandler will only be used if the connection requires the client provide
|
|
|
|
* an SSL certificate to the server. The CallbackHandler must handle the PasswordCallback
|
|
|
|
* to prompt for a password to unlock the keystore containing the SSL certificate.
|
|
|
|
*
|
|
|
|
* @param serviceName the name of the XMPP server to connect to; e.g. <tt>example.com</tt>.
|
|
|
|
* @param callbackHandler the CallbackHandler used to prompt for the password to the keystore.
|
|
|
|
*/
|
|
|
|
public XMPPConnection(String serviceName, CallbackHandler callbackHandler) {
|
|
|
|
// Create the configuration for this new connection
|
2010-02-09 12:55:56 +01:00
|
|
|
super(new ConnectionConfiguration(serviceName));
|
2007-11-30 20:40:31 +01:00
|
|
|
config.setCompressionEnabled(false);
|
|
|
|
config.setSASLAuthenticationEnabled(true);
|
|
|
|
config.setDebuggerEnabled(DEBUG_ENABLED);
|
2010-02-09 12:55:56 +01:00
|
|
|
config.setCallbackHandler(callbackHandler);
|
2007-11-30 20:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new XMPP conection in the same way {@link #XMPPConnection(String,CallbackHandler)} does, but
|
|
|
|
* with no callback handler for password prompting of the keystore. This will work
|
|
|
|
* in most cases, provided the client is not required to provide a certificate to
|
|
|
|
* the server.
|
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
|
2010-02-09 12:55:56 +01:00
|
|
|
super(new ConnectionConfiguration(serviceName));
|
2006-01-17 21:05:31 +01:00
|
|
|
config.setCompressionEnabled(false);
|
|
|
|
config.setSASLAuthenticationEnabled(true);
|
|
|
|
config.setDebuggerEnabled(DEBUG_ENABLED);
|
2007-11-30 20:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new XMPP conection in the same way {@link #XMPPConnection(ConnectionConfiguration,CallbackHandler)} does, but
|
|
|
|
* with no callback handler for password prompting of the keystore. This will work
|
|
|
|
* in most cases, provided the client is not required to provide a certificate to
|
|
|
|
* the server.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param config the connection configuration.
|
|
|
|
*/
|
|
|
|
public XMPPConnection(ConnectionConfiguration config) {
|
2010-02-09 12:55:56 +01:00
|
|
|
super(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
|
2007-11-30 20:40:31 +01:00
|
|
|
* and you must call {@link #connect()}.<p>
|
|
|
|
* <p/>
|
|
|
|
*
|
|
|
|
* The CallbackHandler will only be used if the connection requires the client provide
|
|
|
|
* an SSL certificate to the server. The CallbackHandler must handle the PasswordCallback
|
|
|
|
* to prompt for a password to unlock the keystore containing the SSL certificate.
|
2004-07-06 00:22:02 +02:00
|
|
|
*
|
2007-01-05 00:00:58 +01:00
|
|
|
* @param config the connection configuration.
|
2007-11-30 20:40:31 +01:00
|
|
|
* @param callbackHandler the CallbackHandler used to prompt for the password to the keystore.
|
2004-07-06 00:22:02 +02:00
|
|
|
*/
|
2007-11-30 20:40:31 +01:00
|
|
|
public XMPPConnection(ConnectionConfiguration config, CallbackHandler callbackHandler) {
|
2010-02-09 12:55:56 +01:00
|
|
|
super(config);
|
|
|
|
config.setCallbackHandler(callbackHandler);
|
2005-07-18 23:28:25 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2005-06-02 06:52:40 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2008-10-24 07:17:50 +02:00
|
|
|
* authenticated using SASL if not Non-SASL authentication will be tried. 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.<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
|
2008-10-24 07:17:50 +02:00
|
|
|
* server if not already connected.<p>
|
2005-06-02 06:52:40 +02:00
|
|
|
*
|
2008-10-24 07:17:50 +02:00
|
|
|
* It is possible to log in without sending an initial available presence by using
|
|
|
|
* {@link ConnectionConfiguration#setSendPresence(boolean)}. If this connection is
|
|
|
|
* not interested in loading its roster upon login then use
|
|
|
|
* {@link ConnectionConfiguration#setRosterLoadedAtLogin(boolean)}.
|
|
|
|
* Finally, if you want to not pass a password and instead use a more advanced mechanism
|
|
|
|
* while using SASL then you may be interested in using
|
|
|
|
* {@link ConnectionConfiguration#setCallbackHandler(javax.security.auth.callback.CallbackHandler)}.
|
|
|
|
* For more advanced login settings see {@link ConnectionConfiguration}.
|
2007-11-14 17:27:47 +01:00
|
|
|
*
|
2008-11-13 19:31:37 +01:00
|
|
|
* @param username the username.
|
|
|
|
* @param password the password or <tt>null</tt> if using a CallbackHandler.
|
|
|
|
* @param resource the resource.
|
|
|
|
* @throws XMPPException if an error occurs.
|
2008-08-29 15:04:16 +02:00
|
|
|
* @throws IllegalStateException if not connected to the server, or already logged in
|
2008-11-13 19:31:37 +01:00
|
|
|
* to the serrver.
|
2008-08-29 15:04:16 +02:00
|
|
|
*/
|
2008-10-24 07:17:50 +02:00
|
|
|
public synchronized void login(String username, String password, String resource) 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;
|
2010-02-09 12:55:56 +01:00
|
|
|
if (config.isSASLAuthenticationEnabled() &&
|
2006-01-17 21:05:31 +01:00
|
|
|
saslAuthentication.hasNonAnonymousAuthentication()) {
|
2005-08-27 04:33:08 +02:00
|
|
|
// Authenticate using SASL
|
2008-10-24 07:17:50 +02:00
|
|
|
if (password != null) {
|
|
|
|
response = saslAuthentication.authenticate(username, password, resource);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
response = saslAuthentication
|
2010-02-09 12:55:56 +01:00
|
|
|
.authenticate(username, resource, config.getCallbackHandler());
|
2008-10-24 07:17:50 +02:00
|
|
|
}
|
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
|
2010-02-09 12:55:56 +01:00
|
|
|
config.setServiceName(StringUtils.parseServer(response));
|
2003-08-12 21:30:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2010-02-09 12:55:56 +01:00
|
|
|
this.user = username + "@" + getServiceName();
|
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
|
2010-02-09 12:55:56 +01:00
|
|
|
if (config.isCompressionEnabled()) {
|
2006-01-17 22:28:46 +01:00
|
|
|
useCompression();
|
|
|
|
}
|
|
|
|
|
2007-11-14 17:27:47 +01:00
|
|
|
// Create the roster if it is not a reconnection.
|
|
|
|
if (this.roster == null) {
|
|
|
|
this.roster = new Roster(this);
|
|
|
|
}
|
2010-02-09 12:55:56 +01:00
|
|
|
if (config.isRosterLoadedAtLogin()) {
|
|
|
|
this.roster.reload();
|
2008-10-24 07:17:50 +02:00
|
|
|
}
|
2007-11-14 17:27:47 +01:00
|
|
|
|
|
|
|
// Set presence to online.
|
2010-02-09 12:55:56 +01:00
|
|
|
if (config.isSendPresence()) {
|
2007-11-14 17:27:47 +01:00
|
|
|
packetWriter.sendPacket(new Presence(Presence.Type.available));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Indicate that we're now authenticated.
|
|
|
|
authenticated = true;
|
|
|
|
anonymous = false;
|
|
|
|
|
|
|
|
// Stores the autentication for future reconnection
|
2010-02-09 12:55:56 +01:00
|
|
|
config.setLoginInfo(username, password, resource);
|
2007-11-14 17:27:47 +01:00
|
|
|
|
|
|
|
// If debugging is enabled, change the the debug window title to include the
|
|
|
|
// name we are now logged-in as.
|
|
|
|
// If DEBUG_ENABLED was set to true AFTER the connection was created the debugger
|
|
|
|
// will be null
|
2010-02-09 12:55:56 +01:00
|
|
|
if (config.isDebuggerEnabled() && debugger != null) {
|
2007-11-14 17:27:47 +01:00
|
|
|
debugger.userHasLogged(user);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*
|
2008-11-13 19:31:37 +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
|
2008-11-13 19:31:37 +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;
|
2010-02-09 12:55:56 +01:00
|
|
|
if (config.isSASLAuthenticationEnabled() &&
|
2006-01-17 21:05:31 +01:00
|
|
|
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
|
2010-02-09 12:55:56 +01:00
|
|
|
config.setServiceName(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
|
2010-02-09 12:55:56 +01:00
|
|
|
if (config.isCompressionEnabled()) {
|
2006-01-17 22:28:46 +01:00
|
|
|
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
|
2010-02-09 12:55:56 +01:00
|
|
|
if (config.isDebuggerEnabled() && debugger != null) {
|
2003-11-15 21:23:20 +01:00
|
|
|
debugger.userHasLogged(user);
|
2003-08-12 21:30:51 +02: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;
|
|
|
|
}
|
2010-02-09 12:55:56 +01:00
|
|
|
if (!config.isRosterLoadedAtLogin()) {
|
|
|
|
roster.reload();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isConnected() {
|
|
|
|
return connected;
|
|
|
|
}
|
|
|
|
|
2003-09-18 22:39:14 +02:00
|
|
|
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
|
|
|
public boolean isAuthenticated() {
|
|
|
|
return authenticated;
|
|
|
|
}
|
|
|
|
|
2003-08-12 21:30:51 +02:00
|
|
|
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
|
|
|
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
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.
|
2010-02-09 12:55:56 +01:00
|
|
|
* @deprecated replaced by {@link Connection#addPacketInterceptor(PacketInterceptor, PacketFilter)}.
|
2005-11-02 00:49:07 +01:00
|
|
|
*/
|
|
|
|
public void addPacketWriterInterceptor(PacketInterceptor packetInterceptor,
|
2007-03-20 23:09:15 +01:00
|
|
|
PacketFilter packetFilter) {
|
2010-02-09 12:55:56 +01:00
|
|
|
addPacketInterceptor(packetInterceptor, packetFilter);
|
2005-11-02 00:49:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a packet interceptor.
|
|
|
|
*
|
|
|
|
* @param packetInterceptor the packet interceptor to remove.
|
2010-02-09 12:55:56 +01:00
|
|
|
* @deprecated replaced by {@link Connection#removePacketInterceptor(PacketInterceptor)}.
|
2005-11-02 00:49:07 +01:00
|
|
|
*/
|
|
|
|
public void removePacketWriterInterceptor(PacketInterceptor packetInterceptor) {
|
2010-02-09 12:55:56 +01:00
|
|
|
removePacketInterceptor(packetInterceptor);
|
2003-07-09 18:05:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-09 12:55:56 +01:00
|
|
|
* Registers a packet listener with this connection. The listener will be
|
|
|
|
* notified of every packet that this connection sends. A packet filter determines
|
|
|
|
* which packets will be delivered to the listener. 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-12-19 18:54:38 +01:00
|
|
|
*
|
2010-02-09 12:55:56 +01:00
|
|
|
* @param packetListener the packet listener to notify of sent packets.
|
|
|
|
* @param packetFilter the packet filter to use.
|
|
|
|
* @deprecated replaced by {@link #addPacketSendingListener(PacketListener, PacketFilter)}.
|
2003-12-19 18:54:38 +01:00
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
public void addPacketWriterListener(PacketListener packetListener, PacketFilter packetFilter) {
|
|
|
|
addPacketSendingListener(packetListener, packetFilter);
|
2003-12-19 18:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-09 12:55:56 +01:00
|
|
|
* Removes a packet listener for sending packets from this connection.
|
2003-12-19 18:54:38 +01:00
|
|
|
*
|
2010-02-09 12:55:56 +01:00
|
|
|
* @param packetListener the packet listener to remove.
|
|
|
|
* @deprecated replaced by {@link #removePacketSendingListener(PacketListener)}.
|
2003-12-19 18:54:38 +01:00
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
public void removePacketWriterListener(PacketListener packetListener) {
|
|
|
|
removePacketSendingListener(packetListener);
|
2006-09-15 23:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void connectUsingConfiguration(ConnectionConfiguration config) throws XMPPException {
|
2010-02-09 12:55:56 +01:00
|
|
|
String host = config.getHost();
|
|
|
|
int port = config.getPort();
|
2006-09-15 23:51:08 +02:00
|
|
|
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
|
|
|
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.
|
2010-02-09 12:55:56 +01:00
|
|
|
if (config.isDebuggerEnabled()) {
|
|
|
|
addPacketListener(debugger.getReaderListener(), null);
|
2006-09-14 21:21:38 +02:00
|
|
|
if (debugger.getWriterListener() != null) {
|
2010-02-09 12:55:56 +01:00
|
|
|
addPacketSendingListener(debugger.getWriterListener(), null);
|
2006-09-14 21:21:38 +02:00
|
|
|
}
|
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
|
2010-02-09 12:55:56 +01:00
|
|
|
for (ConnectionCreationListener listener : getConnectionCreationListeners()) {
|
2006-09-16 01:05:01 +02:00
|
|
|
listener.connectionCreated(this);
|
2006-09-15 23:51:08 +02:00
|
|
|
}
|
2006-09-14 21:21:38 +02:00
|
|
|
}
|
2010-02-24 01:45:40 +01:00
|
|
|
else if (!wasAuthenticated) {
|
2006-09-14 21:21:38 +02:00
|
|
|
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.
|
2010-02-09 12:55:56 +01:00
|
|
|
initDebugger();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2010-02-09 12:55:56 +01:00
|
|
|
if (required && config.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
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
if (config.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");
|
2007-11-30 23:11:04 +01:00
|
|
|
KeyStore ks = null;
|
2007-11-14 17:27:47 +01:00
|
|
|
KeyManager[] kms = null;
|
2007-11-30 21:02:01 +01:00
|
|
|
PasswordCallback pcb = null;
|
2007-11-30 20:40:31 +01:00
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
if(config.getCallbackHandler() == null) {
|
2007-11-30 20:40:31 +01:00
|
|
|
ks = null;
|
|
|
|
} else {
|
2007-12-04 04:14:32 +01:00
|
|
|
//System.out.println("Keystore type: "+configuration.getKeystoreType());
|
2010-02-09 12:55:56 +01:00
|
|
|
if(config.getKeystoreType().equals("NONE")) {
|
2007-11-30 23:11:04 +01:00
|
|
|
ks = null;
|
|
|
|
pcb = null;
|
|
|
|
}
|
2010-02-09 12:55:56 +01:00
|
|
|
else if(config.getKeystoreType().equals("PKCS11")) {
|
2007-12-27 17:15:57 +01:00
|
|
|
try {
|
2007-12-27 17:17:58 +01:00
|
|
|
Constructor c = Class.forName("sun.security.pkcs11.SunPKCS11").getConstructor(InputStream.class);
|
2010-02-09 12:55:56 +01:00
|
|
|
String pkcs11Config = "name = SmartCard\nlibrary = "+config.getPKCS11Library();
|
2007-12-27 17:15:57 +01:00
|
|
|
ByteArrayInputStream config = new ByteArrayInputStream(pkcs11Config.getBytes());
|
|
|
|
Provider p = (Provider)c.newInstance(config);
|
|
|
|
Security.addProvider(p);
|
|
|
|
ks = KeyStore.getInstance("PKCS11",p);
|
|
|
|
pcb = new PasswordCallback("PKCS11 Password: ",false);
|
2010-02-09 12:55:56 +01:00
|
|
|
this.config.getCallbackHandler().handle(new Callback[]{pcb});
|
2007-12-27 17:15:57 +01:00
|
|
|
ks.load(null,pcb.getPassword());
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
ks = null;
|
|
|
|
pcb = null;
|
|
|
|
}
|
2007-11-30 20:40:31 +01:00
|
|
|
}
|
2010-02-09 12:55:56 +01:00
|
|
|
else if(config.getKeystoreType().equals("Apple")) {
|
2007-11-30 20:40:31 +01:00
|
|
|
ks = KeyStore.getInstance("KeychainStore","Apple");
|
|
|
|
ks.load(null,null);
|
|
|
|
//pcb = new PasswordCallback("Apple Keychain",false);
|
|
|
|
//pcb.setPassword(null);
|
|
|
|
}
|
|
|
|
else {
|
2010-02-09 12:55:56 +01:00
|
|
|
ks = KeyStore.getInstance(config.getKeystoreType());
|
2007-12-27 17:15:57 +01:00
|
|
|
try {
|
2007-11-30 23:11:04 +01:00
|
|
|
pcb = new PasswordCallback("Keystore Password: ",false);
|
2010-02-09 12:55:56 +01:00
|
|
|
config.getCallbackHandler().handle(new Callback[]{pcb});
|
|
|
|
ks.load(new FileInputStream(config.getKeystorePath()), pcb.getPassword());
|
2007-12-27 17:15:57 +01:00
|
|
|
}
|
|
|
|
catch(Exception e) {
|
|
|
|
ks = null;
|
|
|
|
pcb = null;
|
|
|
|
}
|
2007-11-30 20:40:31 +01:00
|
|
|
}
|
|
|
|
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
|
|
|
|
try {
|
|
|
|
if(pcb == null) {
|
|
|
|
kmf.init(ks,null);
|
|
|
|
} else {
|
|
|
|
kmf.init(ks,pcb.getPassword());
|
2007-11-30 21:02:01 +01:00
|
|
|
pcb.clearPassword();
|
2007-11-30 20:40:31 +01:00
|
|
|
}
|
|
|
|
kms = kmf.getKeyManagers();
|
|
|
|
} catch (NullPointerException npe) {
|
|
|
|
kms = null;
|
|
|
|
}
|
2007-11-14 17:27:47 +01:00
|
|
|
}
|
|
|
|
|
2006-01-17 21:05:31 +01:00
|
|
|
// Verify certificate presented by the server
|
2007-11-14 17:27:47 +01:00
|
|
|
context.init(kms,
|
2010-02-09 12:55:56 +01:00
|
|
|
new javax.net.ssl.TrustManager[]{new ServerTrustManager(getServiceName(), config)},
|
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();
|
2007-12-04 04:14:32 +01:00
|
|
|
//if (((SSLSocket) socket).getWantClientAuth()) {
|
|
|
|
// System.err.println("Connection wants client auth");
|
|
|
|
//}
|
|
|
|
//else if (((SSLSocket) socket).getNeedClientAuth()) {
|
|
|
|
// System.err.println("Connection needs client auth");
|
|
|
|
//}
|
|
|
|
//else {
|
|
|
|
// System.err.println("Connection does not require client auth");
|
|
|
|
// }
|
2005-09-06 00:06:40 +02:00
|
|
|
// 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.
|
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
void setAvailableCompressionMethods(Collection<String> methods) {
|
2006-01-16 18:34:56 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2008-11-13 19:31:37 +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
|
2010-02-09 12:55:56 +01:00
|
|
|
connectUsingConfiguration(config);
|
2006-09-14 21:21:38 +02:00
|
|
|
// 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 {
|
2010-02-09 12:55:56 +01:00
|
|
|
login(config.getUsername(), config.getPassword(),
|
|
|
|
config.getResource());
|
2006-09-14 21:21:38 +02:00
|
|
|
}
|
2010-02-24 01:45:40 +01:00
|
|
|
packetReader.notifyReconnection();
|
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;
|
|
|
|
}
|
|
|
|
}
|
2007-11-14 17:27:47 +01:00
|
|
|
}
|