2014-05-25 12:28:08 +02:00
|
|
|
/**
|
|
|
|
*
|
2020-04-04 13:03:31 +02:00
|
|
|
* Copyright 2009 Jive Software, 2018-2020 Florian Schmaus.
|
2014-05-25 12:28:08 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.FileInputStream;
|
2014-05-25 12:28:08 +02:00
|
|
|
import java.io.IOException;
|
2019-02-04 08:59:39 +01:00
|
|
|
import java.io.InputStream;
|
2014-05-25 12:28:08 +02:00
|
|
|
import java.io.Reader;
|
|
|
|
import java.io.Writer;
|
2019-02-04 08:59:39 +01:00
|
|
|
import java.lang.reflect.Constructor;
|
2019-05-08 11:34:40 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2019-02-04 08:59:39 +01:00
|
|
|
import java.security.KeyManagementException;
|
|
|
|
import java.security.KeyStore;
|
|
|
|
import java.security.KeyStoreException;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.NoSuchProviderException;
|
|
|
|
import java.security.Provider;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
import java.security.Security;
|
|
|
|
import java.security.UnrecoverableKeyException;
|
|
|
|
import java.security.cert.CertificateException;
|
2014-05-25 12:28:08 +02:00
|
|
|
import java.util.Collection;
|
2014-09-11 09:49:16 +02:00
|
|
|
import java.util.HashMap;
|
2018-11-02 21:51:09 +01:00
|
|
|
import java.util.Iterator;
|
2015-01-05 21:42:35 +01:00
|
|
|
import java.util.LinkedHashMap;
|
2014-10-10 00:36:58 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2014-05-25 12:28:08 +02:00
|
|
|
import java.util.Map;
|
2019-04-10 12:18:06 +02:00
|
|
|
import java.util.Queue;
|
2014-10-09 23:55:02 +02:00
|
|
|
import java.util.Set;
|
2014-05-25 12:28:08 +02:00
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
2014-10-09 23:55:02 +02:00
|
|
|
import java.util.concurrent.CopyOnWriteArraySet;
|
2018-04-08 21:21:50 +02:00
|
|
|
import java.util.concurrent.Executor;
|
2014-12-17 18:21:54 +01:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
2018-02-23 17:04:51 +01:00
|
|
|
import java.util.concurrent.ThreadFactory;
|
2014-08-16 00:04:24 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2014-05-25 12:28:08 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2014-09-11 09:49:16 +02:00
|
|
|
import java.util.concurrent.locks.Lock;
|
|
|
|
import java.util.concurrent.locks.ReentrantLock;
|
2014-05-25 12:28:08 +02:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
import javax.net.ssl.KeyManager;
|
|
|
|
import javax.net.ssl.KeyManagerFactory;
|
|
|
|
import javax.net.ssl.SSLContext;
|
2019-09-24 23:32:08 +02:00
|
|
|
import javax.net.ssl.SSLSession;
|
2019-02-04 08:59:39 +01:00
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
|
import javax.security.auth.callback.Callback;
|
|
|
|
import javax.security.auth.callback.CallbackHandler;
|
|
|
|
import javax.security.auth.callback.PasswordCallback;
|
2019-05-08 21:10:39 +02:00
|
|
|
import javax.xml.namespace.QName;
|
2019-02-04 08:59:39 +01:00
|
|
|
|
|
|
|
import org.jivesoftware.smack.ConnectionConfiguration.DnssecMode;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
|
2017-05-03 13:18:15 +02:00
|
|
|
import org.jivesoftware.smack.SmackConfiguration.UnknownIqRequestReplyMode;
|
2014-11-09 18:30:16 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.AlreadyConnectedException;
|
|
|
|
import org.jivesoftware.smack.SmackException.AlreadyLoggedInException;
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.SmackException.NoResponseException;
|
|
|
|
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
2017-11-06 22:38:13 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.NotLoggedInException;
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.SmackException.ResourceBindingNotOfferedException;
|
2016-11-12 11:12:50 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.SecurityRequiredByClientException;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.SmackException.SecurityRequiredException;
|
2019-09-24 23:32:08 +02:00
|
|
|
import org.jivesoftware.smack.SmackException.SmackSaslException;
|
2019-02-09 20:15:09 +01:00
|
|
|
import org.jivesoftware.smack.SmackException.SmackWrappedException;
|
2017-08-12 17:35:20 +02:00
|
|
|
import org.jivesoftware.smack.SmackFuture.InternalSmackFuture;
|
2019-02-04 08:59:39 +01:00
|
|
|
import org.jivesoftware.smack.XMPPException.FailedNonzaException;
|
2015-03-18 09:52:33 +01:00
|
|
|
import org.jivesoftware.smack.XMPPException.StreamErrorException;
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.compress.packet.Compress;
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.compression.XMPPInputOutputStream;
|
2020-04-04 13:03:31 +02:00
|
|
|
import org.jivesoftware.smack.datatypes.UInt16;
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.debugger.SmackDebugger;
|
2017-07-28 11:59:11 +02:00
|
|
|
import org.jivesoftware.smack.debugger.SmackDebuggerFactory;
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.filter.IQReplyFilter;
|
2015-02-26 18:41:17 +01:00
|
|
|
import org.jivesoftware.smack.filter.StanzaFilter;
|
2015-02-19 14:59:44 +01:00
|
|
|
import org.jivesoftware.smack.filter.StanzaIdFilter;
|
2015-01-08 11:01:35 +01:00
|
|
|
import org.jivesoftware.smack.iqrequest.IQRequestHandler;
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.packet.Bind;
|
2015-01-08 11:01:35 +01:00
|
|
|
import org.jivesoftware.smack.packet.ErrorIQ;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
2019-02-04 08:59:39 +01:00
|
|
|
import org.jivesoftware.smack.packet.FullyQualifiedElement;
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.packet.Mechanisms;
|
2015-06-05 08:14:51 +02:00
|
|
|
import org.jivesoftware.smack.packet.Message;
|
2019-10-25 13:57:18 +02:00
|
|
|
import org.jivesoftware.smack.packet.MessageBuilder;
|
|
|
|
import org.jivesoftware.smack.packet.MessageOrPresence;
|
|
|
|
import org.jivesoftware.smack.packet.MessageOrPresenceBuilder;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.packet.Nonza;
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.packet.Presence;
|
2019-10-25 13:57:18 +02:00
|
|
|
import org.jivesoftware.smack.packet.PresenceBuilder;
|
2014-05-25 12:28:08 +02:00
|
|
|
import org.jivesoftware.smack.packet.Session;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2018-04-07 21:25:40 +02:00
|
|
|
import org.jivesoftware.smack.packet.StanzaError;
|
2019-10-24 15:45:08 +02:00
|
|
|
import org.jivesoftware.smack.packet.StanzaFactory;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.packet.StartTls;
|
2015-03-18 09:52:33 +01:00
|
|
|
import org.jivesoftware.smack.packet.StreamError;
|
2019-02-04 08:59:39 +01:00
|
|
|
import org.jivesoftware.smack.packet.StreamOpen;
|
|
|
|
import org.jivesoftware.smack.packet.TopLevelStreamElement;
|
2019-02-15 23:21:30 +01:00
|
|
|
import org.jivesoftware.smack.packet.XmlEnvironment;
|
2019-10-24 15:45:08 +02:00
|
|
|
import org.jivesoftware.smack.packet.id.StanzaIdSource;
|
2014-12-28 17:43:39 +01:00
|
|
|
import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
|
2019-02-10 19:01:47 +01:00
|
|
|
import org.jivesoftware.smack.parsing.SmackParsingException;
|
2015-02-26 18:41:17 +01:00
|
|
|
import org.jivesoftware.smack.provider.ExtensionElementProvider;
|
2019-02-04 08:59:39 +01:00
|
|
|
import org.jivesoftware.smack.provider.NonzaProvider;
|
2014-09-11 23:58:07 +02:00
|
|
|
import org.jivesoftware.smack.provider.ProviderManager;
|
2019-09-24 23:32:08 +02:00
|
|
|
import org.jivesoftware.smack.sasl.SASLErrorException;
|
|
|
|
import org.jivesoftware.smack.sasl.SASLMechanism;
|
2015-05-18 18:21:05 +02:00
|
|
|
import org.jivesoftware.smack.sasl.core.SASLAnonymous;
|
2019-09-24 23:32:08 +02:00
|
|
|
import org.jivesoftware.smack.sasl.packet.SaslNonza;
|
2018-02-26 10:23:02 +01:00
|
|
|
import org.jivesoftware.smack.util.Async;
|
2020-05-17 17:01:42 +02:00
|
|
|
import org.jivesoftware.smack.util.CloseableUtil;
|
2019-09-24 23:32:08 +02:00
|
|
|
import org.jivesoftware.smack.util.CollectionUtil;
|
2019-10-25 13:57:18 +02:00
|
|
|
import org.jivesoftware.smack.util.Consumer;
|
2014-11-09 18:30:16 +01:00
|
|
|
import org.jivesoftware.smack.util.DNSUtil;
|
2019-09-24 23:32:08 +02:00
|
|
|
import org.jivesoftware.smack.util.MultiMap;
|
2015-01-26 20:36:55 +01:00
|
|
|
import org.jivesoftware.smack.util.Objects;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
2014-12-28 17:43:39 +01:00
|
|
|
import org.jivesoftware.smack.util.ParserUtils;
|
2019-10-25 13:57:18 +02:00
|
|
|
import org.jivesoftware.smack.util.Predicate;
|
2014-12-18 14:01:31 +01:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2020-02-12 23:09:36 +01:00
|
|
|
import org.jivesoftware.smack.util.TLSUtils;
|
2019-02-04 08:59:39 +01:00
|
|
|
import org.jivesoftware.smack.util.dns.SmackDaneProvider;
|
|
|
|
import org.jivesoftware.smack.util.dns.SmackDaneVerifier;
|
2019-05-06 22:06:13 +02:00
|
|
|
import org.jivesoftware.smack.xml.XmlPullParser;
|
|
|
|
import org.jivesoftware.smack.xml.XmlPullParserException;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.DomainBareJid;
|
2019-09-24 23:32:08 +02:00
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
2015-05-27 19:29:51 +02:00
|
|
|
import org.jxmpp.jid.EntityFullJid;
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.Jid;
|
2019-02-16 21:26:02 +01:00
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
2015-05-04 20:14:45 +02:00
|
|
|
import org.jxmpp.jid.parts.Resourcepart;
|
2019-02-16 21:26:02 +01:00
|
|
|
import org.jxmpp.stringprep.XmppStringprepException;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.jxmpp.util.XmppStringUtils;
|
2014-05-25 12:28:08 +02:00
|
|
|
|
2018-08-02 09:37:26 +02:00
|
|
|
/**
|
|
|
|
* This abstract class is commonly used as super class for XMPP connection mechanisms like TCP and BOSH. Hence it
|
|
|
|
* provides the methods for connection state management, like {@link #connect()}, {@link #login()} and
|
|
|
|
* {@link #disconnect()} (which are deliberately not provided by the {@link XMPPConnection} interface).
|
|
|
|
* <p>
|
|
|
|
* <b>Note:</b> The default entry point to Smack's documentation is {@link XMPPConnection}. If you are getting started
|
|
|
|
* with Smack, then head over to {@link XMPPConnection} and the come back here.
|
|
|
|
* </p>
|
|
|
|
* <h2>Parsing Exceptions</h2>
|
|
|
|
* <p>
|
|
|
|
* In case a Smack parser (Provider) throws those exceptions are handled over to the {@link ParsingExceptionCallback}. A
|
|
|
|
* common cause for a provider throwing is illegal input, for example a non-numeric String where only Integers are
|
|
|
|
* allowed. Smack's <em>default behavior</em> follows the <b>"fail-hard per default"</b> principle leading to a
|
|
|
|
* termination of the connection on parsing exceptions. This default was chosen to make users eventually aware that they
|
|
|
|
* should configure their own callback and handle those exceptions to prevent the disconnect. Handle a parsing exception
|
|
|
|
* could be as simple as using a non-throwing no-op callback, which would cause the faulty stream element to be taken
|
|
|
|
* out of the stream, i.e., Smack behaves like that element was never received.
|
|
|
|
* </p>
|
|
|
|
* <p>
|
|
|
|
* If the parsing exception is because Smack received illegal input, then please consider informing the authors of the
|
|
|
|
* originating entity about that. If it was thrown because of an bug in a Smack parser, then please consider filling a
|
|
|
|
* bug with Smack.
|
|
|
|
* </p>
|
|
|
|
* <h3>Managing the parsing exception callback</h3>
|
|
|
|
* <p>
|
|
|
|
* The "fail-hard per default" behavior is achieved by using the
|
|
|
|
* {@link org.jivesoftware.smack.parsing.ExceptionThrowingCallbackWithHint} as default parsing exception callback. You
|
|
|
|
* can change the behavior using {@link #setParsingExceptionCallback(ParsingExceptionCallback)} to set a new callback.
|
|
|
|
* Use {@link org.jivesoftware.smack.SmackConfiguration#setDefaultParsingExceptionCallback(ParsingExceptionCallback)} to
|
|
|
|
* set the default callback.
|
|
|
|
* </p>
|
|
|
|
*/
|
2014-05-25 12:28:08 +02:00
|
|
|
public abstract class AbstractXMPPConnection implements XMPPConnection {
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(AbstractXMPPConnection.class.getName());
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
protected static final SmackReactor SMACK_REACTOR;
|
|
|
|
|
|
|
|
static {
|
|
|
|
SMACK_REACTOR = SmackReactor.getInstance();
|
|
|
|
}
|
|
|
|
|
2018-05-09 23:06:12 +02:00
|
|
|
/**
|
2014-05-25 12:28:08 +02:00
|
|
|
* Counter to uniquely identify connections that are created.
|
|
|
|
*/
|
2018-03-29 12:35:11 +02:00
|
|
|
private static final AtomicInteger connectionCounter = new AtomicInteger(0);
|
2014-05-25 12:28:08 +02:00
|
|
|
|
|
|
|
static {
|
|
|
|
// Ensure the SmackConfiguration class is loaded by calling a method in it.
|
|
|
|
SmackConfiguration.getVersion();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A collection of ConnectionListeners which listen for connection closing
|
|
|
|
* and reconnection events.
|
|
|
|
*/
|
2014-10-09 23:55:02 +02:00
|
|
|
protected final Set<ConnectionListener> connectionListeners =
|
2017-12-13 23:10:11 +01:00
|
|
|
new CopyOnWriteArraySet<>();
|
2014-05-25 12:28:08 +02:00
|
|
|
|
|
|
|
/**
|
2017-01-03 11:12:34 +01:00
|
|
|
* A collection of StanzaCollectors which collects packets for a specified filter
|
2014-05-25 12:28:08 +02:00
|
|
|
* and perform blocking and polling operations on the result queue.
|
2014-10-10 02:41:37 +02:00
|
|
|
* <p>
|
|
|
|
* We use a ConcurrentLinkedQueue here, because its Iterator is weakly
|
2017-01-03 11:12:34 +01:00
|
|
|
* consistent and we want {@link #invokeStanzaCollectorsAndNotifyRecvListeners(Stanza)} for-each
|
|
|
|
* loop to be lock free. As drawback, removing a StanzaCollector is O(n).
|
2014-10-10 02:41:37 +02:00
|
|
|
* The alternative would be a synchronized HashSet, but this would mean a
|
|
|
|
* synchronized block around every usage of <code>collectors</code>.
|
|
|
|
* </p>
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
2017-01-03 11:12:34 +01:00
|
|
|
private final Collection<StanzaCollector> collectors = new ConcurrentLinkedQueue<>();
|
2014-05-25 12:28:08 +02:00
|
|
|
|
2019-02-04 21:58:35 +01:00
|
|
|
private final Map<StanzaListener, ListenerWrapper> recvListeners = new LinkedHashMap<>();
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* List of PacketListeners that will be notified synchronously when a new stanza was received.
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
2015-03-01 10:28:15 +01:00
|
|
|
private final Map<StanzaListener, ListenerWrapper> syncRecvListeners = new LinkedHashMap<>();
|
2014-05-25 12:28:08 +02:00
|
|
|
|
2015-01-05 21:42:35 +01:00
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* List of PacketListeners that will be notified asynchronously when a new stanza was received.
|
2015-01-05 21:42:35 +01:00
|
|
|
*/
|
2015-03-01 10:28:15 +01:00
|
|
|
private final Map<StanzaListener, ListenerWrapper> asyncRecvListeners = new LinkedHashMap<>();
|
2014-12-30 20:45:15 +01:00
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* List of PacketListeners that will be notified when a new stanza was sent.
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
2015-03-01 10:28:15 +01:00
|
|
|
private final Map<StanzaListener, ListenerWrapper> sendListeners =
|
2017-12-13 23:10:11 +01:00
|
|
|
new HashMap<>();
|
2014-05-25 12:28:08 +02:00
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* List of PacketListeners that will be notified when a new stanza is about to be
|
|
|
|
* sent to the server. These interceptors may modify the stanza before it is being
|
2014-05-25 12:28:08 +02:00
|
|
|
* actually sent to the server.
|
|
|
|
*/
|
2015-03-01 10:28:15 +01:00
|
|
|
private final Map<StanzaListener, InterceptorWrapper> interceptors =
|
2017-12-13 23:10:11 +01:00
|
|
|
new HashMap<>();
|
2014-05-25 12:28:08 +02:00
|
|
|
|
2019-10-25 13:57:18 +02:00
|
|
|
private final Map<Consumer<MessageBuilder>, GenericInterceptorWrapper<MessageBuilder, Message>> messageInterceptors = new HashMap<>();
|
|
|
|
|
|
|
|
private final Map<Consumer<PresenceBuilder>, GenericInterceptorWrapper<PresenceBuilder, Presence>> presenceInterceptors = new HashMap<>();
|
|
|
|
|
2019-02-15 23:21:30 +01:00
|
|
|
private XmlEnvironment incomingStreamXmlEnvironment;
|
|
|
|
|
2019-09-21 23:00:17 +02:00
|
|
|
protected XmlEnvironment outgoingStreamXmlEnvironment;
|
|
|
|
|
2019-09-24 23:32:08 +02:00
|
|
|
final MultiMap<QName, NonzaCallback> nonzaCallbacksMap = new MultiMap<>();
|
2019-02-04 08:59:39 +01:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
protected final Lock connectionLock = new ReentrantLock();
|
|
|
|
|
2019-06-11 00:10:38 +02:00
|
|
|
protected final Map<QName, FullyQualifiedElement> streamFeatures = new HashMap<>();
|
2014-09-11 09:49:16 +02:00
|
|
|
|
|
|
|
/**
|
2015-01-10 11:18:07 +01:00
|
|
|
* The full JID of the authenticated user, as returned by the resource binding response of the server.
|
|
|
|
* <p>
|
|
|
|
* It is important that we don't infer the user from the login() arguments and the configurations service name, as,
|
|
|
|
* for example, when SASL External is used, the username is not given to login but taken from the 'external'
|
|
|
|
* certificate.
|
|
|
|
* </p>
|
2014-09-11 09:49:16 +02:00
|
|
|
*/
|
2015-05-27 19:29:51 +02:00
|
|
|
protected EntityFullJid user;
|
2014-09-11 09:49:16 +02:00
|
|
|
|
2014-11-01 12:29:41 +01:00
|
|
|
protected boolean connected = false;
|
|
|
|
|
2015-01-28 17:14:33 +01:00
|
|
|
/**
|
|
|
|
* The stream ID, see RFC 6120 § 4.7.3
|
|
|
|
*/
|
|
|
|
protected String streamId;
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
/**
|
2017-01-12 20:57:19 +01:00
|
|
|
* The timeout to wait for a reply in milliseconds.
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
2017-01-12 20:57:19 +01:00
|
|
|
private long replyTimeout = SmackConfiguration.getDefaultReplyTimeout();
|
2014-05-25 12:28:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The SmackDebugger allows to log and debug XML traffic.
|
|
|
|
*/
|
2017-07-28 11:59:11 +02:00
|
|
|
protected final SmackDebugger debugger;
|
2014-05-25 12:28:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Reader which is used for the debugger.
|
|
|
|
*/
|
|
|
|
protected Reader reader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Writer which is used for the debugger.
|
|
|
|
*/
|
|
|
|
protected Writer writer;
|
|
|
|
|
2017-01-06 15:03:28 +01:00
|
|
|
protected final SynchronizationPoint<SmackException> tlsHandled = new SynchronizationPoint<>(this, "establishing TLS");
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
/**
|
|
|
|
* Set to success if the last features stanza from the server has been parsed. A XMPP connection
|
|
|
|
* handshake can invoke multiple features stanzas, e.g. when TLS is activated a second feature
|
|
|
|
* stanza is send by the server. This is set to true once the last feature stanza has been
|
|
|
|
* parsed.
|
|
|
|
*/
|
2019-02-07 20:43:45 +01:00
|
|
|
protected final SynchronizationPoint<SmackException> lastFeaturesReceived = new SynchronizationPoint<>(
|
2015-06-02 17:21:33 +02:00
|
|
|
AbstractXMPPConnection.this, "last stream features received from server");
|
2014-05-25 12:28:08 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
/**
|
2017-01-06 15:03:28 +01:00
|
|
|
* Set to success if the SASL feature has been received.
|
2014-09-11 09:49:16 +02:00
|
|
|
*/
|
2017-01-06 15:03:28 +01:00
|
|
|
protected final SynchronizationPoint<XMPPException> saslFeatureReceived = new SynchronizationPoint<>(
|
2015-06-02 17:21:33 +02:00
|
|
|
AbstractXMPPConnection.this, "SASL mechanisms stream feature from server");
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A synchronization point which is successful if this connection has received the closing
|
|
|
|
* stream element from the remote end-point, i.e. the server.
|
|
|
|
*/
|
|
|
|
protected final SynchronizationPoint<Exception> closingStreamReceived = new SynchronizationPoint<>(
|
|
|
|
this, "stream closing element received");
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
/**
|
|
|
|
* The SASLAuthentication manager that is responsible for authenticating with the server.
|
|
|
|
*/
|
2019-09-24 23:32:08 +02:00
|
|
|
private final SASLAuthentication saslAuthentication;
|
2014-05-25 12:28:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A number to uniquely identify connections that are created. This is distinct from the
|
|
|
|
* connection ID, which is a value sent by the server once a connection is made.
|
|
|
|
*/
|
|
|
|
protected final int connectionCounterValue = connectionCounter.getAndIncrement();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the initial configuration used while creating the connection.
|
|
|
|
*/
|
|
|
|
protected final ConnectionConfiguration config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines how the from attribute of outgoing stanzas should be handled.
|
|
|
|
*/
|
|
|
|
private FromMode fromMode = FromMode.OMITTED;
|
|
|
|
|
|
|
|
protected XMPPInputOutputStream compressionHandler;
|
|
|
|
|
2014-12-28 17:43:39 +01:00
|
|
|
private ParsingExceptionCallback parsingExceptionCallback = SmackConfiguration.getDefaultParsingExceptionCallback();
|
|
|
|
|
2014-12-17 18:21:54 +01:00
|
|
|
/**
|
2015-02-27 23:38:13 +01:00
|
|
|
* A cached thread pool executor service with custom thread factory to set meaningful names on the threads and set
|
2014-12-17 18:21:54 +01:00
|
|
|
* them 'daemon'.
|
|
|
|
*/
|
2018-02-23 17:04:51 +01:00
|
|
|
private static final ExecutorService CACHED_EXECUTOR_SERVICE = Executors.newCachedThreadPool(new ThreadFactory() {
|
|
|
|
@Override
|
|
|
|
public Thread newThread(Runnable runnable) {
|
|
|
|
Thread thread = new Thread(runnable);
|
|
|
|
thread.setName("Smack Cached Executor");
|
|
|
|
thread.setDaemon(true);
|
2019-05-09 11:04:26 +02:00
|
|
|
thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
|
|
|
@Override
|
|
|
|
public void uncaughtException(Thread t, Throwable e) {
|
|
|
|
LOGGER.log(Level.WARNING, t + " encountered uncaught exception", e);
|
|
|
|
}
|
|
|
|
});
|
2018-02-23 17:04:51 +01:00
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
});
|
2014-05-25 12:28:08 +02:00
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
protected static final AsyncButOrdered<AbstractXMPPConnection> ASYNC_BUT_ORDERED = new AsyncButOrdered<>();
|
2015-01-05 21:42:35 +01:00
|
|
|
|
2019-02-04 21:58:35 +01:00
|
|
|
protected final AsyncButOrdered<StanzaListener> inOrderListeners = new AsyncButOrdered<>();
|
|
|
|
|
2019-04-10 12:18:06 +02:00
|
|
|
/**
|
|
|
|
* An executor which uses {@link #asyncGoLimited(Runnable)} to limit the number of asynchronously processed runnables
|
|
|
|
* per connection.
|
|
|
|
*/
|
|
|
|
private final Executor limitedExcutor = new Executor() {
|
|
|
|
@Override
|
|
|
|
public void execute(Runnable runnable) {
|
|
|
|
asyncGoLimited(runnable);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
/**
|
|
|
|
* The used host to establish the connection to
|
|
|
|
*/
|
2014-07-21 16:37:31 +02:00
|
|
|
protected String host;
|
2014-05-25 12:28:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The used port to establish the connection to
|
|
|
|
*/
|
2020-04-04 13:03:31 +02:00
|
|
|
protected UInt16 port;
|
2014-05-25 12:28:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag that indicates if the user is currently authenticated with the server.
|
|
|
|
*/
|
|
|
|
protected boolean authenticated = false;
|
|
|
|
|
2019-03-09 19:19:20 +01:00
|
|
|
// TODO: Migrate to ZonedDateTime once Smack's minimum required Android SDK level is 26 (8.0, Oreo) or higher.
|
|
|
|
protected long authenticatedConnectionInitiallyEstablishedTimestamp;
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
/**
|
|
|
|
* Flag that indicates if the user was authenticated with the server when the connection
|
|
|
|
* to the server was closed (abruptly or not).
|
|
|
|
*/
|
|
|
|
protected boolean wasAuthenticated = false;
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
protected Exception currentConnectionException;
|
|
|
|
|
2019-06-11 00:10:38 +02:00
|
|
|
private final Map<QName, IQRequestHandler> setIqRequestHandler = new HashMap<>();
|
|
|
|
private final Map<QName, IQRequestHandler> getIqRequestHandler = new HashMap<>();
|
2015-01-08 11:01:35 +01:00
|
|
|
|
2019-10-24 15:45:08 +02:00
|
|
|
private final StanzaFactory stanzaFactory;
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
/**
|
2015-02-12 12:13:19 +01:00
|
|
|
* Create a new XMPPConnection to an XMPP server.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-05-25 12:28:08 +02:00
|
|
|
* @param configuration The configuration which is used to establish the connection.
|
|
|
|
*/
|
|
|
|
protected AbstractXMPPConnection(ConnectionConfiguration configuration) {
|
2015-05-03 22:00:41 +02:00
|
|
|
saslAuthentication = new SASLAuthentication(this, configuration);
|
2014-05-25 12:28:08 +02:00
|
|
|
config = configuration;
|
2019-09-24 23:32:08 +02:00
|
|
|
|
|
|
|
// Install the SASL Nonza callbacks.
|
|
|
|
buildNonzaCallback()
|
|
|
|
.listenFor(SaslNonza.Challenge.class, c -> {
|
|
|
|
try {
|
|
|
|
saslAuthentication.challengeReceived(c);
|
|
|
|
} catch (SmackException | InterruptedException e) {
|
|
|
|
saslAuthentication.authenticationFailed(e);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.listenFor(SaslNonza.Success.class, s -> {
|
|
|
|
try {
|
|
|
|
saslAuthentication.authenticated(s);
|
|
|
|
} catch (SmackSaslException | NotConnectedException | InterruptedException e) {
|
|
|
|
saslAuthentication.authenticationFailed(e);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.listenFor(SaslNonza.SASLFailure.class, f -> saslAuthentication.authenticationFailed(f))
|
|
|
|
.install();
|
|
|
|
|
2017-07-28 11:59:11 +02:00
|
|
|
SmackDebuggerFactory debuggerFactory = configuration.getDebuggerFactory();
|
|
|
|
if (debuggerFactory != null) {
|
|
|
|
debugger = debuggerFactory.create(this);
|
|
|
|
} else {
|
|
|
|
debugger = null;
|
|
|
|
}
|
2015-03-17 12:06:55 +01:00
|
|
|
// Notify listeners that a new connection has been established
|
|
|
|
for (ConnectionCreationListener listener : XMPPConnectionRegistry.getConnectionCreationListeners()) {
|
|
|
|
listener.connectionCreated(this);
|
|
|
|
}
|
2019-10-24 15:45:08 +02:00
|
|
|
|
|
|
|
StanzaIdSource stanzaIdSource = configuration.constructStanzaIdSource();
|
|
|
|
stanzaFactory = new StanzaFactory(stanzaIdSource);
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 14:04:46 +02:00
|
|
|
/**
|
|
|
|
* Get the connection configuration used by this connection.
|
|
|
|
*
|
|
|
|
* @return the connection configuration.
|
|
|
|
*/
|
|
|
|
public ConnectionConfiguration getConfiguration() {
|
2014-05-25 12:28:08 +02:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2015-05-18 16:48:23 +02:00
|
|
|
@Override
|
|
|
|
public DomainBareJid getXMPPServiceDomain() {
|
|
|
|
if (xmppServiceDomain != null) {
|
|
|
|
return xmppServiceDomain;
|
2014-11-09 18:30:16 +01:00
|
|
|
}
|
2015-05-18 16:48:23 +02:00
|
|
|
return config.getXMPPServiceDomain();
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getHost() {
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getPort() {
|
2020-04-04 13:03:31 +02:00
|
|
|
final UInt16 port = this.port;
|
|
|
|
if (port == null) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return port.intValue();
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public abstract boolean isSecureConnection();
|
|
|
|
|
2015-03-04 22:42:36 +01:00
|
|
|
protected abstract void sendStanzaInternal(Stanza packet) throws NotConnectedException, InterruptedException;
|
2014-05-25 12:28:08 +02:00
|
|
|
|
2018-05-16 08:09:09 +02:00
|
|
|
@Override
|
|
|
|
public boolean trySendStanza(Stanza stanza) throws NotConnectedException {
|
|
|
|
// Default implementation which falls back to sendStanza() as mentioned in the methods javadoc. May be
|
|
|
|
// overwritten by subclasses.
|
|
|
|
try {
|
|
|
|
sendStanza(stanza);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
LOGGER.log(Level.FINER,
|
|
|
|
"Thread blocked in fallback implementation of trySendStanza(Stanza) was interrupted", e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean trySendStanza(Stanza stanza, long timeout, TimeUnit unit)
|
|
|
|
throws NotConnectedException, InterruptedException {
|
|
|
|
// Default implementation which falls back to sendStanza() as mentioned in the methods javadoc. May be
|
|
|
|
// overwritten by subclasses.
|
|
|
|
sendStanza(stanza);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
@Override
|
2015-07-14 22:41:33 +02:00
|
|
|
public abstract void sendNonza(Nonza element) throws NotConnectedException, InterruptedException;
|
2014-09-11 09:49:16 +02:00
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
@Override
|
|
|
|
public abstract boolean isUsingCompression();
|
|
|
|
|
2019-03-09 17:13:54 +01:00
|
|
|
protected void initState() {
|
|
|
|
saslFeatureReceived.init();
|
|
|
|
lastFeaturesReceived.init();
|
|
|
|
tlsHandled.init();
|
2020-04-13 22:50:02 +02:00
|
|
|
// TODO: We do not init() closingStreamReceived here, as the integration tests use it to check if we waited for
|
|
|
|
// it.
|
2019-03-09 17:13:54 +01:00
|
|
|
}
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
/**
|
2015-04-06 13:51:47 +02:00
|
|
|
* Establishes a connection to the XMPP server. It basically
|
2014-05-25 12:28:08 +02:00
|
|
|
* creates and maintains a connection to the server.
|
|
|
|
* <p>
|
|
|
|
* Listeners will be preserved from a previous connection.
|
2015-04-06 13:51:47 +02:00
|
|
|
* </p>
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-05-25 12:28:08 +02:00
|
|
|
* @throws XMPPException if an error occurs on the XMPP protocol level.
|
|
|
|
* @throws SmackException if an error occurs somewhere else besides XMPP protocol level.
|
2019-10-30 12:02:36 +01:00
|
|
|
* @throws IOException if an I/O error occurred.
|
2015-01-25 19:48:52 +01:00
|
|
|
* @return a reference to this object, to chain <code>connect()</code> with <code>login()</code>.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
2015-02-14 09:43:44 +01:00
|
|
|
public synchronized AbstractXMPPConnection connect() throws SmackException, IOException, XMPPException, InterruptedException {
|
2015-01-28 17:14:33 +01:00
|
|
|
// Check if not already connected
|
2014-11-09 18:30:16 +01:00
|
|
|
throwAlreadyConnectedExceptionIfAppropriate();
|
2015-01-28 17:14:33 +01:00
|
|
|
|
|
|
|
// Reset the connection state
|
2019-03-09 17:13:54 +01:00
|
|
|
initState();
|
2020-04-13 22:50:02 +02:00
|
|
|
closingStreamReceived.init();
|
2015-01-28 17:14:33 +01:00
|
|
|
streamId = null;
|
|
|
|
|
2019-03-04 17:08:53 +01:00
|
|
|
try {
|
|
|
|
// Perform the actual connection to the XMPP service
|
|
|
|
connectInternal();
|
|
|
|
|
|
|
|
// 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() && getConfiguration().getSecurityMode() == SecurityMode.required) {
|
|
|
|
throw new SecurityRequiredByClientException();
|
|
|
|
}
|
|
|
|
} catch (SmackException | IOException | XMPPException | InterruptedException e) {
|
|
|
|
instantShutdown();
|
|
|
|
throw e;
|
2016-11-12 11:12:50 +01:00
|
|
|
}
|
|
|
|
|
2016-11-12 11:03:44 +01:00
|
|
|
// Make note of the fact that we're now connected.
|
|
|
|
connected = true;
|
|
|
|
callConnectionConnectedListener();
|
|
|
|
|
2015-01-25 19:48:52 +01:00
|
|
|
return this;
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract method that concrete subclasses of XMPPConnection need to implement to perform their
|
2014-09-11 09:49:16 +02:00
|
|
|
* way of XMPP connection establishment. Implementations are required to perform an automatic
|
|
|
|
* login if the previous connection state was logged (authenticated).
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws SmackException if Smack detected an exceptional situation.
|
2019-10-30 12:02:36 +01:00
|
|
|
* @throws IOException if an I/O error occurred.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws XMPPException if an XMPP protocol error was received.
|
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
2015-02-14 09:43:44 +01:00
|
|
|
protected abstract void connectInternal() throws SmackException, IOException, XMPPException, InterruptedException;
|
2014-05-25 12:28:08 +02:00
|
|
|
|
2015-05-04 20:14:45 +02:00
|
|
|
private String usedUsername, usedPassword;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The resourcepart used for this connection. May not be the resulting resourcepart if it's null or overridden by the XMPP service.
|
|
|
|
*/
|
|
|
|
private Resourcepart usedResource;
|
2014-12-18 14:01:31 +01:00
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
/**
|
2015-01-18 21:02:06 +01:00
|
|
|
* Logs in to the server using the strongest SASL mechanism supported by
|
2018-05-09 23:06:12 +02:00
|
|
|
* the server. If more than the connection's default stanza timeout elapses in each step of the
|
2015-01-18 21:02:06 +01:00
|
|
|
* authentication process without a response from the server, a
|
|
|
|
* {@link SmackException.NoResponseException} will be thrown.
|
|
|
|
* <p>
|
|
|
|
* Before logging in (i.e. authenticate) to the server the connection must be connected
|
|
|
|
* by calling {@link #connect}.
|
|
|
|
* </p>
|
|
|
|
* <p>
|
2014-05-25 12:28:08 +02:00
|
|
|
* It is possible to log in without sending an initial available presence by using
|
2015-01-22 13:53:50 +01:00
|
|
|
* {@link ConnectionConfiguration.Builder#setSendPresence(boolean)}.
|
2014-05-25 12:28:08 +02:00
|
|
|
* 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
|
2014-12-17 17:01:53 +01:00
|
|
|
* {@link ConnectionConfiguration.Builder#setCallbackHandler(javax.security.auth.callback.CallbackHandler)}.
|
2014-05-25 12:28:08 +02:00
|
|
|
* For more advanced login settings see {@link ConnectionConfiguration}.
|
2015-01-18 21:02:06 +01:00
|
|
|
* </p>
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-05-25 12:28:08 +02:00
|
|
|
* @throws XMPPException if an error occurs on the XMPP protocol level.
|
2015-01-18 21:02:06 +01:00
|
|
|
* @throws SmackException if an error occurs somewhere else besides XMPP protocol level.
|
|
|
|
* @throws IOException if an I/O error occurs during login.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
2015-02-14 09:43:44 +01:00
|
|
|
public synchronized void login() throws XMPPException, SmackException, IOException, InterruptedException {
|
2015-05-03 22:00:41 +02:00
|
|
|
// The previously used username, password and resource take over precedence over the
|
|
|
|
// ones from the connection configuration
|
|
|
|
CharSequence username = usedUsername != null ? usedUsername : config.getUsername();
|
|
|
|
String password = usedPassword != null ? usedPassword : config.getPassword();
|
2015-05-04 20:14:45 +02:00
|
|
|
Resourcepart resource = usedResource != null ? usedResource : config.getResource();
|
2015-05-03 22:00:41 +02:00
|
|
|
login(username, password, resource);
|
2014-12-18 14:01:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-04 20:14:45 +02:00
|
|
|
* Same as {@link #login(CharSequence, String, Resourcepart)}, but takes the resource from the connection
|
2014-12-18 14:01:31 +01:00
|
|
|
* configuration.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param username TODO javadoc me please
|
|
|
|
* @param password TODO javadoc me please
|
|
|
|
* @throws XMPPException if an XMPP protocol error was received.
|
|
|
|
* @throws SmackException if Smack detected an exceptional situation.
|
2019-10-30 12:02:36 +01:00
|
|
|
* @throws IOException if an I/O error occurred.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2014-12-18 14:01:31 +01:00
|
|
|
* @see #login
|
|
|
|
*/
|
2015-01-18 20:54:41 +01:00
|
|
|
public synchronized void login(CharSequence username, String password) throws XMPPException, SmackException,
|
2015-02-14 09:43:44 +01:00
|
|
|
IOException, InterruptedException {
|
2014-12-18 14:01:31 +01:00
|
|
|
login(username, password, config.getResource());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-18 20:42:27 +01:00
|
|
|
* Login with the given username (authorization identity). You may omit the password if a callback handler is used.
|
|
|
|
* If resource is null, then the server will generate one.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param username TODO javadoc me please
|
|
|
|
* @param password TODO javadoc me please
|
|
|
|
* @param resource TODO javadoc me please
|
|
|
|
* @throws XMPPException if an XMPP protocol error was received.
|
|
|
|
* @throws SmackException if Smack detected an exceptional situation.
|
2019-10-30 12:02:36 +01:00
|
|
|
* @throws IOException if an I/O error occurred.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2014-12-18 14:01:31 +01:00
|
|
|
* @see #login
|
|
|
|
*/
|
2015-05-04 20:14:45 +02:00
|
|
|
public synchronized void login(CharSequence username, String password, Resourcepart resource) throws XMPPException,
|
2015-02-14 09:43:44 +01:00
|
|
|
SmackException, IOException, InterruptedException {
|
2015-01-26 20:36:55 +01:00
|
|
|
if (!config.allowNullOrEmptyUsername) {
|
2018-07-17 15:10:39 +02:00
|
|
|
StringUtils.requireNotNullNorEmpty(username, "Username must not be null nor empty");
|
2014-11-09 18:30:16 +01:00
|
|
|
}
|
2015-09-14 09:05:24 +02:00
|
|
|
throwNotConnectedExceptionIfAppropriate("Did you call connect() before login()?");
|
2015-01-17 17:53:36 +01:00
|
|
|
throwAlreadyLoggedInExceptionIfAppropriate();
|
2015-01-18 20:54:41 +01:00
|
|
|
usedUsername = username != null ? username.toString() : null;
|
2014-12-18 14:01:31 +01:00
|
|
|
usedPassword = password;
|
|
|
|
usedResource = resource;
|
2015-05-03 22:00:41 +02:00
|
|
|
loginInternal(usedUsername, usedPassword, usedResource);
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
2015-05-04 20:14:45 +02:00
|
|
|
protected abstract void loginInternal(String username, String password, Resourcepart resource)
|
2015-02-14 09:43:44 +01:00
|
|
|
throws XMPPException, SmackException, IOException, InterruptedException;
|
2014-05-25 12:28:08 +02:00
|
|
|
|
2014-11-01 12:29:41 +01:00
|
|
|
@Override
|
|
|
|
public final boolean isConnected() {
|
|
|
|
return connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final boolean isAuthenticated() {
|
|
|
|
return authenticated;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-05-27 19:29:51 +02:00
|
|
|
public final EntityFullJid getUser() {
|
2014-11-01 12:29:41 +01:00
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2015-01-28 17:14:33 +01:00
|
|
|
@Override
|
|
|
|
public String getStreamId() {
|
|
|
|
if (!isConnected()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return streamId;
|
|
|
|
}
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
protected Resourcepart bindResourceAndEstablishSession(Resourcepart resource) throws XMPPErrorException,
|
2015-03-24 18:33:47 +01:00
|
|
|
SmackException, InterruptedException {
|
2014-05-25 12:28:08 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
// Wait until either:
|
|
|
|
// - the servers last features stanza has been parsed
|
|
|
|
// - the timeout occurs
|
|
|
|
LOGGER.finer("Waiting for last features to be received before continuing with resource binding");
|
2019-02-07 20:43:45 +01:00
|
|
|
lastFeaturesReceived.checkIfSuccessOrWaitOrThrow();
|
2014-05-25 12:28:08 +02:00
|
|
|
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
if (!hasFeature(Bind.ELEMENT, Bind.NAMESPACE)) {
|
2017-12-13 23:10:11 +01:00
|
|
|
// Server never offered resource binding, which is REQUIRED in XMPP client and
|
2014-09-11 09:49:16 +02:00
|
|
|
// server implementations as per RFC6120 7.2
|
|
|
|
throw new ResourceBindingNotOfferedException();
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-04 18:01:05 +02:00
|
|
|
// Resource binding, see RFC6120 7.
|
|
|
|
// Note that we can not use IQReplyFilter here, since the users full JID is not yet
|
|
|
|
// available. It will become available right after the resource has been successfully bound.
|
|
|
|
Bind bindResource = Bind.newSet(resource);
|
2017-01-03 11:12:34 +01:00
|
|
|
StanzaCollector packetCollector = createStanzaCollectorAndSend(new StanzaIdFilter(bindResource), bindResource);
|
2014-08-04 18:01:05 +02:00
|
|
|
Bind response = packetCollector.nextResultOrThrow();
|
2015-01-10 11:18:07 +01:00
|
|
|
// Set the connections user to the result of resource binding. It is important that we don't infer the user
|
|
|
|
// from the login() arguments and the configurations service name, as, for example, when SASL External is used,
|
|
|
|
// the username is not given to login but taken from the 'external' certificate.
|
2014-09-11 09:49:16 +02:00
|
|
|
user = response.getJid();
|
2015-05-18 16:48:23 +02:00
|
|
|
xmppServiceDomain = user.asDomainBareJid();
|
2014-05-25 12:28:08 +02:00
|
|
|
|
2014-12-30 12:37:01 +01:00
|
|
|
Session.Feature sessionFeature = getFeature(Session.ELEMENT, Session.NAMESPACE);
|
|
|
|
// Only bind the session if it's announced as stream feature by the server, is not optional and not disabled
|
|
|
|
// For more information see http://tools.ietf.org/html/draft-cridland-xmpp-session-01
|
2018-04-02 13:27:01 +02:00
|
|
|
if (sessionFeature != null && !sessionFeature.isOptional()) {
|
2014-05-25 12:28:08 +02:00
|
|
|
Session session = new Session();
|
2017-01-03 11:12:34 +01:00
|
|
|
packetCollector = createStanzaCollectorAndSend(new StanzaIdFilter(session), session);
|
2014-08-04 18:01:05 +02:00
|
|
|
packetCollector.nextResultOrThrow();
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
2019-02-04 08:59:39 +01:00
|
|
|
|
|
|
|
return response.getJid().getResourcepart();
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
2015-02-14 09:43:44 +01:00
|
|
|
protected void afterSuccessfulLogin(final boolean resumed) throws NotConnectedException, InterruptedException {
|
2019-03-09 19:19:20 +01:00
|
|
|
if (!resumed) {
|
|
|
|
authenticatedConnectionInitiallyEstablishedTimestamp = System.currentTimeMillis();
|
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
// Indicate that we're now authenticated.
|
|
|
|
this.authenticated = true;
|
2014-05-25 12:28:08 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
// If debugging is enabled, change the the debug window title to include the
|
|
|
|
// name we are now logged-in as.
|
2015-01-18 21:33:03 +01:00
|
|
|
// If DEBUG was set to true AFTER the connection was created the debugger
|
2014-09-11 09:49:16 +02:00
|
|
|
// will be null
|
2017-07-28 11:59:11 +02:00
|
|
|
if (debugger != null) {
|
2014-09-11 09:49:16 +02:00
|
|
|
debugger.userHasLogged(user);
|
|
|
|
}
|
2015-01-07 20:19:04 +01:00
|
|
|
callConnectionAuthenticatedListener(resumed);
|
2014-09-11 09:49:16 +02:00
|
|
|
|
|
|
|
// Set presence to online. It is important that this is done after
|
|
|
|
// callConnectionAuthenticatedListener(), as this call will also
|
|
|
|
// eventually load the roster. And we should load the roster before we
|
|
|
|
// send the initial presence.
|
|
|
|
if (config.isSendPresence() && !resumed) {
|
2019-10-24 15:45:08 +02:00
|
|
|
Presence availablePresence = getStanzaFactory()
|
|
|
|
.buildPresenceStanza()
|
|
|
|
.ofType(Presence.Type.available)
|
|
|
|
.build();
|
|
|
|
sendStanza(availablePresence);
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
@Override
|
2014-12-18 14:01:31 +01:00
|
|
|
public final boolean isAnonymous() {
|
2015-05-18 18:21:05 +02:00
|
|
|
return isAuthenticated() && SASLAnonymous.NAME.equals(getUsedSaslMechansism());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the name of the SASL mechanism that was used to authenticate this connection. This returns the name of
|
2017-12-13 23:10:11 +01:00
|
|
|
* mechanism which was used the last time this connection was authenticated, and will return <code>null</code> if
|
2015-05-18 18:21:05 +02:00
|
|
|
* this connection was not authenticated before.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2015-05-18 18:21:05 +02:00
|
|
|
* @return the name of the used SASL mechanism.
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
|
|
|
public final String getUsedSaslMechansism() {
|
|
|
|
return saslAuthentication.getNameOfLastUsedSaslMechansism();
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
2015-05-18 16:48:23 +02:00
|
|
|
private DomainBareJid xmppServiceDomain;
|
2014-09-11 09:49:16 +02:00
|
|
|
|
|
|
|
protected Lock getConnectionLock() {
|
|
|
|
return connectionLock;
|
|
|
|
}
|
|
|
|
|
2014-11-09 18:30:16 +01:00
|
|
|
protected void throwNotConnectedExceptionIfAppropriate() throws NotConnectedException {
|
2015-07-29 08:20:46 +02:00
|
|
|
throwNotConnectedExceptionIfAppropriate(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void throwNotConnectedExceptionIfAppropriate(String optionalHint) throws NotConnectedException {
|
2014-05-25 12:28:08 +02:00
|
|
|
if (!isConnected()) {
|
2015-07-29 08:20:46 +02:00
|
|
|
throw new NotConnectedException(optionalHint);
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
2014-11-09 18:30:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void throwAlreadyConnectedExceptionIfAppropriate() throws AlreadyConnectedException {
|
|
|
|
if (isConnected()) {
|
|
|
|
throw new AlreadyConnectedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void throwAlreadyLoggedInExceptionIfAppropriate() throws AlreadyLoggedInException {
|
|
|
|
if (isAuthenticated()) {
|
|
|
|
throw new AlreadyLoggedInException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-24 15:45:08 +02:00
|
|
|
@Override
|
|
|
|
public final StanzaFactory getStanzaFactory() {
|
|
|
|
return stanzaFactory;
|
|
|
|
}
|
|
|
|
|
2015-03-04 21:44:43 +01:00
|
|
|
@Override
|
2019-02-04 09:49:51 +01:00
|
|
|
public final void sendStanza(Stanza stanza) throws NotConnectedException, InterruptedException {
|
2015-06-05 08:14:51 +02:00
|
|
|
Objects.requireNonNull(stanza, "Stanza must not be null");
|
2019-07-24 09:18:39 +02:00
|
|
|
assert stanza instanceof Message || stanza instanceof Presence || stanza instanceof IQ;
|
2015-01-26 20:36:55 +01:00
|
|
|
|
2014-11-09 18:30:16 +01:00
|
|
|
throwNotConnectedExceptionIfAppropriate();
|
2014-05-25 12:28:08 +02:00
|
|
|
switch (fromMode) {
|
|
|
|
case OMITTED:
|
2015-06-05 08:14:51 +02:00
|
|
|
stanza.setFrom((Jid) null);
|
2014-05-25 12:28:08 +02:00
|
|
|
break;
|
|
|
|
case USER:
|
2015-06-05 08:14:51 +02:00
|
|
|
stanza.setFrom(getUser());
|
2014-05-25 12:28:08 +02:00
|
|
|
break;
|
|
|
|
case UNCHANGED:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2015-06-05 08:14:51 +02:00
|
|
|
// Invoke interceptors for the new stanza that is about to be sent. Interceptors may modify
|
|
|
|
// the content of the stanza.
|
2019-10-25 13:57:18 +02:00
|
|
|
Stanza stanzaAfterInterceptors = firePacketInterceptors(stanza);
|
|
|
|
sendStanzaInternal(stanzaAfterInterceptors);
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-24 23:32:08 +02:00
|
|
|
* Authenticate a connection.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2019-09-24 23:32:08 +02:00
|
|
|
* @param username the username that is authenticating with the server.
|
|
|
|
* @param password the password to send to the server.
|
|
|
|
* @param authzid the authorization identifier (typically null).
|
|
|
|
* @param sslSession the optional SSL/TLS session (if one was established)
|
|
|
|
* @return the used SASLMechanism.
|
|
|
|
* @throws XMPPErrorException if there was an XMPP error returned.
|
|
|
|
* @throws SASLErrorException if a SASL protocol error was returned.
|
2019-10-30 12:02:36 +01:00
|
|
|
* @throws IOException if an I/O error occurred.
|
2019-09-24 23:32:08 +02:00
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2019-10-30 12:02:36 +01:00
|
|
|
* @throws SmackSaslException if a SASL specific error occurred.
|
2019-09-24 23:32:08 +02:00
|
|
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
|
|
|
* @throws NoResponseException if there was no response from the remote entity.
|
|
|
|
* @throws SmackWrappedException in case of an exception.
|
|
|
|
* @see SASLAuthentication#authenticate(String, String, EntityBareJid, SSLSession)
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
2019-09-24 23:32:08 +02:00
|
|
|
protected final SASLMechanism authenticate(String username, String password, EntityBareJid authzid,
|
|
|
|
SSLSession sslSession) throws XMPPErrorException, SASLErrorException, SmackSaslException,
|
|
|
|
NotConnectedException, NoResponseException, IOException, InterruptedException, SmackWrappedException {
|
|
|
|
SASLMechanism saslMechanism = saslAuthentication.authenticate(username, password, authzid, sslSession);
|
|
|
|
afterSaslAuthenticationSuccess();
|
|
|
|
return saslMechanism;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook for subclasses right after successful SASL authentication. RFC 6120 § 6.4.6. specifies a that the initiating
|
|
|
|
* entity, needs to initiate a new stream in this case. But some transports, like BOSH, requires a special handling.
|
|
|
|
* <p>
|
|
|
|
* Note that we can not reset XMPPTCPConnection's parser here, because this method is invoked by the thread calling
|
|
|
|
* {@link #login()}, but the parser reset has to be done within the reader thread.
|
|
|
|
* </p>
|
|
|
|
*
|
|
|
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
|
|
|
* @throws SmackWrappedException in case of an exception.
|
|
|
|
*/
|
|
|
|
protected void afterSaslAuthenticationSuccess()
|
|
|
|
throws NotConnectedException, InterruptedException, SmackWrappedException {
|
|
|
|
sendStreamOpen();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final boolean isSaslAuthenticated() {
|
|
|
|
return saslAuthentication.authenticationSuccessful();
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the connection by setting presence to unavailable then closing the connection to
|
|
|
|
* the XMPP server. The XMPPConnection can still be used for connecting to the server
|
|
|
|
* again.
|
|
|
|
*
|
|
|
|
*/
|
2015-01-13 23:09:43 +01:00
|
|
|
public void disconnect() {
|
2018-02-20 09:44:05 +01:00
|
|
|
Presence unavailablePresence = null;
|
|
|
|
if (isAuthenticated()) {
|
2019-10-24 15:45:08 +02:00
|
|
|
unavailablePresence = getStanzaFactory().buildPresenceStanza()
|
|
|
|
.ofType(Presence.Type.unavailable)
|
|
|
|
.build();
|
2018-02-20 09:44:05 +01:00
|
|
|
}
|
2015-01-13 23:09:43 +01:00
|
|
|
try {
|
2018-02-20 09:44:05 +01:00
|
|
|
disconnect(unavailablePresence);
|
2015-01-13 23:09:43 +01:00
|
|
|
}
|
|
|
|
catch (NotConnectedException e) {
|
|
|
|
LOGGER.log(Level.FINEST, "Connection is already disconnected", e);
|
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Closes the connection. A custom unavailable presence is sent to the server, followed
|
|
|
|
* by closing the stream. The XMPPConnection can still be used for connecting to the server
|
|
|
|
* again. A custom unavailable presence is useful for communicating offline presence
|
|
|
|
* information such as "On vacation". Typically, just the status text of the presence
|
2018-03-31 14:17:30 +02:00
|
|
|
* stanza is set with online information, but most XMPP servers will deliver the full
|
|
|
|
* presence stanza with whatever data is set.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2018-02-20 09:44:05 +01:00
|
|
|
* @param unavailablePresence the optional presence stanza to send during shutdown.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws NotConnectedException if the XMPP connection is not connected.
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
|
|
|
public synchronized void disconnect(Presence unavailablePresence) throws NotConnectedException {
|
2018-02-20 09:44:05 +01:00
|
|
|
if (unavailablePresence != null) {
|
|
|
|
try {
|
|
|
|
sendStanza(unavailablePresence);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
LOGGER.log(Level.FINE,
|
|
|
|
"Was interrupted while sending unavailable presence. Continuing to disconnect the connection",
|
|
|
|
e);
|
|
|
|
}
|
2015-02-14 09:43:44 +01:00
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
shutdown();
|
|
|
|
callConnectionClosedListener();
|
2014-11-09 11:02:49 +01:00
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
/**
|
|
|
|
* Sends out a notification that there was an error with the connection
|
|
|
|
* and closes the connection.
|
|
|
|
*
|
|
|
|
* @param exception the exception that causes the connection close event.
|
|
|
|
*/
|
2019-03-14 18:23:33 +01:00
|
|
|
protected final void notifyConnectionError(final Exception exception) {
|
2019-02-04 08:59:39 +01:00
|
|
|
if (!isConnected()) {
|
|
|
|
LOGGER.log(Level.INFO, "Connection was already disconnected when attempting to handle " + exception,
|
|
|
|
exception);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-14 18:23:33 +01:00
|
|
|
ASYNC_BUT_ORDERED.performAsyncButOrdered(this, () -> {
|
|
|
|
currentConnectionException = exception;
|
2019-02-04 08:59:39 +01:00
|
|
|
|
2019-03-14 18:23:33 +01:00
|
|
|
for (StanzaCollector collector : collectors) {
|
|
|
|
collector.notifyConnectionError(exception);
|
|
|
|
}
|
|
|
|
SmackWrappedException smackWrappedException = new SmackWrappedException(exception);
|
|
|
|
tlsHandled.reportGenericFailure(smackWrappedException);
|
|
|
|
saslFeatureReceived.reportGenericFailure(smackWrappedException);
|
|
|
|
lastFeaturesReceived.reportGenericFailure(smackWrappedException);
|
2020-04-04 13:03:31 +02:00
|
|
|
closingStreamReceived.reportFailure(smackWrappedException);
|
2019-03-14 18:23:33 +01:00
|
|
|
// TODO From XMPPTCPConnection. Was called in Smack 4.3 where notifyConnectionError() was part of
|
|
|
|
// XMPPTCPConnection. Create delegation method?
|
|
|
|
// maybeCompressFeaturesReceived.reportGenericFailure(smackWrappedException);
|
|
|
|
|
2019-03-16 10:30:16 +01:00
|
|
|
synchronized (AbstractXMPPConnection.this) {
|
|
|
|
notifyAll();
|
|
|
|
|
|
|
|
// Closes the connection temporary. A if the connection supports stream management, then a reconnection is
|
|
|
|
// possible. Note that a connection listener of e.g. XMPPTCPConnection will drop the SM state in
|
|
|
|
// case the Exception is a StreamErrorException.
|
|
|
|
instantShutdown();
|
|
|
|
}
|
2019-03-14 18:23:33 +01:00
|
|
|
|
|
|
|
Async.go(() -> {
|
|
|
|
// Notify connection listeners of the error.
|
|
|
|
callConnectionClosedOnErrorListener(exception);
|
|
|
|
}, AbstractXMPPConnection.this + " callConnectionClosedOnErrorListener()");
|
|
|
|
});
|
2019-02-04 08:59:39 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 17:08:53 +01:00
|
|
|
/**
|
|
|
|
* Performs an unclean disconnect and shutdown of the connection. Does not send a closing stream stanza.
|
|
|
|
*/
|
|
|
|
public abstract void instantShutdown();
|
2019-02-04 08:59:39 +01:00
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
/**
|
|
|
|
* Shuts the current connection down.
|
|
|
|
*/
|
|
|
|
protected abstract void shutdown();
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
protected final boolean waitForClosingStreamTagFromServer() {
|
|
|
|
Exception exception;
|
|
|
|
try {
|
|
|
|
// After we send the closing stream element, check if there was already a
|
|
|
|
// closing stream element sent by the server or wait with a timeout for a
|
|
|
|
// closing stream element to be received from the server.
|
|
|
|
exception = closingStreamReceived.checkIfSuccessOrWait();
|
|
|
|
} catch (InterruptedException | NoResponseException e) {
|
|
|
|
exception = e;
|
|
|
|
}
|
|
|
|
if (exception != null) {
|
|
|
|
LOGGER.log(Level.INFO, "Exception while waiting for closing stream element from the server " + this, exception);
|
|
|
|
}
|
|
|
|
return exception == null;
|
|
|
|
}
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
@Override
|
|
|
|
public void addConnectionListener(ConnectionListener connectionListener) {
|
|
|
|
if (connectionListener == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-09 23:55:02 +02:00
|
|
|
connectionListeners.add(connectionListener);
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeConnectionListener(ConnectionListener connectionListener) {
|
|
|
|
connectionListeners.remove(connectionListener);
|
|
|
|
}
|
|
|
|
|
2018-03-31 14:15:46 +02:00
|
|
|
@Override
|
|
|
|
public <I extends IQ> I sendIqRequestAndWaitForResponse(IQ request)
|
|
|
|
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
|
|
|
|
StanzaCollector collector = createStanzaCollectorAndSend(request);
|
|
|
|
IQ resultResponse = collector.nextResultOrThrow();
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
I concreteResultResponse = (I) resultResponse;
|
|
|
|
return concreteResultResponse;
|
|
|
|
}
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
@Override
|
2017-01-03 11:12:34 +01:00
|
|
|
public StanzaCollector createStanzaCollectorAndSend(IQ packet) throws NotConnectedException, InterruptedException {
|
2015-02-26 18:41:17 +01:00
|
|
|
StanzaFilter packetFilter = new IQReplyFilter(packet, this);
|
2014-05-25 12:28:08 +02:00
|
|
|
// Create the packet collector before sending the packet
|
2017-01-03 11:12:34 +01:00
|
|
|
StanzaCollector packetCollector = createStanzaCollectorAndSend(packetFilter, packet);
|
2014-10-13 10:45:00 +02:00
|
|
|
return packetCollector;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-01-03 11:12:34 +01:00
|
|
|
public StanzaCollector createStanzaCollectorAndSend(StanzaFilter packetFilter, Stanza packet)
|
2015-02-14 09:43:44 +01:00
|
|
|
throws NotConnectedException, InterruptedException {
|
2018-04-23 20:56:54 +02:00
|
|
|
StanzaCollector.Configuration configuration = StanzaCollector.newConfiguration()
|
|
|
|
.setStanzaFilter(packetFilter)
|
|
|
|
.setRequest(packet);
|
2014-10-13 10:45:00 +02:00
|
|
|
// Create the packet collector before sending the packet
|
2018-04-23 20:56:54 +02:00
|
|
|
StanzaCollector packetCollector = createStanzaCollector(configuration);
|
2014-08-04 18:15:42 +02:00
|
|
|
try {
|
|
|
|
// Now we can send the packet as the collector has been created
|
2015-03-04 21:44:43 +01:00
|
|
|
sendStanza(packet);
|
2014-08-04 18:15:42 +02:00
|
|
|
}
|
2015-02-14 09:43:44 +01:00
|
|
|
catch (InterruptedException | NotConnectedException | RuntimeException e) {
|
2014-08-04 18:15:42 +02:00
|
|
|
packetCollector.cancel();
|
|
|
|
throw e;
|
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
return packetCollector;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-01-03 11:12:34 +01:00
|
|
|
public StanzaCollector createStanzaCollector(StanzaFilter packetFilter) {
|
|
|
|
StanzaCollector.Configuration configuration = StanzaCollector.newConfiguration().setStanzaFilter(packetFilter);
|
|
|
|
return createStanzaCollector(configuration);
|
2015-01-16 17:24:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-01-03 11:12:34 +01:00
|
|
|
public StanzaCollector createStanzaCollector(StanzaCollector.Configuration configuration) {
|
|
|
|
StanzaCollector collector = new StanzaCollector(this, configuration);
|
2014-05-25 12:28:08 +02:00
|
|
|
// Add the collector to the list of active collectors.
|
|
|
|
collectors.add(collector);
|
|
|
|
return collector;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-01-03 11:12:34 +01:00
|
|
|
public void removeStanzaCollector(StanzaCollector collector) {
|
2014-05-25 12:28:08 +02:00
|
|
|
collectors.remove(collector);
|
|
|
|
}
|
|
|
|
|
2019-02-04 21:58:35 +01:00
|
|
|
@Override
|
|
|
|
public final void addStanzaListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter) {
|
|
|
|
if (stanzaListener == null) {
|
|
|
|
throw new NullPointerException("Given stanza listener must not be null");
|
|
|
|
}
|
|
|
|
ListenerWrapper wrapper = new ListenerWrapper(stanzaListener, stanzaFilter);
|
|
|
|
synchronized (recvListeners) {
|
|
|
|
recvListeners.put(stanzaListener, wrapper);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final boolean removeStanzaListener(StanzaListener stanzaListener) {
|
|
|
|
synchronized (recvListeners) {
|
|
|
|
return recvListeners.remove(stanzaListener) != null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 21:42:35 +01:00
|
|
|
@Override
|
2015-03-01 10:28:15 +01:00
|
|
|
public void addSyncStanzaListener(StanzaListener packetListener, StanzaFilter packetFilter) {
|
2014-05-25 12:28:08 +02:00
|
|
|
if (packetListener == null) {
|
|
|
|
throw new NullPointerException("Packet listener is null.");
|
|
|
|
}
|
|
|
|
ListenerWrapper wrapper = new ListenerWrapper(packetListener, packetFilter);
|
2015-01-05 21:42:35 +01:00
|
|
|
synchronized (syncRecvListeners) {
|
|
|
|
syncRecvListeners.put(packetListener, wrapper);
|
2014-10-10 00:36:58 +02:00
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-03-01 10:28:15 +01:00
|
|
|
public boolean removeSyncStanzaListener(StanzaListener packetListener) {
|
2015-01-05 21:42:35 +01:00
|
|
|
synchronized (syncRecvListeners) {
|
|
|
|
return syncRecvListeners.remove(packetListener) != null;
|
2014-10-10 00:36:58 +02:00
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
2014-12-30 20:45:15 +01:00
|
|
|
@Override
|
2015-03-01 10:28:15 +01:00
|
|
|
public void addAsyncStanzaListener(StanzaListener packetListener, StanzaFilter packetFilter) {
|
2014-12-30 20:45:15 +01:00
|
|
|
if (packetListener == null) {
|
|
|
|
throw new NullPointerException("Packet listener is null.");
|
|
|
|
}
|
|
|
|
ListenerWrapper wrapper = new ListenerWrapper(packetListener, packetFilter);
|
|
|
|
synchronized (asyncRecvListeners) {
|
|
|
|
asyncRecvListeners.put(packetListener, wrapper);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-03-01 10:28:15 +01:00
|
|
|
public boolean removeAsyncStanzaListener(StanzaListener packetListener) {
|
2014-12-30 20:45:15 +01:00
|
|
|
synchronized (asyncRecvListeners) {
|
|
|
|
return asyncRecvListeners.remove(packetListener) != null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 15:02:55 +01:00
|
|
|
@Override
|
|
|
|
public void addStanzaSendingListener(StanzaListener packetListener, StanzaFilter packetFilter) {
|
2014-05-25 12:28:08 +02:00
|
|
|
if (packetListener == null) {
|
|
|
|
throw new NullPointerException("Packet listener is null.");
|
|
|
|
}
|
|
|
|
ListenerWrapper wrapper = new ListenerWrapper(packetListener, packetFilter);
|
2014-10-10 00:36:58 +02:00
|
|
|
synchronized (sendListeners) {
|
|
|
|
sendListeners.put(packetListener, wrapper);
|
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
2018-02-28 15:02:55 +01:00
|
|
|
@Override
|
|
|
|
public void removeStanzaSendingListener(StanzaListener packetListener) {
|
2014-10-10 00:36:58 +02:00
|
|
|
synchronized (sendListeners) {
|
|
|
|
sendListeners.remove(packetListener);
|
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-04 08:59:39 +01:00
|
|
|
* Process all stanza listeners for sending stanzas.
|
2014-10-25 11:39:16 +02:00
|
|
|
* <p>
|
2015-02-05 11:17:27 +01:00
|
|
|
* Compared to {@link #firePacketInterceptors(Stanza)}, the listeners will be invoked in a new thread.
|
2014-10-25 11:39:16 +02:00
|
|
|
* </p>
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2019-02-04 08:59:39 +01:00
|
|
|
* @param sendTopLevelStreamElement the top level stream element which just got send.
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
2019-02-04 08:59:39 +01:00
|
|
|
// TODO: Rename to fireElementSendingListeners().
|
2014-10-25 11:39:16 +02:00
|
|
|
@SuppressWarnings("javadoc")
|
2019-02-04 08:59:39 +01:00
|
|
|
protected void firePacketSendingListeners(final TopLevelStreamElement sendTopLevelStreamElement) {
|
2017-07-28 11:59:11 +02:00
|
|
|
if (debugger != null) {
|
2019-02-04 08:59:39 +01:00
|
|
|
debugger.onOutgoingStreamElement(sendTopLevelStreamElement);
|
2017-07-28 11:59:11 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
if (!(sendTopLevelStreamElement instanceof Stanza)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Stanza packet = (Stanza) sendTopLevelStreamElement;
|
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
final List<StanzaListener> listenersToNotify = new LinkedList<>();
|
2014-10-10 00:36:58 +02:00
|
|
|
synchronized (sendListeners) {
|
|
|
|
for (ListenerWrapper listenerWrapper : sendListeners.values()) {
|
|
|
|
if (listenerWrapper.filterMatches(packet)) {
|
|
|
|
listenersToNotify.add(listenerWrapper.getListener());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-25 11:39:16 +02:00
|
|
|
if (listenersToNotify.isEmpty()) {
|
|
|
|
return;
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
2014-10-25 11:39:16 +02:00
|
|
|
// Notify in a new thread, because we can
|
2014-12-17 18:21:54 +01:00
|
|
|
asyncGo(new Runnable() {
|
2014-10-25 11:39:16 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-03-01 10:28:15 +01:00
|
|
|
for (StanzaListener listener : listenersToNotify) {
|
2014-10-25 11:39:16 +02:00
|
|
|
try {
|
2017-01-03 11:12:34 +01:00
|
|
|
listener.processStanza(packet);
|
2014-10-25 11:39:16 +02:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Sending listener threw exception", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-05-23 16:45:04 +02:00
|
|
|
}
|
|
|
|
});
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 13:57:18 +02:00
|
|
|
@Deprecated
|
2018-02-28 15:02:55 +01:00
|
|
|
@Override
|
|
|
|
public void addStanzaInterceptor(StanzaListener packetInterceptor,
|
2015-02-26 18:41:17 +01:00
|
|
|
StanzaFilter packetFilter) {
|
2014-05-25 12:28:08 +02:00
|
|
|
if (packetInterceptor == null) {
|
|
|
|
throw new NullPointerException("Packet interceptor is null.");
|
|
|
|
}
|
2014-10-10 12:22:23 +02:00
|
|
|
InterceptorWrapper interceptorWrapper = new InterceptorWrapper(packetInterceptor, packetFilter);
|
|
|
|
synchronized (interceptors) {
|
|
|
|
interceptors.put(packetInterceptor, interceptorWrapper);
|
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 13:57:18 +02:00
|
|
|
@Deprecated
|
2018-02-28 15:02:55 +01:00
|
|
|
@Override
|
|
|
|
public void removeStanzaInterceptor(StanzaListener packetInterceptor) {
|
2014-10-10 12:22:23 +02:00
|
|
|
synchronized (interceptors) {
|
|
|
|
interceptors.remove(packetInterceptor);
|
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
2019-10-25 13:57:18 +02:00
|
|
|
private static <MPB extends MessageOrPresenceBuilder<MP, MPB>, MP extends MessageOrPresence<MPB>> void addInterceptor(
|
|
|
|
Map<Consumer<MPB>, GenericInterceptorWrapper<MPB, MP>> interceptors, Consumer<MPB> interceptor,
|
|
|
|
Predicate<MP> filter) {
|
|
|
|
Objects.requireNonNull(interceptor, "Interceptor must not be null");
|
|
|
|
|
|
|
|
GenericInterceptorWrapper<MPB, MP> interceptorWrapper = new GenericInterceptorWrapper<>(interceptor, filter);
|
|
|
|
|
|
|
|
synchronized (interceptors) {
|
|
|
|
interceptors.put(interceptor, interceptorWrapper);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static <MPB extends MessageOrPresenceBuilder<MP, MPB>, MP extends MessageOrPresence<MPB>> void removeInterceptor(
|
|
|
|
Map<Consumer<MPB>, GenericInterceptorWrapper<MPB, MP>> interceptors, Consumer<MPB> interceptor) {
|
|
|
|
synchronized (interceptors) {
|
|
|
|
interceptors.remove(interceptor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addMessageInterceptor(Consumer<MessageBuilder> messageInterceptor, Predicate<Message> messageFilter) {
|
|
|
|
addInterceptor(messageInterceptors, messageInterceptor, messageFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeMessageInterceptor(Consumer<MessageBuilder> messageInterceptor) {
|
|
|
|
removeInterceptor(messageInterceptors, messageInterceptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addPresenceInterceptor(Consumer<PresenceBuilder> presenceInterceptor,
|
|
|
|
Predicate<Presence> presenceFilter) {
|
|
|
|
addInterceptor(presenceInterceptors, presenceInterceptor, presenceFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removePresenceInterceptor(Consumer<PresenceBuilder> presenceInterceptor) {
|
|
|
|
removeInterceptor(presenceInterceptors, presenceInterceptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static <MPB extends MessageOrPresenceBuilder<MP, MPB>, MP extends MessageOrPresence<MPB>> MP fireMessageOrPresenceInterceptors(
|
|
|
|
MP messageOrPresence, Map<Consumer<MPB>, GenericInterceptorWrapper<MPB, MP>> interceptors) {
|
|
|
|
List<Consumer<MPB>> interceptorsToInvoke = new LinkedList<>();
|
|
|
|
synchronized (interceptors) {
|
|
|
|
for (GenericInterceptorWrapper<MPB, MP> interceptorWrapper : interceptors.values()) {
|
|
|
|
if (interceptorWrapper.filterMatches(messageOrPresence)) {
|
|
|
|
Consumer<MPB> interceptor = interceptorWrapper.getInterceptor();
|
|
|
|
interceptorsToInvoke.add(interceptor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid transforming the stanza to a builder if there is no interceptor.
|
|
|
|
if (interceptorsToInvoke.isEmpty()) {
|
|
|
|
return messageOrPresence;
|
|
|
|
}
|
|
|
|
|
|
|
|
MPB builder = messageOrPresence.asBuilder();
|
|
|
|
for (Consumer<MPB> interceptor : interceptorsToInvoke) {
|
|
|
|
interceptor.accept(builder);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now that the interceptors have (probably) modified the stanza in its builder form, we need to re-assemble it.
|
|
|
|
messageOrPresence = builder.build();
|
|
|
|
return messageOrPresence;
|
|
|
|
}
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* Process interceptors. Interceptors may modify the stanza that is about to be sent.
|
|
|
|
* Since the thread that requested to send the stanza will invoke all interceptors, it
|
2014-05-25 12:28:08 +02:00
|
|
|
* is important that interceptors perform their work as soon as possible so that the
|
|
|
|
* thread does not remain blocked for a long period.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2019-10-25 13:57:18 +02:00
|
|
|
* @param packet the stanza that is going to be sent to the server.
|
|
|
|
* @return the, potentially modified stanza, after the interceptors are run.
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
2019-10-25 13:57:18 +02:00
|
|
|
private Stanza firePacketInterceptors(Stanza packet) {
|
2017-12-13 23:10:11 +01:00
|
|
|
List<StanzaListener> interceptorsToInvoke = new LinkedList<>();
|
2014-10-10 12:22:23 +02:00
|
|
|
synchronized (interceptors) {
|
2014-05-25 12:28:08 +02:00
|
|
|
for (InterceptorWrapper interceptorWrapper : interceptors.values()) {
|
2014-10-10 12:22:23 +02:00
|
|
|
if (interceptorWrapper.filterMatches(packet)) {
|
|
|
|
interceptorsToInvoke.add(interceptorWrapper.getInterceptor());
|
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-01 10:28:15 +01:00
|
|
|
for (StanzaListener interceptor : interceptorsToInvoke) {
|
2014-10-12 19:19:44 +02:00
|
|
|
try {
|
2017-01-03 11:12:34 +01:00
|
|
|
interceptor.processStanza(packet);
|
2014-10-12 19:19:44 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
LOGGER.log(Level.SEVERE, "Packet interceptor threw exception", e);
|
|
|
|
}
|
2014-10-10 12:22:23 +02:00
|
|
|
}
|
2019-10-25 13:57:18 +02:00
|
|
|
|
|
|
|
final Stanza stanzaAfterInterceptors;
|
|
|
|
if (packet instanceof Message) {
|
|
|
|
Message message = (Message) packet;
|
|
|
|
stanzaAfterInterceptors = fireMessageOrPresenceInterceptors(message, messageInterceptors);
|
|
|
|
}
|
|
|
|
else if (packet instanceof Presence) {
|
|
|
|
Presence presence = (Presence) packet;
|
|
|
|
stanzaAfterInterceptors = fireMessageOrPresenceInterceptors(presence, presenceInterceptors);
|
|
|
|
} else {
|
|
|
|
// We do not (yet) support interceptors for IQ stanzas.
|
|
|
|
assert packet instanceof IQ;
|
|
|
|
stanzaAfterInterceptors = packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stanzaAfterInterceptors;
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the {@link #debugger}. You can specify a customized {@link SmackDebugger}
|
|
|
|
* by setup the system property <code>smack.debuggerClass</code> to the implementation.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-05-25 12:28:08 +02:00
|
|
|
* @throws IllegalStateException if the reader or writer isn't yet initialized.
|
|
|
|
* @throws IllegalArgumentException if the SmackDebugger can't be loaded.
|
|
|
|
*/
|
|
|
|
protected void initDebugger() {
|
|
|
|
if (reader == null || writer == null) {
|
|
|
|
throw new NullPointerException("Reader or writer isn't initialized.");
|
|
|
|
}
|
|
|
|
// If debugging is enabled, we open a window and write out all network traffic.
|
2017-07-28 11:59:11 +02:00
|
|
|
if (debugger != null) {
|
|
|
|
// Obtain new reader and writer from the existing debugger
|
|
|
|
reader = debugger.newConnectionReader(reader);
|
|
|
|
writer = debugger.newConnectionWriter(writer);
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 20:57:19 +01:00
|
|
|
@Override
|
|
|
|
public long getReplyTimeout() {
|
|
|
|
return replyTimeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setReplyTimeout(long timeout) {
|
2019-08-22 15:28:54 +02:00
|
|
|
if (Long.MAX_VALUE - System.currentTimeMillis() < timeout) {
|
|
|
|
throw new IllegalArgumentException("Extremely long reply timeout");
|
2019-04-26 17:00:26 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-08-22 15:28:54 +02:00
|
|
|
replyTimeout = timeout;
|
2019-04-26 17:00:26 +02:00
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 13:18:15 +02:00
|
|
|
private SmackConfiguration.UnknownIqRequestReplyMode unknownIqRequestReplyMode = SmackConfiguration.getUnknownIqRequestReplyMode();
|
|
|
|
|
2017-12-23 20:21:19 +01:00
|
|
|
/**
|
|
|
|
* Set how Smack behaves when an unknown IQ request has been received.
|
|
|
|
*
|
|
|
|
* @param unknownIqRequestReplyMode reply mode.
|
|
|
|
*/
|
2017-05-03 13:18:15 +02:00
|
|
|
public void setUnknownIqRequestReplyMode(UnknownIqRequestReplyMode unknownIqRequestReplyMode) {
|
|
|
|
this.unknownIqRequestReplyMode = Objects.requireNonNull(unknownIqRequestReplyMode, "Mode must not be null");
|
|
|
|
}
|
2015-01-23 20:12:21 +01:00
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
protected final NonzaCallback.Builder buildNonzaCallback() {
|
|
|
|
return new NonzaCallback.Builder(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected <SN extends Nonza, FN extends Nonza> SN sendAndWaitForResponse(Nonza nonza, Class<SN> successNonzaClass,
|
|
|
|
Class<FN> failedNonzaClass)
|
|
|
|
throws NoResponseException, NotConnectedException, InterruptedException, FailedNonzaException {
|
|
|
|
NonzaCallback.Builder builder = buildNonzaCallback();
|
|
|
|
SN successNonza = NonzaCallback.sendAndWaitForResponse(builder, nonza, successNonzaClass, failedNonzaClass);
|
|
|
|
return successNonza;
|
|
|
|
}
|
|
|
|
|
2019-02-10 19:01:47 +01:00
|
|
|
protected final void parseAndProcessNonza(XmlPullParser parser) throws IOException, XmlPullParserException, SmackParsingException {
|
2019-09-24 23:32:08 +02:00
|
|
|
ParserUtils.assertAtStartTag(parser);
|
|
|
|
|
|
|
|
final int initialDepth = parser.getDepth();
|
2019-02-04 08:59:39 +01:00
|
|
|
final String element = parser.getName();
|
|
|
|
final String namespace = parser.getNamespace();
|
2019-05-08 21:10:39 +02:00
|
|
|
final QName key = new QName(namespace, element);
|
2019-02-04 08:59:39 +01:00
|
|
|
|
|
|
|
NonzaProvider<? extends Nonza> nonzaProvider = ProviderManager.getNonzaProvider(key);
|
|
|
|
if (nonzaProvider == null) {
|
|
|
|
LOGGER.severe("Unknown nonza: " + key);
|
2019-09-24 23:32:08 +02:00
|
|
|
ParserUtils.forwardToEndTagOfDepth(parser, initialDepth);
|
2019-02-04 08:59:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-24 23:32:08 +02:00
|
|
|
List<NonzaCallback> nonzaCallbacks;
|
|
|
|
synchronized (nonzaCallbacksMap) {
|
|
|
|
nonzaCallbacks = nonzaCallbacksMap.getAll(key);
|
|
|
|
nonzaCallbacks = CollectionUtil.newListWith(nonzaCallbacks);
|
2019-02-04 08:59:39 +01:00
|
|
|
}
|
2019-09-24 23:32:08 +02:00
|
|
|
if (nonzaCallbacks == null) {
|
2019-02-04 08:59:39 +01:00
|
|
|
LOGGER.info("No nonza callback for " + key);
|
2019-09-24 23:32:08 +02:00
|
|
|
ParserUtils.forwardToEndTagOfDepth(parser, initialDepth);
|
2019-02-04 08:59:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-15 23:21:30 +01:00
|
|
|
Nonza nonza = nonzaProvider.parse(parser, incomingStreamXmlEnvironment);
|
2019-02-04 08:59:39 +01:00
|
|
|
|
2019-09-24 23:32:08 +02:00
|
|
|
for (NonzaCallback nonzaCallback : nonzaCallbacks) {
|
|
|
|
nonzaCallback.onNonzaReceived(nonza);
|
|
|
|
}
|
2019-02-04 08:59:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 10:41:50 +01:00
|
|
|
protected void parseAndProcessStanza(XmlPullParser parser)
|
|
|
|
throws XmlPullParserException, IOException, InterruptedException {
|
2014-12-28 17:43:39 +01:00
|
|
|
ParserUtils.assertAtStartTag(parser);
|
|
|
|
int parserDepth = parser.getDepth();
|
2015-02-05 11:17:27 +01:00
|
|
|
Stanza stanza = null;
|
2014-12-28 17:43:39 +01:00
|
|
|
try {
|
2019-02-15 23:21:30 +01:00
|
|
|
stanza = PacketParserUtils.parseStanza(parser, incomingStreamXmlEnvironment);
|
2014-12-28 17:43:39 +01:00
|
|
|
}
|
2019-08-27 09:00:22 +02:00
|
|
|
catch (XmlPullParserException | SmackParsingException | IOException | IllegalArgumentException e) {
|
2014-12-28 17:43:39 +01:00
|
|
|
CharSequence content = PacketParserUtils.parseContentDepth(parser,
|
|
|
|
parserDepth);
|
2015-05-18 12:35:13 +02:00
|
|
|
UnparseableStanza message = new UnparseableStanza(content, e);
|
2014-12-28 17:43:39 +01:00
|
|
|
ParsingExceptionCallback callback = getParsingExceptionCallback();
|
|
|
|
if (callback != null) {
|
2015-05-18 12:35:13 +02:00
|
|
|
callback.handleUnparsableStanza(message);
|
2014-12-28 17:43:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ParserUtils.assertAtEndTag(parser);
|
|
|
|
if (stanza != null) {
|
2015-06-05 14:04:17 +02:00
|
|
|
processStanza(stanza);
|
2014-12-28 17:43:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* Processes a stanza after it's been fully parsed by looping through the installed
|
|
|
|
* stanza collectors and listeners and letting them examine the stanza to see if
|
2014-05-25 12:28:08 +02:00
|
|
|
* they are a match with the filter.
|
|
|
|
*
|
2015-06-05 14:04:17 +02:00
|
|
|
* @param stanza the stanza to process.
|
2019-08-30 12:08:30 +02:00
|
|
|
* @throws InterruptedException if the calling thread was interrupted.
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
2015-11-22 23:14:02 +01:00
|
|
|
protected void processStanza(final Stanza stanza) throws InterruptedException {
|
2019-07-24 09:18:39 +02:00
|
|
|
assert stanza != null;
|
2017-07-28 11:59:11 +02:00
|
|
|
|
|
|
|
final SmackDebugger debugger = this.debugger;
|
|
|
|
if (debugger != null) {
|
|
|
|
debugger.onIncomingStreamElement(stanza);
|
|
|
|
}
|
|
|
|
|
2014-12-28 00:47:31 +01:00
|
|
|
lastStanzaReceived = System.currentTimeMillis();
|
2014-05-25 12:28:08 +02:00
|
|
|
// Deliver the incoming packet to listeners.
|
2018-02-23 16:48:15 +01:00
|
|
|
invokeStanzaCollectorsAndNotifyRecvListeners(stanza);
|
2014-10-30 23:36:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-03 11:12:34 +01:00
|
|
|
* Invoke {@link StanzaCollector#processStanza(Stanza)} for every
|
2018-03-31 14:17:30 +02:00
|
|
|
* StanzaCollector with the given packet. Also notify the receive listeners with a matching stanza filter about the packet.
|
2018-02-23 16:48:15 +01:00
|
|
|
* <p>
|
|
|
|
* This method will be invoked by the connections incoming processing thread which may be shared across multiple connections and
|
|
|
|
* thus it is important that no user code, e.g. in form of a callback, is invoked by this method. For the same reason,
|
|
|
|
* this method must not block for an extended period of time.
|
|
|
|
* </p>
|
2014-10-30 23:36:40 +01:00
|
|
|
*
|
2018-03-31 14:17:30 +02:00
|
|
|
* @param packet the stanza to notify the StanzaCollectors and receive listeners about.
|
2014-10-30 23:36:40 +01:00
|
|
|
*/
|
2017-01-03 11:12:34 +01:00
|
|
|
protected void invokeStanzaCollectorsAndNotifyRecvListeners(final Stanza packet) {
|
2015-01-08 11:01:35 +01:00
|
|
|
if (packet instanceof IQ) {
|
|
|
|
final IQ iq = (IQ) packet;
|
2017-08-16 21:06:23 +02:00
|
|
|
if (iq.isRequestIQ()) {
|
2018-12-14 17:18:53 +01:00
|
|
|
final IQ iqRequest = iq;
|
2019-06-11 00:10:38 +02:00
|
|
|
final QName key = iqRequest.getChildElementQName();
|
2017-12-13 23:10:11 +01:00
|
|
|
IQRequestHandler iqRequestHandler;
|
2017-08-16 21:06:23 +02:00
|
|
|
final IQ.Type type = iq.getType();
|
2015-01-08 11:01:35 +01:00
|
|
|
switch (type) {
|
|
|
|
case set:
|
|
|
|
synchronized (setIqRequestHandler) {
|
|
|
|
iqRequestHandler = setIqRequestHandler.get(key);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case get:
|
|
|
|
synchronized (getIqRequestHandler) {
|
|
|
|
iqRequestHandler = getIqRequestHandler.get(key);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IllegalStateException("Should only encounter IQ type 'get' or 'set'");
|
|
|
|
}
|
|
|
|
if (iqRequestHandler == null) {
|
2018-04-07 21:25:40 +02:00
|
|
|
StanzaError.Condition replyCondition;
|
2017-05-03 13:18:15 +02:00
|
|
|
switch (unknownIqRequestReplyMode) {
|
|
|
|
case doNotReply:
|
2015-01-23 20:12:21 +01:00
|
|
|
return;
|
2017-05-03 13:18:15 +02:00
|
|
|
case replyFeatureNotImplemented:
|
2018-04-07 21:25:40 +02:00
|
|
|
replyCondition = StanzaError.Condition.feature_not_implemented;
|
2017-05-03 13:18:15 +02:00
|
|
|
break;
|
|
|
|
case replyServiceUnavailable:
|
2018-04-07 21:25:40 +02:00
|
|
|
replyCondition = StanzaError.Condition.service_unavailable;
|
2017-05-03 13:18:15 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new AssertionError();
|
2015-01-23 20:12:21 +01:00
|
|
|
}
|
2017-05-03 13:18:15 +02:00
|
|
|
|
2015-01-08 11:01:35 +01:00
|
|
|
// If the IQ stanza is of type "get" or "set" with no registered IQ request handler, then answer an
|
2015-12-06 05:31:44 +01:00
|
|
|
// IQ of type 'error' with condition 'service-unavailable'.
|
2019-02-04 08:59:39 +01:00
|
|
|
final ErrorIQ errorIQ = IQ.createErrorResponse(iq, StanzaError.getBuilder(
|
2019-10-24 15:45:08 +02:00
|
|
|
replyCondition).build());
|
2019-02-04 08:59:39 +01:00
|
|
|
// Use async sendStanza() here, since if sendStanza() would block, then some connections, e.g.
|
|
|
|
// XmppNioTcpConnection, would deadlock, as this operation is performed in the same thread that is
|
|
|
|
asyncGo(() -> {
|
|
|
|
try {
|
|
|
|
sendStanza(errorIQ);
|
|
|
|
}
|
|
|
|
catch (InterruptedException | NotConnectedException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Exception while sending error IQ to unkown IQ request", e);
|
|
|
|
}
|
|
|
|
});
|
2015-01-08 11:01:35 +01:00
|
|
|
} else {
|
2018-04-08 21:21:50 +02:00
|
|
|
Executor executorService = null;
|
2015-01-08 11:01:35 +01:00
|
|
|
switch (iqRequestHandler.getMode()) {
|
|
|
|
case sync:
|
2018-04-08 21:21:50 +02:00
|
|
|
executorService = ASYNC_BUT_ORDERED.asExecutorFor(this);
|
2015-01-08 11:01:35 +01:00
|
|
|
break;
|
|
|
|
case async:
|
2019-04-10 12:18:06 +02:00
|
|
|
executorService = limitedExcutor;
|
2015-01-08 11:01:35 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
final IQRequestHandler finalIqRequestHandler = iqRequestHandler;
|
|
|
|
executorService.execute(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
IQ response = finalIqRequestHandler.handleIQRequest(iq);
|
|
|
|
if (response == null) {
|
|
|
|
// It is not ideal if the IQ request handler does not return an IQ response, because RFC
|
|
|
|
// 6120 § 8.1.2 does specify that a response is mandatory. But some APIs, mostly the
|
2015-01-23 20:12:21 +01:00
|
|
|
// file transfer one, does not always return a result, so we need to handle this case.
|
|
|
|
// Also sometimes a request handler may decide that it's better to not send a response,
|
|
|
|
// e.g. to avoid presence leaks.
|
2015-01-08 11:01:35 +01:00
|
|
|
return;
|
|
|
|
}
|
2018-12-14 17:18:53 +01:00
|
|
|
|
2019-04-08 23:04:46 +02:00
|
|
|
assert response.isResponseIQ();
|
2018-12-14 17:18:53 +01:00
|
|
|
|
|
|
|
response.setTo(iqRequest.getFrom());
|
|
|
|
response.setStanzaId(iqRequest.getStanzaId());
|
2015-01-08 11:01:35 +01:00
|
|
|
try {
|
2015-03-04 21:44:43 +01:00
|
|
|
sendStanza(response);
|
2015-01-08 11:01:35 +01:00
|
|
|
}
|
2015-02-14 09:43:44 +01:00
|
|
|
catch (InterruptedException | NotConnectedException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Exception while sending response to IQ request", e);
|
2015-01-08 11:01:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-08-16 21:06:23 +02:00
|
|
|
// The following returns makes it impossible for packet listeners and collectors to
|
|
|
|
// filter for IQ request stanzas, i.e. IQs of type 'set' or 'get'. This is the
|
|
|
|
// desired behavior.
|
|
|
|
return;
|
2015-01-08 11:01:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 20:45:15 +01:00
|
|
|
// First handle the async recv listeners. Note that this code is very similar to what follows a few lines below,
|
|
|
|
// the only difference is that asyncRecvListeners is used here and that the packet listeners are started in
|
|
|
|
// their own thread.
|
2017-12-13 23:10:11 +01:00
|
|
|
final Collection<StanzaListener> listenersToNotify = new LinkedList<>();
|
2019-02-04 21:58:35 +01:00
|
|
|
extractMatchingListeners(packet, asyncRecvListeners, listenersToNotify);
|
2015-03-01 10:28:15 +01:00
|
|
|
for (final StanzaListener listener : listenersToNotify) {
|
2019-04-10 12:18:06 +02:00
|
|
|
asyncGoLimited(new Runnable() {
|
2014-12-30 20:45:15 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2017-01-03 11:12:34 +01:00
|
|
|
listener.processStanza(packet);
|
2014-12-30 20:45:15 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
LOGGER.log(Level.SEVERE, "Exception in async packet listener", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-30 23:36:40 +01:00
|
|
|
// Loop through all collectors and notify the appropriate ones.
|
2017-05-23 16:45:04 +02:00
|
|
|
for (StanzaCollector collector : collectors) {
|
2017-01-03 11:12:34 +01:00
|
|
|
collector.processStanza(packet);
|
2014-10-30 23:36:40 +01:00
|
|
|
}
|
|
|
|
|
2015-01-05 21:42:35 +01:00
|
|
|
listenersToNotify.clear();
|
2019-02-04 21:58:35 +01:00
|
|
|
extractMatchingListeners(packet, recvListeners, listenersToNotify);
|
|
|
|
for (StanzaListener stanzaListener : listenersToNotify) {
|
|
|
|
inOrderListeners.performAsyncButOrdered(stanzaListener, () -> {
|
|
|
|
try {
|
|
|
|
stanzaListener.processStanza(packet);
|
2014-10-10 00:36:58 +02:00
|
|
|
}
|
2019-02-04 21:58:35 +01:00
|
|
|
catch (NotConnectedException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Got not connected exception, aborting", e);
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
LOGGER.log(Level.SEVERE, "Exception in packet listener", e);
|
|
|
|
}
|
|
|
|
});
|
2014-10-10 00:36:58 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 21:58:35 +01:00
|
|
|
// Notify the receive listeners interested in the packet
|
|
|
|
listenersToNotify.clear();
|
|
|
|
extractMatchingListeners(packet, syncRecvListeners, listenersToNotify);
|
2015-01-05 21:42:35 +01:00
|
|
|
// Decouple incoming stanza processing from listener invocation. Unlike async listeners, this uses a single
|
|
|
|
// threaded executor service and therefore keeps the order.
|
2018-04-08 21:21:50 +02:00
|
|
|
ASYNC_BUT_ORDERED.performAsyncButOrdered(this, new Runnable() {
|
2015-01-05 21:42:35 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2018-11-02 21:51:09 +01:00
|
|
|
// As listeners are able to remove themselves and because the timepoint where it is decided to invoke a
|
|
|
|
// listener is a different timepoint where the listener is actually invoked (here), we have to check
|
|
|
|
// again if the listener is still active.
|
|
|
|
Iterator<StanzaListener> it = listenersToNotify.iterator();
|
|
|
|
synchronized (syncRecvListeners) {
|
|
|
|
while (it.hasNext()) {
|
|
|
|
StanzaListener stanzaListener = it.next();
|
|
|
|
if (!syncRecvListeners.containsKey(stanzaListener)) {
|
|
|
|
// The listener was removed from syncRecvListener, also remove him from listenersToNotify.
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-01 10:28:15 +01:00
|
|
|
for (StanzaListener listener : listenersToNotify) {
|
2015-01-05 21:42:35 +01:00
|
|
|
try {
|
2017-01-03 11:12:34 +01:00
|
|
|
listener.processStanza(packet);
|
2017-05-23 16:45:04 +02:00
|
|
|
} catch (NotConnectedException e) {
|
2015-01-05 21:42:35 +01:00
|
|
|
LOGGER.log(Level.WARNING, "Got not connected exception, aborting", e);
|
|
|
|
break;
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOGGER.log(Level.SEVERE, "Exception in packet listener", e);
|
|
|
|
}
|
|
|
|
}
|
2014-10-10 00:36:58 +02:00
|
|
|
}
|
2015-01-05 21:42:35 +01:00
|
|
|
});
|
2014-10-10 00:36:58 +02:00
|
|
|
}
|
|
|
|
|
2019-02-04 21:58:35 +01:00
|
|
|
private static void extractMatchingListeners(Stanza stanza, Map<StanzaListener, ListenerWrapper> listeners,
|
|
|
|
Collection<StanzaListener> listenersToNotify) {
|
|
|
|
synchronized (listeners) {
|
|
|
|
for (ListenerWrapper listenerWrapper : listeners.values()) {
|
|
|
|
if (listenerWrapper.filterMatches(stanza)) {
|
|
|
|
listenersToNotify.add(listenerWrapper.getListener());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
/**
|
|
|
|
* Sets whether the connection has already logged in the server. This method assures that the
|
|
|
|
* {@link #wasAuthenticated} flag is never reset once it has ever been set.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
2014-11-09 18:30:16 +01:00
|
|
|
protected void setWasAuthenticated() {
|
2014-05-25 12:28:08 +02:00
|
|
|
// Never reset the flag if the connection has ever been authenticated
|
|
|
|
if (!wasAuthenticated) {
|
|
|
|
wasAuthenticated = authenticated;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void callConnectionConnectedListener() {
|
2015-01-10 01:23:20 +01:00
|
|
|
for (ConnectionListener listener : connectionListeners) {
|
2014-05-25 12:28:08 +02:00
|
|
|
listener.connected(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 20:19:04 +01:00
|
|
|
protected void callConnectionAuthenticatedListener(boolean resumed) {
|
2015-01-10 01:23:20 +01:00
|
|
|
for (ConnectionListener listener : connectionListeners) {
|
2015-01-07 20:19:04 +01:00
|
|
|
try {
|
|
|
|
listener.authenticated(this, resumed);
|
|
|
|
} catch (Exception e) {
|
|
|
|
// Catch and print any exception so we can recover
|
|
|
|
// from a faulty listener and finish the shutdown process
|
|
|
|
LOGGER.log(Level.SEVERE, "Exception in authenticated listener", e);
|
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void callConnectionClosedListener() {
|
2015-01-10 01:23:20 +01:00
|
|
|
for (ConnectionListener listener : connectionListeners) {
|
2014-05-25 12:28:08 +02:00
|
|
|
try {
|
|
|
|
listener.connectionClosed();
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
// Catch and print any exception so we can recover
|
|
|
|
// from a faulty listener and finish the shutdown process
|
|
|
|
LOGGER.log(Level.SEVERE, "Error in listener while closing connection", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
private void callConnectionClosedOnErrorListener(Exception e) {
|
2015-03-18 09:52:33 +01:00
|
|
|
boolean logWarning = true;
|
|
|
|
if (e instanceof StreamErrorException) {
|
|
|
|
StreamErrorException see = (StreamErrorException) e;
|
|
|
|
if (see.getStreamError().getCondition() == StreamError.Condition.not_authorized
|
|
|
|
&& wasAuthenticated) {
|
|
|
|
logWarning = false;
|
|
|
|
LOGGER.log(Level.FINE,
|
|
|
|
"Connection closed with not-authorized stream error after it was already authenticated. The account was likely deleted/unregistered on the server");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (logWarning) {
|
2015-06-09 22:25:19 +02:00
|
|
|
LOGGER.log(Level.WARNING, "Connection " + this + " closed with error", e);
|
2015-03-18 09:52:33 +01:00
|
|
|
}
|
2015-01-10 01:23:20 +01:00
|
|
|
for (ConnectionListener listener : connectionListeners) {
|
2014-05-25 12:28:08 +02:00
|
|
|
try {
|
|
|
|
listener.connectionClosedOnError(e);
|
|
|
|
}
|
|
|
|
catch (Exception e2) {
|
|
|
|
// Catch and print any exception so we can recover
|
|
|
|
// from a faulty listener
|
|
|
|
LOGGER.log(Level.SEVERE, "Error in listener while closing connection", e2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* A wrapper class to associate a stanza filter with a listener.
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
|
|
|
protected static class ListenerWrapper {
|
|
|
|
|
2015-03-01 10:28:15 +01:00
|
|
|
private final StanzaListener packetListener;
|
2015-02-26 18:41:17 +01:00
|
|
|
private final StanzaFilter packetFilter;
|
2014-05-25 12:28:08 +02:00
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* Create a class which associates a stanza filter with a listener.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2018-03-31 14:17:30 +02:00
|
|
|
* @param packetListener the stanza listener.
|
2014-05-25 12:28:08 +02:00
|
|
|
* @param packetFilter the associated filter or null if it listen for all packets.
|
|
|
|
*/
|
2015-03-01 10:28:15 +01:00
|
|
|
public ListenerWrapper(StanzaListener packetListener, StanzaFilter packetFilter) {
|
2014-05-25 12:28:08 +02:00
|
|
|
this.packetListener = packetListener;
|
|
|
|
this.packetFilter = packetFilter;
|
|
|
|
}
|
|
|
|
|
2015-02-05 11:17:27 +01:00
|
|
|
public boolean filterMatches(Stanza packet) {
|
2014-10-10 00:36:58 +02:00
|
|
|
return packetFilter == null || packetFilter.accept(packet);
|
|
|
|
}
|
|
|
|
|
2015-03-01 10:28:15 +01:00
|
|
|
public StanzaListener getListener() {
|
2014-10-10 00:36:58 +02:00
|
|
|
return packetListener;
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* A wrapper class to associate a stanza filter with an interceptor.
|
2014-05-25 12:28:08 +02:00
|
|
|
*/
|
2019-10-25 13:57:18 +02:00
|
|
|
@Deprecated
|
|
|
|
// TODO: Remove once addStanzaInterceptor is gone.
|
2014-05-25 12:28:08 +02:00
|
|
|
protected static class InterceptorWrapper {
|
|
|
|
|
2015-03-01 10:28:15 +01:00
|
|
|
private final StanzaListener packetInterceptor;
|
2015-02-26 18:41:17 +01:00
|
|
|
private final StanzaFilter packetFilter;
|
2014-05-25 12:28:08 +02:00
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* Create a class which associates a stanza filter with an interceptor.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-05-25 12:28:08 +02:00
|
|
|
* @param packetInterceptor the interceptor.
|
|
|
|
* @param packetFilter the associated filter or null if it intercepts all packets.
|
|
|
|
*/
|
2015-03-01 10:28:15 +01:00
|
|
|
public InterceptorWrapper(StanzaListener packetInterceptor, StanzaFilter packetFilter) {
|
2014-05-25 12:28:08 +02:00
|
|
|
this.packetInterceptor = packetInterceptor;
|
|
|
|
this.packetFilter = packetFilter;
|
|
|
|
}
|
|
|
|
|
2015-02-05 11:17:27 +01:00
|
|
|
public boolean filterMatches(Stanza packet) {
|
2014-10-10 12:22:23 +02:00
|
|
|
return packetFilter == null || packetFilter.accept(packet);
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
|
2015-03-01 10:28:15 +01:00
|
|
|
public StanzaListener getInterceptor() {
|
2014-10-10 12:22:23 +02:00
|
|
|
return packetInterceptor;
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 13:57:18 +02:00
|
|
|
private static final class GenericInterceptorWrapper<MPB extends MessageOrPresenceBuilder<MP, MPB>, MP extends MessageOrPresence<MPB>> {
|
|
|
|
private final Consumer<MPB> stanzaInterceptor;
|
|
|
|
private final Predicate<MP> stanzaFilter;
|
|
|
|
|
|
|
|
private GenericInterceptorWrapper(Consumer<MPB> stanzaInterceptor, Predicate<MP> stanzaFilter) {
|
|
|
|
this.stanzaInterceptor = stanzaInterceptor;
|
|
|
|
this.stanzaFilter = stanzaFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean filterMatches(MP stanza) {
|
|
|
|
return stanzaFilter == null || stanzaFilter.test(stanza);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Consumer<MPB> getInterceptor() {
|
|
|
|
return stanzaInterceptor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
@Override
|
|
|
|
public int getConnectionCounter() {
|
|
|
|
return connectionCounterValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setFromMode(FromMode fromMode) {
|
|
|
|
this.fromMode = fromMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FromMode getFromMode() {
|
|
|
|
return this.fromMode;
|
|
|
|
}
|
|
|
|
|
2019-02-10 19:01:47 +01:00
|
|
|
protected final void parseFeatures(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
|
2014-09-11 09:49:16 +02:00
|
|
|
streamFeatures.clear();
|
|
|
|
final int initialDepth = parser.getDepth();
|
|
|
|
while (true) {
|
2019-05-06 22:06:13 +02:00
|
|
|
XmlPullParser.Event eventType = parser.next();
|
2014-09-11 09:49:16 +02:00
|
|
|
|
2019-05-06 22:06:13 +02:00
|
|
|
if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getDepth() == initialDepth + 1) {
|
2019-02-04 08:59:39 +01:00
|
|
|
FullyQualifiedElement streamFeature = null;
|
2014-09-11 09:49:16 +02:00
|
|
|
String name = parser.getName();
|
|
|
|
String namespace = parser.getNamespace();
|
|
|
|
switch (name) {
|
|
|
|
case StartTls.ELEMENT:
|
2014-09-11 23:58:07 +02:00
|
|
|
streamFeature = PacketParserUtils.parseStartTlsFeature(parser);
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
case Mechanisms.ELEMENT:
|
2014-09-11 23:58:07 +02:00
|
|
|
streamFeature = new Mechanisms(PacketParserUtils.parseMechanisms(parser));
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
case Bind.ELEMENT:
|
2014-09-11 23:58:07 +02:00
|
|
|
streamFeature = Bind.Feature.INSTANCE;
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
case Session.ELEMENT:
|
2014-12-30 12:37:01 +01:00
|
|
|
streamFeature = PacketParserUtils.parseSessionFeature(parser);
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
case Compress.Feature.ELEMENT:
|
2014-09-11 23:58:07 +02:00
|
|
|
streamFeature = PacketParserUtils.parseCompressionFeature(parser);
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
default:
|
2015-02-26 18:41:17 +01:00
|
|
|
ExtensionElementProvider<ExtensionElement> provider = ProviderManager.getStreamFeatureProvider(name, namespace);
|
2014-09-11 23:58:07 +02:00
|
|
|
if (provider != null) {
|
2019-02-15 23:21:30 +01:00
|
|
|
streamFeature = provider.parse(parser, incomingStreamXmlEnvironment);
|
2014-09-11 23:58:07 +02:00
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-09-11 23:58:07 +02:00
|
|
|
if (streamFeature != null) {
|
|
|
|
addStreamFeature(streamFeature);
|
|
|
|
}
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
2019-05-06 22:06:13 +02:00
|
|
|
else if (eventType == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) {
|
2014-09-11 09:49:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-02-04 08:59:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected final void parseFeaturesAndNotify(XmlPullParser parser) throws Exception {
|
|
|
|
parseFeatures(parser);
|
2014-09-11 09:49:16 +02:00
|
|
|
|
|
|
|
if (hasFeature(Mechanisms.ELEMENT, Mechanisms.NAMESPACE)) {
|
|
|
|
// Only proceed with SASL auth if TLS is disabled or if the server doesn't announce it
|
|
|
|
if (!hasFeature(StartTls.ELEMENT, StartTls.NAMESPACE)
|
|
|
|
|| config.getSecurityMode() == SecurityMode.disabled) {
|
2017-01-06 15:03:28 +01:00
|
|
|
tlsHandled.reportSuccess();
|
2014-09-11 09:49:16 +02:00
|
|
|
saslFeatureReceived.reportSuccess();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the server reported the bind feature then we are that that we did SASL and maybe
|
|
|
|
// STARTTLS. We can then report that the last 'stream:features' have been parsed
|
|
|
|
if (hasFeature(Bind.ELEMENT, Bind.NAMESPACE)) {
|
|
|
|
if (!hasFeature(Compress.Feature.ELEMENT, Compress.NAMESPACE)
|
|
|
|
|| !config.isCompressionEnabled()) {
|
|
|
|
// This was was last features from the server is either it did not contain
|
|
|
|
// compression or if we disabled it
|
|
|
|
lastFeaturesReceived.reportSuccess();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
afterFeaturesReceived();
|
|
|
|
}
|
|
|
|
|
2015-03-24 18:33:47 +01:00
|
|
|
@SuppressWarnings("unused")
|
2015-02-14 09:43:44 +01:00
|
|
|
protected void afterFeaturesReceived() throws SecurityRequiredException, NotConnectedException, InterruptedException {
|
2014-09-11 09:49:16 +02:00
|
|
|
// Default implementation does nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
@Override
|
2019-02-04 08:59:39 +01:00
|
|
|
public <F extends FullyQualifiedElement> F getFeature(String element, String namespace) {
|
2019-06-11 00:10:38 +02:00
|
|
|
QName qname = new QName(namespace, element);
|
|
|
|
return (F) streamFeatures.get(qname);
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasFeature(String element, String namespace) {
|
|
|
|
return getFeature(element, namespace) != null;
|
|
|
|
}
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
protected void addStreamFeature(FullyQualifiedElement feature) {
|
2019-06-11 00:10:38 +02:00
|
|
|
QName key = feature.getQName();
|
2014-09-11 09:49:16 +02:00
|
|
|
streamFeatures.put(key, feature);
|
|
|
|
}
|
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
@Override
|
|
|
|
public SmackFuture<IQ, Exception> sendIqRequestAsync(IQ request) {
|
|
|
|
return sendIqRequestAsync(request, getReplyTimeout());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SmackFuture<IQ, Exception> sendIqRequestAsync(IQ request, long timeout) {
|
|
|
|
StanzaFilter replyFilter = new IQReplyFilter(request, this);
|
|
|
|
return sendAsync(request, replyFilter, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public <S extends Stanza> SmackFuture<S, Exception> sendAsync(S stanza, final StanzaFilter replyFilter) {
|
|
|
|
return sendAsync(stanza, replyFilter, getReplyTimeout());
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("FutureReturnValueIgnored")
|
|
|
|
@Override
|
|
|
|
public <S extends Stanza> SmackFuture<S, Exception> sendAsync(S stanza, final StanzaFilter replyFilter, long timeout) {
|
|
|
|
Objects.requireNonNull(stanza, "stanza must not be null");
|
|
|
|
// While Smack allows to add PacketListeners with a PacketFilter value of 'null', we
|
|
|
|
// disallow it here in the async API as it makes no sense
|
|
|
|
Objects.requireNonNull(replyFilter, "replyFilter must not be null");
|
|
|
|
|
|
|
|
final InternalSmackFuture<S, Exception> future = new InternalSmackFuture<>();
|
|
|
|
|
|
|
|
final StanzaListener stanzaListener = new StanzaListener() {
|
|
|
|
@Override
|
|
|
|
public void processStanza(Stanza stanza) throws NotConnectedException, InterruptedException {
|
|
|
|
boolean removed = removeAsyncStanzaListener(this);
|
|
|
|
if (!removed) {
|
|
|
|
// We lost a race against the "no response" handling runnable. Avoid calling the callback, as the
|
|
|
|
// exception callback will be invoked (if any).
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
XMPPErrorException.ifHasErrorThenThrow(stanza);
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
S s = (S) stanza;
|
|
|
|
future.setResult(s);
|
|
|
|
}
|
|
|
|
catch (XMPPErrorException exception) {
|
|
|
|
future.setException(exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-02-23 18:50:02 +01:00
|
|
|
schedule(new Runnable() {
|
2017-08-12 17:35:20 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
boolean removed = removeAsyncStanzaListener(stanzaListener);
|
|
|
|
if (!removed) {
|
|
|
|
// We lost a race against the stanza listener, he already removed itself because he received a
|
|
|
|
// reply. There is nothing more to do here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the packetListener got removed, then it was never run and
|
|
|
|
// we never received a response, inform the exception callback
|
|
|
|
Exception exception;
|
|
|
|
if (!isConnected()) {
|
|
|
|
// If the connection is no longer connected, throw a not connected exception.
|
|
|
|
exception = new NotConnectedException(AbstractXMPPConnection.this, replyFilter);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
exception = NoResponseException.newWith(AbstractXMPPConnection.this, replyFilter);
|
|
|
|
}
|
|
|
|
future.setException(exception);
|
|
|
|
}
|
|
|
|
}, timeout, TimeUnit.MILLISECONDS);
|
|
|
|
|
|
|
|
addAsyncStanzaListener(stanzaListener, replyFilter);
|
|
|
|
try {
|
|
|
|
sendStanza(stanza);
|
|
|
|
}
|
|
|
|
catch (NotConnectedException | InterruptedException exception) {
|
|
|
|
future.setException(exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2017-03-06 17:17:15 +01:00
|
|
|
@SuppressWarnings("FutureReturnValueIgnored")
|
2015-01-07 19:35:11 +01:00
|
|
|
@Override
|
2015-03-01 10:28:15 +01:00
|
|
|
public void addOneTimeSyncCallback(final StanzaListener callback, final StanzaFilter packetFilter) {
|
|
|
|
final StanzaListener packetListener = new StanzaListener() {
|
2015-01-07 19:35:11 +01:00
|
|
|
@Override
|
2017-11-06 22:38:13 +01:00
|
|
|
public void processStanza(Stanza packet) throws NotConnectedException, InterruptedException, NotLoggedInException {
|
2015-01-07 19:35:11 +01:00
|
|
|
try {
|
2017-01-03 11:12:34 +01:00
|
|
|
callback.processStanza(packet);
|
2015-01-07 19:35:11 +01:00
|
|
|
} finally {
|
2015-03-01 10:28:15 +01:00
|
|
|
removeSyncStanzaListener(this);
|
2015-01-07 19:35:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-03-01 10:28:15 +01:00
|
|
|
addSyncStanzaListener(packetListener, packetFilter);
|
2018-02-23 18:50:02 +01:00
|
|
|
schedule(new Runnable() {
|
2015-01-07 19:35:11 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-03-01 10:28:15 +01:00
|
|
|
removeSyncStanzaListener(packetListener);
|
2015-01-07 19:35:11 +01:00
|
|
|
}
|
2017-01-12 20:57:19 +01:00
|
|
|
}, getReplyTimeout(), TimeUnit.MILLISECONDS);
|
2015-01-07 19:35:11 +01:00
|
|
|
}
|
|
|
|
|
2015-01-08 11:01:35 +01:00
|
|
|
@Override
|
|
|
|
public IQRequestHandler registerIQRequestHandler(final IQRequestHandler iqRequestHandler) {
|
2019-06-11 00:10:38 +02:00
|
|
|
final QName key = iqRequestHandler.getQName();
|
2015-01-08 11:01:35 +01:00
|
|
|
switch (iqRequestHandler.getType()) {
|
|
|
|
case set:
|
|
|
|
synchronized (setIqRequestHandler) {
|
|
|
|
return setIqRequestHandler.put(key, iqRequestHandler);
|
|
|
|
}
|
|
|
|
case get:
|
|
|
|
synchronized (getIqRequestHandler) {
|
|
|
|
return getIqRequestHandler.put(key, iqRequestHandler);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Only IQ type of 'get' and 'set' allowed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final IQRequestHandler unregisterIQRequestHandler(IQRequestHandler iqRequestHandler) {
|
|
|
|
return unregisterIQRequestHandler(iqRequestHandler.getElement(), iqRequestHandler.getNamespace(),
|
|
|
|
iqRequestHandler.getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IQRequestHandler unregisterIQRequestHandler(String element, String namespace, IQ.Type type) {
|
2019-06-11 00:10:38 +02:00
|
|
|
final QName key = new QName(namespace, element);
|
2015-01-08 11:01:35 +01:00
|
|
|
switch (type) {
|
|
|
|
case set:
|
|
|
|
synchronized (setIqRequestHandler) {
|
|
|
|
return setIqRequestHandler.remove(key);
|
|
|
|
}
|
|
|
|
case get:
|
|
|
|
synchronized (getIqRequestHandler) {
|
|
|
|
return getIqRequestHandler.remove(key);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Only IQ type of 'get' and 'set' allowed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 10:33:52 +02:00
|
|
|
private long lastStanzaReceived;
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2014-08-20 10:33:52 +02:00
|
|
|
public long getLastStanzaReceived() {
|
|
|
|
return lastStanzaReceived;
|
|
|
|
}
|
|
|
|
|
2019-03-09 19:19:20 +01:00
|
|
|
/**
|
|
|
|
* Get the timestamp when the connection was the first time authenticated, i.e., when the first successful login was
|
|
|
|
* performed. Note that this value is not reset on disconnect, so it represents the timestamp from the last
|
|
|
|
* authenticated connection. The value is also not reset on stream resumption.
|
|
|
|
*
|
|
|
|
* @return the timestamp or {@code null}.
|
|
|
|
* @since 4.3.3
|
|
|
|
*/
|
|
|
|
public final long getAuthenticatedConnectionInitiallyEstablishedTimestamp() {
|
|
|
|
return authenticatedConnectionInitiallyEstablishedTimestamp;
|
|
|
|
}
|
|
|
|
|
2014-12-28 17:43:39 +01:00
|
|
|
/**
|
|
|
|
* Install a parsing exception callback, which will be invoked once an exception is encountered while parsing a
|
2015-03-29 12:15:32 +02:00
|
|
|
* stanza.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-12-28 17:43:39 +01:00
|
|
|
* @param callback the callback to install
|
|
|
|
*/
|
|
|
|
public void setParsingExceptionCallback(ParsingExceptionCallback callback) {
|
|
|
|
parsingExceptionCallback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current active parsing exception callback.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-12-28 17:43:39 +01:00
|
|
|
* @return the active exception callback or null if there is none
|
|
|
|
*/
|
|
|
|
public ParsingExceptionCallback getParsingExceptionCallback() {
|
|
|
|
return parsingExceptionCallback;
|
|
|
|
}
|
|
|
|
|
2015-04-22 09:28:30 +02:00
|
|
|
@Override
|
|
|
|
public final String toString() {
|
2015-05-27 19:29:51 +02:00
|
|
|
EntityFullJid localEndpoint = getUser();
|
2019-07-24 09:18:39 +02:00
|
|
|
String localEndpointString = localEndpoint == null ? "not-authenticated" : localEndpoint.toString();
|
2015-04-22 09:28:30 +02:00
|
|
|
return getClass().getSimpleName() + '[' + localEndpointString + "] (" + getConnectionCounter() + ')';
|
|
|
|
}
|
|
|
|
|
2019-04-10 12:18:06 +02:00
|
|
|
/**
|
|
|
|
* A queue of deferred runnables that where not executed immediately because {@link #currentAsyncRunnables} reached
|
|
|
|
* {@link #maxAsyncRunnables}. Note that we use a {@code LinkedList} in order to avoid space blowups in case the
|
|
|
|
* list ever becomes very big and shrinks again.
|
|
|
|
*/
|
|
|
|
private final Queue<Runnable> deferredAsyncRunnables = new LinkedList<>();
|
|
|
|
|
|
|
|
private int deferredAsyncRunnablesCount;
|
|
|
|
|
|
|
|
private int deferredAsyncRunnablesCountPrevious;
|
|
|
|
|
|
|
|
private int maxAsyncRunnables = SmackConfiguration.getDefaultConcurrencyLevelLimit();
|
|
|
|
|
|
|
|
private int currentAsyncRunnables;
|
|
|
|
|
|
|
|
protected void asyncGoLimited(final Runnable runnable) {
|
|
|
|
Runnable wrappedRunnable = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
runnable.run();
|
|
|
|
|
|
|
|
synchronized (deferredAsyncRunnables) {
|
|
|
|
Runnable defferredRunnable = deferredAsyncRunnables.poll();
|
|
|
|
if (defferredRunnable == null) {
|
|
|
|
currentAsyncRunnables--;
|
|
|
|
} else {
|
|
|
|
deferredAsyncRunnablesCount--;
|
|
|
|
asyncGo(defferredRunnable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
synchronized (deferredAsyncRunnables) {
|
|
|
|
if (currentAsyncRunnables < maxAsyncRunnables) {
|
|
|
|
currentAsyncRunnables++;
|
|
|
|
asyncGo(wrappedRunnable);
|
|
|
|
} else {
|
|
|
|
deferredAsyncRunnablesCount++;
|
|
|
|
deferredAsyncRunnables.add(wrappedRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
final int HIGH_WATERMARK = 100;
|
|
|
|
final int INFORM_WATERMARK = 20;
|
|
|
|
|
|
|
|
final int deferredAsyncRunnablesCount = this.deferredAsyncRunnablesCount;
|
|
|
|
|
|
|
|
if (deferredAsyncRunnablesCount >= HIGH_WATERMARK
|
|
|
|
&& deferredAsyncRunnablesCountPrevious < HIGH_WATERMARK) {
|
|
|
|
LOGGER.log(Level.WARNING, "High watermark of " + HIGH_WATERMARK + " simultaneous executing runnables reached");
|
|
|
|
} else if (deferredAsyncRunnablesCount >= INFORM_WATERMARK
|
|
|
|
&& deferredAsyncRunnablesCountPrevious < INFORM_WATERMARK) {
|
|
|
|
LOGGER.log(Level.INFO, INFORM_WATERMARK + " simultaneous executing runnables reached");
|
|
|
|
}
|
|
|
|
|
|
|
|
deferredAsyncRunnablesCountPrevious = deferredAsyncRunnablesCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMaxAsyncOperations(int maxAsyncOperations) {
|
|
|
|
if (maxAsyncOperations < 1) {
|
|
|
|
throw new IllegalArgumentException("Max async operations must be greater than 0");
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized (deferredAsyncRunnables) {
|
|
|
|
maxAsyncRunnables = maxAsyncOperations;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 21:11:55 +02:00
|
|
|
protected static void asyncGo(Runnable runnable) {
|
2018-02-23 17:04:51 +01:00
|
|
|
CACHED_EXECUTOR_SERVICE.execute(runnable);
|
2014-12-17 18:21:54 +01:00
|
|
|
}
|
2014-12-28 17:43:39 +01:00
|
|
|
|
2020-04-11 22:07:16 +02:00
|
|
|
@SuppressWarnings("static-method")
|
2020-04-04 13:03:31 +02:00
|
|
|
protected final SmackReactor getReactor() {
|
|
|
|
return SMACK_REACTOR;
|
|
|
|
}
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
protected static ScheduledAction schedule(Runnable runnable, long delay, TimeUnit unit) {
|
2020-05-16 21:18:27 +02:00
|
|
|
return SMACK_REACTOR.schedule(runnable, delay, unit, ScheduledAction.Kind.NonBlocking);
|
2019-02-04 08:59:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void onStreamOpen(XmlPullParser parser) {
|
|
|
|
// We found an opening stream.
|
|
|
|
if ("jabber:client".equals(parser.getNamespace(null))) {
|
|
|
|
streamId = parser.getAttributeValue("", "id");
|
2019-02-15 23:21:30 +01:00
|
|
|
incomingStreamXmlEnvironment = XmlEnvironment.from(parser);
|
|
|
|
|
2019-02-16 21:26:02 +01:00
|
|
|
String reportedServerDomainString = parser.getAttributeValue("", "from");
|
|
|
|
if (reportedServerDomainString == null) {
|
|
|
|
// RFC 6120 § 4.7.1. makes no explicit statement whether or not 'from' in the stream open from the server
|
|
|
|
// in c2s connections is required or not.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DomainBareJid reportedServerDomain;
|
|
|
|
try {
|
|
|
|
reportedServerDomain = JidCreate.domainBareFrom(reportedServerDomainString);
|
|
|
|
DomainBareJid configuredXmppServiceDomain = config.getXMPPServiceDomain();
|
|
|
|
if (!configuredXmppServiceDomain.equals(reportedServerDomain)) {
|
|
|
|
LOGGER.warning("Domain reported by server '" + reportedServerDomain
|
|
|
|
+ "' does not match configured domain '" + configuredXmppServiceDomain + "'");
|
|
|
|
}
|
|
|
|
} catch (XmppStringprepException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "XMPP service domain '" + reportedServerDomainString
|
|
|
|
+ "' as reported by server could not be transformed to a valid JID", e);
|
|
|
|
}
|
2019-02-04 08:59:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void sendStreamOpen() throws NotConnectedException, InterruptedException {
|
|
|
|
// If possible, provide the receiving entity of the stream open tag, i.e. the server, as much information as
|
|
|
|
// possible. The 'to' attribute is *always* available. The 'from' attribute if set by the user and no external
|
|
|
|
// mechanism is used to determine the local entity (user). And the 'id' attribute is available after the first
|
|
|
|
// response from the server (see e.g. RFC 6120 § 9.1.1 Step 2.)
|
|
|
|
CharSequence to = getXMPPServiceDomain();
|
|
|
|
CharSequence from = null;
|
|
|
|
CharSequence localpart = config.getUsername();
|
|
|
|
if (localpart != null) {
|
|
|
|
from = XmppStringUtils.completeJidFrom(localpart, to);
|
|
|
|
}
|
|
|
|
String id = getStreamId();
|
2019-09-21 23:00:17 +02:00
|
|
|
|
|
|
|
StreamOpen streamOpen = new StreamOpen(to, from, id, config.getXmlLang(), StreamOpen.StreamContentNamespace.client);
|
|
|
|
sendNonza(streamOpen);
|
|
|
|
|
|
|
|
XmlEnvironment.Builder xmlEnvironmentBuilder = XmlEnvironment.builder();
|
|
|
|
xmlEnvironmentBuilder.with(streamOpen);
|
|
|
|
outgoingStreamXmlEnvironment = xmlEnvironmentBuilder.build();
|
2019-02-04 08:59:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static final class SmackTlsContext {
|
|
|
|
public final SSLContext sslContext;
|
|
|
|
public final SmackDaneVerifier daneVerifier;
|
|
|
|
|
|
|
|
private SmackTlsContext(SSLContext sslContext, SmackDaneVerifier daneVerifier) {
|
|
|
|
assert sslContext != null;
|
|
|
|
this.sslContext = sslContext;
|
|
|
|
this.daneVerifier = daneVerifier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final SmackTlsContext getSmackTlsContext() throws KeyManagementException, NoSuchAlgorithmException,
|
|
|
|
CertificateException, IOException, UnrecoverableKeyException, KeyStoreException, NoSuchProviderException {
|
|
|
|
SmackDaneVerifier daneVerifier = null;
|
|
|
|
|
|
|
|
if (config.getDnssecMode() == DnssecMode.needsDnssecAndDane) {
|
|
|
|
SmackDaneProvider daneProvider = DNSUtil.getDaneProvider();
|
|
|
|
if (daneProvider == null) {
|
|
|
|
throw new UnsupportedOperationException("DANE enabled but no SmackDaneProvider configured");
|
|
|
|
}
|
|
|
|
daneVerifier = daneProvider.newInstance();
|
|
|
|
if (daneVerifier == null) {
|
|
|
|
throw new IllegalStateException("DANE requested but DANE provider did not return a DANE verifier");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SSLContext context = this.config.getCustomSSLContext();
|
|
|
|
KeyStore ks = null;
|
|
|
|
PasswordCallback pcb = null;
|
|
|
|
|
|
|
|
if (context == null) {
|
|
|
|
final String keyStoreType = config.getKeystoreType();
|
|
|
|
final CallbackHandler callbackHandler = config.getCallbackHandler();
|
|
|
|
final String keystorePath = config.getKeystorePath();
|
|
|
|
if ("PKCS11".equals(keyStoreType)) {
|
|
|
|
try {
|
|
|
|
Constructor<?> c = Class.forName("sun.security.pkcs11.SunPKCS11").getConstructor(InputStream.class);
|
|
|
|
String pkcs11Config = "name = SmartCard\nlibrary = " + config.getPKCS11Library();
|
2019-05-08 11:34:40 +02:00
|
|
|
ByteArrayInputStream config = new ByteArrayInputStream(pkcs11Config.getBytes(StandardCharsets.UTF_8));
|
2019-02-04 08:59:39 +01:00
|
|
|
Provider p = (Provider) c.newInstance(config);
|
|
|
|
Security.addProvider(p);
|
2019-05-17 21:56:46 +02:00
|
|
|
ks = KeyStore.getInstance("PKCS11", p);
|
|
|
|
pcb = new PasswordCallback("PKCS11 Password: ", false);
|
2019-02-04 08:59:39 +01:00
|
|
|
callbackHandler.handle(new Callback[] {pcb});
|
2019-05-17 21:56:46 +02:00
|
|
|
ks.load(null, pcb.getPassword());
|
2019-02-04 08:59:39 +01:00
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Exception", e);
|
|
|
|
ks = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ("Apple".equals(keyStoreType)) {
|
2019-05-17 21:56:46 +02:00
|
|
|
ks = KeyStore.getInstance("KeychainStore", "Apple");
|
|
|
|
ks.load(null, null);
|
2019-02-04 08:59:39 +01:00
|
|
|
// pcb = new PasswordCallback("Apple Keychain",false);
|
|
|
|
// pcb.setPassword(null);
|
|
|
|
}
|
|
|
|
else if (keyStoreType != null) {
|
|
|
|
ks = KeyStore.getInstance(keyStoreType);
|
|
|
|
if (callbackHandler != null && StringUtils.isNotEmpty(keystorePath)) {
|
|
|
|
try {
|
|
|
|
pcb = new PasswordCallback("Keystore Password: ", false);
|
|
|
|
callbackHandler.handle(new Callback[] { pcb });
|
|
|
|
ks.load(new FileInputStream(keystorePath), pcb.getPassword());
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Exception", e);
|
|
|
|
ks = null;
|
|
|
|
}
|
|
|
|
} else {
|
2020-03-09 13:39:19 +01:00
|
|
|
InputStream stream = TLSUtils.getDefaultTruststoreStreamIfPossible();
|
|
|
|
try {
|
|
|
|
// Note that PKCS12 keystores need a password one some Java platforms. Hence we try the famous
|
|
|
|
// 'changeit' here. See https://bugs.openjdk.java.net/browse/JDK-8194702
|
|
|
|
char[] password = "changeit".toCharArray();
|
|
|
|
try {
|
|
|
|
ks.load(stream, password);
|
|
|
|
} finally {
|
2020-05-17 17:01:42 +02:00
|
|
|
CloseableUtil.maybeClose(stream);
|
2020-03-09 13:39:19 +01:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.log(Level.FINE, "KeyStore load() threw, attempting 'jks' fallback", e);
|
|
|
|
|
|
|
|
ks = KeyStore.getInstance("jks");
|
|
|
|
// Open the stream again, so that we read it from the beginning.
|
|
|
|
stream = TLSUtils.getDefaultTruststoreStreamIfPossible();
|
|
|
|
try {
|
|
|
|
ks.load(stream, null);
|
|
|
|
} finally {
|
2020-05-17 17:01:42 +02:00
|
|
|
CloseableUtil.maybeClose(stream);
|
2020-03-09 13:39:19 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-04 08:59:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyManager[] kms = null;
|
|
|
|
|
|
|
|
if (ks != null) {
|
|
|
|
String keyManagerFactoryAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
|
|
|
|
KeyManagerFactory kmf = null;
|
|
|
|
try {
|
|
|
|
kmf = KeyManagerFactory.getInstance(keyManagerFactoryAlgorithm);
|
|
|
|
}
|
|
|
|
catch (NoSuchAlgorithmException e) {
|
|
|
|
LOGGER.log(Level.FINE, "Could get the default KeyManagerFactory for the '"
|
|
|
|
+ keyManagerFactoryAlgorithm + "' algorithm", e);
|
|
|
|
}
|
|
|
|
if (kmf != null) {
|
|
|
|
try {
|
|
|
|
if (pcb == null) {
|
|
|
|
kmf.init(ks, null);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
kmf.init(ks, pcb.getPassword());
|
|
|
|
pcb.clearPassword();
|
|
|
|
}
|
|
|
|
kms = kmf.getKeyManagers();
|
|
|
|
}
|
|
|
|
catch (NullPointerException npe) {
|
|
|
|
LOGGER.log(Level.WARNING, "NullPointerException", npe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user didn't specify a SSLContext, use the default one
|
|
|
|
context = SSLContext.getInstance("TLS");
|
|
|
|
|
|
|
|
final SecureRandom secureRandom = new java.security.SecureRandom();
|
2020-02-12 23:09:36 +01:00
|
|
|
X509TrustManager trustManager = config.getCustomX509TrustManager();
|
|
|
|
if (trustManager == null) {
|
|
|
|
trustManager = TLSUtils.getDefaultX509TrustManager(ks);
|
|
|
|
}
|
2019-02-04 08:59:39 +01:00
|
|
|
|
|
|
|
if (daneVerifier != null) {
|
|
|
|
// User requested DANE verification.
|
2020-02-12 23:09:36 +01:00
|
|
|
daneVerifier.init(context, kms, trustManager, secureRandom);
|
2019-02-04 08:59:39 +01:00
|
|
|
} else {
|
2020-02-12 23:09:36 +01:00
|
|
|
TrustManager[] customTrustManagers = new TrustManager[] { trustManager };
|
2019-02-04 08:59:39 +01:00
|
|
|
context.init(kms, customTrustManagers, secureRandom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SmackTlsContext(context, daneVerifier);
|
2015-01-15 21:36:23 +01:00
|
|
|
}
|
2014-05-25 12:28:08 +02:00
|
|
|
}
|