001/**
002 *
003 * Copyright 2009 Jive Software, 2018-2020 Florian Schmaus.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack;
018
019import java.io.IOException;
020import java.io.Reader;
021import java.io.Writer;
022import java.util.Collection;
023import java.util.HashMap;
024import java.util.Iterator;
025import java.util.LinkedHashMap;
026import java.util.LinkedList;
027import java.util.List;
028import java.util.Map;
029import java.util.Queue;
030import java.util.Set;
031import java.util.concurrent.ConcurrentLinkedQueue;
032import java.util.concurrent.CopyOnWriteArraySet;
033import java.util.concurrent.Executor;
034import java.util.concurrent.ExecutorService;
035import java.util.concurrent.Executors;
036import java.util.concurrent.ThreadFactory;
037import java.util.concurrent.TimeUnit;
038import java.util.concurrent.atomic.AtomicInteger;
039import java.util.concurrent.locks.Lock;
040import java.util.concurrent.locks.ReentrantLock;
041import java.util.logging.Level;
042import java.util.logging.Logger;
043
044import javax.net.ssl.SSLSession;
045import javax.xml.namespace.QName;
046
047import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
048import org.jivesoftware.smack.SmackConfiguration.UnknownIqRequestReplyMode;
049import org.jivesoftware.smack.SmackException.AlreadyConnectedException;
050import org.jivesoftware.smack.SmackException.AlreadyLoggedInException;
051import org.jivesoftware.smack.SmackException.NoResponseException;
052import org.jivesoftware.smack.SmackException.NotConnectedException;
053import org.jivesoftware.smack.SmackException.NotLoggedInException;
054import org.jivesoftware.smack.SmackException.ResourceBindingNotOfferedException;
055import org.jivesoftware.smack.SmackException.SecurityRequiredByClientException;
056import org.jivesoftware.smack.SmackException.SecurityRequiredException;
057import org.jivesoftware.smack.SmackException.SmackSaslException;
058import org.jivesoftware.smack.SmackException.SmackWrappedException;
059import org.jivesoftware.smack.SmackFuture.InternalSmackFuture;
060import org.jivesoftware.smack.XMPPException.FailedNonzaException;
061import org.jivesoftware.smack.XMPPException.StreamErrorException;
062import org.jivesoftware.smack.XMPPException.XMPPErrorException;
063import org.jivesoftware.smack.compress.packet.Compress;
064import org.jivesoftware.smack.compression.XMPPInputOutputStream;
065import org.jivesoftware.smack.datatypes.UInt16;
066import org.jivesoftware.smack.debugger.SmackDebugger;
067import org.jivesoftware.smack.debugger.SmackDebuggerFactory;
068import org.jivesoftware.smack.filter.IQReplyFilter;
069import org.jivesoftware.smack.filter.StanzaFilter;
070import org.jivesoftware.smack.filter.StanzaIdFilter;
071import org.jivesoftware.smack.internal.SmackTlsContext;
072import org.jivesoftware.smack.iqrequest.IQRequestHandler;
073import org.jivesoftware.smack.packet.Bind;
074import org.jivesoftware.smack.packet.ErrorIQ;
075import org.jivesoftware.smack.packet.ExtensionElement;
076import org.jivesoftware.smack.packet.FullyQualifiedElement;
077import org.jivesoftware.smack.packet.IQ;
078import org.jivesoftware.smack.packet.Mechanisms;
079import org.jivesoftware.smack.packet.Message;
080import org.jivesoftware.smack.packet.MessageBuilder;
081import org.jivesoftware.smack.packet.MessageOrPresence;
082import org.jivesoftware.smack.packet.MessageOrPresenceBuilder;
083import org.jivesoftware.smack.packet.Nonza;
084import org.jivesoftware.smack.packet.Presence;
085import org.jivesoftware.smack.packet.PresenceBuilder;
086import org.jivesoftware.smack.packet.Session;
087import org.jivesoftware.smack.packet.Stanza;
088import org.jivesoftware.smack.packet.StanzaError;
089import org.jivesoftware.smack.packet.StanzaFactory;
090import org.jivesoftware.smack.packet.StartTls;
091import org.jivesoftware.smack.packet.StreamError;
092import org.jivesoftware.smack.packet.StreamOpen;
093import org.jivesoftware.smack.packet.TopLevelStreamElement;
094import org.jivesoftware.smack.packet.XmlEnvironment;
095import org.jivesoftware.smack.packet.id.StanzaIdSource;
096import org.jivesoftware.smack.parsing.ParsingExceptionCallback;
097import org.jivesoftware.smack.parsing.SmackParsingException;
098import org.jivesoftware.smack.provider.ExtensionElementProvider;
099import org.jivesoftware.smack.provider.NonzaProvider;
100import org.jivesoftware.smack.provider.ProviderManager;
101import org.jivesoftware.smack.sasl.SASLErrorException;
102import org.jivesoftware.smack.sasl.SASLMechanism;
103import org.jivesoftware.smack.sasl.core.SASLAnonymous;
104import org.jivesoftware.smack.sasl.packet.SaslNonza;
105import org.jivesoftware.smack.util.Async;
106import org.jivesoftware.smack.util.CollectionUtil;
107import org.jivesoftware.smack.util.Consumer;
108import org.jivesoftware.smack.util.MultiMap;
109import org.jivesoftware.smack.util.Objects;
110import org.jivesoftware.smack.util.PacketParserUtils;
111import org.jivesoftware.smack.util.ParserUtils;
112import org.jivesoftware.smack.util.Predicate;
113import org.jivesoftware.smack.util.StringUtils;
114import org.jivesoftware.smack.util.Supplier;
115import org.jivesoftware.smack.xml.XmlPullParser;
116import org.jivesoftware.smack.xml.XmlPullParserException;
117
118import org.jxmpp.jid.DomainBareJid;
119import org.jxmpp.jid.EntityBareJid;
120import org.jxmpp.jid.EntityFullJid;
121import org.jxmpp.jid.Jid;
122import org.jxmpp.jid.impl.JidCreate;
123import org.jxmpp.jid.parts.Resourcepart;
124import org.jxmpp.stringprep.XmppStringprepException;
125import org.jxmpp.util.XmppStringUtils;
126
127/**
128 * This abstract class is commonly used as super class for XMPP connection mechanisms like TCP and BOSH. Hence it
129 * provides the methods for connection state management, like {@link #connect()}, {@link #login()} and
130 * {@link #disconnect()} (which are deliberately not provided by the {@link XMPPConnection} interface).
131 * <p>
132 * <b>Note:</b> The default entry point to Smack's documentation is {@link XMPPConnection}. If you are getting started
133 * with Smack, then head over to {@link XMPPConnection} and the come back here.
134 * </p>
135 * <h2>Parsing Exceptions</h2>
136 * <p>
137 * In case a Smack parser (Provider) throws those exceptions are handled over to the {@link ParsingExceptionCallback}. A
138 * common cause for a provider throwing is illegal input, for example a non-numeric String where only Integers are
139 * allowed. Smack's <em>default behavior</em> follows the <b>"fail-hard per default"</b> principle leading to a
140 * termination of the connection on parsing exceptions. This default was chosen to make users eventually aware that they
141 * should configure their own callback and handle those exceptions to prevent the disconnect. Handle a parsing exception
142 * could be as simple as using a non-throwing no-op callback, which would cause the faulty stream element to be taken
143 * out of the stream, i.e., Smack behaves like that element was never received.
144 * </p>
145 * <p>
146 * If the parsing exception is because Smack received illegal input, then please consider informing the authors of the
147 * originating entity about that. If it was thrown because of an bug in a Smack parser, then please consider filling a
148 * bug with Smack.
149 * </p>
150 * <h3>Managing the parsing exception callback</h3>
151 * <p>
152 * The "fail-hard per default" behavior is achieved by using the
153 * {@link org.jivesoftware.smack.parsing.ExceptionThrowingCallbackWithHint} as default parsing exception callback. You
154 * can change the behavior using {@link #setParsingExceptionCallback(ParsingExceptionCallback)} to set a new callback.
155 * Use {@link org.jivesoftware.smack.SmackConfiguration#setDefaultParsingExceptionCallback(ParsingExceptionCallback)} to
156 * set the default callback.
157 * </p>
158 */
159public abstract class AbstractXMPPConnection implements XMPPConnection {
160    private static final Logger LOGGER = Logger.getLogger(AbstractXMPPConnection.class.getName());
161
162    protected static final SmackReactor SMACK_REACTOR;
163
164    static {
165        SMACK_REACTOR = SmackReactor.getInstance();
166    }
167
168    /**
169     * Counter to uniquely identify connections that are created.
170     */
171    private static final AtomicInteger connectionCounter = new AtomicInteger(0);
172
173    static {
174        Smack.ensureInitialized();
175    }
176
177    protected enum SyncPointState {
178        initial,
179        request_sent,
180        successful,
181    }
182
183    /**
184     * A collection of ConnectionListeners which listen for connection closing
185     * and reconnection events.
186     */
187    protected final Set<ConnectionListener> connectionListeners =
188            new CopyOnWriteArraySet<>();
189
190    /**
191     * A collection of StanzaCollectors which collects packets for a specified filter
192     * and perform blocking and polling operations on the result queue.
193     * <p>
194     * We use a ConcurrentLinkedQueue here, because its Iterator is weakly
195     * consistent and we want {@link #invokeStanzaCollectorsAndNotifyRecvListeners(Stanza)} for-each
196     * loop to be lock free. As drawback, removing a StanzaCollector is O(n).
197     * The alternative would be a synchronized HashSet, but this would mean a
198     * synchronized block around every usage of <code>collectors</code>.
199     * </p>
200     */
201    private final Collection<StanzaCollector> collectors = new ConcurrentLinkedQueue<>();
202
203    private final Map<StanzaListener, ListenerWrapper> recvListeners = new LinkedHashMap<>();
204
205    /**
206     * List of PacketListeners that will be notified synchronously when a new stanza was received.
207     */
208    private final Map<StanzaListener, ListenerWrapper> syncRecvListeners = new LinkedHashMap<>();
209
210    /**
211     * List of PacketListeners that will be notified asynchronously when a new stanza was received.
212     */
213    private final Map<StanzaListener, ListenerWrapper> asyncRecvListeners = new LinkedHashMap<>();
214
215    /**
216     * List of PacketListeners that will be notified when a new stanza was sent.
217     */
218    private final Map<StanzaListener, ListenerWrapper> sendListeners =
219            new HashMap<>();
220
221    /**
222     * List of PacketListeners that will be notified when a new stanza is about to be
223     * sent to the server. These interceptors may modify the stanza before it is being
224     * actually sent to the server.
225     */
226    private final Map<StanzaListener, InterceptorWrapper> interceptors =
227            new HashMap<>();
228
229    private final Map<Consumer<MessageBuilder>, GenericInterceptorWrapper<MessageBuilder, Message>> messageInterceptors = new HashMap<>();
230
231    private final Map<Consumer<PresenceBuilder>, GenericInterceptorWrapper<PresenceBuilder, Presence>> presenceInterceptors = new HashMap<>();
232
233    private XmlEnvironment incomingStreamXmlEnvironment;
234
235    protected XmlEnvironment outgoingStreamXmlEnvironment;
236
237    final MultiMap<QName, NonzaCallback> nonzaCallbacksMap = new MultiMap<>();
238
239    protected final Lock connectionLock = new ReentrantLock();
240
241    protected final Map<QName, FullyQualifiedElement> streamFeatures = new HashMap<>();
242
243    /**
244     * The full JID of the authenticated user, as returned by the resource binding response of the server.
245     * <p>
246     * It is important that we don't infer the user from the login() arguments and the configurations service name, as,
247     * for example, when SASL External is used, the username is not given to login but taken from the 'external'
248     * certificate.
249     * </p>
250     */
251    protected EntityFullJid user;
252
253    protected boolean connected = false;
254
255    /**
256     * The stream ID, see RFC 6120 § 4.7.3
257     */
258    protected String streamId;
259
260    /**
261     * The timeout to wait for a reply in milliseconds.
262     */
263    private long replyTimeout = SmackConfiguration.getDefaultReplyTimeout();
264
265    /**
266     * The SmackDebugger allows to log and debug XML traffic.
267     */
268    protected final SmackDebugger debugger;
269
270    /**
271     * The Reader which is used for the debugger.
272     */
273    protected Reader reader;
274
275    /**
276     * The Writer which is used for the debugger.
277     */
278    protected Writer writer;
279
280    protected SmackException currentSmackException;
281    protected XMPPException currentXmppException;
282
283    protected boolean tlsHandled;
284
285    /**
286     * Set to <code>true</code> if the last features stanza from the server has been parsed. A XMPP connection
287     * handshake can invoke multiple features stanzas, e.g. when TLS is activated a second feature
288     * stanza is send by the server. This is set to true once the last feature stanza has been
289     * parsed.
290     */
291    protected boolean lastFeaturesReceived;
292
293    /**
294     * Set to <code>true</code> if the SASL feature has been received.
295     */
296    protected boolean saslFeatureReceived;
297
298    /**
299     * A synchronization point which is successful if this connection has received the closing
300     * stream element from the remote end-point, i.e. the server.
301     */
302    protected boolean closingStreamReceived;
303
304    /**
305     * The SASLAuthentication manager that is responsible for authenticating with the server.
306     */
307    private final SASLAuthentication saslAuthentication;
308
309    /**
310     * A number to uniquely identify connections that are created. This is distinct from the
311     * connection ID, which is a value sent by the server once a connection is made.
312     */
313    protected final int connectionCounterValue = connectionCounter.getAndIncrement();
314
315    /**
316     * Holds the initial configuration used while creating the connection.
317     */
318    protected final ConnectionConfiguration config;
319
320    /**
321     * Defines how the from attribute of outgoing stanzas should be handled.
322     */
323    private FromMode fromMode = FromMode.OMITTED;
324
325    protected XMPPInputOutputStream compressionHandler;
326
327    private ParsingExceptionCallback parsingExceptionCallback = SmackConfiguration.getDefaultParsingExceptionCallback();
328
329    /**
330     * A cached thread pool executor service with custom thread factory to set meaningful names on the threads and set
331     * them 'daemon'.
332     */
333    private static final ExecutorService CACHED_EXECUTOR_SERVICE = Executors.newCachedThreadPool(new ThreadFactory() {
334        @Override
335        public Thread newThread(Runnable runnable) {
336            Thread thread = new Thread(runnable);
337            thread.setName("Smack Cached Executor");
338            thread.setDaemon(true);
339            thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
340                @Override
341                public void uncaughtException(Thread t, Throwable e) {
342                    LOGGER.log(Level.WARNING, t + " encountered uncaught exception", e);
343                }
344            });
345            return thread;
346        }
347    });
348
349    protected static final AsyncButOrdered<AbstractXMPPConnection> ASYNC_BUT_ORDERED = new AsyncButOrdered<>();
350
351    protected final AsyncButOrdered<StanzaListener> inOrderListeners = new AsyncButOrdered<>();
352
353    /**
354     * The used host to establish the connection to
355     */
356    protected String host;
357
358    /**
359     * The used port to establish the connection to
360     */
361    protected UInt16 port;
362
363    /**
364     * Flag that indicates if the user is currently authenticated with the server.
365     */
366    protected boolean authenticated = false;
367
368    // TODO: Migrate to ZonedDateTime once Smack's minimum required Android SDK level is 26 (8.0, Oreo) or higher.
369    protected long authenticatedConnectionInitiallyEstablishedTimestamp;
370
371    /**
372     * Flag that indicates if the user was authenticated with the server when the connection
373     * to the server was closed (abruptly or not).
374     */
375    protected boolean wasAuthenticated = false;
376
377    private final Map<QName, IQRequestHandler> setIqRequestHandler = new HashMap<>();
378    private final Map<QName, IQRequestHandler> getIqRequestHandler = new HashMap<>();
379
380    private final StanzaFactory stanzaFactory;
381
382    /**
383     * Create a new XMPPConnection to an XMPP server.
384     *
385     * @param configuration The configuration which is used to establish the connection.
386     */
387    protected AbstractXMPPConnection(ConnectionConfiguration configuration) {
388        saslAuthentication = new SASLAuthentication(this, configuration);
389        config = configuration;
390
391        // Install the SASL Nonza callbacks.
392        buildNonzaCallback()
393            .listenFor(SaslNonza.Challenge.class, c -> {
394                try {
395                    saslAuthentication.challengeReceived(c);
396                } catch (SmackException | InterruptedException e) {
397                    saslAuthentication.authenticationFailed(e);
398                }
399            })
400            .listenFor(SaslNonza.Success.class, s -> {
401                try {
402                    saslAuthentication.authenticated(s);
403                } catch (SmackSaslException | NotConnectedException | InterruptedException e) {
404                    saslAuthentication.authenticationFailed(e);
405                }
406            })
407            .listenFor(SaslNonza.SASLFailure.class, f -> saslAuthentication.authenticationFailed(f))
408            .install();
409
410        SmackDebuggerFactory debuggerFactory = configuration.getDebuggerFactory();
411        if (debuggerFactory != null) {
412            debugger = debuggerFactory.create(this);
413        } else {
414            debugger = null;
415        }
416        // Notify listeners that a new connection has been established
417        for (ConnectionCreationListener listener : XMPPConnectionRegistry.getConnectionCreationListeners()) {
418            listener.connectionCreated(this);
419        }
420
421        StanzaIdSource stanzaIdSource = configuration.constructStanzaIdSource();
422        stanzaFactory = new StanzaFactory(stanzaIdSource);
423    }
424
425    /**
426     * Get the connection configuration used by this connection.
427     *
428     * @return the connection configuration.
429     */
430    public ConnectionConfiguration getConfiguration() {
431        return config;
432    }
433
434    @Override
435    public DomainBareJid getXMPPServiceDomain() {
436        if (xmppServiceDomain != null) {
437            return xmppServiceDomain;
438        }
439        return config.getXMPPServiceDomain();
440    }
441
442    @Override
443    public String getHost() {
444        return host;
445    }
446
447    @Override
448    public int getPort() {
449        final UInt16 port = this.port;
450        if (port == null) {
451            return -1;
452        }
453
454        return port.intValue();
455    }
456
457    @Override
458    public abstract boolean isSecureConnection();
459
460    protected abstract void sendStanzaInternal(Stanza packet) throws NotConnectedException, InterruptedException;
461
462    @Override
463    public boolean trySendStanza(Stanza stanza) throws NotConnectedException {
464        // Default implementation which falls back to sendStanza() as mentioned in the methods javadoc. May be
465        // overwritten by subclasses.
466        try {
467            sendStanza(stanza);
468        } catch (InterruptedException e) {
469            LOGGER.log(Level.FINER,
470                            "Thread blocked in fallback implementation of trySendStanza(Stanza) was interrupted", e);
471            return false;
472        }
473        return true;
474    }
475
476    @Override
477    public boolean trySendStanza(Stanza stanza, long timeout, TimeUnit unit)
478                    throws NotConnectedException, InterruptedException {
479        // Default implementation which falls back to sendStanza() as mentioned in the methods javadoc. May be
480        // overwritten by subclasses.
481        sendStanza(stanza);
482        return true;
483    }
484
485    @Override
486    public abstract void sendNonza(Nonza element) throws NotConnectedException, InterruptedException;
487
488    @Override
489    public abstract boolean isUsingCompression();
490
491    protected void initState() {
492        currentSmackException = null;
493        currentXmppException = null;
494        saslFeatureReceived = lastFeaturesReceived = tlsHandled = false;
495        // TODO: We do not init closingStreamReceived here, as the integration tests use it to check if we waited for
496        // it.
497    }
498
499    /**
500     * Establishes a connection to the XMPP server. It basically
501     * creates and maintains a connection to the server.
502     * <p>
503     * Listeners will be preserved from a previous connection.
504     * </p>
505     *
506     * @throws XMPPException if an error occurs on the XMPP protocol level.
507     * @throws SmackException if an error occurs somewhere else besides XMPP protocol level.
508     * @throws IOException if an I/O error occurred.
509     * @return a reference to this object, to chain <code>connect()</code> with <code>login()</code>.
510     * @throws InterruptedException if the calling thread was interrupted.
511     */
512    public synchronized AbstractXMPPConnection connect() throws SmackException, IOException, XMPPException, InterruptedException {
513        // Check if not already connected
514        throwAlreadyConnectedExceptionIfAppropriate();
515
516        // Notify connection listeners that we are trying to connect
517        callConnectionConnectingListener();
518
519        // Reset the connection state
520        initState();
521        closingStreamReceived = false;
522        streamId = null;
523
524        // The connection should not be connected nor marked as such prior calling connectInternal().
525        assert !connected;
526
527        try {
528            // Perform the actual connection to the XMPP service
529            connectInternal();
530
531            // If TLS is required but the server doesn't offer it, disconnect
532            // from the server and throw an error. First check if we've already negotiated TLS
533            // and are secure, however (features get parsed a second time after TLS is established).
534            if (!isSecureConnection() && getConfiguration().getSecurityMode() == SecurityMode.required) {
535                throw new SecurityRequiredByClientException();
536            }
537        } catch (SmackException | IOException | XMPPException | InterruptedException e) {
538            instantShutdown();
539            throw e;
540        }
541
542        // If connectInternal() did not throw, then this connection must now be marked as connected.
543        assert connected;
544
545        callConnectionConnectedListener();
546
547        return this;
548    }
549
550    /**
551     * Abstract method that concrete subclasses of XMPPConnection need to implement to perform their
552     * way of XMPP connection establishment. Implementations are required to perform an automatic
553     * login if the previous connection state was logged (authenticated).
554     *
555     * @throws SmackException if Smack detected an exceptional situation.
556     * @throws IOException if an I/O error occurred.
557     * @throws XMPPException if an XMPP protocol error was received.
558     * @throws InterruptedException if the calling thread was interrupted.
559     */
560    protected abstract void connectInternal() throws SmackException, IOException, XMPPException, InterruptedException;
561
562    private String usedUsername, usedPassword;
563
564    /**
565     * The resourcepart used for this connection. May not be the resulting resourcepart if it's null or overridden by the XMPP service.
566     */
567    private Resourcepart usedResource;
568
569    /**
570     * Logs in to the server using the strongest SASL mechanism supported by
571     * the server. If more than the connection's default stanza timeout elapses in each step of the
572     * authentication process without a response from the server, a
573     * {@link SmackException.NoResponseException} will be thrown.
574     * <p>
575     * Before logging in (i.e. authenticate) to the server the connection must be connected
576     * by calling {@link #connect}.
577     * </p>
578     * <p>
579     * It is possible to log in without sending an initial available presence by using
580     * {@link ConnectionConfiguration.Builder#setSendPresence(boolean)}.
581     * Finally, if you want to not pass a password and instead use a more advanced mechanism
582     * while using SASL then you may be interested in using
583     * {@link ConnectionConfiguration.Builder#setCallbackHandler(javax.security.auth.callback.CallbackHandler)}.
584     * For more advanced login settings see {@link ConnectionConfiguration}.
585     * </p>
586     *
587     * @throws XMPPException if an error occurs on the XMPP protocol level.
588     * @throws SmackException if an error occurs somewhere else besides XMPP protocol level.
589     * @throws IOException if an I/O error occurs during login.
590     * @throws InterruptedException if the calling thread was interrupted.
591     */
592    public synchronized void login() throws XMPPException, SmackException, IOException, InterruptedException {
593        // The previously used username, password and resource take over precedence over the
594        // ones from the connection configuration
595        CharSequence username = usedUsername != null ? usedUsername : config.getUsername();
596        String password = usedPassword != null ? usedPassword : config.getPassword();
597        Resourcepart resource = usedResource != null ? usedResource : config.getResource();
598        login(username, password, resource);
599    }
600
601    /**
602     * Same as {@link #login(CharSequence, String, Resourcepart)}, but takes the resource from the connection
603     * configuration.
604     *
605     * @param username TODO javadoc me please
606     * @param password TODO javadoc me please
607     * @throws XMPPException if an XMPP protocol error was received.
608     * @throws SmackException if Smack detected an exceptional situation.
609     * @throws IOException if an I/O error occurred.
610     * @throws InterruptedException if the calling thread was interrupted.
611     * @see #login
612     */
613    public synchronized void login(CharSequence username, String password) throws XMPPException, SmackException,
614                    IOException, InterruptedException {
615        login(username, password, config.getResource());
616    }
617
618    /**
619     * Login with the given username (authorization identity). You may omit the password if a callback handler is used.
620     * If resource is null, then the server will generate one.
621     *
622     * @param username TODO javadoc me please
623     * @param password TODO javadoc me please
624     * @param resource TODO javadoc me please
625     * @throws XMPPException if an XMPP protocol error was received.
626     * @throws SmackException if Smack detected an exceptional situation.
627     * @throws IOException if an I/O error occurred.
628     * @throws InterruptedException if the calling thread was interrupted.
629     * @see #login
630     */
631    public synchronized void login(CharSequence username, String password, Resourcepart resource) throws XMPPException,
632                    SmackException, IOException, InterruptedException {
633        if (!config.allowNullOrEmptyUsername) {
634            StringUtils.requireNotNullNorEmpty(username, "Username must not be null nor empty");
635        }
636        throwNotConnectedExceptionIfAppropriate("Did you call connect() before login()?");
637        throwAlreadyLoggedInExceptionIfAppropriate();
638        usedUsername = username != null ? username.toString() : null;
639        usedPassword = password;
640        usedResource = resource;
641        loginInternal(usedUsername, usedPassword, usedResource);
642    }
643
644    protected abstract void loginInternal(String username, String password, Resourcepart resource)
645                    throws XMPPException, SmackException, IOException, InterruptedException;
646
647    @Override
648    public final boolean isConnected() {
649        return connected;
650    }
651
652    @Override
653    public final boolean isAuthenticated() {
654        return authenticated;
655    }
656
657    @Override
658    public final EntityFullJid getUser() {
659        return user;
660    }
661
662    @Override
663    public String getStreamId() {
664        if (!isConnected()) {
665            return null;
666        }
667        return streamId;
668    }
669
670    protected final void throwCurrentConnectionException() throws SmackException, XMPPException {
671        if (currentSmackException != null) {
672            throw currentSmackException;
673        } else if (currentXmppException != null) {
674            throw currentXmppException;
675        }
676
677        throw new AssertionError("No current connection exception set, although throwCurrentException() was called");
678    }
679
680    protected final boolean hasCurrentConnectionException() {
681        return currentSmackException != null || currentXmppException != null;
682    }
683
684    protected final void setCurrentConnectionExceptionAndNotify(Exception exception) {
685        if (exception instanceof SmackException) {
686            currentSmackException = (SmackException) exception;
687        } else if (exception instanceof XMPPException) {
688            currentXmppException = (XMPPException) exception;
689        } else {
690            currentSmackException = new SmackException.SmackWrappedException(exception);
691        }
692
693        notifyWaitingThreads();
694    }
695
696    /**
697     * We use an extra object for {@link #notifyWaitingThreads()} and {@link #waitForConditionOrConnectionException(Supplier)}, because all state
698     * changing methods of the connection are synchronized using the connection instance as monitor. If we now would
699     * also use the connection instance for the internal process to wait for a condition, the {@link Object#wait()}
700     * would leave the monitor when it waites, which would allow for another potential call to a state changing function
701     * to proceed.
702     */
703    private final Object internalMonitor = new Object();
704
705    protected final void notifyWaitingThreads() {
706        synchronized (internalMonitor) {
707            internalMonitor.notifyAll();
708        }
709    }
710
711    protected final boolean waitFor(Supplier<Boolean> condition) throws InterruptedException {
712        final long deadline = System.currentTimeMillis() + getReplyTimeout();
713        synchronized (internalMonitor) {
714            while (!condition.get().booleanValue()) {
715                final long now = System.currentTimeMillis();
716                if (now >= deadline) {
717                    return false;
718                }
719                internalMonitor.wait(deadline - now);
720            }
721        }
722        return true;
723    }
724
725    protected final boolean waitForConditionOrConnectionException(Supplier<Boolean> condition) throws InterruptedException {
726        return waitFor(() -> condition.get().booleanValue() || hasCurrentConnectionException());
727    }
728
729    protected final void waitForConditionOrConnectionException(Supplier<Boolean> condition, String waitFor) throws InterruptedException, NoResponseException {
730        boolean success = waitForConditionOrConnectionException(condition);
731        if (!success) {
732            throw NoResponseException.newWith(this, waitFor);
733        }
734    }
735
736    protected final void waitForConditionOrThrowConnectionException(Supplier<Boolean> condition, String waitFor) throws InterruptedException, SmackException, XMPPException {
737        waitForConditionOrConnectionException(condition, waitFor);
738        if (hasCurrentConnectionException()) {
739            throwCurrentConnectionException();
740        }
741    }
742
743    protected Resourcepart bindResourceAndEstablishSession(Resourcepart resource)
744                    throws SmackException, InterruptedException, XMPPException {
745        // Wait until either:
746        // - the servers last features stanza has been parsed
747        // - the timeout occurs
748        LOGGER.finer("Waiting for last features to be received before continuing with resource binding");
749        waitForConditionOrThrowConnectionException(() -> lastFeaturesReceived, "last stream features received from server");
750
751        if (!hasFeature(Bind.ELEMENT, Bind.NAMESPACE)) {
752            // Server never offered resource binding, which is REQUIRED in XMPP client and
753            // server implementations as per RFC6120 7.2
754            throw new ResourceBindingNotOfferedException();
755        }
756
757        // Resource binding, see RFC6120 7.
758        // Note that we can not use IQReplyFilter here, since the users full JID is not yet
759        // available. It will become available right after the resource has been successfully bound.
760        Bind bindResource = Bind.newSet(resource);
761        StanzaCollector packetCollector = createStanzaCollectorAndSend(new StanzaIdFilter(bindResource), bindResource);
762        Bind response = packetCollector.nextResultOrThrow();
763        // Set the connections user to the result of resource binding. It is important that we don't infer the user
764        // from the login() arguments and the configurations service name, as, for example, when SASL External is used,
765        // the username is not given to login but taken from the 'external' certificate.
766        user = response.getJid();
767        xmppServiceDomain = user.asDomainBareJid();
768
769        Session.Feature sessionFeature = getFeature(Session.Feature.class);
770        // Only bind the session if it's announced as stream feature by the server, is not optional and not disabled
771        // For more information see http://tools.ietf.org/html/draft-cridland-xmpp-session-01
772        if (sessionFeature != null && !sessionFeature.isOptional()) {
773            Session session = new Session();
774            packetCollector = createStanzaCollectorAndSend(new StanzaIdFilter(session), session);
775            packetCollector.nextResultOrThrow();
776        }
777
778        return response.getJid().getResourcepart();
779    }
780
781    protected void afterSuccessfulLogin(final boolean resumed) throws NotConnectedException, InterruptedException {
782        if (!resumed) {
783            authenticatedConnectionInitiallyEstablishedTimestamp = System.currentTimeMillis();
784        }
785        // Indicate that we're now authenticated.
786        this.authenticated = true;
787
788        // If debugging is enabled, change the the debug window title to include the
789        // name we are now logged-in as.
790        // If DEBUG was set to true AFTER the connection was created the debugger
791        // will be null
792        if (debugger != null) {
793            debugger.userHasLogged(user);
794        }
795        callConnectionAuthenticatedListener(resumed);
796
797        // Set presence to online. It is important that this is done after
798        // callConnectionAuthenticatedListener(), as this call will also
799        // eventually load the roster. And we should load the roster before we
800        // send the initial presence.
801        if (config.isSendPresence() && !resumed) {
802            Presence availablePresence = getStanzaFactory()
803                            .buildPresenceStanza()
804                            .ofType(Presence.Type.available)
805                            .build();
806            sendStanza(availablePresence);
807        }
808    }
809
810    @Override
811    public final boolean isAnonymous() {
812        return isAuthenticated() && SASLAnonymous.NAME.equals(getUsedSaslMechansism());
813    }
814
815    /**
816     * Get the name of the SASL mechanism that was used to authenticate this connection. This returns the name of
817     * mechanism which was used the last time this connection was authenticated, and will return <code>null</code> if
818     * this connection was not authenticated before.
819     *
820     * @return the name of the used SASL mechanism.
821     * @since 4.2
822     */
823    public final String getUsedSaslMechansism() {
824        return saslAuthentication.getNameOfLastUsedSaslMechansism();
825    }
826
827    private DomainBareJid xmppServiceDomain;
828
829    protected Lock getConnectionLock() {
830        return connectionLock;
831    }
832
833    protected void throwNotConnectedExceptionIfAppropriate() throws NotConnectedException {
834        throwNotConnectedExceptionIfAppropriate(null);
835    }
836
837    protected void throwNotConnectedExceptionIfAppropriate(String optionalHint) throws NotConnectedException {
838        if (!isConnected()) {
839            throw new NotConnectedException(optionalHint);
840        }
841    }
842
843    protected void throwAlreadyConnectedExceptionIfAppropriate() throws AlreadyConnectedException {
844        if (isConnected()) {
845            throw new AlreadyConnectedException();
846        }
847    }
848
849    protected void throwAlreadyLoggedInExceptionIfAppropriate() throws AlreadyLoggedInException {
850        if (isAuthenticated()) {
851            throw new AlreadyLoggedInException();
852        }
853    }
854
855    @Override
856    public final StanzaFactory getStanzaFactory() {
857        return stanzaFactory;
858    }
859
860    @Override
861    public final void sendStanza(Stanza stanza) throws NotConnectedException, InterruptedException {
862        Objects.requireNonNull(stanza, "Stanza must not be null");
863        assert stanza instanceof Message || stanza instanceof Presence || stanza instanceof IQ;
864
865        throwNotConnectedExceptionIfAppropriate();
866        switch (fromMode) {
867        case OMITTED:
868            stanza.setFrom((Jid) null);
869            break;
870        case USER:
871            stanza.setFrom(getUser());
872            break;
873        case UNCHANGED:
874        default:
875            break;
876        }
877        // Invoke interceptors for the new stanza that is about to be sent. Interceptors may modify
878        // the content of the stanza.
879        Stanza stanzaAfterInterceptors = firePacketInterceptors(stanza);
880        sendStanzaInternal(stanzaAfterInterceptors);
881    }
882
883    /**
884     * Authenticate a connection.
885     *
886     * @param username the username that is authenticating with the server.
887     * @param password the password to send to the server.
888     * @param authzid the authorization identifier (typically null).
889     * @param sslSession the optional SSL/TLS session (if one was established)
890     * @return the used SASLMechanism.
891     * @throws XMPPErrorException if there was an XMPP error returned.
892     * @throws SASLErrorException if a SASL protocol error was returned.
893     * @throws IOException if an I/O error occurred.
894     * @throws InterruptedException if the calling thread was interrupted.
895     * @throws SmackSaslException if a SASL specific error occurred.
896     * @throws NotConnectedException if the XMPP connection is not connected.
897     * @throws NoResponseException if there was no response from the remote entity.
898     * @throws SmackWrappedException in case of an exception.
899     * @see SASLAuthentication#authenticate(String, String, EntityBareJid, SSLSession)
900     */
901    protected final SASLMechanism authenticate(String username, String password, EntityBareJid authzid,
902                    SSLSession sslSession) throws XMPPErrorException, SASLErrorException, SmackSaslException,
903                    NotConnectedException, NoResponseException, IOException, InterruptedException, SmackWrappedException {
904        SASLMechanism saslMechanism = saslAuthentication.authenticate(username, password, authzid, sslSession);
905        afterSaslAuthenticationSuccess();
906        return saslMechanism;
907    }
908
909    /**
910     * Hook for subclasses right after successful SASL authentication. RFC 6120 § 6.4.6. specifies a that the initiating
911     * entity, needs to initiate a new stream in this case. But some transports, like BOSH, requires a special handling.
912     * <p>
913     * Note that we can not reset XMPPTCPConnection's parser here, because this method is invoked by the thread calling
914     * {@link #login()}, but the parser reset has to be done within the reader thread.
915     * </p>
916     *
917     * @throws NotConnectedException if the XMPP connection is not connected.
918     * @throws InterruptedException if the calling thread was interrupted.
919     * @throws SmackWrappedException in case of an exception.
920     */
921    protected void afterSaslAuthenticationSuccess()
922                    throws NotConnectedException, InterruptedException, SmackWrappedException {
923        sendStreamOpen();
924    }
925
926    protected final boolean isSaslAuthenticated() {
927        return saslAuthentication.authenticationSuccessful();
928    }
929
930    /**
931     * Closes the connection by setting presence to unavailable then closing the connection to
932     * the XMPP server. The XMPPConnection can still be used for connecting to the server
933     * again.
934     *
935     */
936    public void disconnect() {
937        Presence unavailablePresence = null;
938        if (isAuthenticated()) {
939            unavailablePresence = getStanzaFactory().buildPresenceStanza()
940                            .ofType(Presence.Type.unavailable)
941                            .build();
942        }
943        try {
944            disconnect(unavailablePresence);
945        }
946        catch (NotConnectedException e) {
947            LOGGER.log(Level.FINEST, "Connection is already disconnected", e);
948        }
949    }
950
951    /**
952     * Closes the connection. A custom unavailable presence is sent to the server, followed
953     * by closing the stream. The XMPPConnection can still be used for connecting to the server
954     * again. A custom unavailable presence is useful for communicating offline presence
955     * information such as "On vacation". Typically, just the status text of the presence
956     * stanza is set with online information, but most XMPP servers will deliver the full
957     * presence stanza with whatever data is set.
958     *
959     * @param unavailablePresence the optional presence stanza to send during shutdown.
960     * @throws NotConnectedException if the XMPP connection is not connected.
961     */
962    public synchronized void disconnect(Presence unavailablePresence) throws NotConnectedException {
963        if (unavailablePresence != null) {
964            try {
965                sendStanza(unavailablePresence);
966            } catch (InterruptedException e) {
967                LOGGER.log(Level.FINE,
968                        "Was interrupted while sending unavailable presence. Continuing to disconnect the connection",
969                        e);
970            }
971        }
972        shutdown();
973        callConnectionClosedListener();
974    }
975
976    private final Object notifyConnectionErrorMonitor = new Object();
977
978    /**
979     * Sends out a notification that there was an error with the connection
980     * and closes the connection.
981     *
982     * @param exception the exception that causes the connection close event.
983     */
984    protected final void notifyConnectionError(final Exception exception) {
985        synchronized (notifyConnectionErrorMonitor) {
986            if (!isConnected()) {
987                LOGGER.log(Level.INFO, "Connection was already disconnected when attempting to handle " + exception,
988                                exception);
989                return;
990            }
991
992            // Note that we first have to set the current connection exception and notify waiting threads, as one of them
993            // could hold the instance lock, which we also need later when calling instantShutdown().
994            setCurrentConnectionExceptionAndNotify(exception);
995
996            // Closes the connection temporary. A if the connection supports stream management, then a reconnection is
997            // possible. Note that a connection listener of e.g. XMPPTCPConnection will drop the SM state in
998            // case the Exception is a StreamErrorException.
999            instantShutdown();
1000
1001            for (StanzaCollector collector : collectors) {
1002                collector.notifyConnectionError(exception);
1003            }
1004
1005            Async.go(() -> {
1006                // Notify connection listeners of the error.
1007                callConnectionClosedOnErrorListener(exception);
1008            }, AbstractXMPPConnection.this + " callConnectionClosedOnErrorListener()");
1009        }
1010    }
1011
1012    /**
1013     * Performs an unclean disconnect and shutdown of the connection. Does not send a closing stream stanza.
1014     */
1015    public abstract void instantShutdown();
1016
1017    /**
1018     * Shuts the current connection down.
1019     */
1020    protected abstract void shutdown();
1021
1022    protected final boolean waitForClosingStreamTagFromServer() {
1023        try {
1024            waitForConditionOrThrowConnectionException(() -> closingStreamReceived, "closing stream tag from the server");
1025        } catch (InterruptedException | SmackException | XMPPException e) {
1026            LOGGER.log(Level.INFO, "Exception while waiting for closing stream element from the server " + this, e);
1027            return false;
1028        }
1029        return true;
1030    }
1031
1032    @Override
1033    public void addConnectionListener(ConnectionListener connectionListener) {
1034        if (connectionListener == null) {
1035            return;
1036        }
1037        connectionListeners.add(connectionListener);
1038    }
1039
1040    @Override
1041    public void removeConnectionListener(ConnectionListener connectionListener) {
1042        connectionListeners.remove(connectionListener);
1043    }
1044
1045    @Override
1046    public <I extends IQ> I sendIqRequestAndWaitForResponse(IQ request)
1047            throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
1048        StanzaCollector collector = createStanzaCollectorAndSend(request);
1049        IQ resultResponse = collector.nextResultOrThrow();
1050        @SuppressWarnings("unchecked")
1051        I concreteResultResponse = (I) resultResponse;
1052        return concreteResultResponse;
1053    }
1054
1055    @Override
1056    public StanzaCollector createStanzaCollectorAndSend(IQ packet) throws NotConnectedException, InterruptedException {
1057        StanzaFilter packetFilter = new IQReplyFilter(packet, this);
1058        // Create the packet collector before sending the packet
1059        StanzaCollector packetCollector = createStanzaCollectorAndSend(packetFilter, packet);
1060        return packetCollector;
1061    }
1062
1063    @Override
1064    public StanzaCollector createStanzaCollectorAndSend(StanzaFilter packetFilter, Stanza packet)
1065                    throws NotConnectedException, InterruptedException {
1066        StanzaCollector.Configuration configuration = StanzaCollector.newConfiguration()
1067                        .setStanzaFilter(packetFilter)
1068                        .setRequest(packet);
1069        // Create the packet collector before sending the packet
1070        StanzaCollector packetCollector = createStanzaCollector(configuration);
1071        try {
1072            // Now we can send the packet as the collector has been created
1073            sendStanza(packet);
1074        }
1075        catch (InterruptedException | NotConnectedException | RuntimeException e) {
1076            packetCollector.cancel();
1077            throw e;
1078        }
1079        return packetCollector;
1080    }
1081
1082    @Override
1083    public StanzaCollector createStanzaCollector(StanzaFilter packetFilter) {
1084        StanzaCollector.Configuration configuration = StanzaCollector.newConfiguration().setStanzaFilter(packetFilter);
1085        return createStanzaCollector(configuration);
1086    }
1087
1088    @Override
1089    public StanzaCollector createStanzaCollector(StanzaCollector.Configuration configuration) {
1090        StanzaCollector collector = new StanzaCollector(this, configuration);
1091        // Add the collector to the list of active collectors.
1092        collectors.add(collector);
1093        return collector;
1094    }
1095
1096    @Override
1097    public void removeStanzaCollector(StanzaCollector collector) {
1098        collectors.remove(collector);
1099    }
1100
1101    @Override
1102    public final void addStanzaListener(StanzaListener stanzaListener, StanzaFilter stanzaFilter) {
1103        if (stanzaListener == null) {
1104            throw new NullPointerException("Given stanza listener must not be null");
1105        }
1106        ListenerWrapper wrapper = new ListenerWrapper(stanzaListener, stanzaFilter);
1107        synchronized (recvListeners) {
1108            recvListeners.put(stanzaListener, wrapper);
1109        }
1110    }
1111
1112    @Override
1113    public final boolean removeStanzaListener(StanzaListener stanzaListener) {
1114        synchronized (recvListeners) {
1115            return recvListeners.remove(stanzaListener) != null;
1116        }
1117    }
1118
1119    @Override
1120    public void addSyncStanzaListener(StanzaListener packetListener, StanzaFilter packetFilter) {
1121        if (packetListener == null) {
1122            throw new NullPointerException("Packet listener is null.");
1123        }
1124        ListenerWrapper wrapper = new ListenerWrapper(packetListener, packetFilter);
1125        synchronized (syncRecvListeners) {
1126            syncRecvListeners.put(packetListener, wrapper);
1127        }
1128    }
1129
1130    @Override
1131    public boolean removeSyncStanzaListener(StanzaListener packetListener) {
1132        synchronized (syncRecvListeners) {
1133            return syncRecvListeners.remove(packetListener) != null;
1134        }
1135    }
1136
1137    @Override
1138    public void addAsyncStanzaListener(StanzaListener packetListener, StanzaFilter packetFilter) {
1139        if (packetListener == null) {
1140            throw new NullPointerException("Packet listener is null.");
1141        }
1142        ListenerWrapper wrapper = new ListenerWrapper(packetListener, packetFilter);
1143        synchronized (asyncRecvListeners) {
1144            asyncRecvListeners.put(packetListener, wrapper);
1145        }
1146    }
1147
1148    @Override
1149    public boolean removeAsyncStanzaListener(StanzaListener packetListener) {
1150        synchronized (asyncRecvListeners) {
1151            return asyncRecvListeners.remove(packetListener) != null;
1152        }
1153    }
1154
1155    @Override
1156    public void addStanzaSendingListener(StanzaListener packetListener, StanzaFilter packetFilter) {
1157        if (packetListener == null) {
1158            throw new NullPointerException("Packet listener is null.");
1159        }
1160        ListenerWrapper wrapper = new ListenerWrapper(packetListener, packetFilter);
1161        synchronized (sendListeners) {
1162            sendListeners.put(packetListener, wrapper);
1163        }
1164    }
1165
1166    @Override
1167    public void removeStanzaSendingListener(StanzaListener packetListener) {
1168        synchronized (sendListeners) {
1169            sendListeners.remove(packetListener);
1170        }
1171    }
1172
1173    /**
1174     * Process all stanza listeners for sending stanzas.
1175     * <p>
1176     * Compared to {@link #firePacketInterceptors(Stanza)}, the listeners will be invoked in a new thread.
1177     * </p>
1178     *
1179     * @param sendTopLevelStreamElement the top level stream element which just got send.
1180     */
1181    // TODO: Rename to fireElementSendingListeners().
1182    @SuppressWarnings("javadoc")
1183    protected void firePacketSendingListeners(final TopLevelStreamElement sendTopLevelStreamElement) {
1184        if (debugger != null) {
1185            debugger.onOutgoingStreamElement(sendTopLevelStreamElement);
1186        }
1187
1188        if (!(sendTopLevelStreamElement instanceof Stanza)) {
1189            return;
1190        }
1191        Stanza packet = (Stanza) sendTopLevelStreamElement;
1192
1193        final List<StanzaListener> listenersToNotify = new LinkedList<>();
1194        synchronized (sendListeners) {
1195            for (ListenerWrapper listenerWrapper : sendListeners.values()) {
1196                if (listenerWrapper.filterMatches(packet)) {
1197                    listenersToNotify.add(listenerWrapper.getListener());
1198                }
1199            }
1200        }
1201        if (listenersToNotify.isEmpty()) {
1202            return;
1203        }
1204        // Notify in a new thread, because we can
1205        asyncGo(new Runnable() {
1206            @Override
1207            public void run() {
1208                for (StanzaListener listener : listenersToNotify) {
1209                    try {
1210                        listener.processStanza(packet);
1211                    }
1212                    catch (Exception e) {
1213                        LOGGER.log(Level.WARNING, "Sending listener threw exception", e);
1214                        continue;
1215                    }
1216                }
1217            }
1218        });
1219    }
1220
1221    @Deprecated
1222    @Override
1223    public void addStanzaInterceptor(StanzaListener packetInterceptor,
1224            StanzaFilter packetFilter) {
1225        if (packetInterceptor == null) {
1226            throw new NullPointerException("Packet interceptor is null.");
1227        }
1228        InterceptorWrapper interceptorWrapper = new InterceptorWrapper(packetInterceptor, packetFilter);
1229        synchronized (interceptors) {
1230            interceptors.put(packetInterceptor, interceptorWrapper);
1231        }
1232    }
1233
1234    @Deprecated
1235    @Override
1236    public void removeStanzaInterceptor(StanzaListener packetInterceptor) {
1237        synchronized (interceptors) {
1238            interceptors.remove(packetInterceptor);
1239        }
1240    }
1241
1242    private static <MPB extends MessageOrPresenceBuilder<MP, MPB>, MP extends MessageOrPresence<MPB>> void addInterceptor(
1243                    Map<Consumer<MPB>, GenericInterceptorWrapper<MPB, MP>> interceptors, Consumer<MPB> interceptor,
1244                    Predicate<MP> filter) {
1245        Objects.requireNonNull(interceptor, "Interceptor must not be null");
1246
1247        GenericInterceptorWrapper<MPB, MP> interceptorWrapper = new GenericInterceptorWrapper<>(interceptor, filter);
1248
1249        synchronized (interceptors) {
1250            interceptors.put(interceptor, interceptorWrapper);
1251        }
1252    }
1253
1254    private static <MPB extends MessageOrPresenceBuilder<MP, MPB>, MP extends MessageOrPresence<MPB>> void removeInterceptor(
1255                    Map<Consumer<MPB>, GenericInterceptorWrapper<MPB, MP>> interceptors, Consumer<MPB> interceptor) {
1256        synchronized (interceptors) {
1257            interceptors.remove(interceptor);
1258        }
1259    }
1260
1261    @Override
1262    public void addMessageInterceptor(Consumer<MessageBuilder> messageInterceptor, Predicate<Message> messageFilter) {
1263        addInterceptor(messageInterceptors, messageInterceptor, messageFilter);
1264    }
1265
1266    @Override
1267    public void removeMessageInterceptor(Consumer<MessageBuilder> messageInterceptor) {
1268        removeInterceptor(messageInterceptors, messageInterceptor);
1269    }
1270
1271    @Override
1272    public void addPresenceInterceptor(Consumer<PresenceBuilder> presenceInterceptor,
1273                    Predicate<Presence> presenceFilter) {
1274        addInterceptor(presenceInterceptors, presenceInterceptor, presenceFilter);
1275    }
1276
1277    @Override
1278    public void removePresenceInterceptor(Consumer<PresenceBuilder> presenceInterceptor) {
1279        removeInterceptor(presenceInterceptors, presenceInterceptor);
1280    }
1281
1282    private static <MPB extends MessageOrPresenceBuilder<MP, MPB>, MP extends MessageOrPresence<MPB>> MP fireMessageOrPresenceInterceptors(
1283                    MP messageOrPresence, Map<Consumer<MPB>, GenericInterceptorWrapper<MPB, MP>> interceptors) {
1284        List<Consumer<MPB>> interceptorsToInvoke = new LinkedList<>();
1285        synchronized (interceptors) {
1286            for (GenericInterceptorWrapper<MPB, MP> interceptorWrapper : interceptors.values()) {
1287                if (interceptorWrapper.filterMatches(messageOrPresence)) {
1288                    Consumer<MPB> interceptor = interceptorWrapper.getInterceptor();
1289                    interceptorsToInvoke.add(interceptor);
1290                }
1291            }
1292        }
1293
1294        // Avoid transforming the stanza to a builder if there is no interceptor.
1295        if (interceptorsToInvoke.isEmpty()) {
1296            return messageOrPresence;
1297        }
1298
1299        MPB builder = messageOrPresence.asBuilder();
1300        for (Consumer<MPB> interceptor : interceptorsToInvoke) {
1301            interceptor.accept(builder);
1302        }
1303
1304        // Now that the interceptors have (probably) modified the stanza in its builder form, we need to re-assemble it.
1305        messageOrPresence = builder.build();
1306        return messageOrPresence;
1307    }
1308
1309    /**
1310     * Process interceptors. Interceptors may modify the stanza that is about to be sent.
1311     * Since the thread that requested to send the stanza will invoke all interceptors, it
1312     * is important that interceptors perform their work as soon as possible so that the
1313     * thread does not remain blocked for a long period.
1314     *
1315     * @param packet the stanza that is going to be sent to the server.
1316     * @return the, potentially modified stanza, after the interceptors are run.
1317     */
1318    private Stanza firePacketInterceptors(Stanza packet) {
1319        List<StanzaListener> interceptorsToInvoke = new LinkedList<>();
1320        synchronized (interceptors) {
1321            for (InterceptorWrapper interceptorWrapper : interceptors.values()) {
1322                if (interceptorWrapper.filterMatches(packet)) {
1323                    interceptorsToInvoke.add(interceptorWrapper.getInterceptor());
1324                }
1325            }
1326        }
1327        for (StanzaListener interceptor : interceptorsToInvoke) {
1328            try {
1329                interceptor.processStanza(packet);
1330            } catch (Exception e) {
1331                LOGGER.log(Level.SEVERE, "Packet interceptor threw exception", e);
1332            }
1333        }
1334
1335        final Stanza stanzaAfterInterceptors;
1336        if (packet instanceof Message) {
1337            Message message = (Message) packet;
1338            stanzaAfterInterceptors = fireMessageOrPresenceInterceptors(message, messageInterceptors);
1339        }
1340        else if (packet instanceof Presence) {
1341            Presence presence = (Presence) packet;
1342            stanzaAfterInterceptors = fireMessageOrPresenceInterceptors(presence, presenceInterceptors);
1343        } else {
1344            // We do not (yet) support interceptors for IQ stanzas.
1345            assert packet instanceof IQ;
1346            stanzaAfterInterceptors = packet;
1347        }
1348
1349        return stanzaAfterInterceptors;
1350    }
1351
1352    /**
1353     * Initialize the {@link #debugger}. You can specify a customized {@link SmackDebugger}
1354     * by setup the system property <code>smack.debuggerClass</code> to the implementation.
1355     *
1356     * @throws IllegalStateException if the reader or writer isn't yet initialized.
1357     * @throws IllegalArgumentException if the SmackDebugger can't be loaded.
1358     */
1359    protected void initDebugger() {
1360        if (reader == null || writer == null) {
1361            throw new NullPointerException("Reader or writer isn't initialized.");
1362        }
1363        // If debugging is enabled, we open a window and write out all network traffic.
1364        if (debugger != null) {
1365            // Obtain new reader and writer from the existing debugger
1366            reader = debugger.newConnectionReader(reader);
1367            writer = debugger.newConnectionWriter(writer);
1368        }
1369    }
1370
1371    @Override
1372    public long getReplyTimeout() {
1373        return replyTimeout;
1374    }
1375
1376    @Override
1377    public void setReplyTimeout(long timeout) {
1378        if (Long.MAX_VALUE - System.currentTimeMillis() < timeout) {
1379            throw new IllegalArgumentException("Extremely long reply timeout");
1380        }
1381        else {
1382            replyTimeout = timeout;
1383        }
1384    }
1385
1386    private SmackConfiguration.UnknownIqRequestReplyMode unknownIqRequestReplyMode = SmackConfiguration.getUnknownIqRequestReplyMode();
1387
1388    /**
1389     * Set how Smack behaves when an unknown IQ request has been received.
1390     *
1391     * @param unknownIqRequestReplyMode reply mode.
1392     */
1393    public void setUnknownIqRequestReplyMode(UnknownIqRequestReplyMode unknownIqRequestReplyMode) {
1394        this.unknownIqRequestReplyMode = Objects.requireNonNull(unknownIqRequestReplyMode, "Mode must not be null");
1395    }
1396
1397    protected final NonzaCallback.Builder buildNonzaCallback() {
1398        return new NonzaCallback.Builder(this);
1399    }
1400
1401    protected <SN extends Nonza, FN extends Nonza> SN sendAndWaitForResponse(Nonza nonza, Class<SN> successNonzaClass,
1402                    Class<FN> failedNonzaClass)
1403                    throws NoResponseException, NotConnectedException, InterruptedException, FailedNonzaException {
1404        NonzaCallback.Builder builder = buildNonzaCallback();
1405        SN successNonza = NonzaCallback.sendAndWaitForResponse(builder, nonza, successNonzaClass, failedNonzaClass);
1406        return successNonza;
1407    }
1408
1409    protected final void parseAndProcessNonza(XmlPullParser parser) throws IOException, XmlPullParserException, SmackParsingException {
1410        ParserUtils.assertAtStartTag(parser);
1411
1412        final int initialDepth = parser.getDepth();
1413        final String element = parser.getName();
1414        final String namespace = parser.getNamespace();
1415        final QName key = new QName(namespace, element);
1416
1417        NonzaProvider<? extends Nonza> nonzaProvider = ProviderManager.getNonzaProvider(key);
1418        if (nonzaProvider == null) {
1419            LOGGER.severe("Unknown nonza: " + key);
1420            ParserUtils.forwardToEndTagOfDepth(parser, initialDepth);
1421            return;
1422        }
1423
1424        List<NonzaCallback> nonzaCallbacks;
1425        synchronized (nonzaCallbacksMap) {
1426            nonzaCallbacks = nonzaCallbacksMap.getAll(key);
1427            nonzaCallbacks = CollectionUtil.newListWith(nonzaCallbacks);
1428        }
1429        if (nonzaCallbacks == null) {
1430            LOGGER.info("No nonza callback for " + key);
1431            ParserUtils.forwardToEndTagOfDepth(parser, initialDepth);
1432            return;
1433        }
1434
1435        Nonza nonza = nonzaProvider.parse(parser, incomingStreamXmlEnvironment);
1436
1437        for (NonzaCallback nonzaCallback : nonzaCallbacks) {
1438            nonzaCallback.onNonzaReceived(nonza);
1439        }
1440    }
1441
1442    protected void parseAndProcessStanza(XmlPullParser parser)
1443                    throws XmlPullParserException, IOException, InterruptedException {
1444        ParserUtils.assertAtStartTag(parser);
1445        int parserDepth = parser.getDepth();
1446        Stanza stanza = null;
1447        try {
1448            stanza = PacketParserUtils.parseStanza(parser, incomingStreamXmlEnvironment);
1449        }
1450        catch (XmlPullParserException | SmackParsingException | IOException | IllegalArgumentException e) {
1451            CharSequence content = PacketParserUtils.parseContentDepth(parser,
1452                            parserDepth);
1453            UnparseableStanza message = new UnparseableStanza(content, e);
1454            ParsingExceptionCallback callback = getParsingExceptionCallback();
1455            if (callback != null) {
1456                callback.handleUnparsableStanza(message);
1457            }
1458        }
1459        ParserUtils.assertAtEndTag(parser);
1460        if (stanza != null) {
1461            processStanza(stanza);
1462        }
1463    }
1464
1465    /**
1466     * Processes a stanza after it's been fully parsed by looping through the installed
1467     * stanza collectors and listeners and letting them examine the stanza to see if
1468     * they are a match with the filter.
1469     *
1470     * @param stanza the stanza to process.
1471     * @throws InterruptedException if the calling thread was interrupted.
1472     */
1473    protected void processStanza(final Stanza stanza) throws InterruptedException {
1474        assert stanza != null;
1475
1476        final SmackDebugger debugger = this.debugger;
1477        if (debugger != null) {
1478            debugger.onIncomingStreamElement(stanza);
1479        }
1480
1481        lastStanzaReceived = System.currentTimeMillis();
1482        // Deliver the incoming packet to listeners.
1483        invokeStanzaCollectorsAndNotifyRecvListeners(stanza);
1484    }
1485
1486    /**
1487     * Invoke {@link StanzaCollector#processStanza(Stanza)} for every
1488     * StanzaCollector with the given packet. Also notify the receive listeners with a matching stanza filter about the packet.
1489     * <p>
1490     * This method will be invoked by the connections incoming processing thread which may be shared across multiple connections and
1491     * thus it is important that no user code, e.g. in form of a callback, is invoked by this method. For the same reason,
1492     * this method must not block for an extended period of time.
1493     * </p>
1494     *
1495     * @param packet the stanza to notify the StanzaCollectors and receive listeners about.
1496     */
1497    protected void invokeStanzaCollectorsAndNotifyRecvListeners(final Stanza packet) {
1498        if (packet instanceof IQ) {
1499            final IQ iq = (IQ) packet;
1500            if (iq.isRequestIQ()) {
1501                final IQ iqRequest = iq;
1502                final QName key = iqRequest.getChildElementQName();
1503                IQRequestHandler iqRequestHandler;
1504                final IQ.Type type = iq.getType();
1505                switch (type) {
1506                case set:
1507                    synchronized (setIqRequestHandler) {
1508                        iqRequestHandler = setIqRequestHandler.get(key);
1509                    }
1510                    break;
1511                case get:
1512                    synchronized (getIqRequestHandler) {
1513                        iqRequestHandler = getIqRequestHandler.get(key);
1514                    }
1515                    break;
1516                default:
1517                    throw new IllegalStateException("Should only encounter IQ type 'get' or 'set'");
1518                }
1519                if (iqRequestHandler == null) {
1520                    StanzaError.Condition replyCondition;
1521                    switch (unknownIqRequestReplyMode) {
1522                    case doNotReply:
1523                        return;
1524                    case replyFeatureNotImplemented:
1525                        replyCondition = StanzaError.Condition.feature_not_implemented;
1526                        break;
1527                    case replyServiceUnavailable:
1528                        replyCondition = StanzaError.Condition.service_unavailable;
1529                        break;
1530                    default:
1531                        throw new AssertionError();
1532                    }
1533
1534                    // If the IQ stanza is of type "get" or "set" with no registered IQ request handler, then answer an
1535                    // IQ of type 'error' with condition 'service-unavailable'.
1536                    final ErrorIQ errorIQ = IQ.createErrorResponse(iq, StanzaError.getBuilder(
1537                                    replyCondition).build());
1538                    // Use async sendStanza() here, since if sendStanza() would block, then some connections, e.g.
1539                    // XmppNioTcpConnection, would deadlock, as this operation is performed in the same thread that is
1540                    asyncGo(() -> {
1541                        try {
1542                            sendStanza(errorIQ);
1543                        }
1544                        catch (InterruptedException | NotConnectedException e) {
1545                            LOGGER.log(Level.WARNING, "Exception while sending error IQ to unkown IQ request", e);
1546                        }
1547                    });
1548                } else {
1549                    Executor executorService = null;
1550                    switch (iqRequestHandler.getMode()) {
1551                    case sync:
1552                        executorService = ASYNC_BUT_ORDERED.asExecutorFor(this);
1553                        break;
1554                    case async:
1555                        executorService = this::asyncGoLimited;
1556                        break;
1557                    }
1558                    final IQRequestHandler finalIqRequestHandler = iqRequestHandler;
1559                    executorService.execute(new Runnable() {
1560                        @Override
1561                        public void run() {
1562                            IQ response = finalIqRequestHandler.handleIQRequest(iq);
1563                            if (response == null) {
1564                                // It is not ideal if the IQ request handler does not return an IQ response, because RFC
1565                                // 6120 § 8.1.2 does specify that a response is mandatory. But some APIs, mostly the
1566                                // file transfer one, does not always return a result, so we need to handle this case.
1567                                // Also sometimes a request handler may decide that it's better to not send a response,
1568                                // e.g. to avoid presence leaks.
1569                                return;
1570                            }
1571
1572                            assert response.isResponseIQ();
1573
1574                            response.setTo(iqRequest.getFrom());
1575                            response.setStanzaId(iqRequest.getStanzaId());
1576                            try {
1577                                sendStanza(response);
1578                            }
1579                            catch (InterruptedException | NotConnectedException e) {
1580                                LOGGER.log(Level.WARNING, "Exception while sending response to IQ request", e);
1581                            }
1582                        }
1583                    });
1584                }
1585                // The following returns makes it impossible for packet listeners and collectors to
1586                // filter for IQ request stanzas, i.e. IQs of type 'set' or 'get'. This is the
1587                // desired behavior.
1588                return;
1589            }
1590        }
1591
1592        // First handle the async recv listeners. Note that this code is very similar to what follows a few lines below,
1593        // the only difference is that asyncRecvListeners is used here and that the packet listeners are started in
1594        // their own thread.
1595        final Collection<StanzaListener> listenersToNotify = new LinkedList<>();
1596        extractMatchingListeners(packet, asyncRecvListeners, listenersToNotify);
1597        for (final StanzaListener listener : listenersToNotify) {
1598            asyncGoLimited(new Runnable() {
1599                @Override
1600                public void run() {
1601                    try {
1602                        listener.processStanza(packet);
1603                    } catch (Exception e) {
1604                        LOGGER.log(Level.SEVERE, "Exception in async packet listener", e);
1605                    }
1606                }
1607            });
1608        }
1609
1610        // Loop through all collectors and notify the appropriate ones.
1611        for (StanzaCollector collector : collectors) {
1612            collector.processStanza(packet);
1613        }
1614
1615        listenersToNotify.clear();
1616        extractMatchingListeners(packet, recvListeners, listenersToNotify);
1617        for (StanzaListener stanzaListener : listenersToNotify) {
1618            inOrderListeners.performAsyncButOrdered(stanzaListener, () -> {
1619                try {
1620                    stanzaListener.processStanza(packet);
1621                }
1622                catch (NotConnectedException e) {
1623                    LOGGER.log(Level.WARNING, "Got not connected exception, aborting", e);
1624                }
1625                catch (Exception e) {
1626                    LOGGER.log(Level.SEVERE, "Exception in packet listener", e);
1627                }
1628            });
1629        }
1630
1631        // Notify the receive listeners interested in the packet
1632        listenersToNotify.clear();
1633        extractMatchingListeners(packet, syncRecvListeners, listenersToNotify);
1634        // Decouple incoming stanza processing from listener invocation. Unlike async listeners, this uses a single
1635        // threaded executor service and therefore keeps the order.
1636        ASYNC_BUT_ORDERED.performAsyncButOrdered(this, new Runnable() {
1637            @Override
1638            public void run() {
1639                // As listeners are able to remove themselves and because the timepoint where it is decided to invoke a
1640                // listener is a different timepoint where the listener is actually invoked (here), we have to check
1641                // again if the listener is still active.
1642                Iterator<StanzaListener> it = listenersToNotify.iterator();
1643                synchronized (syncRecvListeners) {
1644                    while (it.hasNext()) {
1645                        StanzaListener stanzaListener = it.next();
1646                        if (!syncRecvListeners.containsKey(stanzaListener)) {
1647                            // The listener was removed from syncRecvListener, also remove him from listenersToNotify.
1648                            it.remove();
1649                        }
1650                    }
1651                }
1652                for (StanzaListener listener : listenersToNotify) {
1653                    try {
1654                        listener.processStanza(packet);
1655                    } catch (NotConnectedException e) {
1656                        LOGGER.log(Level.WARNING, "Got not connected exception, aborting", e);
1657                        break;
1658                    } catch (Exception e) {
1659                        LOGGER.log(Level.SEVERE, "Exception in packet listener", e);
1660                    }
1661                }
1662            }
1663        });
1664    }
1665
1666    private static void extractMatchingListeners(Stanza stanza, Map<StanzaListener, ListenerWrapper> listeners,
1667                    Collection<StanzaListener> listenersToNotify) {
1668        synchronized (listeners) {
1669            for (ListenerWrapper listenerWrapper : listeners.values()) {
1670                if (listenerWrapper.filterMatches(stanza)) {
1671                    listenersToNotify.add(listenerWrapper.getListener());
1672                }
1673            }
1674        }
1675    }
1676
1677    /**
1678     * Sets whether the connection has already logged in the server. This method assures that the
1679     * {@link #wasAuthenticated} flag is never reset once it has ever been set.
1680     *
1681     */
1682    protected void setWasAuthenticated() {
1683        // Never reset the flag if the connection has ever been authenticated
1684        if (!wasAuthenticated) {
1685            wasAuthenticated = authenticated;
1686        }
1687    }
1688
1689    protected void callConnectionConnectingListener() {
1690        for (ConnectionListener listener : connectionListeners) {
1691            listener.connecting(this);
1692        }
1693    }
1694
1695    protected void callConnectionConnectedListener() {
1696        for (ConnectionListener listener : connectionListeners) {
1697            listener.connected(this);
1698        }
1699    }
1700
1701    protected void callConnectionAuthenticatedListener(boolean resumed) {
1702        for (ConnectionListener listener : connectionListeners) {
1703            try {
1704                listener.authenticated(this, resumed);
1705            } catch (Exception e) {
1706                // Catch and print any exception so we can recover
1707                // from a faulty listener and finish the shutdown process
1708                LOGGER.log(Level.SEVERE, "Exception in authenticated listener", e);
1709            }
1710        }
1711    }
1712
1713    void callConnectionClosedListener() {
1714        for (ConnectionListener listener : connectionListeners) {
1715            try {
1716                listener.connectionClosed();
1717            }
1718            catch (Exception e) {
1719                // Catch and print any exception so we can recover
1720                // from a faulty listener and finish the shutdown process
1721                LOGGER.log(Level.SEVERE, "Error in listener while closing connection", e);
1722            }
1723        }
1724    }
1725
1726    private void callConnectionClosedOnErrorListener(Exception e) {
1727        boolean logWarning = true;
1728        if (e instanceof StreamErrorException) {
1729            StreamErrorException see = (StreamErrorException) e;
1730            if (see.getStreamError().getCondition() == StreamError.Condition.not_authorized
1731                            && wasAuthenticated) {
1732                logWarning = false;
1733                LOGGER.log(Level.FINE,
1734                                "Connection closed with not-authorized stream error after it was already authenticated. The account was likely deleted/unregistered on the server");
1735            }
1736        }
1737        if (logWarning) {
1738            LOGGER.log(Level.WARNING, "Connection " + this + " closed with error", e);
1739        }
1740        for (ConnectionListener listener : connectionListeners) {
1741            try {
1742                listener.connectionClosedOnError(e);
1743            }
1744            catch (Exception e2) {
1745                // Catch and print any exception so we can recover
1746                // from a faulty listener
1747                LOGGER.log(Level.SEVERE, "Error in listener while closing connection", e2);
1748            }
1749        }
1750    }
1751
1752    /**
1753     * A wrapper class to associate a stanza filter with a listener.
1754     */
1755    protected static class ListenerWrapper {
1756
1757        private final StanzaListener packetListener;
1758        private final StanzaFilter packetFilter;
1759
1760        /**
1761         * Create a class which associates a stanza filter with a listener.
1762         *
1763         * @param packetListener the stanza listener.
1764         * @param packetFilter the associated filter or null if it listen for all packets.
1765         */
1766        public ListenerWrapper(StanzaListener packetListener, StanzaFilter packetFilter) {
1767            this.packetListener = packetListener;
1768            this.packetFilter = packetFilter;
1769        }
1770
1771        public boolean filterMatches(Stanza packet) {
1772            return packetFilter == null || packetFilter.accept(packet);
1773        }
1774
1775        public StanzaListener getListener() {
1776            return packetListener;
1777        }
1778    }
1779
1780    /**
1781     * A wrapper class to associate a stanza filter with an interceptor.
1782     */
1783    @Deprecated
1784    // TODO: Remove once addStanzaInterceptor is gone.
1785    protected static class InterceptorWrapper {
1786
1787        private final StanzaListener packetInterceptor;
1788        private final StanzaFilter packetFilter;
1789
1790        /**
1791         * Create a class which associates a stanza filter with an interceptor.
1792         *
1793         * @param packetInterceptor the interceptor.
1794         * @param packetFilter the associated filter or null if it intercepts all packets.
1795         */
1796        public InterceptorWrapper(StanzaListener packetInterceptor, StanzaFilter packetFilter) {
1797            this.packetInterceptor = packetInterceptor;
1798            this.packetFilter = packetFilter;
1799        }
1800
1801        public boolean filterMatches(Stanza packet) {
1802            return packetFilter == null || packetFilter.accept(packet);
1803        }
1804
1805        public StanzaListener getInterceptor() {
1806            return packetInterceptor;
1807        }
1808    }
1809
1810    private static final class GenericInterceptorWrapper<MPB extends MessageOrPresenceBuilder<MP, MPB>, MP extends MessageOrPresence<MPB>> {
1811        private final Consumer<MPB> stanzaInterceptor;
1812        private final Predicate<MP> stanzaFilter;
1813
1814        private GenericInterceptorWrapper(Consumer<MPB> stanzaInterceptor, Predicate<MP> stanzaFilter) {
1815            this.stanzaInterceptor = stanzaInterceptor;
1816            this.stanzaFilter = stanzaFilter;
1817        }
1818
1819        private boolean filterMatches(MP stanza) {
1820            return stanzaFilter == null || stanzaFilter.test(stanza);
1821        }
1822
1823        public Consumer<MPB> getInterceptor() {
1824            return stanzaInterceptor;
1825        }
1826    }
1827
1828    @Override
1829    public int getConnectionCounter() {
1830        return connectionCounterValue;
1831    }
1832
1833    @Override
1834    public void setFromMode(FromMode fromMode) {
1835        this.fromMode = fromMode;
1836    }
1837
1838    @Override
1839    public FromMode getFromMode() {
1840        return this.fromMode;
1841    }
1842
1843    protected final void parseFeatures(XmlPullParser parser) throws XmlPullParserException, IOException, SmackParsingException {
1844        streamFeatures.clear();
1845        final int initialDepth = parser.getDepth();
1846        while (true) {
1847            XmlPullParser.Event eventType = parser.next();
1848
1849            if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getDepth() == initialDepth + 1) {
1850                FullyQualifiedElement streamFeature = null;
1851                String name = parser.getName();
1852                String namespace = parser.getNamespace();
1853                switch (name) {
1854                case StartTls.ELEMENT:
1855                    streamFeature = PacketParserUtils.parseStartTlsFeature(parser);
1856                    break;
1857                case Mechanisms.ELEMENT:
1858                    streamFeature = new Mechanisms(PacketParserUtils.parseMechanisms(parser));
1859                    break;
1860                case Bind.ELEMENT:
1861                    streamFeature = Bind.Feature.INSTANCE;
1862                    break;
1863                case Session.ELEMENT:
1864                    streamFeature = PacketParserUtils.parseSessionFeature(parser);
1865                    break;
1866                case Compress.Feature.ELEMENT:
1867                    streamFeature = PacketParserUtils.parseCompressionFeature(parser);
1868                    break;
1869                default:
1870                    ExtensionElementProvider<ExtensionElement> provider = ProviderManager.getStreamFeatureProvider(name, namespace);
1871                    if (provider != null) {
1872                        streamFeature = provider.parse(parser, incomingStreamXmlEnvironment);
1873                    }
1874                    break;
1875                }
1876                if (streamFeature != null) {
1877                    addStreamFeature(streamFeature);
1878                }
1879            }
1880            else if (eventType == XmlPullParser.Event.END_ELEMENT && parser.getDepth() == initialDepth) {
1881                break;
1882            }
1883        }
1884    }
1885
1886    protected final void parseFeaturesAndNotify(XmlPullParser parser) throws Exception {
1887        parseFeatures(parser);
1888
1889        if (hasFeature(Mechanisms.ELEMENT, Mechanisms.NAMESPACE)) {
1890            // Only proceed with SASL auth if TLS is disabled or if the server doesn't announce it
1891            if (!hasFeature(StartTls.ELEMENT, StartTls.NAMESPACE)
1892                            || config.getSecurityMode() == SecurityMode.disabled) {
1893                tlsHandled = saslFeatureReceived = true;
1894                notifyWaitingThreads();
1895            }
1896        }
1897
1898        // If the server reported the bind feature then we are that that we did SASL and maybe
1899        // STARTTLS. We can then report that the last 'stream:features' have been parsed
1900        if (hasFeature(Bind.ELEMENT, Bind.NAMESPACE)) {
1901            if (!hasFeature(Compress.Feature.ELEMENT, Compress.NAMESPACE)
1902                            || !config.isCompressionEnabled()) {
1903                // This where the last stream features from the server, either it did not contain
1904                // compression or we disabled it.
1905                lastFeaturesReceived = true;
1906                notifyWaitingThreads();
1907            }
1908        }
1909        afterFeaturesReceived();
1910    }
1911
1912    @SuppressWarnings("unused")
1913    protected void afterFeaturesReceived() throws SecurityRequiredException, NotConnectedException, InterruptedException {
1914        // Default implementation does nothing
1915    }
1916
1917    @SuppressWarnings("unchecked")
1918    @Override
1919    public <F extends FullyQualifiedElement> F getFeature(QName qname) {
1920        return (F) streamFeatures.get(qname);
1921    }
1922
1923    @Override
1924    public boolean hasFeature(QName qname) {
1925        return streamFeatures.containsKey(qname);
1926    }
1927
1928    protected void addStreamFeature(FullyQualifiedElement feature) {
1929        QName key = feature.getQName();
1930        streamFeatures.put(key, feature);
1931    }
1932
1933    @Override
1934    public SmackFuture<IQ, Exception> sendIqRequestAsync(IQ request) {
1935        return sendIqRequestAsync(request, getReplyTimeout());
1936    }
1937
1938    @Override
1939    public SmackFuture<IQ, Exception> sendIqRequestAsync(IQ request, long timeout) {
1940        StanzaFilter replyFilter = new IQReplyFilter(request, this);
1941        return sendAsync(request, replyFilter, timeout);
1942    }
1943
1944    @Override
1945    public <S extends Stanza> SmackFuture<S, Exception> sendAsync(S stanza, final StanzaFilter replyFilter) {
1946        return sendAsync(stanza, replyFilter, getReplyTimeout());
1947    }
1948
1949    @SuppressWarnings("FutureReturnValueIgnored")
1950    @Override
1951    public <S extends Stanza> SmackFuture<S, Exception> sendAsync(S stanza, final StanzaFilter replyFilter, long timeout) {
1952        Objects.requireNonNull(stanza, "stanza must not be null");
1953        // While Smack allows to add PacketListeners with a PacketFilter value of 'null', we
1954        // disallow it here in the async API as it makes no sense
1955        Objects.requireNonNull(replyFilter, "replyFilter must not be null");
1956
1957        final InternalSmackFuture<S, Exception> future = new InternalSmackFuture<>();
1958
1959        final StanzaListener stanzaListener = new StanzaListener() {
1960            @Override
1961            public void processStanza(Stanza stanza) throws NotConnectedException, InterruptedException {
1962                boolean removed = removeAsyncStanzaListener(this);
1963                if (!removed) {
1964                    // We lost a race against the "no response" handling runnable. Avoid calling the callback, as the
1965                    // exception callback will be invoked (if any).
1966                    return;
1967                }
1968                try {
1969                    XMPPErrorException.ifHasErrorThenThrow(stanza);
1970                    @SuppressWarnings("unchecked")
1971                    S s = (S) stanza;
1972                    future.setResult(s);
1973                }
1974                catch (XMPPErrorException exception) {
1975                    future.setException(exception);
1976                }
1977            }
1978        };
1979        schedule(new Runnable() {
1980            @Override
1981            public void run() {
1982                boolean removed = removeAsyncStanzaListener(stanzaListener);
1983                if (!removed) {
1984                    // We lost a race against the stanza listener, he already removed itself because he received a
1985                    // reply. There is nothing more to do here.
1986                    return;
1987                }
1988
1989                // If the packetListener got removed, then it was never run and
1990                // we never received a response, inform the exception callback
1991                Exception exception;
1992                if (!isConnected()) {
1993                    // If the connection is no longer connected, throw a not connected exception.
1994                    exception = new NotConnectedException(AbstractXMPPConnection.this, replyFilter);
1995                }
1996                else {
1997                    exception = NoResponseException.newWith(AbstractXMPPConnection.this, replyFilter);
1998                }
1999                future.setException(exception);
2000            }
2001        }, timeout, TimeUnit.MILLISECONDS);
2002
2003        addAsyncStanzaListener(stanzaListener, replyFilter);
2004        try {
2005            sendStanza(stanza);
2006        }
2007        catch (NotConnectedException | InterruptedException exception) {
2008            future.setException(exception);
2009        }
2010
2011        return future;
2012    }
2013
2014    @SuppressWarnings("FutureReturnValueIgnored")
2015    @Override
2016    public void addOneTimeSyncCallback(final StanzaListener callback, final StanzaFilter packetFilter) {
2017        final StanzaListener packetListener = new StanzaListener() {
2018            @Override
2019            public void processStanza(Stanza packet) throws NotConnectedException, InterruptedException, NotLoggedInException {
2020                try {
2021                    callback.processStanza(packet);
2022                } finally {
2023                    removeSyncStanzaListener(this);
2024                }
2025            }
2026        };
2027        addSyncStanzaListener(packetListener, packetFilter);
2028        schedule(new Runnable() {
2029            @Override
2030            public void run() {
2031                removeSyncStanzaListener(packetListener);
2032            }
2033        }, getReplyTimeout(), TimeUnit.MILLISECONDS);
2034    }
2035
2036    @Override
2037    public IQRequestHandler registerIQRequestHandler(final IQRequestHandler iqRequestHandler) {
2038        final QName key = iqRequestHandler.getQName();
2039        switch (iqRequestHandler.getType()) {
2040        case set:
2041            synchronized (setIqRequestHandler) {
2042                return setIqRequestHandler.put(key, iqRequestHandler);
2043            }
2044        case get:
2045            synchronized (getIqRequestHandler) {
2046                return getIqRequestHandler.put(key, iqRequestHandler);
2047            }
2048        default:
2049            throw new IllegalArgumentException("Only IQ type of 'get' and 'set' allowed");
2050        }
2051    }
2052
2053    @Override
2054    public final IQRequestHandler unregisterIQRequestHandler(IQRequestHandler iqRequestHandler) {
2055        return unregisterIQRequestHandler(iqRequestHandler.getElement(), iqRequestHandler.getNamespace(),
2056                        iqRequestHandler.getType());
2057    }
2058
2059    @Override
2060    public IQRequestHandler unregisterIQRequestHandler(String element, String namespace, IQ.Type type) {
2061        final QName key = new QName(namespace, element);
2062        switch (type) {
2063        case set:
2064            synchronized (setIqRequestHandler) {
2065                return setIqRequestHandler.remove(key);
2066            }
2067        case get:
2068            synchronized (getIqRequestHandler) {
2069                return getIqRequestHandler.remove(key);
2070            }
2071        default:
2072            throw new IllegalArgumentException("Only IQ type of 'get' and 'set' allowed");
2073        }
2074    }
2075
2076    private long lastStanzaReceived;
2077
2078    @Override
2079    public long getLastStanzaReceived() {
2080        return lastStanzaReceived;
2081    }
2082
2083    /**
2084     * Get the timestamp when the connection was the first time authenticated, i.e., when the first successful login was
2085     * performed. Note that this value is not reset on disconnect, so it represents the timestamp from the last
2086     * authenticated connection. The value is also not reset on stream resumption.
2087     *
2088     * @return the timestamp or {@code null}.
2089     * @since 4.3.3
2090     */
2091    public final long getAuthenticatedConnectionInitiallyEstablishedTimestamp() {
2092        return authenticatedConnectionInitiallyEstablishedTimestamp;
2093    }
2094
2095    /**
2096     * Install a parsing exception callback, which will be invoked once an exception is encountered while parsing a
2097     * stanza.
2098     *
2099     * @param callback the callback to install
2100     */
2101    public void setParsingExceptionCallback(ParsingExceptionCallback callback) {
2102        parsingExceptionCallback = callback;
2103    }
2104
2105    /**
2106     * Get the current active parsing exception callback.
2107     *
2108     * @return the active exception callback or null if there is none
2109     */
2110    public ParsingExceptionCallback getParsingExceptionCallback() {
2111        return parsingExceptionCallback;
2112    }
2113
2114    @Override
2115    public final String toString() {
2116        EntityFullJid localEndpoint = getUser();
2117        String localEndpointString = localEndpoint == null ?  "not-authenticated" : localEndpoint.toString();
2118        return getClass().getSimpleName() + '[' + localEndpointString + "] (" + getConnectionCounter() + ')';
2119    }
2120
2121    /**
2122     * A queue of deferred runnables that where not executed immediately because {@link #currentAsyncRunnables} reached
2123     * {@link #maxAsyncRunnables}. Note that we use a {@code LinkedList} in order to avoid space blowups in case the
2124     * list ever becomes very big and shrinks again.
2125     */
2126    private final Queue<Runnable> deferredAsyncRunnables = new LinkedList<>();
2127
2128    private int deferredAsyncRunnablesCount;
2129
2130    private int deferredAsyncRunnablesCountPrevious;
2131
2132    private int maxAsyncRunnables = SmackConfiguration.getDefaultConcurrencyLevelLimit();
2133
2134    private int currentAsyncRunnables;
2135
2136    protected void asyncGoLimited(final Runnable runnable) {
2137        Runnable wrappedRunnable = new Runnable() {
2138            @Override
2139            public void run() {
2140                runnable.run();
2141
2142                synchronized (deferredAsyncRunnables) {
2143                    Runnable defferredRunnable = deferredAsyncRunnables.poll();
2144                    if (defferredRunnable == null) {
2145                        currentAsyncRunnables--;
2146                    } else {
2147                        deferredAsyncRunnablesCount--;
2148                        asyncGo(defferredRunnable);
2149                    }
2150                }
2151            }
2152        };
2153
2154        synchronized (deferredAsyncRunnables) {
2155            if (currentAsyncRunnables < maxAsyncRunnables) {
2156                currentAsyncRunnables++;
2157                asyncGo(wrappedRunnable);
2158            } else {
2159                deferredAsyncRunnablesCount++;
2160                deferredAsyncRunnables.add(wrappedRunnable);
2161            }
2162
2163            final int HIGH_WATERMARK = 100;
2164            final int INFORM_WATERMARK = 20;
2165
2166            final int deferredAsyncRunnablesCount = this.deferredAsyncRunnablesCount;
2167
2168            if (deferredAsyncRunnablesCount >= HIGH_WATERMARK
2169                    && deferredAsyncRunnablesCountPrevious < HIGH_WATERMARK) {
2170                LOGGER.log(Level.WARNING, "High watermark of " + HIGH_WATERMARK + " simultaneous executing runnables reached");
2171            } else if (deferredAsyncRunnablesCount >= INFORM_WATERMARK
2172                    && deferredAsyncRunnablesCountPrevious < INFORM_WATERMARK) {
2173                LOGGER.log(Level.INFO, INFORM_WATERMARK + " simultaneous executing runnables reached");
2174            }
2175
2176            deferredAsyncRunnablesCountPrevious = deferredAsyncRunnablesCount;
2177        }
2178    }
2179
2180    public void setMaxAsyncOperations(int maxAsyncOperations) {
2181        if (maxAsyncOperations < 1) {
2182            throw new IllegalArgumentException("Max async operations must be greater than 0");
2183        }
2184
2185        synchronized (deferredAsyncRunnables) {
2186            maxAsyncRunnables = maxAsyncOperations;
2187        }
2188    }
2189
2190    protected static void asyncGo(Runnable runnable) {
2191        CACHED_EXECUTOR_SERVICE.execute(runnable);
2192    }
2193
2194    @SuppressWarnings("static-method")
2195    protected final SmackReactor getReactor() {
2196        return SMACK_REACTOR;
2197    }
2198
2199    protected static ScheduledAction schedule(Runnable runnable, long delay, TimeUnit unit) {
2200        return SMACK_REACTOR.schedule(runnable, delay, unit, ScheduledAction.Kind.NonBlocking);
2201    }
2202
2203    protected void onStreamOpen(XmlPullParser parser) {
2204        // We found an opening stream.
2205        if ("jabber:client".equals(parser.getNamespace(null))) {
2206            streamId = parser.getAttributeValue("", "id");
2207            incomingStreamXmlEnvironment = XmlEnvironment.from(parser);
2208
2209            String reportedServerDomainString = parser.getAttributeValue("", "from");
2210            if (reportedServerDomainString == null) {
2211                // RFC 6120 § 4.7.1. makes no explicit statement whether or not 'from' in the stream open from the server
2212                // in c2s connections is required or not.
2213                return;
2214            }
2215            DomainBareJid reportedServerDomain;
2216            try {
2217                reportedServerDomain = JidCreate.domainBareFrom(reportedServerDomainString);
2218                DomainBareJid configuredXmppServiceDomain = config.getXMPPServiceDomain();
2219                if (!configuredXmppServiceDomain.equals(reportedServerDomain)) {
2220                    LOGGER.warning("Domain reported by server '" + reportedServerDomain
2221                            + "' does not match configured domain '" + configuredXmppServiceDomain + "'");
2222                }
2223            } catch (XmppStringprepException e) {
2224                LOGGER.log(Level.WARNING, "XMPP service domain '" + reportedServerDomainString
2225                        + "' as reported by server could not be transformed to a valid JID", e);
2226            }
2227        }
2228    }
2229
2230    protected void sendStreamOpen() throws NotConnectedException, InterruptedException {
2231        // If possible, provide the receiving entity of the stream open tag, i.e. the server, as much information as
2232        // possible. The 'to' attribute is *always* available. The 'from' attribute if set by the user and no external
2233        // mechanism is used to determine the local entity (user). And the 'id' attribute is available after the first
2234        // response from the server (see e.g. RFC 6120 § 9.1.1 Step 2.)
2235        CharSequence to = getXMPPServiceDomain();
2236        CharSequence from = null;
2237        CharSequence localpart = config.getUsername();
2238        if (localpart != null) {
2239            from = XmppStringUtils.completeJidFrom(localpart, to);
2240        }
2241        String id = getStreamId();
2242
2243        StreamOpen streamOpen = new StreamOpen(to, from, id, config.getXmlLang(), StreamOpen.StreamContentNamespace.client);
2244        sendNonza(streamOpen);
2245
2246        XmlEnvironment.Builder xmlEnvironmentBuilder = XmlEnvironment.builder();
2247        xmlEnvironmentBuilder.with(streamOpen);
2248        outgoingStreamXmlEnvironment = xmlEnvironmentBuilder.build();
2249    }
2250
2251    protected final SmackTlsContext getSmackTlsContext() {
2252        return config.smackTlsContext;
2253    }
2254}