2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2004-11-03 00:53:30 +01:00
|
|
|
* 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
|
|
|
*/
|
2014-05-15 15:04:46 +02:00
|
|
|
package org.jivesoftware.smack.tcp;
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.AbstractXMPPConnection;
|
2014-05-15 15:04:46 +02:00
|
|
|
import org.jivesoftware.smack.ConnectionConfiguration;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
|
2014-05-15 15:04:46 +02:00
|
|
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
|
|
|
import org.jivesoftware.smack.ConnectionListener;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.PacketListener;
|
2014-05-15 15:04:46 +02:00
|
|
|
import org.jivesoftware.smack.SmackConfiguration;
|
|
|
|
import org.jivesoftware.smack.SmackException;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.SmackException.AlreadyConnectedException;
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.AlreadyLoggedInException;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.SmackException.NoResponseException;
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
|
|
|
import org.jivesoftware.smack.SmackException.ConnectionException;
|
2014-05-26 22:02:04 +02:00
|
|
|
import org.jivesoftware.smack.SmackException.SecurityRequiredException;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.SynchronizationPoint;
|
2014-05-26 22:02:04 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException.StreamErrorException;
|
2014-05-15 15:04:46 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
|
|
|
import org.jivesoftware.smack.XMPPException;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
|
|
|
import org.jivesoftware.smack.compress.packet.Compressed;
|
2013-02-26 10:26:41 +01:00
|
|
|
import org.jivesoftware.smack.compression.XMPPInputOutputStream;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.filter.PacketFilter;
|
|
|
|
import org.jivesoftware.smack.compress.packet.Compress;
|
|
|
|
import org.jivesoftware.smack.packet.Element;
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.packet.Message;
|
|
|
|
import org.jivesoftware.smack.packet.StreamOpen;
|
2005-09-06 00:06:40 +02:00
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
|
|
|
import org.jivesoftware.smack.packet.Presence;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.packet.StartTls;
|
2013-06-22 19:01:40 +02:00
|
|
|
import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
|
2014-05-26 22:02:04 +02:00
|
|
|
import org.jivesoftware.smack.parsing.UnparsablePacket;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.sasl.packet.SaslStreamElements;
|
|
|
|
import org.jivesoftware.smack.sasl.packet.SaslStreamElements.Challenge;
|
|
|
|
import org.jivesoftware.smack.sasl.packet.SaslStreamElements.SASLFailure;
|
|
|
|
import org.jivesoftware.smack.sasl.packet.SaslStreamElements.Success;
|
|
|
|
import org.jivesoftware.smack.packet.PlainStreamElement;
|
|
|
|
import org.jivesoftware.smack.packet.XMPPError;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.SMUtils;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.StreamManagementException;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.StreamManagementException.StreamManagementNotEnabledException;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.StreamManagementException.StreamIdDoesNotMatchException;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement.AckAnswer;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement.AckRequest;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement.Enable;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement.Enabled;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement.Failed;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement.Resume;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement.Resumed;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.packet.StreamManagement.StreamManagementFeature;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.predicates.Predicate;
|
|
|
|
import org.jivesoftware.smack.tcp.sm.provider.ParseStreamManagement;
|
2014-05-26 22:02:04 +02:00
|
|
|
import org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown;
|
|
|
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2014-05-29 09:21:04 +02:00
|
|
|
import org.jivesoftware.smack.util.TLSUtils;
|
2013-03-18 09:53:11 +01:00
|
|
|
import org.jivesoftware.smack.util.dns.HostAddress;
|
2014-05-26 22:02:04 +02:00
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2014-07-21 18:42:44 +02:00
|
|
|
import javax.net.ssl.HostnameVerifier;
|
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;
|
2013-02-26 10:26:41 +01:00
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.OutputStreamWriter;
|
2014-05-15 15:04:46 +02:00
|
|
|
import java.io.Writer;
|
2005-09-06 00:06:40 +02:00
|
|
|
import java.lang.reflect.Constructor;
|
2005-08-27 04:33:08 +02:00
|
|
|
import java.net.Socket;
|
2014-05-29 09:21:04 +02:00
|
|
|
import java.security.KeyManagementException;
|
2008-08-12 19:42:44 +02:00
|
|
|
import java.security.KeyStore;
|
2014-05-29 09:21:04 +02:00
|
|
|
import java.security.KeyStoreException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.NoSuchProviderException;
|
2008-08-12 19:42:44 +02:00
|
|
|
import java.security.Provider;
|
|
|
|
import java.security.Security;
|
2014-05-29 09:21:04 +02:00
|
|
|
import java.security.UnrecoverableKeyException;
|
|
|
|
import java.security.cert.CertificateException;
|
2014-09-11 09:49:16 +02:00
|
|
|
import java.util.ArrayList;
|
2006-07-18 07:14:33 +02:00
|
|
|
import java.util.Collection;
|
2013-03-18 09:53:11 +01:00
|
|
|
import java.util.Iterator;
|
2014-09-11 09:49:16 +02:00
|
|
|
import java.util.LinkedHashSet;
|
2013-03-18 09:53:11 +01:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2014-04-03 22:50:13 +02:00
|
|
|
import java.util.Locale;
|
2014-09-11 09:49:16 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
|
|
import java.util.concurrent.BlockingQueue;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
2014-04-23 09:37:16 +02:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
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
|
2014-09-11 09:49:16 +02:00
|
|
|
* to a XMPP server and is specified in the XMPP Core (RFC 6120).
|
2010-02-09 12:55:56 +01:00
|
|
|
*
|
2014-03-10 18:31:45 +01:00
|
|
|
* @see XMPPConnection
|
2003-01-13 17:58:47 +01:00
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
2014-05-25 12:28:08 +02:00
|
|
|
public class XMPPTCPConnection extends AbstractXMPPConnection {
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
private static final int QUEUE_SIZE = 500;
|
2014-04-23 09:37:16 +02:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(XMPPTCPConnection.class.getName());
|
|
|
|
|
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
|
|
|
*/
|
2014-05-26 22:02:04 +02:00
|
|
|
private Socket socket;
|
2005-09-05 22:00:45 +02:00
|
|
|
|
2014-05-26 22:02:04 +02:00
|
|
|
private String connectionID = null;
|
2003-01-13 17:58:47 +01:00
|
|
|
private boolean connected = false;
|
2014-05-26 22:02:04 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private boolean disconnectedButResumeable = false;
|
|
|
|
|
2013-01-06 15:02:44 +01:00
|
|
|
// socketClosed is used concurrent
|
2014-04-09 12:16:44 +02:00
|
|
|
// by XMPPTCPConnection, PacketReader, PacketWriter
|
2013-01-06 15:02:44 +01:00
|
|
|
private volatile boolean socketClosed = false;
|
|
|
|
|
2005-08-27 04:33:08 +02:00
|
|
|
private boolean usingTLS = false;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2013-06-22 19:01:40 +02:00
|
|
|
private ParsingExceptionCallback parsingExceptionCallback = SmackConfiguration.getDefaultParsingExceptionCallback();
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
/**
|
|
|
|
* Protected access level because of unit test purposes
|
|
|
|
*/
|
|
|
|
protected PacketWriter packetWriter;
|
2003-01-13 17:58:47 +01:00
|
|
|
|
2006-01-16 18:34:56 +01:00
|
|
|
/**
|
2014-09-11 09:49:16 +02:00
|
|
|
* Protected access level because of unit test purposes
|
2006-01-16 18:34:56 +01:00
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
protected PacketReader packetReader;
|
|
|
|
|
|
|
|
private final SynchronizationPoint<Exception> initalOpenStreamSend = new SynchronizationPoint<Exception>(this);
|
2013-02-26 10:26:41 +01:00
|
|
|
|
2006-01-16 18:34:56 +01:00
|
|
|
/**
|
2014-09-11 09:49:16 +02:00
|
|
|
*
|
2006-01-16 18:34:56 +01:00
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
private final SynchronizationPoint<XMPPException> maybeCompressFeaturesReceived = new SynchronizationPoint<XMPPException>(
|
|
|
|
this);
|
2006-09-16 00:42:06 +02:00
|
|
|
|
2014-04-27 12:27:12 +02:00
|
|
|
/**
|
2014-09-11 09:49:16 +02:00
|
|
|
*
|
2014-04-27 12:27:12 +02:00
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
private final SynchronizationPoint<XMPPException> compressSyncPoint = new SynchronizationPoint<XMPPException>(
|
|
|
|
this);
|
|
|
|
|
|
|
|
private static boolean useSmDefault = false;
|
|
|
|
|
|
|
|
private static boolean useSmResumptionDefault = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The stream ID of the stream that is currently resumable, ie. the stream we hold the state
|
|
|
|
* for in {@link #clientHandledStanzasCount}, {@link #serverHandledStanzasCount} and
|
|
|
|
* {@link #unacknowledgedStanzas}.
|
|
|
|
*/
|
|
|
|
private String smSessionId;
|
|
|
|
|
|
|
|
private final SynchronizationPoint<XMPPException> smResumedSyncPoint = new SynchronizationPoint<XMPPException>(
|
|
|
|
this);
|
|
|
|
|
|
|
|
private final SynchronizationPoint<XMPPException> smEnabledSyncPoint = new SynchronizationPoint<XMPPException>(
|
|
|
|
this);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The client's preferred maximum resumption time in seconds.
|
|
|
|
*/
|
|
|
|
private int smClientMaxResumptionTime = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The server's preferred maximum resumption time in seconds.
|
|
|
|
*/
|
|
|
|
private int smServerMaxResumptimTime = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether Stream Management (XEP-198) should be used if it's supported by the server.
|
|
|
|
*/
|
|
|
|
private boolean useSm = useSmDefault;
|
|
|
|
private boolean useSmResumption = useSmResumptionDefault;
|
|
|
|
private long serverHandledStanzasCount = 0;
|
|
|
|
private long clientHandledStanzasCount = 0;
|
|
|
|
private BlockingQueue<Packet> unacknowledgedStanzas;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This listeners are invoked for every stanza that got acknowledged.
|
|
|
|
* <p>
|
|
|
|
* We use a {@link ConccurrentLinkedQueue} here in order to allow the listeners to remove
|
|
|
|
* themselves after they have been invoked.
|
|
|
|
* </p>
|
|
|
|
*/
|
|
|
|
private final Collection<PacketListener> stanzaAcknowledgedListeners = new ConcurrentLinkedQueue<PacketListener>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This listeners are invoked for a acknowledged stanza that has the given stanza ID. They will
|
|
|
|
* only be invoked once and automatically removed after that.
|
|
|
|
*/
|
|
|
|
private final Map<String, PacketListener> idStanzaAcknowledgedListeners = new ConcurrentHashMap<String, PacketListener>();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Predicates that determine if an stream management ack should be requested from the server.
|
|
|
|
* <p>
|
|
|
|
* We use a linked hash set here, so that the order how the predicates are added matches the
|
|
|
|
* order in which they are invoked in order to determine if an ack request should be send or not.
|
|
|
|
* </p>
|
|
|
|
*/
|
|
|
|
private final Set<PacketFilter> requestAckPredicates = new LinkedHashSet<PacketFilter>();
|
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
|
2014-04-09 12:16:44 +02:00
|
|
|
* {@link #XMPPTCPConnection(ConnectionConfiguration)} constructor.<p>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2014-04-09 12:16:44 +02:00
|
|
|
* Note that XMPPTCPConnection 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.
|
|
|
|
*/
|
2014-04-09 12:16:44 +02:00
|
|
|
public XMPPTCPConnection(String serviceName, CallbackHandler callbackHandler) {
|
2007-11-30 20:40:31 +01:00
|
|
|
// Create the configuration for this new connection
|
2010-02-09 12:55:56 +01:00
|
|
|
super(new ConnectionConfiguration(serviceName));
|
|
|
|
config.setCallbackHandler(callbackHandler);
|
2007-11-30 20:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-09 12:16:44 +02:00
|
|
|
* Creates a new XMPP connection in the same way {@link #XMPPTCPConnection(String,CallbackHandler)} does, but
|
2007-11-30 20:40:31 +01:00
|
|
|
* 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
|
|
|
*/
|
2014-04-09 12:16:44 +02:00
|
|
|
public XMPPTCPConnection(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));
|
2007-11-30 20:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-09 12:16:44 +02:00
|
|
|
* Creates a new XMPP connection in the same way {@link #XMPPTCPConnection(ConnectionConfiguration,CallbackHandler)} does, but
|
2007-11-30 20:40:31 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-04-09 12:16:44 +02:00
|
|
|
public XMPPTCPConnection(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
|
2014-04-09 12:16:44 +02:00
|
|
|
* {@link #XMPPTCPConnection(String)} constructor is a better approach.<p>
|
2007-03-20 23:09:15 +01:00
|
|
|
* <p/>
|
2014-04-09 12:16:44 +02:00
|
|
|
* Note that XMPPTCPConnection 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
|
|
|
*/
|
2014-04-09 12:16:44 +02:00
|
|
|
public XMPPTCPConnection(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
|
|
|
}
|
|
|
|
|
2014-05-26 22:02:04 +02:00
|
|
|
@Override
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-26 22:02:04 +02:00
|
|
|
@Override
|
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
|
|
|
}
|
|
|
|
|
2013-06-22 19:01:40 +02:00
|
|
|
/**
|
|
|
|
* Install a parsing exception callback, which will be invoked once an exception is encountered while parsing a
|
|
|
|
* stanza
|
|
|
|
*
|
|
|
|
* @param callback the callback to install
|
|
|
|
*/
|
|
|
|
public void setParsingExceptionCallback(ParsingExceptionCallback callback) {
|
|
|
|
parsingExceptionCallback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current active parsing exception callback.
|
|
|
|
*
|
|
|
|
* @return the active exception callback or null if there is none
|
|
|
|
*/
|
|
|
|
public ParsingExceptionCallback getParsingExceptionCallback() {
|
|
|
|
return parsingExceptionCallback;
|
|
|
|
}
|
|
|
|
|
2011-03-28 15:13:41 +02:00
|
|
|
@Override
|
2014-08-01 10:34:47 +02:00
|
|
|
public synchronized void login(String username, String password, String resource) throws XMPPException, SmackException, IOException {
|
2003-01-13 17:58:47 +01:00
|
|
|
if (!isConnected()) {
|
2014-03-12 11:50:05 +01:00
|
|
|
throw new NotConnectedException();
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
if (authenticated && !disconnectedButResumeable) {
|
2014-03-12 11:50:05 +01:00
|
|
|
throw new AlreadyLoggedInException();
|
2003-03-09 00:09:48 +01:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
|
|
|
|
// Wait with SASL auth until the SASL mechanisms have been received
|
|
|
|
saslFeatureReceived.checkIfSuccessOrWaitOrThrow();
|
|
|
|
|
2004-11-15 09:08:23 +01:00
|
|
|
// Do partial version of nameprep on the username.
|
2014-08-12 19:15:01 +02:00
|
|
|
if (username != null) {
|
|
|
|
username = username.toLowerCase(Locale.US).trim();
|
|
|
|
}
|
2003-01-16 02:01:27 +01:00
|
|
|
|
2014-02-26 21:57:42 +01:00
|
|
|
if (saslAuthentication.hasNonAnonymousAuthentication()) {
|
2005-08-27 04:33:08 +02:00
|
|
|
// Authenticate using SASL
|
2008-10-24 07:17:50 +02:00
|
|
|
if (password != null) {
|
2014-04-27 12:27:12 +02:00
|
|
|
saslAuthentication.authenticate(username, password, resource);
|
2008-10-24 07:17:50 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-04-27 12:27:12 +02:00
|
|
|
saslAuthentication.authenticate(resource, config.getCallbackHandler());
|
2008-10-24 07:17:50 +02:00
|
|
|
}
|
2014-02-26 21:57:42 +01:00
|
|
|
} else {
|
2014-08-01 10:34:47 +02:00
|
|
|
throw new SmackException("No non-anonymous SASL authentication mechanism available");
|
2003-01-15 15:54:11 +01:00
|
|
|
}
|
|
|
|
|
2014-04-27 12:27:12 +02:00
|
|
|
// If compression is enabled then request the server to use stream compression. XEP-170
|
|
|
|
// recommends to perform stream compression before resource binding.
|
|
|
|
if (config.isCompressionEnabled()) {
|
|
|
|
useCompression();
|
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
if (isSmResumptionPossible()) {
|
|
|
|
smResumedSyncPoint.sendAndWaitForResponse(new Resume(clientHandledStanzasCount, smSessionId));
|
|
|
|
if (smResumedSyncPoint.wasSuccessful()) {
|
|
|
|
// We successfully resumed the stream, be done here
|
|
|
|
afterSuccessfulLogin(false, true);
|
|
|
|
return;
|
2003-08-12 21:30:51 +02:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
// SM resumption failed, what Smack does here is to report success of
|
|
|
|
// lastFeaturesReceived in case of sm resumption was answered with 'failed' so that
|
|
|
|
// normal resource binding can be tried.
|
|
|
|
LOGGER.fine("Stream resumption failed, continuing with normal stream establishment process");
|
2003-08-12 21:30:51 +02:00
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
bindResourceAndEstablishSession(resource);
|
2010-08-15 18:32:09 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
List<Packet> previouslyUnackedStanzas = new LinkedList<Packet>();
|
|
|
|
if (unacknowledgedStanzas != null) {
|
|
|
|
// There was a previous connection with SM enabled but that was either not resumable or
|
|
|
|
// failed to resume. Make sure that we (re-)send the unacknowledged stanzas.
|
|
|
|
unacknowledgedStanzas.drainTo(previouslyUnackedStanzas);
|
|
|
|
}
|
|
|
|
if (isSmAvailable() && useSm) {
|
|
|
|
// Remove what is maybe left from previously stream managed sessions
|
|
|
|
unacknowledgedStanzas = new ArrayBlockingQueue<Packet>(QUEUE_SIZE);
|
|
|
|
clientHandledStanzasCount = 0;
|
|
|
|
serverHandledStanzasCount = 0;
|
|
|
|
// XEP-198 3. Enabling Stream Management. If the server response to 'Enable' is 'Failed'
|
|
|
|
// then this is a non recoverable error and we therefore throw an exception.
|
|
|
|
smEnabledSyncPoint.sendAndWaitForResponseOrThrow(new Enable(useSmResumption, smClientMaxResumptionTime));
|
|
|
|
synchronized (requestAckPredicates) {
|
|
|
|
if (requestAckPredicates.isEmpty()) {
|
|
|
|
// Assure that we have at lest one predicate set up that so that we request acks
|
|
|
|
// for the server and eventually flush some stanzas from the unacknowledged
|
|
|
|
// stanza queue
|
|
|
|
requestAckPredicates.add(Predicate.forMessagesOrAfter5Stanzas());
|
|
|
|
}
|
|
|
|
}
|
2007-11-14 17:27:47 +01:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
// (Re-)send the stanzas *after* we tried to enable SM
|
|
|
|
for (Packet stanza : previouslyUnackedStanzas) {
|
|
|
|
sendPacketInternal(stanza);
|
2014-08-18 18:48:57 +02:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
|
|
|
|
// Stores the authentication for future reconnection
|
|
|
|
setLoginInfo(username, password, resource);
|
|
|
|
afterSuccessfulLogin(false, false);
|
2007-11-14 17:27:47 +01:00
|
|
|
}
|
2003-02-10 06:01:01 +01:00
|
|
|
|
2011-03-28 15:13:41 +02:00
|
|
|
@Override
|
2014-08-01 10:34:47 +02:00
|
|
|
public synchronized void loginAnonymously() throws XMPPException, SmackException, IOException {
|
2003-08-12 21:30:51 +02:00
|
|
|
if (!isConnected()) {
|
2014-03-12 11:50:05 +01:00
|
|
|
throw new NotConnectedException();
|
2003-08-12 21:30:51 +02:00
|
|
|
}
|
|
|
|
if (authenticated) {
|
2014-03-12 11:50:05 +01:00
|
|
|
throw new AlreadyLoggedInException();
|
2003-08-12 21:30:51 +02:00
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
// Wait with SASL auth until the SASL mechanisms have been received
|
|
|
|
saslFeatureReceived.checkIfSuccessOrWaitOrThrow();
|
|
|
|
|
2014-02-26 21:57:42 +01:00
|
|
|
if (saslAuthentication.hasAnonymousAuthentication()) {
|
2014-04-27 12:27:12 +02:00
|
|
|
saslAuthentication.authenticateAnonymously();
|
2003-08-12 21:30:51 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-08-01 10:34:47 +02:00
|
|
|
throw new SmackException("No anonymous SASL authentication mechanism available");
|
2003-08-12 21:30:51 +02:00
|
|
|
}
|
2005-09-06 00:06:40 +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();
|
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
bindResourceAndEstablishSession(null);
|
2003-08-12 21:30:51 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
afterSuccessfulLogin(true, false);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2014-05-26 22:02:04 +02:00
|
|
|
@Override
|
2003-01-13 17:58:47 +01:00
|
|
|
public boolean isConnected() {
|
|
|
|
return connected;
|
|
|
|
}
|
|
|
|
|
2014-05-26 22:02:04 +02:00
|
|
|
@Override
|
2003-09-18 22:39:14 +02:00
|
|
|
public boolean isSecureConnection() {
|
2014-05-26 22:02:04 +02:00
|
|
|
return usingTLS;
|
2003-09-18 22:39:14 +02:00
|
|
|
}
|
|
|
|
|
2013-01-06 15:02:44 +01:00
|
|
|
public boolean isSocketClosed() {
|
|
|
|
return socketClosed;
|
|
|
|
}
|
|
|
|
|
2014-05-26 22:02:04 +02:00
|
|
|
@Override
|
2003-03-09 00:09:48 +01:00
|
|
|
public boolean isAuthenticated() {
|
|
|
|
return authenticated;
|
|
|
|
}
|
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
2014-05-04 20:31:25 +02:00
|
|
|
* Shuts the current connection down. After this method returns, the connection must be ready
|
|
|
|
* for re-use by connect.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2014-05-04 20:31:25 +02:00
|
|
|
@Override
|
|
|
|
protected void shutdown() {
|
2014-09-11 09:49:16 +02:00
|
|
|
shutdown(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs an unclean disconnect and shutdown of the connection. Does not send a closing stream stanza.
|
|
|
|
*/
|
|
|
|
public void instantShutdown() {
|
|
|
|
shutdown(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void shutdown(boolean instant) {
|
2013-01-06 15:02:44 +01:00
|
|
|
if (packetReader != null) {
|
|
|
|
packetReader.shutdown();
|
|
|
|
}
|
|
|
|
if (packetWriter != null) {
|
2014-09-11 09:49:16 +02:00
|
|
|
packetWriter.shutdown(instant);
|
2013-01-06 15:02:44 +01:00
|
|
|
}
|
|
|
|
|
2013-06-22 19:02:09 +02:00
|
|
|
// Set socketClosed to true. This will cause the PacketReader
|
|
|
|
// and PacketWriter to ignore any Exceptions that are thrown
|
|
|
|
// because of a read/write from/to a closed stream.
|
|
|
|
// It is *important* that this is done before socket.close()!
|
2013-01-06 15:02:44 +01:00
|
|
|
socketClosed = true;
|
|
|
|
try {
|
|
|
|
socket.close();
|
|
|
|
} catch (Exception e) {
|
2014-04-23 09:37:16 +02:00
|
|
|
LOGGER.log(Level.WARNING, "shutdown", e);
|
2013-01-06 15:02:44 +01:00
|
|
|
}
|
|
|
|
|
2014-05-04 20:31:25 +02:00
|
|
|
setWasAuthenticated(authenticated);
|
2014-09-11 09:49:16 +02:00
|
|
|
// If we are able to resume the stream, then don't set
|
|
|
|
// connected/authenticated/usingTLS to false since we like behave like we are still
|
|
|
|
// connected (e.g. sendPacket should not throw a NotConnectedException).
|
|
|
|
if (isSmResumptionPossible() && instant) {
|
|
|
|
disconnectedButResumeable = true;
|
|
|
|
} else {
|
|
|
|
authenticated = false;
|
|
|
|
connected = false;
|
|
|
|
usingTLS = false;
|
|
|
|
disconnectedButResumeable = false;
|
|
|
|
}
|
2013-06-22 19:02:09 +02:00
|
|
|
reader = null;
|
|
|
|
writer = null;
|
2014-09-11 09:49:16 +02:00
|
|
|
maybeCompressFeaturesReceived.init();
|
|
|
|
compressSyncPoint.init();
|
|
|
|
smResumedSyncPoint.init();
|
|
|
|
smEnabledSyncPoint.init();
|
|
|
|
initalOpenStreamSend.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void send(PlainStreamElement element) throws NotConnectedException {
|
|
|
|
packetWriter.sendStreamElement(element);
|
2006-09-14 21:21:38 +02:00
|
|
|
}
|
2006-09-16 00:42:06 +02:00
|
|
|
|
2014-05-15 15:04:46 +02:00
|
|
|
@Override
|
|
|
|
protected void sendPacketInternal(Packet packet) throws NotConnectedException {
|
2014-09-11 09:49:16 +02:00
|
|
|
packetWriter.sendStreamElement(packet);
|
|
|
|
if (isSmEnabled()) {
|
|
|
|
for (PacketFilter requestAckPredicate : requestAckPredicates) {
|
|
|
|
if (requestAckPredicate.accept(packet)) {
|
|
|
|
requestSmAcknowledgementInternal();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
2014-03-12 11:50:05 +01:00
|
|
|
private void connectUsingConfiguration(ConnectionConfiguration config) throws SmackException, IOException {
|
2014-03-18 17:29:38 +01:00
|
|
|
try {
|
2014-05-15 15:04:46 +02:00
|
|
|
maybeResolveDns();
|
2014-03-18 17:29:38 +01:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
throw new SmackException(e);
|
|
|
|
}
|
2013-03-18 09:53:11 +01:00
|
|
|
Iterator<HostAddress> it = config.getHostAddresses().iterator();
|
|
|
|
List<HostAddress> failedAddresses = new LinkedList<HostAddress>();
|
|
|
|
while (it.hasNext()) {
|
2014-07-21 16:37:31 +02:00
|
|
|
Exception exception = null;
|
2013-03-18 09:53:11 +01:00
|
|
|
HostAddress hostAddress = it.next();
|
|
|
|
String host = hostAddress.getFQDN();
|
|
|
|
int port = hostAddress.getPort();
|
|
|
|
try {
|
|
|
|
if (config.getSocketFactory() == null) {
|
|
|
|
this.socket = new Socket(host, port);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.socket = config.getSocketFactory().createSocket(host, port);
|
|
|
|
}
|
2014-03-12 11:50:05 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
exception = e;
|
2006-09-15 23:51:08 +02:00
|
|
|
}
|
2013-03-18 09:53:11 +01:00
|
|
|
if (exception == null) {
|
|
|
|
// We found a host to connect to, break here
|
2014-07-21 16:37:31 +02:00
|
|
|
this.host = host;
|
|
|
|
this.port = port;
|
2013-03-18 09:53:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
hostAddress.setException(exception);
|
|
|
|
failedAddresses.add(hostAddress);
|
|
|
|
if (!it.hasNext()) {
|
|
|
|
// There are no more host addresses to try
|
|
|
|
// throw an exception and report all tried
|
|
|
|
// HostAddresses in the exception
|
2014-03-12 11:50:05 +01:00
|
|
|
throw new ConnectionException(failedAddresses);
|
2006-09-15 23:51:08 +02:00
|
|
|
}
|
2003-12-19 18:54:38 +01:00
|
|
|
}
|
2013-01-06 15:02:44 +01:00
|
|
|
socketClosed = false;
|
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.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws SmackException if the server failes to respond back or if there is anther error.
|
|
|
|
* @throws IOException
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2014-03-12 11:50:05 +01:00
|
|
|
private void initConnection() throws SmackException, IOException {
|
2006-11-10 20:06:33 +01:00
|
|
|
boolean isFirstInitialization = packetReader == null || packetWriter == null;
|
2013-03-18 20:58:48 +01:00
|
|
|
compressionHandler = null;
|
2006-11-10 20:06:33 +01:00
|
|
|
|
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) {
|
2014-05-26 22:02:04 +02:00
|
|
|
packetWriter = new PacketWriter();
|
|
|
|
packetReader = new PacketReader();
|
2006-09-14 21:21:38 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
2004-09-24 04:15:55 +02:00
|
|
|
// Start the packet writer. This will open a XMPP stream to the server
|
2014-09-11 09:49:16 +02:00
|
|
|
packetWriter.init();
|
2004-09-24 04:15:55 +02:00
|
|
|
// Start the packet reader. The startup() method will block until we
|
2014-09-11 09:49:16 +02:00
|
|
|
// get an opening stream packet back from server
|
|
|
|
packetReader.init();
|
2004-09-24 04:15:55 +02:00
|
|
|
|
|
|
|
// Make note of the fact that we're now connected.
|
|
|
|
connected = true;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-09-24 04:15:55 +02:00
|
|
|
}
|
2014-04-28 17:35:07 +02:00
|
|
|
catch (SmackException ex) {
|
2014-09-11 09:49:16 +02:00
|
|
|
// An exception occurred in setting up the connection. Note that
|
|
|
|
// it's important here that we do an instant shutdown here, as this
|
|
|
|
// will not send a closing stream element, which will destroy
|
|
|
|
// Stream Management state on the server, which is not what we want.
|
|
|
|
instantShutdown();
|
|
|
|
// Everything stopped. Now throw the exception.
|
2014-05-04 20:31:25 +02:00
|
|
|
throw ex;
|
2005-08-27 04:33:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
private void initReaderAndWriter() throws IOException, SmackException {
|
2005-08-27 04:33:08 +02:00
|
|
|
try {
|
2014-09-11 09:49:16 +02:00
|
|
|
InputStream is = socket.getInputStream();
|
|
|
|
OutputStream os = socket.getOutputStream();
|
|
|
|
if (compressionHandler != null) {
|
|
|
|
is = compressionHandler.getInputStream(is);
|
|
|
|
os = compressionHandler.getOutputStream(os);
|
2006-01-16 18:34:56 +01:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
// OutputStreamWriter is already buffered, no need to wrap it into a BufferedWriter
|
|
|
|
writer = new OutputStreamWriter(os, "UTF-8");
|
|
|
|
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
|
|
|
throw e;
|
2005-08-27 04:33:08 +02:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
catch (Exception e) {
|
|
|
|
throw new SmackException(e);
|
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
|
|
|
/**
|
|
|
|
* 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
|
2014-04-22 22:45:20 +02:00
|
|
|
* connection has finished the handshake or an error occurred while securing the connection.
|
2014-05-29 09:21:04 +02:00
|
|
|
* @throws IOException
|
|
|
|
* @throws CertificateException
|
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
|
* @throws NoSuchProviderException
|
|
|
|
* @throws KeyStoreException
|
|
|
|
* @throws UnrecoverableKeyException
|
|
|
|
* @throws KeyManagementException
|
2014-09-11 09:49:16 +02:00
|
|
|
* @throws SmackException
|
2006-09-15 23:51:08 +02:00
|
|
|
* @throws Exception if an exception occurs.
|
2005-08-27 04:33:08 +02:00
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
private void proceedTLSReceived() throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException, NoSuchProviderException, UnrecoverableKeyException, KeyManagementException, SmackException {
|
2013-02-26 09:44:17 +01:00
|
|
|
SSLContext context = this.config.getCustomSSLContext();
|
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;
|
2013-02-26 09:44:17 +01:00
|
|
|
} else if (context == null) {
|
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 {
|
2012-10-26 12:47:55 +02: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
|
|
|
}
|
|
|
|
|
2014-04-22 22:45:20 +02:00
|
|
|
// If the user didn't specify a SSLContext, use the default one
|
2013-02-26 09:44:17 +01:00
|
|
|
if (context == null) {
|
|
|
|
context = SSLContext.getInstance("TLS");
|
2014-02-10 12:07:39 +01:00
|
|
|
context.init(kms, null, new java.security.SecureRandom());
|
2013-02-26 09:44:17 +01:00
|
|
|
}
|
2005-09-06 00:06:40 +02:00
|
|
|
Socket plain = socket;
|
|
|
|
// Secure the plain connection
|
|
|
|
socket = context.getSocketFactory().createSocket(plain,
|
2013-01-31 23:31:31 +01:00
|
|
|
plain.getInetAddress().getHostAddress(), plain.getPort(), true);
|
2005-09-06 00:06:40 +02:00
|
|
|
// Initialize the reader and writer with the new secured version
|
|
|
|
initReaderAndWriter();
|
2014-04-28 08:29:12 +02:00
|
|
|
|
2014-05-29 09:21:04 +02:00
|
|
|
final SSLSocket sslSocket = (SSLSocket) socket;
|
|
|
|
TLSUtils.setEnabledProtocolsAndCiphers(sslSocket, config.getEnabledSSLProtocols(), config.getEnabledSSLCiphers());
|
|
|
|
|
|
|
|
// Proceed to do the handshake
|
|
|
|
sslSocket.startHandshake();
|
|
|
|
|
2014-07-21 18:42:44 +02:00
|
|
|
final HostnameVerifier verifier = getConfiguration().getHostnameVerifier();
|
2014-07-26 21:55:52 +02:00
|
|
|
if (verifier == null) {
|
|
|
|
throw new IllegalStateException("No HostnameVerifier set. Use connectionConfiguration.setHostnameVerifier() to configure.");
|
|
|
|
} else if (!verifier.verify(getServiceName(), sslSocket.getSession())) {
|
2014-07-21 18:42:44 +02:00
|
|
|
throw new CertificateException("Hostname verification of certificate failed. Certificate does not authenticate " + getServiceName());
|
|
|
|
}
|
|
|
|
|
2005-09-06 00:06:40 +02:00
|
|
|
// Set that TLS was successful
|
|
|
|
usingTLS = true;
|
2006-01-16 18:34:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-26 10:26:41 +01:00
|
|
|
* Returns the compression handler that can be used for one compression methods offered by the server.
|
|
|
|
*
|
|
|
|
* @return a instance of XMPPInputOutputStream or null if no suitable instance was found
|
|
|
|
*
|
2006-01-16 18:34:56 +01:00
|
|
|
*/
|
2013-02-26 10:26:41 +01:00
|
|
|
private XMPPInputOutputStream maybeGetCompressionHandler() {
|
2014-09-11 09:49:16 +02:00
|
|
|
Compress.Feature compression = getFeature(Compress.Feature.ELEMENT, Compress.NAMESPACE);
|
|
|
|
if (compression == null) {
|
|
|
|
// Server does not support compression
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
for (XMPPInputOutputStream handler : SmackConfiguration.getCompresionHandlers()) {
|
2013-02-26 10:26:41 +01:00
|
|
|
String method = handler.getCompressionMethod();
|
2014-09-11 09:49:16 +02:00
|
|
|
if (compression.getMethods().contains(method))
|
2013-02-26 10:26:41 +01:00
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
return null;
|
2006-01-16 18:34:56 +01:00
|
|
|
}
|
|
|
|
|
2014-05-26 22:02:04 +02:00
|
|
|
@Override
|
2006-01-16 18:34:56 +01:00
|
|
|
public boolean isUsingCompression() {
|
2014-09-11 09:49:16 +02:00
|
|
|
return compressionHandler != null && compressSyncPoint.wasSuccessful();
|
2006-01-16 18:34:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-11 09:49:16 +02:00
|
|
|
* <p>
|
2006-01-16 18:34:56 +01:00
|
|
|
* 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
|
2014-04-23 09:50:39 +02:00
|
|
|
* un/compress network data so under high load the server performance might be affected.
|
2014-09-11 09:49:16 +02:00
|
|
|
* </p>
|
2014-04-23 09:50:39 +02: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
|
2014-09-11 09:49:16 +02:00
|
|
|
* before authentication took place.
|
|
|
|
* </p>
|
2006-01-16 18:34:56 +01:00
|
|
|
*
|
2014-09-11 09:49:16 +02:00
|
|
|
* @throws NotConnectedException
|
|
|
|
* @throws XMPPException
|
|
|
|
* @throws NoResponseException
|
2006-01-16 18:34:56 +01:00
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
private void useCompression() throws NotConnectedException, NoResponseException, XMPPException {
|
|
|
|
maybeCompressFeaturesReceived.checkIfSuccessOrWait();
|
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
|
2013-02-26 10:26:41 +01:00
|
|
|
if ((compressionHandler = maybeGetCompressionHandler()) != null) {
|
2014-09-11 09:49:16 +02:00
|
|
|
compressSyncPoint.sendAndWaitForResponseOrThrow(new Compress(compressionHandler.getCompressionMethod()));
|
|
|
|
} else {
|
|
|
|
LOGGER.warning("Could not enable compression because no matching handler/method pair was found");
|
2014-04-27 12:27:12 +02:00
|
|
|
}
|
2006-01-16 18:34:56 +01:00
|
|
|
}
|
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.
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws SmackException
|
|
|
|
* @throws IOException
|
2006-09-14 21:21:38 +02:00
|
|
|
*/
|
2014-04-27 12:27:12 +02:00
|
|
|
@Override
|
2014-05-15 15:04:46 +02:00
|
|
|
protected void connectInternal() throws SmackException, IOException, XMPPException {
|
2014-09-11 09:49:16 +02:00
|
|
|
if (connected && !disconnectedButResumeable) {
|
|
|
|
throw new AlreadyConnectedException();
|
|
|
|
}
|
2013-02-26 10:26:41 +01:00
|
|
|
// Establishes the connection, readers and writers
|
2010-02-09 12:55:56 +01:00
|
|
|
connectUsingConfiguration(config);
|
2014-09-11 09:49:16 +02:00
|
|
|
callConnectionConnectedListener();
|
|
|
|
|
2013-05-18 00:05:40 +02:00
|
|
|
// Automatically makes the login if the user was previously connected successfully
|
2006-09-14 21:21:38 +02:00
|
|
|
// to the server and the connection was terminated abruptly
|
2014-09-11 09:49:16 +02:00
|
|
|
if (wasAuthenticated) {
|
2006-09-14 21:21:38 +02:00
|
|
|
// Make the login
|
2013-05-18 00:05:24 +02:00
|
|
|
if (isAnonymous()) {
|
|
|
|
// Make the anonymous login
|
|
|
|
loginAnonymously();
|
2007-03-20 23:09:15 +01:00
|
|
|
}
|
2013-05-18 00:05:24 +02:00
|
|
|
else {
|
|
|
|
login(config.getUsername(), config.getPassword(), config.getResource());
|
2006-09-14 21:21:38 +02:00
|
|
|
}
|
2013-05-18 00:05:24 +02:00
|
|
|
notifyReconnection();
|
2006-09-14 21:21:38 +02:00
|
|
|
}
|
|
|
|
}
|
2006-09-16 00:42:06 +02:00
|
|
|
|
2013-03-18 20:58:48 +01:00
|
|
|
/**
|
|
|
|
* Sends out a notification that there was an error with the connection
|
|
|
|
* and closes the connection. Also prints the stack trace of the given exception
|
|
|
|
*
|
|
|
|
* @param e the exception that causes the connection close event.
|
|
|
|
*/
|
2014-05-26 22:02:04 +02:00
|
|
|
private synchronized void notifyConnectionError(Exception e) {
|
2013-03-18 20:58:48 +01:00
|
|
|
// Listeners were already notified of the exception, return right here.
|
2014-01-17 11:40:10 +01:00
|
|
|
if ((packetReader == null || packetReader.done) &&
|
2014-09-11 09:49:16 +02:00
|
|
|
(packetWriter == null || packetWriter.done())) return;
|
2013-03-18 20:58:48 +01:00
|
|
|
|
|
|
|
// Closes the connection temporary. A reconnection is possible
|
2014-09-11 09:49:16 +02:00
|
|
|
instantShutdown();
|
2014-05-04 20:31:25 +02:00
|
|
|
|
2013-03-18 20:58:48 +01:00
|
|
|
// Notify connection listeners of the error.
|
2014-03-14 01:48:33 +01:00
|
|
|
callConnectionClosedOnErrorListener(e);
|
2013-03-18 20:58:48 +01:00
|
|
|
}
|
|
|
|
|
2014-05-26 22:02:04 +02:00
|
|
|
/**
|
|
|
|
* Sends a notification indicating that the connection was reconnected successfully.
|
|
|
|
*/
|
|
|
|
private void notifyReconnection() {
|
|
|
|
// Notify connection listeners of the reconnection.
|
|
|
|
for (ConnectionListener listener : getConnectionListeners()) {
|
|
|
|
try {
|
|
|
|
listener.reconnectionSuccessful();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// Catch and print any exception so we can recover
|
|
|
|
// from a faulty listener
|
|
|
|
LOGGER.log(Level.WARNING, "notifyReconnection()", e);
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 15:04:46 +02:00
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
/**
|
|
|
|
* For unit testing purposes
|
|
|
|
*
|
|
|
|
* @param writer
|
|
|
|
*/
|
|
|
|
protected void setWriter(Writer writer) {
|
|
|
|
this.writer = writer;
|
|
|
|
}
|
2014-05-15 15:04:46 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
@Override
|
|
|
|
protected void afterFeaturesReceived() throws SecurityRequiredException, NotConnectedException {
|
|
|
|
StartTls startTlsFeature = getFeature(StartTls.ELEMENT, StartTls.NAMESPACE);
|
|
|
|
if (startTlsFeature != null) {
|
|
|
|
if (startTlsFeature.required() && config.getSecurityMode() == SecurityMode.disabled) {
|
|
|
|
notifyConnectionError(new SecurityRequiredException(
|
|
|
|
"TLS required by server but not allowed by connection configuration"));
|
|
|
|
return;
|
|
|
|
}
|
2014-05-15 15:04:46 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
if (config.getSecurityMode() == ConnectionConfiguration.SecurityMode.disabled) {
|
|
|
|
// Do not secure the connection using TLS since TLS was disabled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
send(new StartTls());
|
|
|
|
}
|
|
|
|
// If TLS is required but the server doesn't offer it, disconnect
|
|
|
|
// from the server and throw an error. First check if we've already negotiated TLS
|
|
|
|
// and are secure, however (features get parsed a second time after TLS is established).
|
|
|
|
if (!isSecureConnection() && startTlsFeature == null
|
|
|
|
&& getConfiguration().getSecurityMode() == SecurityMode.required) {
|
|
|
|
throw new SecurityRequiredException();
|
|
|
|
}
|
2014-05-15 15:04:46 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
if (getSASLAuthentication().authenticationSuccessful()) {
|
|
|
|
// If we have received features after the SASL has been successfully completed, then we
|
|
|
|
// have also *maybe* received, as it is an optional feature, the compression feature
|
|
|
|
// from the server.
|
|
|
|
maybeCompressFeaturesReceived.reportSuccess();
|
|
|
|
}
|
|
|
|
}
|
2014-05-15 15:04:46 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
/**
|
|
|
|
* Resets the parser using the latest connection's reader. Reseting the parser is necessary
|
|
|
|
* when the plain connection has been secured or when a new opening stream element is going
|
|
|
|
* to be sent by the server.
|
|
|
|
*
|
|
|
|
* @throws SmackException if the parser could not be reset.
|
|
|
|
*/
|
|
|
|
void openStream() throws SmackException {
|
|
|
|
send(new StreamOpen(getServiceName()));
|
|
|
|
try {
|
|
|
|
packetReader.parser = PacketParserUtils.newXmppParser(reader);
|
|
|
|
}
|
|
|
|
catch (XmlPullParserException e) {
|
|
|
|
throw new SmackException(e);
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected class PacketReader {
|
|
|
|
|
|
|
|
private Thread readerThread;
|
|
|
|
|
|
|
|
XmlPullParser parser;
|
|
|
|
|
|
|
|
private volatile boolean done;
|
2014-05-15 15:04:46 +02:00
|
|
|
|
2014-05-26 22:02:04 +02:00
|
|
|
/**
|
|
|
|
* Initializes the reader in order to be used. The reader is initialized during the
|
|
|
|
* first connection and when reconnecting due to an abruptly disconnection.
|
|
|
|
*
|
|
|
|
* @throws SmackException if the parser could not be reset.
|
|
|
|
*/
|
|
|
|
void init() throws SmackException {
|
|
|
|
done = false;
|
|
|
|
|
|
|
|
readerThread = new Thread() {
|
|
|
|
public void run() {
|
2014-09-11 09:49:16 +02:00
|
|
|
parsePackets();
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
readerThread.setName("Smack Packet Reader (" + getConnectionCounter() + ")");
|
|
|
|
readerThread.setDaemon(true);
|
|
|
|
readerThread.start();
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
2014-05-15 15:04:46 +02:00
|
|
|
|
2014-05-26 22:02:04 +02:00
|
|
|
/**
|
|
|
|
* Shuts the packet reader down. This method simply sets the 'done' flag to true.
|
|
|
|
*/
|
|
|
|
void shutdown() {
|
|
|
|
done = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse top-level packets in order to process them further.
|
|
|
|
*
|
|
|
|
* @param thread the thread that is being used by the reader to parse incoming packets.
|
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
private void parsePackets() {
|
2014-05-26 22:02:04 +02:00
|
|
|
try {
|
2014-09-11 09:49:16 +02:00
|
|
|
initalOpenStreamSend.checkIfSuccessOrWait();
|
2014-05-26 22:02:04 +02:00
|
|
|
int eventType = parser.getEventType();
|
|
|
|
do {
|
|
|
|
if (eventType == XmlPullParser.START_TAG) {
|
2014-09-11 09:49:16 +02:00
|
|
|
final String name = parser.getName();
|
|
|
|
switch (name) {
|
|
|
|
case Message.ELEMENT:
|
|
|
|
case IQ.ELEMENT:
|
|
|
|
case Presence.ELEMENT:
|
|
|
|
int parserDepth = parser.getDepth();
|
|
|
|
Packet packet;
|
|
|
|
try {
|
|
|
|
packet = PacketParserUtils.parseStanza(parser,
|
|
|
|
XMPPTCPConnection.this);
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
ParsingExceptionCallback callback = getParsingExceptionCallback();
|
|
|
|
CharSequence content = PacketParserUtils.parseContentDepth(parser,
|
|
|
|
parserDepth);
|
|
|
|
UnparsablePacket message = new UnparsablePacket(content, e);
|
|
|
|
if (callback != null) {
|
|
|
|
callback.handleUnparsablePacket(message);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} finally {
|
|
|
|
clientHandledStanzasCount = SMUtils.incrementHeight(clientHandledStanzasCount);
|
|
|
|
reportStanzaReceived();
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
|
|
|
processPacket(packet);
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
case "stream":
|
|
|
|
// We found an opening stream.
|
2014-05-26 22:02:04 +02:00
|
|
|
if ("jabber:client".equals(parser.getNamespace(null))) {
|
|
|
|
// Get the connection id.
|
|
|
|
for (int i=0; i<parser.getAttributeCount(); i++) {
|
|
|
|
if (parser.getAttributeName(i).equals("id")) {
|
|
|
|
// Save the connectionID
|
|
|
|
connectionID = parser.getAttributeValue(i);
|
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
// According to RFC 6120 4.7.1 response
|
|
|
|
// stream headers in c2s and s2s of the
|
|
|
|
// receiving entity MUST include the 'from'
|
|
|
|
// attribute.
|
2014-05-26 22:02:04 +02:00
|
|
|
else if (parser.getAttributeName(i).equals("from")) {
|
|
|
|
// Use the server name that the server says that it is.
|
|
|
|
setServiceName(parser.getAttributeValue(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
case "error":
|
2014-05-26 22:02:04 +02:00
|
|
|
throw new StreamErrorException(PacketParserUtils.parseStreamError(parser));
|
2014-09-11 09:49:16 +02:00
|
|
|
case "features":
|
2014-05-26 22:02:04 +02:00
|
|
|
parseFeatures(parser);
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
case "proceed":
|
2014-05-29 09:21:04 +02:00
|
|
|
try {
|
|
|
|
// Secure the connection by negotiating TLS
|
|
|
|
proceedTLSReceived();
|
2014-09-11 09:49:16 +02:00
|
|
|
// Send a new opening stream to the server
|
|
|
|
openStream();
|
2014-05-29 09:21:04 +02:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2014-09-11 09:49:16 +02:00
|
|
|
// We report any failure regarding TLS in the second stage of XMPP
|
|
|
|
// connection establishment, namely the SASL authentication
|
|
|
|
saslFeatureReceived.reportFailure(new SmackException(e));
|
2014-05-29 09:21:04 +02:00
|
|
|
throw e;
|
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
case "failure":
|
2014-05-26 22:02:04 +02:00
|
|
|
String namespace = parser.getNamespace(null);
|
2014-09-11 09:49:16 +02:00
|
|
|
switch (namespace) {
|
|
|
|
case "urn:ietf:params:xml:ns:xmpp-tls":
|
2014-05-26 22:02:04 +02:00
|
|
|
// TLS negotiation has failed. The server will close the connection
|
2014-09-11 09:49:16 +02:00
|
|
|
// TODO Parse failure stanza
|
|
|
|
throw new XMPPErrorException("TLS negotiation has failed", null);
|
|
|
|
case "http://jabber.org/protocol/compress":
|
2014-05-26 22:02:04 +02:00
|
|
|
// Stream compression has been denied. This is a recoverable
|
|
|
|
// situation. It is still possible to authenticate and
|
|
|
|
// use the connection but using an uncompressed connection
|
2014-09-11 09:49:16 +02:00
|
|
|
// TODO Parse failure stanza
|
|
|
|
compressSyncPoint.reportFailure(new XMPPErrorException(
|
|
|
|
"Could not establish compression", null));
|
|
|
|
break;
|
|
|
|
case SaslStreamElements.NAMESPACE:
|
2014-05-26 22:02:04 +02:00
|
|
|
// SASL authentication has failed. The server may close the connection
|
|
|
|
// depending on the number of retries
|
|
|
|
final SASLFailure failure = PacketParserUtils.parseSASLFailure(parser);
|
|
|
|
getSASLAuthentication().authenticationFailed(failure);
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
case Challenge.ELEMENT:
|
2014-05-26 22:02:04 +02:00
|
|
|
// The server is challenging the SASL authentication made by the client
|
|
|
|
String challengeData = parser.nextText();
|
|
|
|
getSASLAuthentication().challengeReceived(challengeData);
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
case Success.ELEMENT:
|
2014-08-01 10:34:47 +02:00
|
|
|
Success success = new Success(parser.nextText());
|
2014-05-26 22:02:04 +02:00
|
|
|
// We now need to bind a resource for the connection
|
|
|
|
// Open a new stream and wait for the response
|
2014-09-11 09:49:16 +02:00
|
|
|
openStream();
|
2014-05-26 22:02:04 +02:00
|
|
|
// The SASL authentication with the server was successful. The next step
|
|
|
|
// will be to bind the resource
|
2014-08-01 10:34:47 +02:00
|
|
|
getSASLAuthentication().authenticated(success);
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
case Compressed.ELEMENT:
|
2014-05-26 22:02:04 +02:00
|
|
|
// Server confirmed that it's possible to use stream compression. Start
|
|
|
|
// stream compression
|
2014-09-11 09:49:16 +02:00
|
|
|
// Initialize the reader and writer with the new compressed version
|
|
|
|
initReaderAndWriter();
|
|
|
|
// Send a new opening stream to the server
|
|
|
|
openStream();
|
|
|
|
// Notify that compression is being used
|
|
|
|
compressSyncPoint.reportSuccess();
|
|
|
|
break;
|
|
|
|
case Enabled.ELEMENT:
|
|
|
|
Enabled enabled = ParseStreamManagement.enabled(parser);
|
|
|
|
if (enabled.isResumeSet()) {
|
|
|
|
smSessionId = enabled.getId();
|
|
|
|
if (StringUtils.isNullOrEmpty(smSessionId)) {
|
|
|
|
XMPPErrorException xmppException = new XMPPErrorException(
|
|
|
|
"Stream Management 'enabled' element with resume attribute but without session id received",
|
|
|
|
new XMPPError(
|
|
|
|
XMPPError.Condition.bad_request));
|
|
|
|
smEnabledSyncPoint.reportFailure(xmppException);
|
|
|
|
throw xmppException;
|
|
|
|
}
|
|
|
|
smServerMaxResumptimTime = enabled.getMaxResumptionTime();
|
|
|
|
} else {
|
|
|
|
// Mark this a aon-resumable stream by setting smSessionId to null
|
|
|
|
smSessionId = null;
|
|
|
|
}
|
|
|
|
smEnabledSyncPoint.reportSuccess();
|
|
|
|
LOGGER.fine("Stream Management (XEP-198): succesfully enabled");
|
|
|
|
break;
|
|
|
|
case Failed.ELEMENT:
|
|
|
|
Failed failed = ParseStreamManagement.failed(parser);
|
|
|
|
XMPPError xmppError = failed.getXMPPError();
|
|
|
|
XMPPException xmppException = new XMPPErrorException("Stream Management failed", xmppError);
|
|
|
|
// If only XEP-198 would specify different failure elements for the SM
|
|
|
|
// enable and SM resume failure case. But this is not the case, so we
|
|
|
|
// need to determine if this is a 'Failed' response for either 'Enable'
|
|
|
|
// or 'Resume'.
|
|
|
|
if (smResumedSyncPoint.requestSent()) {
|
|
|
|
smResumedSyncPoint.reportFailure(xmppException);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!smEnabledSyncPoint.requestSent()) {
|
|
|
|
throw new IllegalStateException("Failed element received but SM was not previously enabled");
|
|
|
|
}
|
|
|
|
smEnabledSyncPoint.reportFailure(xmppException);
|
|
|
|
// Report success for last lastFeaturesReceived so that in case a
|
|
|
|
// failed resumption, we can continue with normal resource binding.
|
|
|
|
// See text of XEP-198 5. below Example 11.
|
|
|
|
lastFeaturesReceived.reportSuccess();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Resumed.ELEMENT:
|
|
|
|
Resumed resumed = ParseStreamManagement.resumed(parser);
|
|
|
|
if (!smSessionId.equals(resumed.getPrevId())) {
|
|
|
|
throw new StreamIdDoesNotMatchException(smSessionId, resumed.getPrevId());
|
|
|
|
}
|
|
|
|
// First, drop the stanzas already handled by the server
|
|
|
|
processHandledCount(resumed.getHandledCount());
|
|
|
|
// Then re-send what is left in the unacknowledged queue
|
|
|
|
List<Packet> stanzasToResend = new LinkedList<Packet>();
|
|
|
|
stanzasToResend.addAll(unacknowledgedStanzas);
|
|
|
|
for (Packet stanza : stanzasToResend) {
|
|
|
|
packetWriter.sendStreamElement(stanza);
|
|
|
|
}
|
|
|
|
smResumedSyncPoint.reportSuccess();
|
|
|
|
smEnabledSyncPoint.reportSuccess();
|
|
|
|
LOGGER.fine("Stream Management (XEP-198): Stream resumed");
|
|
|
|
break;
|
|
|
|
case AckAnswer.ELEMENT:
|
|
|
|
AckAnswer ackAnswer = ParseStreamManagement.ackAnswer(parser);
|
|
|
|
processHandledCount(ackAnswer.getHandledCount());
|
|
|
|
break;
|
|
|
|
case AckRequest.ELEMENT:
|
|
|
|
// AckRequest stanzas are trival, no need to parse them
|
|
|
|
if (smEnabledSyncPoint.wasSuccessful()) {
|
|
|
|
packetWriter.sendStreamElement(new AckAnswer(clientHandledStanzasCount));
|
|
|
|
} else {
|
|
|
|
LOGGER.warning("SM Ack Request received while SM is not enabled");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOGGER.warning("Unkown top level stream element: " + name);
|
|
|
|
break;
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (eventType == XmlPullParser.END_TAG) {
|
|
|
|
if (parser.getName().equals("stream")) {
|
|
|
|
// Disconnect the connection
|
|
|
|
disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
eventType = parser.next();
|
2014-09-11 09:49:16 +02:00
|
|
|
} while (!done && eventType != XmlPullParser.END_DOCUMENT);
|
2013-03-18 20:58:48 +01:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2014-05-26 22:02:04 +02:00
|
|
|
// The exception can be ignored if the the connection is 'done'
|
|
|
|
// or if the it was caused because the socket got closed
|
|
|
|
if (!(done || isSocketClosed())) {
|
|
|
|
// Close the connection and notify connection listeners of the
|
|
|
|
// error.
|
|
|
|
notifyConnectionError(e);
|
|
|
|
}
|
2013-03-18 20:58:48 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected class PacketWriter {
|
2014-09-11 09:49:16 +02:00
|
|
|
public static final int QUEUE_SIZE = XMPPTCPConnection.QUEUE_SIZE;
|
2014-05-26 22:02:04 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
private final ArrayBlockingQueueWithShutdown<Element> queue = new ArrayBlockingQueueWithShutdown<Element>(
|
|
|
|
QUEUE_SIZE, true);
|
2014-05-26 22:02:04 +02:00
|
|
|
|
|
|
|
private Thread writerThread;
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
/**
|
|
|
|
* Needs to be protected for unit testing purposes.
|
|
|
|
*/
|
|
|
|
protected SynchronizationPoint<NoResponseException> shutdownDone = new SynchronizationPoint<NoResponseException>(
|
|
|
|
XMPPTCPConnection.this);
|
2014-05-26 22:02:04 +02:00
|
|
|
|
|
|
|
/**
|
2014-09-11 09:49:16 +02:00
|
|
|
* If set, the packet writer is shut down
|
2014-05-26 22:02:04 +02:00
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
protected volatile Long shutdownTimestamp = null;
|
|
|
|
|
|
|
|
private volatile boolean instantShutdown;
|
2014-05-26 22:02:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the writer in order to be used. It is called at the first connection and also
|
|
|
|
* is invoked if the connection is disconnected by an error.
|
|
|
|
*/
|
|
|
|
void init() {
|
2014-09-11 09:49:16 +02:00
|
|
|
shutdownDone.init();
|
|
|
|
shutdownTimestamp = null;
|
|
|
|
|
|
|
|
if (unacknowledgedStanzas != null) {
|
|
|
|
// It's possible that there are new stanzas in the writer queue that
|
|
|
|
// came in while we were disconnected but resumable, drain those into
|
|
|
|
// the unacknowledged queue so that they get resent now
|
|
|
|
drainWriterQueueToUnacknowledgedStanzas();
|
|
|
|
}
|
2014-05-26 22:02:04 +02:00
|
|
|
|
|
|
|
queue.start();
|
|
|
|
writerThread = new Thread() {
|
|
|
|
public void run() {
|
2014-09-11 09:49:16 +02:00
|
|
|
writePackets();
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
writerThread.setName("Smack Packet Writer (" + getConnectionCounter() + ")");
|
|
|
|
writerThread.setDaemon(true);
|
2014-09-11 09:49:16 +02:00
|
|
|
writerThread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean done() {
|
|
|
|
return shutdownTimestamp != null;
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-11 09:49:16 +02:00
|
|
|
* Sends the specified element to the server.
|
2014-05-26 22:02:04 +02:00
|
|
|
*
|
2014-09-11 09:49:16 +02:00
|
|
|
* @param element the element to send.
|
2014-05-26 22:02:04 +02:00
|
|
|
* @throws NotConnectedException
|
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
protected void sendStreamElement(Element element) throws NotConnectedException {
|
|
|
|
if (done() && !isSmResumptionPossible()) {
|
|
|
|
// Don't throw a NotConnectedException is there is an resumable stream available
|
2014-05-26 22:02:04 +02:00
|
|
|
throw new NotConnectedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2014-09-11 09:49:16 +02:00
|
|
|
queue.put(element);
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
|
|
|
catch (InterruptedException ie) {
|
|
|
|
throw new NotConnectedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shuts down the packet writer. Once this method has been called, no further
|
|
|
|
* packets will be written to the server.
|
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
void shutdown(boolean instant) {
|
|
|
|
instantShutdown = instant;
|
|
|
|
shutdownTimestamp = System.currentTimeMillis();
|
2014-05-26 22:02:04 +02:00
|
|
|
queue.shutdown();
|
2014-09-11 09:49:16 +02:00
|
|
|
try {
|
|
|
|
shutdownDone.checkIfSuccessOrWait();
|
|
|
|
}
|
|
|
|
catch (NoResponseException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "NoResponseException", e);
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-11 09:49:16 +02:00
|
|
|
* Returns the next available element from the queue for writing.
|
2014-05-26 22:02:04 +02:00
|
|
|
*
|
2014-09-11 09:49:16 +02:00
|
|
|
* @return the next element for writing.
|
2014-05-26 22:02:04 +02:00
|
|
|
*/
|
2014-09-11 09:49:16 +02:00
|
|
|
private Element nextStreamElement() {
|
|
|
|
// TODO not sure if nextStreamElement and/or this done() condition still required.
|
|
|
|
// Couldn't this be done in writePackets too?
|
|
|
|
if (done()) {
|
2014-05-26 22:02:04 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
Element packet = null;
|
2014-05-26 22:02:04 +02:00
|
|
|
try {
|
|
|
|
packet = queue.take();
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
private void writePackets() {
|
2014-05-26 22:02:04 +02:00
|
|
|
try {
|
|
|
|
openStream();
|
2014-09-11 09:49:16 +02:00
|
|
|
initalOpenStreamSend.reportSuccess();
|
2014-05-26 22:02:04 +02:00
|
|
|
// Write out packets from the queue.
|
2014-09-11 09:49:16 +02:00
|
|
|
while (!done()) {
|
|
|
|
Element packet = nextStreamElement();
|
2014-05-26 22:02:04 +02:00
|
|
|
if (packet != null) {
|
2014-09-11 09:49:16 +02:00
|
|
|
// Check if the stream element should be put to the unacknowledgedStanza
|
|
|
|
// queue. Note that we can not do the put() in sendPacketInternal() and the
|
|
|
|
// packet order is not stable at this point (sendPacketInternal() can be
|
|
|
|
// called concurrently).
|
|
|
|
if (isSmEnabled() && packet instanceof Packet) {
|
|
|
|
// If the unacknowledgedStanza queue is nearly full, request an new ack
|
|
|
|
// from the server in order to drain it
|
|
|
|
if (unacknowledgedStanzas.size() == 0.8 * XMPPTCPConnection.QUEUE_SIZE) {
|
|
|
|
writer.write(AckRequest.INSTANCE.toXML().toString());
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
unacknowledgedStanzas.put((Packet) packet);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
|
|
|
}
|
2014-05-26 22:02:04 +02:00
|
|
|
writer.write(packet.toXML().toString());
|
|
|
|
if (queue.isEmpty()) {
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
if (!instantShutdown) {
|
|
|
|
// Flush out the rest of the queue. If the queue is extremely large, it's
|
|
|
|
// possible we won't have time to entirely flush it before the socket is forced
|
|
|
|
// closed by the shutdown process.
|
|
|
|
try {
|
|
|
|
while (!queue.isEmpty()) {
|
|
|
|
Element packet = queue.remove();
|
|
|
|
writer.write(packet.toXML().toString());
|
|
|
|
}
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
LOGGER.log(Level.WARNING,
|
|
|
|
"Exception flushing queue during shutdown, ignore and continue",
|
|
|
|
e);
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
// Close the stream.
|
2014-05-26 22:02:04 +02:00
|
|
|
try {
|
2014-09-11 09:49:16 +02:00
|
|
|
writer.write("</stream:stream>");
|
|
|
|
writer.flush();
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
2014-09-11 09:49:16 +02:00
|
|
|
LOGGER.log(Level.WARNING, "Exception writing closing stream element", e);
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
// Delete the queue contents (hopefully nothing is left).
|
|
|
|
queue.clear();
|
|
|
|
} else if (instantShutdown && isSmEnabled()) {
|
|
|
|
// This was an instantShutdown and SM is enabled, drain all remaining stanzas
|
|
|
|
// into the unacknowledgedStanzas queue
|
|
|
|
drainWriterQueueToUnacknowledgedStanzas();
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
try {
|
|
|
|
writer.close();
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
catch (Exception e) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
catch (Exception e) {
|
2014-05-26 22:02:04 +02:00
|
|
|
// The exception can be ignored if the the connection is 'done'
|
|
|
|
// or if the it was caused because the socket got closed
|
2014-09-11 09:49:16 +02:00
|
|
|
if (!(done() || isSocketClosed())) {
|
|
|
|
notifyConnectionError(e);
|
|
|
|
} else {
|
|
|
|
LOGGER.log(Level.FINE, "Ignoring Exception in writePackets()", e);
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
} finally {
|
|
|
|
shutdownDone.reportSuccess();
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
private void drainWriterQueueToUnacknowledgedStanzas() {
|
|
|
|
List<Element> elements = new ArrayList<Element>(queue.size());
|
|
|
|
queue.drainTo(elements);
|
|
|
|
for (Element element : elements) {
|
|
|
|
if (element instanceof Packet) {
|
|
|
|
unacknowledgedStanzas.add((Packet) element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setUseStreamManagementDefault(boolean useSmDefault) {
|
|
|
|
XMPPTCPConnection.useSmDefault = useSmDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setUseStreamManagementResumptiodDefault(boolean useSmResupmptionDefault) {
|
|
|
|
XMPPTCPConnection.useSmResumptionDefault = useSmResupmptionDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUseStreamManagement(boolean useSm) {
|
|
|
|
this.useSm = useSm;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUseStreamManagementResumption(boolean useSmResumption) {
|
|
|
|
this.useSmResumption = useSmResumption;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the preferred resumption time in seconds.
|
|
|
|
* @param resumptionTime the preferred resumption time in seconds
|
|
|
|
*/
|
|
|
|
public void setPreferredResumptionTime(int resumptionTime) {
|
|
|
|
smClientMaxResumptionTime = resumptionTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean addRequestAckPredicate(PacketFilter predicate) {
|
|
|
|
synchronized (requestAckPredicates) {
|
|
|
|
return requestAckPredicates.add(predicate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean removeRequestAckPredicate(PacketFilter predicate) {
|
|
|
|
synchronized (requestAckPredicates) {
|
|
|
|
return requestAckPredicates.remove(predicate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeAllRequestAckPredicates() {
|
|
|
|
synchronized (requestAckPredicates) {
|
|
|
|
requestAckPredicates.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void requestSmAcknowledgement() throws StreamManagementNotEnabledException, NotConnectedException {
|
|
|
|
if (!isSmEnabled()) {
|
|
|
|
throw new StreamManagementException.StreamManagementNotEnabledException();
|
|
|
|
}
|
|
|
|
requestSmAcknowledgementInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void requestSmAcknowledgementInternal() throws NotConnectedException {
|
|
|
|
packetWriter.sendStreamElement(AckRequest.INSTANCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addStanzaAcknowledgedListener(PacketListener listener) {
|
|
|
|
stanzaAcknowledgedListeners.add(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean removeStanzaAcknowledgedListener(PacketListener listener) {
|
|
|
|
return stanzaAcknowledgedListeners.remove(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeAllStanzaAcknowledgedListeners() {
|
|
|
|
stanzaAcknowledgedListeners.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public PacketListener addIdStanzaAcknowledgedListener(String id, PacketListener listener) {
|
|
|
|
return idStanzaAcknowledgedListeners.put(id, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
public PacketListener removeIdStanzaAcknowledgedListener(String id) {
|
|
|
|
return idStanzaAcknowledgedListeners.remove(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeAllIdStanzaAcknowledgedListeners() {
|
|
|
|
idStanzaAcknowledgedListeners.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSmAvailable() {
|
|
|
|
return hasFeature(StreamManagementFeature.ELEMENT, StreamManagement.NAMESPACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSmEnabled() {
|
|
|
|
return smEnabledSyncPoint.wasSuccessful();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isDisconnectedButSmResumptionPossible() {
|
|
|
|
return disconnectedButResumeable && isSmResumptionPossible();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isSmResumptionPossible() {
|
|
|
|
// There is no resumable stream available
|
|
|
|
if (smSessionId == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
final Long shutdownTimestamp = packetWriter.shutdownTimestamp;
|
|
|
|
// Seems like we are already reconnected, report true
|
|
|
|
if (shutdownTimestamp == null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if resumption time is over
|
|
|
|
long current = System.currentTimeMillis();
|
|
|
|
int clientResumptionTime = smClientMaxResumptionTime > 0 ? smClientMaxResumptionTime : Integer.MAX_VALUE;
|
|
|
|
int serverResumptionTime = smServerMaxResumptimTime > 0 ? smServerMaxResumptimTime : Integer.MAX_VALUE;
|
|
|
|
long maxResumptionMillies = Math.max(clientResumptionTime, serverResumptionTime) * 1000;
|
|
|
|
if (shutdownTimestamp + maxResumptionMillies > current) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void processHandledCount(long handledCount) throws NotConnectedException {
|
|
|
|
long ackedStanzasCount = SMUtils.calculateDelta(handledCount, serverHandledStanzasCount);
|
|
|
|
List<Packet> ackedStanzas = new ArrayList<Packet>(
|
|
|
|
handledCount <= Integer.MAX_VALUE ? (int) handledCount
|
|
|
|
: Integer.MAX_VALUE);
|
|
|
|
for (long i = 0; i < ackedStanzasCount; i++) {
|
|
|
|
Packet ackedStanza = unacknowledgedStanzas.poll();
|
|
|
|
// If the server ack'ed a stanza, then it must be in the
|
|
|
|
// unacknowledged stanza queue. There can be no exception.
|
|
|
|
assert(ackedStanza != null);
|
|
|
|
ackedStanzas.add(ackedStanza);
|
|
|
|
}
|
|
|
|
for (Packet ackedStanza : ackedStanzas) {
|
|
|
|
for (PacketListener listener : stanzaAcknowledgedListeners) {
|
|
|
|
listener.processPacket(ackedStanza);
|
|
|
|
}
|
|
|
|
String id = ackedStanza.getPacketID();
|
|
|
|
if (id != null) {
|
|
|
|
PacketListener listener = idStanzaAcknowledgedListeners.remove(id);
|
|
|
|
if (listener != null) {
|
|
|
|
listener.processPacket(ackedStanza);
|
|
|
|
}
|
|
|
|
}
|
2014-05-26 22:02:04 +02:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
serverHandledStanzasCount = handledCount;
|
2013-03-18 20:58:48 +01:00
|
|
|
}
|
2007-11-14 17:27:47 +01:00
|
|
|
}
|