001/** 002 * 003 * Copyright 2003-2007 Jive Software. 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.tcp; 018 019import java.io.BufferedReader; 020import java.io.ByteArrayInputStream; 021import java.io.FileInputStream; 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.InputStreamReader; 025import java.io.OutputStream; 026import java.io.OutputStreamWriter; 027import java.io.Writer; 028import java.lang.reflect.Constructor; 029import java.net.InetAddress; 030import java.net.InetSocketAddress; 031import java.net.Socket; 032import java.security.KeyManagementException; 033import java.security.KeyStore; 034import java.security.KeyStoreException; 035import java.security.NoSuchAlgorithmException; 036import java.security.NoSuchProviderException; 037import java.security.Provider; 038import java.security.SecureRandom; 039import java.security.Security; 040import java.security.UnrecoverableKeyException; 041import java.security.cert.CertificateException; 042import java.util.ArrayList; 043import java.util.Collection; 044import java.util.Iterator; 045import java.util.LinkedHashSet; 046import java.util.LinkedList; 047import java.util.List; 048import java.util.Map; 049import java.util.Set; 050import java.util.concurrent.ArrayBlockingQueue; 051import java.util.concurrent.BlockingQueue; 052import java.util.concurrent.ConcurrentHashMap; 053import java.util.concurrent.ConcurrentLinkedQueue; 054import java.util.concurrent.TimeUnit; 055import java.util.concurrent.atomic.AtomicBoolean; 056import java.util.logging.Level; 057import java.util.logging.Logger; 058 059import javax.net.SocketFactory; 060import javax.net.ssl.HostnameVerifier; 061import javax.net.ssl.KeyManager; 062import javax.net.ssl.KeyManagerFactory; 063import javax.net.ssl.SSLContext; 064import javax.net.ssl.SSLSession; 065import javax.net.ssl.SSLSocket; 066import javax.net.ssl.TrustManager; 067import javax.net.ssl.X509TrustManager; 068import javax.security.auth.callback.Callback; 069import javax.security.auth.callback.CallbackHandler; 070import javax.security.auth.callback.PasswordCallback; 071 072import org.jivesoftware.smack.AbstractConnectionListener; 073import org.jivesoftware.smack.AbstractXMPPConnection; 074import org.jivesoftware.smack.ConnectionConfiguration; 075import org.jivesoftware.smack.ConnectionConfiguration.DnssecMode; 076import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode; 077import org.jivesoftware.smack.SmackConfiguration; 078import org.jivesoftware.smack.SmackException; 079import org.jivesoftware.smack.SmackException.AlreadyConnectedException; 080import org.jivesoftware.smack.SmackException.AlreadyLoggedInException; 081import org.jivesoftware.smack.SmackException.ConnectionException; 082import org.jivesoftware.smack.SmackException.NoResponseException; 083import org.jivesoftware.smack.SmackException.NotConnectedException; 084import org.jivesoftware.smack.SmackException.NotLoggedInException; 085import org.jivesoftware.smack.SmackException.SecurityRequiredByServerException; 086import org.jivesoftware.smack.StanzaListener; 087import org.jivesoftware.smack.SynchronizationPoint; 088import org.jivesoftware.smack.XMPPConnection; 089import org.jivesoftware.smack.XMPPException; 090import org.jivesoftware.smack.XMPPException.FailedNonzaException; 091import org.jivesoftware.smack.XMPPException.StreamErrorException; 092import org.jivesoftware.smack.compress.packet.Compress; 093import org.jivesoftware.smack.compress.packet.Compressed; 094import org.jivesoftware.smack.compression.XMPPInputOutputStream; 095import org.jivesoftware.smack.filter.StanzaFilter; 096import org.jivesoftware.smack.packet.Element; 097import org.jivesoftware.smack.packet.IQ; 098import org.jivesoftware.smack.packet.Message; 099import org.jivesoftware.smack.packet.Nonza; 100import org.jivesoftware.smack.packet.Presence; 101import org.jivesoftware.smack.packet.Stanza; 102import org.jivesoftware.smack.packet.StartTls; 103import org.jivesoftware.smack.packet.StreamError; 104import org.jivesoftware.smack.packet.StreamOpen; 105import org.jivesoftware.smack.proxy.ProxyInfo; 106import org.jivesoftware.smack.sasl.packet.SaslStreamElements; 107import org.jivesoftware.smack.sasl.packet.SaslStreamElements.Challenge; 108import org.jivesoftware.smack.sasl.packet.SaslStreamElements.SASLFailure; 109import org.jivesoftware.smack.sasl.packet.SaslStreamElements.Success; 110import org.jivesoftware.smack.sm.SMUtils; 111import org.jivesoftware.smack.sm.StreamManagementException; 112import org.jivesoftware.smack.sm.StreamManagementException.StreamIdDoesNotMatchException; 113import org.jivesoftware.smack.sm.StreamManagementException.StreamManagementCounterError; 114import org.jivesoftware.smack.sm.StreamManagementException.StreamManagementNotEnabledException; 115import org.jivesoftware.smack.sm.packet.StreamManagement; 116import org.jivesoftware.smack.sm.packet.StreamManagement.AckAnswer; 117import org.jivesoftware.smack.sm.packet.StreamManagement.AckRequest; 118import org.jivesoftware.smack.sm.packet.StreamManagement.Enable; 119import org.jivesoftware.smack.sm.packet.StreamManagement.Enabled; 120import org.jivesoftware.smack.sm.packet.StreamManagement.Failed; 121import org.jivesoftware.smack.sm.packet.StreamManagement.Resume; 122import org.jivesoftware.smack.sm.packet.StreamManagement.Resumed; 123import org.jivesoftware.smack.sm.packet.StreamManagement.StreamManagementFeature; 124import org.jivesoftware.smack.sm.predicates.Predicate; 125import org.jivesoftware.smack.sm.provider.ParseStreamManagement; 126import org.jivesoftware.smack.util.ArrayBlockingQueueWithShutdown; 127import org.jivesoftware.smack.util.Async; 128import org.jivesoftware.smack.util.DNSUtil; 129import org.jivesoftware.smack.util.PacketParserUtils; 130import org.jivesoftware.smack.util.StringUtils; 131import org.jivesoftware.smack.util.TLSUtils; 132import org.jivesoftware.smack.util.XmlStringBuilder; 133import org.jivesoftware.smack.util.dns.HostAddress; 134import org.jivesoftware.smack.util.dns.SmackDaneProvider; 135import org.jivesoftware.smack.util.dns.SmackDaneVerifier; 136 137import org.jxmpp.jid.impl.JidCreate; 138import org.jxmpp.jid.parts.Resourcepart; 139import org.jxmpp.stringprep.XmppStringprepException; 140import org.jxmpp.util.XmppStringUtils; 141import org.xmlpull.v1.XmlPullParser; 142import org.xmlpull.v1.XmlPullParserException; 143 144/** 145 * Creates a socket connection to an XMPP server. This is the default connection 146 * to an XMPP server and is specified in the XMPP Core (RFC 6120). 147 * 148 * @see XMPPConnection 149 * @author Matt Tucker 150 */ 151public class XMPPTCPConnection extends AbstractXMPPConnection { 152 153 private static final int QUEUE_SIZE = 500; 154 private static final Logger LOGGER = Logger.getLogger(XMPPTCPConnection.class.getName()); 155 156 /** 157 * The socket which is used for this connection. 158 */ 159 private Socket socket; 160 161 /** 162 * 163 */ 164 private boolean disconnectedButResumeable = false; 165 166 private SSLSocket secureSocket; 167 168 /** 169 * Protected access level because of unit test purposes 170 */ 171 protected PacketWriter packetWriter; 172 173 /** 174 * Protected access level because of unit test purposes 175 */ 176 protected PacketReader packetReader; 177 178 private final SynchronizationPoint<Exception> initalOpenStreamSend = new SynchronizationPoint<>( 179 this, "initial open stream element send to server"); 180 181 /** 182 * 183 */ 184 private final SynchronizationPoint<XMPPException> maybeCompressFeaturesReceived = new SynchronizationPoint<XMPPException>( 185 this, "stream compression feature"); 186 187 /** 188 * 189 */ 190 private final SynchronizationPoint<SmackException> compressSyncPoint = new SynchronizationPoint<>( 191 this, "stream compression"); 192 193 /** 194 * A synchronization point which is successful if this connection has received the closing 195 * stream element from the remote end-point, i.e. the server. 196 */ 197 private final SynchronizationPoint<Exception> closingStreamReceived = new SynchronizationPoint<>( 198 this, "stream closing element received"); 199 200 /** 201 * The default bundle and defer callback, used for new connections. 202 * @see bundleAndDeferCallback 203 */ 204 private static BundleAndDeferCallback defaultBundleAndDeferCallback; 205 206 /** 207 * The used bundle and defer callback. 208 * <p> 209 * Although this field may be set concurrently, the 'volatile' keyword was deliberately not added, in order to avoid 210 * having a 'volatile' read within the writer threads loop. 211 * </p> 212 */ 213 private BundleAndDeferCallback bundleAndDeferCallback = defaultBundleAndDeferCallback; 214 215 private static boolean useSmDefault = true; 216 217 private static boolean useSmResumptionDefault = true; 218 219 /** 220 * The stream ID of the stream that is currently resumable, ie. the stream we hold the state 221 * for in {@link #clientHandledStanzasCount}, {@link #serverHandledStanzasCount} and 222 * {@link #unacknowledgedStanzas}. 223 */ 224 private String smSessionId; 225 226 private final SynchronizationPoint<FailedNonzaException> smResumedSyncPoint = new SynchronizationPoint<>( 227 this, "stream resumed element"); 228 229 private final SynchronizationPoint<SmackException> smEnabledSyncPoint = new SynchronizationPoint<>( 230 this, "stream enabled element"); 231 232 /** 233 * The client's preferred maximum resumption time in seconds. 234 */ 235 private int smClientMaxResumptionTime = -1; 236 237 /** 238 * The server's preferred maximum resumption time in seconds. 239 */ 240 private int smServerMaxResumptimTime = -1; 241 242 /** 243 * Indicates whether Stream Management (XEP-198) should be used if it's supported by the server. 244 */ 245 private boolean useSm = useSmDefault; 246 private boolean useSmResumption = useSmResumptionDefault; 247 248 /** 249 * The counter that the server sends the client about it's current height. For example, if the server sends 250 * {@code <a h='42'/>}, then this will be set to 42 (while also handling the {@link #unacknowledgedStanzas} queue). 251 */ 252 private long serverHandledStanzasCount = 0; 253 254 /** 255 * The counter for stanzas handled ("received") by the client. 256 * <p> 257 * Note that we don't need to synchronize this counter. Although JLS 17.7 states that reads and writes to longs are 258 * not atomic, it guarantees that there are at most 2 separate writes, one to each 32-bit half. And since 259 * {@link SMUtils#incrementHeight(long)} masks the lower 32 bit, we only operate on one half of the long and 260 * therefore have no concurrency problem because the read/write operations on one half are guaranteed to be atomic. 261 * </p> 262 */ 263 private long clientHandledStanzasCount = 0; 264 265 private BlockingQueue<Stanza> unacknowledgedStanzas; 266 267 /** 268 * Set to true if Stream Management was at least once enabled for this connection. 269 */ 270 private boolean smWasEnabledAtLeastOnce = false; 271 272 /** 273 * This listeners are invoked for every stanza that got acknowledged. 274 * <p> 275 * We use a {@link ConccurrentLinkedQueue} here in order to allow the listeners to remove 276 * themselves after they have been invoked. 277 * </p> 278 */ 279 private final Collection<StanzaListener> stanzaAcknowledgedListeners = new ConcurrentLinkedQueue<StanzaListener>(); 280 281 /** 282 * This listeners are invoked for a acknowledged stanza that has the given stanza ID. They will 283 * only be invoked once and automatically removed after that. 284 */ 285 private final Map<String, StanzaListener> stanzaIdAcknowledgedListeners = new ConcurrentHashMap<String, StanzaListener>(); 286 287 /** 288 * Predicates that determine if an stream management ack should be requested from the server. 289 * <p> 290 * We use a linked hash set here, so that the order how the predicates are added matches the 291 * order in which they are invoked in order to determine if an ack request should be send or not. 292 * </p> 293 */ 294 private final Set<StanzaFilter> requestAckPredicates = new LinkedHashSet<StanzaFilter>(); 295 296 private final XMPPTCPConnectionConfiguration config; 297 298 /** 299 * Creates a new XMPP connection over TCP (optionally using proxies). 300 * <p> 301 * Note that XMPPTCPConnection constructors do not establish a connection to the server 302 * and you must call {@link #connect()}. 303 * </p> 304 * 305 * @param config the connection configuration. 306 */ 307 public XMPPTCPConnection(XMPPTCPConnectionConfiguration config) { 308 super(config); 309 this.config = config; 310 addConnectionListener(new AbstractConnectionListener() { 311 @Override 312 public void connectionClosedOnError(Exception e) { 313 if (e instanceof XMPPException.StreamErrorException || e instanceof StreamManagementException) { 314 dropSmState(); 315 } 316 } 317 }); 318 } 319 320 /** 321 * Creates a new XMPP connection over TCP. 322 * <p> 323 * Note that {@code jid} must be the bare JID, e.g. "user@example.org". More fine-grained control over the 324 * connection settings is available using the {@link #XMPPTCPConnection(XMPPTCPConnectionConfiguration)} 325 * constructor. 326 * </p> 327 * 328 * @param jid the bare JID used by the client. 329 * @param password the password or authentication token. 330 * @throws XmppStringprepException 331 */ 332 public XMPPTCPConnection(CharSequence jid, String password) throws XmppStringprepException { 333 this(XmppStringUtils.parseLocalpart(jid.toString()), password, XmppStringUtils.parseDomain(jid.toString())); 334 } 335 336 /** 337 * Creates a new XMPP connection over TCP. 338 * <p> 339 * This is the simplest constructor for connecting to an XMPP server. Alternatively, 340 * you can get fine-grained control over connection settings using the 341 * {@link #XMPPTCPConnection(XMPPTCPConnectionConfiguration)} constructor. 342 * </p> 343 * @param username 344 * @param password 345 * @param serviceName 346 * @throws XmppStringprepException 347 */ 348 public XMPPTCPConnection(CharSequence username, String password, String serviceName) throws XmppStringprepException { 349 this(XMPPTCPConnectionConfiguration.builder().setUsernameAndPassword(username, password).setXmppDomain( 350 JidCreate.domainBareFrom(serviceName)).build()); 351 } 352 353 @Override 354 protected void throwNotConnectedExceptionIfAppropriate() throws NotConnectedException { 355 if (packetWriter == null) { 356 throw new NotConnectedException(); 357 } 358 packetWriter.throwNotConnectedExceptionIfDoneAndResumptionNotPossible(); 359 } 360 361 @Override 362 protected void throwAlreadyConnectedExceptionIfAppropriate() throws AlreadyConnectedException { 363 if (isConnected() && !disconnectedButResumeable) { 364 throw new AlreadyConnectedException(); 365 } 366 } 367 368 @Override 369 protected void throwAlreadyLoggedInExceptionIfAppropriate() throws AlreadyLoggedInException { 370 if (isAuthenticated() && !disconnectedButResumeable) { 371 throw new AlreadyLoggedInException(); 372 } 373 } 374 375 @Override 376 protected void afterSuccessfulLogin(final boolean resumed) throws NotConnectedException, InterruptedException { 377 // Reset the flag in case it was set 378 disconnectedButResumeable = false; 379 super.afterSuccessfulLogin(resumed); 380 } 381 382 @Override 383 protected synchronized void loginInternal(String username, String password, Resourcepart resource) throws XMPPException, 384 SmackException, IOException, InterruptedException { 385 // Authenticate using SASL 386 SSLSession sslSession = secureSocket != null ? secureSocket.getSession() : null; 387 saslAuthentication.authenticate(username, password, config.getAuthzid(), sslSession); 388 389 // If compression is enabled then request the server to use stream compression. XEP-170 390 // recommends to perform stream compression before resource binding. 391 maybeEnableCompression(); 392 393 if (isSmResumptionPossible()) { 394 smResumedSyncPoint.sendAndWaitForResponse(new Resume(clientHandledStanzasCount, smSessionId)); 395 if (smResumedSyncPoint.wasSuccessful()) { 396 // We successfully resumed the stream, be done here 397 afterSuccessfulLogin(true); 398 return; 399 } 400 // SM resumption failed, what Smack does here is to report success of 401 // lastFeaturesReceived in case of sm resumption was answered with 'failed' so that 402 // normal resource binding can be tried. 403 LOGGER.fine("Stream resumption failed, continuing with normal stream establishment process"); 404 } 405 406 List<Stanza> previouslyUnackedStanzas = new LinkedList<Stanza>(); 407 if (unacknowledgedStanzas != null) { 408 // There was a previous connection with SM enabled but that was either not resumable or 409 // failed to resume. Make sure that we (re-)send the unacknowledged stanzas. 410 unacknowledgedStanzas.drainTo(previouslyUnackedStanzas); 411 // Reset unacknowledged stanzas to 'null' to signal that we never send 'enable' in this 412 // XMPP session (There maybe was an enabled in a previous XMPP session of this 413 // connection instance though). This is used in writePackets to decide if stanzas should 414 // be added to the unacknowledged stanzas queue, because they have to be added right 415 // after the 'enable' stream element has been sent. 416 dropSmState(); 417 } 418 419 // Now bind the resource. It is important to do this *after* we dropped an eventually 420 // existing Stream Management state. As otherwise <bind/> and <session/> may end up in 421 // unacknowledgedStanzas and become duplicated on reconnect. See SMACK-706. 422 bindResourceAndEstablishSession(resource); 423 424 if (isSmAvailable() && useSm) { 425 // Remove what is maybe left from previously stream managed sessions 426 serverHandledStanzasCount = 0; 427 // XEP-198 3. Enabling Stream Management. If the server response to 'Enable' is 'Failed' 428 // then this is a non recoverable error and we therefore throw an exception. 429 smEnabledSyncPoint.sendAndWaitForResponseOrThrow(new Enable(useSmResumption, smClientMaxResumptionTime)); 430 synchronized (requestAckPredicates) { 431 if (requestAckPredicates.isEmpty()) { 432 // Assure that we have at lest one predicate set up that so that we request acks 433 // for the server and eventually flush some stanzas from the unacknowledged 434 // stanza queue 435 requestAckPredicates.add(Predicate.forMessagesOrAfter5Stanzas()); 436 } 437 } 438 } 439 // (Re-)send the stanzas *after* we tried to enable SM 440 for (Stanza stanza : previouslyUnackedStanzas) { 441 sendStanzaInternal(stanza); 442 } 443 444 afterSuccessfulLogin(false); 445 } 446 447 @Override 448 public boolean isSecureConnection() { 449 return secureSocket != null; 450 } 451 452 /** 453 * Shuts the current connection down. After this method returns, the connection must be ready 454 * for re-use by connect. 455 */ 456 @Override 457 protected void shutdown() { 458 if (isSmEnabled()) { 459 try { 460 // Try to send a last SM Acknowledgement. Most servers won't find this information helpful, as the SM 461 // state is dropped after a clean disconnect anyways. OTOH it doesn't hurt much either. 462 sendSmAcknowledgementInternal(); 463 } catch (InterruptedException | NotConnectedException e) { 464 LOGGER.log(Level.FINE, "Can not send final SM ack as connection is not connected", e); 465 } 466 } 467 shutdown(false); 468 } 469 470 /** 471 * Performs an unclean disconnect and shutdown of the connection. Does not send a closing stream stanza. 472 */ 473 public synchronized void instantShutdown() { 474 shutdown(true); 475 } 476 477 private void shutdown(boolean instant) { 478 if (disconnectedButResumeable) { 479 return; 480 } 481 482 // First shutdown the writer, this will result in a closing stream element getting send to 483 // the server 484 if (packetWriter != null) { 485 LOGGER.finer("PacketWriter shutdown()"); 486 packetWriter.shutdown(instant); 487 } 488 LOGGER.finer("PacketWriter has been shut down"); 489 490 if (!instant) { 491 try { 492 // After we send the closing stream element, check if there was already a 493 // closing stream element sent by the server or wait with a timeout for a 494 // closing stream element to be received from the server. 495 @SuppressWarnings("unused") 496 Exception res = closingStreamReceived.checkIfSuccessOrWait(); 497 } catch (InterruptedException | NoResponseException e) { 498 LOGGER.log(Level.INFO, "Exception while waiting for closing stream element from the server " + this, e); 499 } 500 } 501 502 if (packetReader != null) { 503 LOGGER.finer("PacketReader shutdown()"); 504 packetReader.shutdown(); 505 } 506 LOGGER.finer("PacketReader has been shut down"); 507 508 try { 509 socket.close(); 510 } catch (Exception e) { 511 LOGGER.log(Level.WARNING, "shutdown", e); 512 } 513 514 setWasAuthenticated(); 515 // If we are able to resume the stream, then don't set 516 // connected/authenticated/usingTLS to false since we like behave like we are still 517 // connected (e.g. sendStanza should not throw a NotConnectedException). 518 if (isSmResumptionPossible() && instant) { 519 disconnectedButResumeable = true; 520 } else { 521 disconnectedButResumeable = false; 522 // Reset the stream management session id to null, since if the stream is cleanly closed, i.e. sending a closing 523 // stream tag, there is no longer a stream to resume. 524 smSessionId = null; 525 } 526 authenticated = false; 527 connected = false; 528 secureSocket = null; 529 reader = null; 530 writer = null; 531 532 maybeCompressFeaturesReceived.init(); 533 compressSyncPoint.init(); 534 smResumedSyncPoint.init(); 535 smEnabledSyncPoint.init(); 536 initalOpenStreamSend.init(); 537 } 538 539 @Override 540 public void sendNonza(Nonza element) throws NotConnectedException, InterruptedException { 541 packetWriter.sendStreamElement(element); 542 } 543 544 @Override 545 protected void sendStanzaInternal(Stanza packet) throws NotConnectedException, InterruptedException { 546 packetWriter.sendStreamElement(packet); 547 if (isSmEnabled()) { 548 for (StanzaFilter requestAckPredicate : requestAckPredicates) { 549 if (requestAckPredicate.accept(packet)) { 550 requestSmAcknowledgementInternal(); 551 break; 552 } 553 } 554 } 555 } 556 557 private void connectUsingConfiguration() throws ConnectionException, IOException { 558 List<HostAddress> failedAddresses = populateHostAddresses(); 559 SocketFactory socketFactory = config.getSocketFactory(); 560 ProxyInfo proxyInfo = config.getProxyInfo(); 561 int timeout = config.getConnectTimeout(); 562 if (socketFactory == null) { 563 socketFactory = SocketFactory.getDefault(); 564 } 565 for (HostAddress hostAddress : hostAddresses) { 566 Iterator<InetAddress> inetAddresses = null; 567 String host = hostAddress.getHost(); 568 int port = hostAddress.getPort(); 569 if (proxyInfo == null) { 570 inetAddresses = hostAddress.getInetAddresses().iterator(); 571 assert (inetAddresses.hasNext()); 572 573 innerloop: while (inetAddresses.hasNext()) { 574 // Create a *new* Socket before every connection attempt, i.e. connect() call, since Sockets are not 575 // re-usable after a failed connection attempt. See also SMACK-724. 576 socket = socketFactory.createSocket(); 577 578 final InetAddress inetAddress = inetAddresses.next(); 579 final String inetAddressAndPort = inetAddress + " at port " + port; 580 LOGGER.finer("Trying to establish TCP connection to " + inetAddressAndPort); 581 try { 582 socket.connect(new InetSocketAddress(inetAddress, port), timeout); 583 } catch (Exception e) { 584 hostAddress.setException(inetAddress, e); 585 if (inetAddresses.hasNext()) { 586 continue innerloop; 587 } else { 588 break innerloop; 589 } 590 } 591 LOGGER.finer("Established TCP connection to " + inetAddressAndPort); 592 // We found a host to connect to, return here 593 this.host = host; 594 this.port = port; 595 return; 596 } 597 failedAddresses.add(hostAddress); 598 } else { 599 socket = socketFactory.createSocket(); 600 StringUtils.requireNotNullOrEmpty(host, "Host of HostAddress " + hostAddress + " must not be null when using a Proxy"); 601 final String hostAndPort = host + " at port " + port; 602 LOGGER.finer("Trying to establish TCP connection via Proxy to " + hostAndPort); 603 try { 604 proxyInfo.getProxySocketConnection().connect(socket, host, port, timeout); 605 } catch (IOException e) { 606 hostAddress.setException(e); 607 continue; 608 } 609 LOGGER.finer("Established TCP connection to " + hostAndPort); 610 // We found a host to connect to, return here 611 this.host = host; 612 this.port = port; 613 return; 614 } 615 } 616 // There are no more host addresses to try 617 // throw an exception and report all tried 618 // HostAddresses in the exception 619 throw ConnectionException.from(failedAddresses); 620 } 621 622 /** 623 * Initializes the connection by creating a stanza(/packet) reader and writer and opening a 624 * XMPP stream to the server. 625 * 626 * @throws XMPPException if establishing a connection to the server fails. 627 * @throws SmackException if the server failes to respond back or if there is anther error. 628 * @throws IOException 629 */ 630 private void initConnection() throws IOException { 631 boolean isFirstInitialization = packetReader == null || packetWriter == null; 632 compressionHandler = null; 633 634 // Set the reader and writer instance variables 635 initReaderAndWriter(); 636 637 if (isFirstInitialization) { 638 packetWriter = new PacketWriter(); 639 packetReader = new PacketReader(); 640 641 // If debugging is enabled, we should start the thread that will listen for 642 // all packets and then log them. 643 if (config.isDebuggerEnabled()) { 644 addAsyncStanzaListener(debugger.getReaderListener(), null); 645 if (debugger.getWriterListener() != null) { 646 addPacketSendingListener(debugger.getWriterListener(), null); 647 } 648 } 649 } 650 // Start the packet writer. This will open an XMPP stream to the server 651 packetWriter.init(); 652 // Start the packet reader. The startup() method will block until we 653 // get an opening stream packet back from server 654 packetReader.init(); 655 } 656 657 private void initReaderAndWriter() throws IOException { 658 InputStream is = socket.getInputStream(); 659 OutputStream os = socket.getOutputStream(); 660 if (compressionHandler != null) { 661 is = compressionHandler.getInputStream(is); 662 os = compressionHandler.getOutputStream(os); 663 } 664 // OutputStreamWriter is already buffered, no need to wrap it into a BufferedWriter 665 writer = new OutputStreamWriter(os, "UTF-8"); 666 reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); 667 668 // If debugging is enabled, we open a window and write out all network traffic. 669 initDebugger(); 670 } 671 672 /** 673 * The server has indicated that TLS negotiation can start. We now need to secure the 674 * existing plain connection and perform a handshake. This method won't return until the 675 * connection has finished the handshake or an error occurred while securing the connection. 676 * @throws IOException 677 * @throws CertificateException 678 * @throws NoSuchAlgorithmException 679 * @throws NoSuchProviderException 680 * @throws KeyStoreException 681 * @throws UnrecoverableKeyException 682 * @throws KeyManagementException 683 * @throws SmackException 684 * @throws Exception if an exception occurs. 685 */ 686 private void proceedTLSReceived() throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException, NoSuchProviderException, UnrecoverableKeyException, KeyManagementException, SmackException { 687 SmackDaneVerifier daneVerifier = null; 688 689 if (config.getDnssecMode() == DnssecMode.needsDnssecAndDane) { 690 SmackDaneProvider daneProvider = DNSUtil.getDaneProvider(); 691 if (daneProvider == null) { 692 throw new UnsupportedOperationException("DANE enabled but no SmackDaneProvider configured"); 693 } 694 daneVerifier = daneProvider.newInstance(); 695 if (daneVerifier == null) { 696 throw new IllegalStateException("DANE requested but DANE provider did not return a DANE verifier"); 697 } 698 } 699 700 SSLContext context = this.config.getCustomSSLContext(); 701 KeyStore ks = null; 702 PasswordCallback pcb = null; 703 704 if (context == null) { 705 final String keyStoreType = config.getKeystoreType(); 706 final CallbackHandler callbackHandler = config.getCallbackHandler(); 707 final String keystorePath = config.getKeystorePath(); 708 if ("PKCS11".equals(keyStoreType)) { 709 try { 710 Constructor<?> c = Class.forName("sun.security.pkcs11.SunPKCS11").getConstructor(InputStream.class); 711 String pkcs11Config = "name = SmartCard\nlibrary = " + config.getPKCS11Library(); 712 ByteArrayInputStream config = new ByteArrayInputStream(pkcs11Config.getBytes(StringUtils.UTF8)); 713 Provider p = (Provider) c.newInstance(config); 714 Security.addProvider(p); 715 ks = KeyStore.getInstance("PKCS11",p); 716 pcb = new PasswordCallback("PKCS11 Password: ",false); 717 callbackHandler.handle(new Callback[] {pcb}); 718 ks.load(null,pcb.getPassword()); 719 } 720 catch (Exception e) { 721 LOGGER.log(Level.WARNING, "Exception", e); 722 ks = null; 723 } 724 } 725 else if ("Apple".equals(keyStoreType)) { 726 ks = KeyStore.getInstance("KeychainStore","Apple"); 727 ks.load(null,null); 728 // pcb = new PasswordCallback("Apple Keychain",false); 729 // pcb.setPassword(null); 730 } 731 else if (keyStoreType != null) { 732 ks = KeyStore.getInstance(keyStoreType); 733 if (callbackHandler != null && StringUtils.isNotEmpty(keystorePath)) { 734 try { 735 pcb = new PasswordCallback("Keystore Password: ", false); 736 callbackHandler.handle(new Callback[] { pcb }); 737 ks.load(new FileInputStream(keystorePath), pcb.getPassword()); 738 } 739 catch (Exception e) { 740 LOGGER.log(Level.WARNING, "Exception", e); 741 ks = null; 742 } 743 } else { 744 ks.load(null, null); 745 } 746 } 747 748 KeyManager[] kms = null; 749 750 if (ks != null) { 751 String keyManagerFactoryAlgorithm = KeyManagerFactory.getDefaultAlgorithm(); 752 KeyManagerFactory kmf = null; 753 try { 754 kmf = KeyManagerFactory.getInstance(keyManagerFactoryAlgorithm); 755 } 756 catch (NoSuchAlgorithmException e) { 757 LOGGER.log(Level.FINE, "Could get the default KeyManagerFactory for the '" 758 + keyManagerFactoryAlgorithm + "' algorithm", e); 759 } 760 if (kmf != null) { 761 try { 762 if (pcb == null) { 763 kmf.init(ks, null); 764 } 765 else { 766 kmf.init(ks, pcb.getPassword()); 767 pcb.clearPassword(); 768 } 769 kms = kmf.getKeyManagers(); 770 } 771 catch (NullPointerException npe) { 772 LOGGER.log(Level.WARNING, "NullPointerException", npe); 773 } 774 } 775 } 776 777 // If the user didn't specify a SSLContext, use the default one 778 context = SSLContext.getInstance("TLS"); 779 780 final SecureRandom secureRandom = new java.security.SecureRandom(); 781 X509TrustManager customTrustManager = config.getCustomX509TrustManager(); 782 783 if (daneVerifier != null) { 784 // User requested DANE verification. 785 daneVerifier.init(context, kms, customTrustManager, secureRandom); 786 } else { 787 TrustManager[] customTrustManagers = null; 788 if (customTrustManager != null) { 789 customTrustManagers = new TrustManager[] { customTrustManager }; 790 } 791 context.init(kms, customTrustManagers, secureRandom); 792 } 793 } 794 795 Socket plain = socket; 796 // Secure the plain connection 797 socket = context.getSocketFactory().createSocket(plain, 798 host, plain.getPort(), true); 799 800 final SSLSocket sslSocket = (SSLSocket) socket; 801 // Immediately set the enabled SSL protocols and ciphers. See SMACK-712 why this is 802 // important (at least on certain platforms) and it seems to be a good idea anyways to 803 // prevent an accidental implicit handshake. 804 TLSUtils.setEnabledProtocolsAndCiphers(sslSocket, config.getEnabledSSLProtocols(), config.getEnabledSSLCiphers()); 805 806 // Initialize the reader and writer with the new secured version 807 initReaderAndWriter(); 808 809 // Proceed to do the handshake 810 sslSocket.startHandshake(); 811 812 if (daneVerifier != null) { 813 daneVerifier.finish(sslSocket); 814 } 815 816 final HostnameVerifier verifier = getConfiguration().getHostnameVerifier(); 817 if (verifier == null) { 818 throw new IllegalStateException("No HostnameVerifier set. Use connectionConfiguration.setHostnameVerifier() to configure."); 819 } else if (!verifier.verify(getXMPPServiceDomain().toString(), sslSocket.getSession())) { 820 throw new CertificateException("Hostname verification of certificate failed. Certificate does not authenticate " + getXMPPServiceDomain()); 821 } 822 823 // Set that TLS was successful 824 secureSocket = sslSocket; 825 } 826 827 /** 828 * Returns the compression handler that can be used for one compression methods offered by the server. 829 * 830 * @return a instance of XMPPInputOutputStream or null if no suitable instance was found 831 * 832 */ 833 private static XMPPInputOutputStream maybeGetCompressionHandler(Compress.Feature compression) { 834 for (XMPPInputOutputStream handler : SmackConfiguration.getCompresionHandlers()) { 835 String method = handler.getCompressionMethod(); 836 if (compression.getMethods().contains(method)) 837 return handler; 838 } 839 return null; 840 } 841 842 @Override 843 public boolean isUsingCompression() { 844 return compressionHandler != null && compressSyncPoint.wasSuccessful(); 845 } 846 847 /** 848 * <p> 849 * Starts using stream compression that will compress network traffic. Traffic can be 850 * reduced up to 90%. Therefore, stream compression is ideal when using a slow speed network 851 * connection. However, the server and the client will need to use more CPU time in order to 852 * un/compress network data so under high load the server performance might be affected. 853 * </p> 854 * <p> 855 * Stream compression has to have been previously offered by the server. Currently only the 856 * zlib method is supported by the client. Stream compression negotiation has to be done 857 * before authentication took place. 858 * </p> 859 * 860 * @throws NotConnectedException 861 * @throws SmackException 862 * @throws NoResponseException 863 * @throws InterruptedException 864 */ 865 private void maybeEnableCompression() throws NotConnectedException, NoResponseException, SmackException, InterruptedException { 866 if (!config.isCompressionEnabled()) { 867 return; 868 } 869 maybeCompressFeaturesReceived.checkIfSuccessOrWait(); 870 Compress.Feature compression = getFeature(Compress.Feature.ELEMENT, Compress.NAMESPACE); 871 if (compression == null) { 872 // Server does not support compression 873 return; 874 } 875 // If stream compression was offered by the server and we want to use 876 // compression then send compression request to the server 877 if ((compressionHandler = maybeGetCompressionHandler(compression)) != null) { 878 compressSyncPoint.sendAndWaitForResponseOrThrow(new Compress(compressionHandler.getCompressionMethod())); 879 } else { 880 LOGGER.warning("Could not enable compression because no matching handler/method pair was found"); 881 } 882 } 883 884 /** 885 * Establishes a connection to the XMPP server. It basically 886 * creates and maintains a socket connection to the server. 887 * <p> 888 * Listeners will be preserved from a previous connection if the reconnection 889 * occurs after an abrupt termination. 890 * </p> 891 * 892 * @throws XMPPException if an error occurs while trying to establish the connection. 893 * @throws SmackException 894 * @throws IOException 895 * @throws InterruptedException 896 */ 897 @Override 898 protected void connectInternal() throws SmackException, IOException, XMPPException, InterruptedException { 899 closingStreamReceived.init(); 900 // Establishes the TCP connection to the server and does setup the reader and writer. Throws an exception if 901 // there is an error establishing the connection 902 connectUsingConfiguration(); 903 904 // We connected successfully to the servers TCP port 905 initConnection(); 906 } 907 908 /** 909 * Sends out a notification that there was an error with the connection 910 * and closes the connection. Also prints the stack trace of the given exception 911 * 912 * @param e the exception that causes the connection close event. 913 */ 914 private synchronized void notifyConnectionError(Exception e) { 915 // Listeners were already notified of the exception, return right here. 916 if ((packetReader == null || packetReader.done) && 917 (packetWriter == null || packetWriter.done())) return; 918 919 // Closes the connection temporary. A reconnection is possible 920 // Note that a connection listener of XMPPTCPConnection will drop the SM state in 921 // case the Exception is a StreamErrorException. 922 instantShutdown(); 923 924 // Notify connection listeners of the error. 925 callConnectionClosedOnErrorListener(e); 926 } 927 928 /** 929 * For unit testing purposes 930 * 931 * @param writer 932 */ 933 protected void setWriter(Writer writer) { 934 this.writer = writer; 935 } 936 937 @Override 938 protected void afterFeaturesReceived() throws NotConnectedException, InterruptedException { 939 StartTls startTlsFeature = getFeature(StartTls.ELEMENT, StartTls.NAMESPACE); 940 if (startTlsFeature != null) { 941 if (startTlsFeature.required() && config.getSecurityMode() == SecurityMode.disabled) { 942 SmackException smackException = new SecurityRequiredByServerException(); 943 tlsHandled.reportFailure(smackException); 944 notifyConnectionError(smackException); 945 return; 946 } 947 948 if (config.getSecurityMode() != ConnectionConfiguration.SecurityMode.disabled) { 949 sendNonza(new StartTls()); 950 } else { 951 tlsHandled.reportSuccess(); 952 } 953 } else { 954 tlsHandled.reportSuccess(); 955 } 956 957 if (getSASLAuthentication().authenticationSuccessful()) { 958 // If we have received features after the SASL has been successfully completed, then we 959 // have also *maybe* received, as it is an optional feature, the compression feature 960 // from the server. 961 maybeCompressFeaturesReceived.reportSuccess(); 962 } 963 } 964 965 /** 966 * Resets the parser using the latest connection's reader. Reseting the parser is necessary 967 * when the plain connection has been secured or when a new opening stream element is going 968 * to be sent by the server. 969 * 970 * @throws SmackException if the parser could not be reset. 971 * @throws InterruptedException 972 */ 973 void openStream() throws SmackException, InterruptedException { 974 // If possible, provide the receiving entity of the stream open tag, i.e. the server, as much information as 975 // possible. The 'to' attribute is *always* available. The 'from' attribute if set by the user and no external 976 // mechanism is used to determine the local entity (user). And the 'id' attribute is available after the first 977 // response from the server (see e.g. RFC 6120 ยง 9.1.1 Step 2.) 978 CharSequence to = getXMPPServiceDomain(); 979 CharSequence from = null; 980 CharSequence localpart = config.getUsername(); 981 if (localpart != null) { 982 from = XmppStringUtils.completeJidFrom(localpart, to); 983 } 984 String id = getStreamId(); 985 sendNonza(new StreamOpen(to, from, id)); 986 try { 987 packetReader.parser = PacketParserUtils.newXmppParser(reader); 988 } 989 catch (XmlPullParserException e) { 990 throw new SmackException(e); 991 } 992 } 993 994 protected class PacketReader { 995 996 XmlPullParser parser; 997 998 private volatile boolean done; 999 1000 /** 1001 * Initializes the reader in order to be used. The reader is initialized during the 1002 * first connection and when reconnecting due to an abruptly disconnection. 1003 */ 1004 void init() { 1005 done = false; 1006 1007 Async.go(new Runnable() { 1008 @Override 1009 public void run() { 1010 parsePackets(); 1011 } 1012 }, "Smack Packet Reader (" + getConnectionCounter() + ")"); 1013 } 1014 1015 /** 1016 * Shuts the stanza(/packet) reader down. This method simply sets the 'done' flag to true. 1017 */ 1018 void shutdown() { 1019 done = true; 1020 } 1021 1022 /** 1023 * Parse top-level packets in order to process them further. 1024 * 1025 * @param thread the thread that is being used by the reader to parse incoming packets. 1026 */ 1027 private void parsePackets() { 1028 try { 1029 initalOpenStreamSend.checkIfSuccessOrWait(); 1030 int eventType = parser.getEventType(); 1031 while (!done) { 1032 switch (eventType) { 1033 case XmlPullParser.START_TAG: 1034 final String name = parser.getName(); 1035 switch (name) { 1036 case Message.ELEMENT: 1037 case IQ.IQ_ELEMENT: 1038 case Presence.ELEMENT: 1039 try { 1040 parseAndProcessStanza(parser); 1041 } finally { 1042 clientHandledStanzasCount = SMUtils.incrementHeight(clientHandledStanzasCount); 1043 } 1044 break; 1045 case "stream": 1046 // We found an opening stream. 1047 if ("jabber:client".equals(parser.getNamespace(null))) { 1048 streamId = parser.getAttributeValue("", "id"); 1049 String reportedServerDomain = parser.getAttributeValue("", "from"); 1050 assert (config.getXMPPServiceDomain().equals(reportedServerDomain)); 1051 } 1052 break; 1053 case "error": 1054 StreamError streamError = PacketParserUtils.parseStreamError(parser); 1055 saslFeatureReceived.reportFailure(new StreamErrorException(streamError)); 1056 // Mark the tlsHandled sync point as success, we will use the saslFeatureReceived sync 1057 // point to report the error, which is checked immediately after tlsHandled in 1058 // connectInternal(). 1059 tlsHandled.reportSuccess(); 1060 throw new StreamErrorException(streamError); 1061 case "features": 1062 parseFeatures(parser); 1063 break; 1064 case "proceed": 1065 try { 1066 // Secure the connection by negotiating TLS 1067 proceedTLSReceived(); 1068 // Send a new opening stream to the server 1069 openStream(); 1070 } 1071 catch (Exception e) { 1072 SmackException smackException = new SmackException(e); 1073 tlsHandled.reportFailure(smackException); 1074 throw e; 1075 } 1076 break; 1077 case "failure": 1078 String namespace = parser.getNamespace(null); 1079 switch (namespace) { 1080 case "urn:ietf:params:xml:ns:xmpp-tls": 1081 // TLS negotiation has failed. The server will close the connection 1082 // TODO Parse failure stanza 1083 throw new SmackException("TLS negotiation has failed"); 1084 case "http://jabber.org/protocol/compress": 1085 // Stream compression has been denied. This is a recoverable 1086 // situation. It is still possible to authenticate and 1087 // use the connection but using an uncompressed connection 1088 // TODO Parse failure stanza 1089 compressSyncPoint.reportFailure(new SmackException( 1090 "Could not establish compression")); 1091 break; 1092 case SaslStreamElements.NAMESPACE: 1093 // SASL authentication has failed. The server may close the connection 1094 // depending on the number of retries 1095 final SASLFailure failure = PacketParserUtils.parseSASLFailure(parser); 1096 getSASLAuthentication().authenticationFailed(failure); 1097 break; 1098 } 1099 break; 1100 case Challenge.ELEMENT: 1101 // The server is challenging the SASL authentication made by the client 1102 String challengeData = parser.nextText(); 1103 getSASLAuthentication().challengeReceived(challengeData); 1104 break; 1105 case Success.ELEMENT: 1106 Success success = new Success(parser.nextText()); 1107 // We now need to bind a resource for the connection 1108 // Open a new stream and wait for the response 1109 openStream(); 1110 // The SASL authentication with the server was successful. The next step 1111 // will be to bind the resource 1112 getSASLAuthentication().authenticated(success); 1113 break; 1114 case Compressed.ELEMENT: 1115 // Server confirmed that it's possible to use stream compression. Start 1116 // stream compression 1117 // Initialize the reader and writer with the new compressed version 1118 initReaderAndWriter(); 1119 // Send a new opening stream to the server 1120 openStream(); 1121 // Notify that compression is being used 1122 compressSyncPoint.reportSuccess(); 1123 break; 1124 case Enabled.ELEMENT: 1125 Enabled enabled = ParseStreamManagement.enabled(parser); 1126 if (enabled.isResumeSet()) { 1127 smSessionId = enabled.getId(); 1128 if (StringUtils.isNullOrEmpty(smSessionId)) { 1129 SmackException xmppException = new SmackException("Stream Management 'enabled' element with resume attribute but without session id received"); 1130 smEnabledSyncPoint.reportFailure(xmppException); 1131 throw xmppException; 1132 } 1133 smServerMaxResumptimTime = enabled.getMaxResumptionTime(); 1134 } else { 1135 // Mark this a non-resumable stream by setting smSessionId to null 1136 smSessionId = null; 1137 } 1138 clientHandledStanzasCount = 0; 1139 smWasEnabledAtLeastOnce = true; 1140 smEnabledSyncPoint.reportSuccess(); 1141 LOGGER.fine("Stream Management (XEP-198): succesfully enabled"); 1142 break; 1143 case Failed.ELEMENT: 1144 Failed failed = ParseStreamManagement.failed(parser); 1145 FailedNonzaException xmppException = new FailedNonzaException(failed, failed.getXMPPErrorCondition()); 1146 // If only XEP-198 would specify different failure elements for the SM 1147 // enable and SM resume failure case. But this is not the case, so we 1148 // need to determine if this is a 'Failed' response for either 'Enable' 1149 // or 'Resume'. 1150 if (smResumedSyncPoint.requestSent()) { 1151 smResumedSyncPoint.reportFailure(xmppException); 1152 } 1153 else { 1154 if (!smEnabledSyncPoint.requestSent()) { 1155 throw new IllegalStateException("Failed element received but SM was not previously enabled"); 1156 } 1157 smEnabledSyncPoint.reportFailure(new SmackException(xmppException)); 1158 // Report success for last lastFeaturesReceived so that in case a 1159 // failed resumption, we can continue with normal resource binding. 1160 // See text of XEP-198 5. below Example 11. 1161 lastFeaturesReceived.reportSuccess(); 1162 } 1163 break; 1164 case Resumed.ELEMENT: 1165 Resumed resumed = ParseStreamManagement.resumed(parser); 1166 if (!smSessionId.equals(resumed.getPrevId())) { 1167 throw new StreamIdDoesNotMatchException(smSessionId, resumed.getPrevId()); 1168 } 1169 // Mark SM as enabled 1170 smEnabledSyncPoint.reportSuccess(); 1171 // First, drop the stanzas already handled by the server 1172 processHandledCount(resumed.getHandledCount()); 1173 // Then re-send what is left in the unacknowledged queue 1174 List<Stanza> stanzasToResend = new ArrayList<>(unacknowledgedStanzas.size()); 1175 unacknowledgedStanzas.drainTo(stanzasToResend); 1176 for (Stanza stanza : stanzasToResend) { 1177 sendStanzaInternal(stanza); 1178 } 1179 // If there where stanzas resent, then request a SM ack for them. 1180 // Writer's sendStreamElement() won't do it automatically based on 1181 // predicates. 1182 if (!stanzasToResend.isEmpty()) { 1183 requestSmAcknowledgementInternal(); 1184 } 1185 // Mark SM resumption as successful 1186 smResumedSyncPoint.reportSuccess(); 1187 LOGGER.fine("Stream Management (XEP-198): Stream resumed"); 1188 break; 1189 case AckAnswer.ELEMENT: 1190 AckAnswer ackAnswer = ParseStreamManagement.ackAnswer(parser); 1191 processHandledCount(ackAnswer.getHandledCount()); 1192 break; 1193 case AckRequest.ELEMENT: 1194 ParseStreamManagement.ackRequest(parser); 1195 if (smEnabledSyncPoint.wasSuccessful()) { 1196 sendSmAcknowledgementInternal(); 1197 } else { 1198 LOGGER.warning("SM Ack Request received while SM is not enabled"); 1199 } 1200 break; 1201 default: 1202 LOGGER.warning("Unknown top level stream element: " + name); 1203 break; 1204 } 1205 break; 1206 case XmlPullParser.END_TAG: 1207 final String endTagName = parser.getName(); 1208 if ("stream".equals(endTagName)) { 1209 if (!parser.getNamespace().equals("http://etherx.jabber.org/streams")) { 1210 LOGGER.warning(XMPPTCPConnection.this + " </stream> but different namespace " + parser.getNamespace()); 1211 break; 1212 } 1213 1214 // Check if the queue was already shut down before reporting success on closing stream tag 1215 // received. This avoids a race if there is a disconnect(), followed by a connect(), which 1216 // did re-start the queue again, causing this writer to assume that the queue is not 1217 // shutdown, which results in a call to disconnect(). 1218 final boolean queueWasShutdown = packetWriter.queue.isShutdown(); 1219 closingStreamReceived.reportSuccess(); 1220 1221 if (queueWasShutdown) { 1222 // We received a closing stream element *after* we initiated the 1223 // termination of the session by sending a closing stream element to 1224 // the server first 1225 return; 1226 } else { 1227 // We received a closing stream element from the server without us 1228 // sending a closing stream element first. This means that the 1229 // server wants to terminate the session, therefore disconnect 1230 // the connection 1231 LOGGER.info(XMPPTCPConnection.this 1232 + " received closing </stream> element." 1233 + " Server wants to terminate the connection, calling disconnect()"); 1234 disconnect(); 1235 } 1236 } 1237 break; 1238 case XmlPullParser.END_DOCUMENT: 1239 // END_DOCUMENT only happens in an error case, as otherwise we would see a 1240 // closing stream element before. 1241 throw new SmackException( 1242 "Parser got END_DOCUMENT event. This could happen e.g. if the server closed the connection without sending a closing stream element"); 1243 } 1244 eventType = parser.next(); 1245 } 1246 } 1247 catch (Exception e) { 1248 closingStreamReceived.reportFailure(e); 1249 // The exception can be ignored if the the connection is 'done' 1250 // or if the it was caused because the socket got closed 1251 if (!(done || packetWriter.queue.isShutdown())) { 1252 // Close the connection and notify connection listeners of the 1253 // error. 1254 notifyConnectionError(e); 1255 } 1256 } 1257 } 1258 } 1259 1260 protected class PacketWriter { 1261 public static final int QUEUE_SIZE = XMPPTCPConnection.QUEUE_SIZE; 1262 1263 private final ArrayBlockingQueueWithShutdown<Element> queue = new ArrayBlockingQueueWithShutdown<Element>( 1264 QUEUE_SIZE, true); 1265 1266 /** 1267 * Needs to be protected for unit testing purposes. 1268 */ 1269 protected SynchronizationPoint<NoResponseException> shutdownDone = new SynchronizationPoint<NoResponseException>( 1270 XMPPTCPConnection.this, "shutdown completed"); 1271 1272 /** 1273 * If set, the stanza(/packet) writer is shut down 1274 */ 1275 protected volatile Long shutdownTimestamp = null; 1276 1277 private volatile boolean instantShutdown; 1278 1279 /** 1280 * True if some preconditions are given to start the bundle and defer mechanism. 1281 * <p> 1282 * This will likely get set to true right after the start of the writer thread, because 1283 * {@link #nextStreamElement()} will check if {@link queue} is empty, which is probably the case, and then set 1284 * this field to true. 1285 * </p> 1286 */ 1287 private boolean shouldBundleAndDefer; 1288 1289 /** 1290 * Initializes the writer in order to be used. It is called at the first connection and also 1291 * is invoked if the connection is disconnected by an error. 1292 */ 1293 void init() { 1294 shutdownDone.init(); 1295 shutdownTimestamp = null; 1296 1297 if (unacknowledgedStanzas != null) { 1298 // It's possible that there are new stanzas in the writer queue that 1299 // came in while we were disconnected but resumable, drain those into 1300 // the unacknowledged queue so that they get resent now 1301 drainWriterQueueToUnacknowledgedStanzas(); 1302 } 1303 1304 queue.start(); 1305 Async.go(new Runnable() { 1306 @Override 1307 public void run() { 1308 writePackets(); 1309 } 1310 }, "Smack Packet Writer (" + getConnectionCounter() + ")"); 1311 } 1312 1313 private boolean done() { 1314 return shutdownTimestamp != null; 1315 } 1316 1317 protected void throwNotConnectedExceptionIfDoneAndResumptionNotPossible() throws NotConnectedException { 1318 final boolean done = done(); 1319 if (done) { 1320 final boolean smResumptionPossbile = isSmResumptionPossible(); 1321 // Don't throw a NotConnectedException is there is an resumable stream available 1322 if (!smResumptionPossbile) { 1323 throw new NotConnectedException(XMPPTCPConnection.this, "done=" + done 1324 + " smResumptionPossible=" + smResumptionPossbile); 1325 } 1326 } 1327 } 1328 1329 /** 1330 * Sends the specified element to the server. 1331 * 1332 * @param element the element to send. 1333 * @throws NotConnectedException 1334 * @throws InterruptedException 1335 */ 1336 protected void sendStreamElement(Element element) throws NotConnectedException, InterruptedException { 1337 throwNotConnectedExceptionIfDoneAndResumptionNotPossible(); 1338 try { 1339 queue.put(element); 1340 } 1341 catch (InterruptedException e) { 1342 // put() may throw an InterruptedException for two reasons: 1343 // 1. If the queue was shut down 1344 // 2. If the thread was interrupted 1345 // so we have to check which is the case 1346 throwNotConnectedExceptionIfDoneAndResumptionNotPossible(); 1347 // If the method above did not throw, then the sending thread was interrupted 1348 throw e; 1349 } 1350 } 1351 1352 /** 1353 * Shuts down the stanza(/packet) writer. Once this method has been called, no further 1354 * packets will be written to the server. 1355 * @throws InterruptedException 1356 */ 1357 void shutdown(boolean instant) { 1358 instantShutdown = instant; 1359 queue.shutdown(); 1360 shutdownTimestamp = System.currentTimeMillis(); 1361 try { 1362 shutdownDone.checkIfSuccessOrWait(); 1363 } 1364 catch (NoResponseException | InterruptedException e) { 1365 LOGGER.log(Level.WARNING, "shutdownDone was not marked as successful by the writer thread", e); 1366 } 1367 } 1368 1369 /** 1370 * Maybe return the next available element from the queue for writing. If the queue is shut down <b>or</b> a 1371 * spurious interrupt occurs, <code>null</code> is returned. So it is important to check the 'done' condition in 1372 * that case. 1373 * 1374 * @return the next element for writing or null. 1375 */ 1376 private Element nextStreamElement() { 1377 // It is important the we check if the queue is empty before removing an element from it 1378 if (queue.isEmpty()) { 1379 shouldBundleAndDefer = true; 1380 } 1381 Element packet = null; 1382 try { 1383 packet = queue.take(); 1384 } 1385 catch (InterruptedException e) { 1386 if (!queue.isShutdown()) { 1387 // Users shouldn't try to interrupt the packet writer thread 1388 LOGGER.log(Level.WARNING, "Packet writer thread was interrupted. Don't do that. Use disconnect() instead.", e); 1389 } 1390 } 1391 return packet; 1392 } 1393 1394 private void writePackets() { 1395 Exception writerException = null; 1396 try { 1397 openStream(); 1398 initalOpenStreamSend.reportSuccess(); 1399 // Write out packets from the queue. 1400 while (!done()) { 1401 Element element = nextStreamElement(); 1402 if (element == null) { 1403 continue; 1404 } 1405 1406 // Get a local version of the bundle and defer callback, in case it's unset 1407 // between the null check and the method invocation 1408 final BundleAndDeferCallback localBundleAndDeferCallback = bundleAndDeferCallback; 1409 // If the preconditions are given (e.g. bundleAndDefer callback is set, queue is 1410 // empty), then we could wait a bit for further stanzas attempting to decrease 1411 // our energy consumption 1412 if (localBundleAndDeferCallback != null && isAuthenticated() && shouldBundleAndDefer) { 1413 // Reset shouldBundleAndDefer to false, nextStreamElement() will set it to true once the 1414 // queue is empty again. 1415 shouldBundleAndDefer = false; 1416 final AtomicBoolean bundlingAndDeferringStopped = new AtomicBoolean(); 1417 final int bundleAndDeferMillis = localBundleAndDeferCallback.getBundleAndDeferMillis(new BundleAndDefer( 1418 bundlingAndDeferringStopped)); 1419 if (bundleAndDeferMillis > 0) { 1420 long remainingWait = bundleAndDeferMillis; 1421 final long waitStart = System.currentTimeMillis(); 1422 synchronized (bundlingAndDeferringStopped) { 1423 while (!bundlingAndDeferringStopped.get() && remainingWait > 0) { 1424 bundlingAndDeferringStopped.wait(remainingWait); 1425 remainingWait = bundleAndDeferMillis 1426 - (System.currentTimeMillis() - waitStart); 1427 } 1428 } 1429 } 1430 } 1431 1432 Stanza packet = null; 1433 if (element instanceof Stanza) { 1434 packet = (Stanza) element; 1435 } 1436 else if (element instanceof Enable) { 1437 // The client needs to add messages to the unacknowledged stanzas queue 1438 // right after it sent 'enabled'. Stanza will be added once 1439 // unacknowledgedStanzas is not null. 1440 unacknowledgedStanzas = new ArrayBlockingQueue<>(QUEUE_SIZE); 1441 } 1442 maybeAddToUnacknowledgedStanzas(packet); 1443 1444 CharSequence elementXml = element.toXML(); 1445 if (elementXml instanceof XmlStringBuilder) { 1446 ((XmlStringBuilder) elementXml).write(writer); 1447 } 1448 else { 1449 writer.write(elementXml.toString()); 1450 } 1451 1452 if (queue.isEmpty()) { 1453 writer.flush(); 1454 } 1455 if (packet != null) { 1456 firePacketSendingListeners(packet); 1457 } 1458 } 1459 if (!instantShutdown) { 1460 // Flush out the rest of the queue. 1461 try { 1462 while (!queue.isEmpty()) { 1463 Element packet = queue.remove(); 1464 if (packet instanceof Stanza) { 1465 Stanza stanza = (Stanza) packet; 1466 maybeAddToUnacknowledgedStanzas(stanza); 1467 } 1468 writer.write(packet.toXML().toString()); 1469 } 1470 writer.flush(); 1471 } 1472 catch (Exception e) { 1473 LOGGER.log(Level.WARNING, 1474 "Exception flushing queue during shutdown, ignore and continue", 1475 e); 1476 } 1477 1478 // Close the stream. 1479 try { 1480 writer.write("</stream:stream>"); 1481 writer.flush(); 1482 } 1483 catch (Exception e) { 1484 LOGGER.log(Level.WARNING, "Exception writing closing stream element", e); 1485 } 1486 1487 // Delete the queue contents (hopefully nothing is left). 1488 queue.clear(); 1489 } else if (instantShutdown && isSmEnabled()) { 1490 // This was an instantShutdown and SM is enabled, drain all remaining stanzas 1491 // into the unacknowledgedStanzas queue 1492 drainWriterQueueToUnacknowledgedStanzas(); 1493 } 1494 // Do *not* close the writer here, as it will cause the socket 1495 // to get closed. But we may want to receive further stanzas 1496 // until the closing stream tag is received. The socket will be 1497 // closed in shutdown(). 1498 } 1499 catch (Exception e) { 1500 // The exception can be ignored if the the connection is 'done' 1501 // or if the it was caused because the socket got closed 1502 if (!(done() || queue.isShutdown())) { 1503 writerException = e; 1504 } else { 1505 LOGGER.log(Level.FINE, "Ignoring Exception in writePackets()", e); 1506 } 1507 } finally { 1508 LOGGER.fine("Reporting shutdownDone success in writer thread"); 1509 shutdownDone.reportSuccess(); 1510 } 1511 // Delay notifyConnectionError after shutdownDone has been reported in the finally block. 1512 if (writerException != null) { 1513 notifyConnectionError(writerException); 1514 } 1515 } 1516 1517 private void drainWriterQueueToUnacknowledgedStanzas() { 1518 List<Element> elements = new ArrayList<Element>(queue.size()); 1519 queue.drainTo(elements); 1520 for (Element element : elements) { 1521 if (element instanceof Stanza) { 1522 unacknowledgedStanzas.add((Stanza) element); 1523 } 1524 } 1525 } 1526 1527 private void maybeAddToUnacknowledgedStanzas(Stanza stanza) throws IOException { 1528 // Check if the stream element should be put to the unacknowledgedStanza 1529 // queue. Note that we can not do the put() in sendStanzaInternal() and the 1530 // packet order is not stable at this point (sendStanzaInternal() can be 1531 // called concurrently). 1532 if (unacknowledgedStanzas != null && stanza != null) { 1533 // If the unacknowledgedStanza queue is nearly full, request an new ack 1534 // from the server in order to drain it 1535 if (unacknowledgedStanzas.size() == 0.8 * XMPPTCPConnection.QUEUE_SIZE) { 1536 writer.write(AckRequest.INSTANCE.toXML().toString()); 1537 writer.flush(); 1538 } 1539 try { 1540 // It is important the we put the stanza in the unacknowledged stanza 1541 // queue before we put it on the wire 1542 unacknowledgedStanzas.put(stanza); 1543 } 1544 catch (InterruptedException e) { 1545 throw new IllegalStateException(e); 1546 } 1547 } 1548 } 1549 } 1550 1551 /** 1552 * Set if Stream Management should be used by default for new connections. 1553 * 1554 * @param useSmDefault true to use Stream Management for new connections. 1555 */ 1556 public static void setUseStreamManagementDefault(boolean useSmDefault) { 1557 XMPPTCPConnection.useSmDefault = useSmDefault; 1558 } 1559 1560 /** 1561 * Set if Stream Management resumption should be used by default for new connections. 1562 * 1563 * @param useSmResumptionDefault true to use Stream Management resumption for new connections. 1564 * @deprecated use {@link #setUseStreamManagementResumptionDefault(boolean)} instead. 1565 */ 1566 @Deprecated 1567 public static void setUseStreamManagementResumptiodDefault(boolean useSmResumptionDefault) { 1568 setUseStreamManagementResumptionDefault(useSmResumptionDefault); 1569 } 1570 1571 /** 1572 * Set if Stream Management resumption should be used by default for new connections. 1573 * 1574 * @param useSmResumptionDefault true to use Stream Management resumption for new connections. 1575 */ 1576 public static void setUseStreamManagementResumptionDefault(boolean useSmResumptionDefault) { 1577 if (useSmResumptionDefault) { 1578 // Also enable SM is resumption is enabled 1579 setUseStreamManagementDefault(useSmResumptionDefault); 1580 } 1581 XMPPTCPConnection.useSmResumptionDefault = useSmResumptionDefault; 1582 } 1583 1584 /** 1585 * Set if Stream Management should be used if supported by the server. 1586 * 1587 * @param useSm true to use Stream Management. 1588 */ 1589 public void setUseStreamManagement(boolean useSm) { 1590 this.useSm = useSm; 1591 } 1592 1593 /** 1594 * Set if Stream Management resumption should be used if supported by the server. 1595 * 1596 * @param useSmResumption true to use Stream Management resumption. 1597 */ 1598 public void setUseStreamManagementResumption(boolean useSmResumption) { 1599 if (useSmResumption) { 1600 // Also enable SM is resumption is enabled 1601 setUseStreamManagement(useSmResumption); 1602 } 1603 this.useSmResumption = useSmResumption; 1604 } 1605 1606 /** 1607 * Set the preferred resumption time in seconds. 1608 * @param resumptionTime the preferred resumption time in seconds 1609 */ 1610 public void setPreferredResumptionTime(int resumptionTime) { 1611 smClientMaxResumptionTime = resumptionTime; 1612 } 1613 1614 /** 1615 * Add a predicate for Stream Management acknowledgment requests. 1616 * <p> 1617 * Those predicates are used to determine when a Stream Management acknowledgement request is send to the server. 1618 * Some pre-defined predicates are found in the <code>org.jivesoftware.smack.sm.predicates</code> package. 1619 * </p> 1620 * <p> 1621 * If not predicate is configured, the {@link Predicate#forMessagesOrAfter5Stanzas()} will be used. 1622 * </p> 1623 * 1624 * @param predicate the predicate to add. 1625 * @return if the predicate was not already active. 1626 */ 1627 public boolean addRequestAckPredicate(StanzaFilter predicate) { 1628 synchronized (requestAckPredicates) { 1629 return requestAckPredicates.add(predicate); 1630 } 1631 } 1632 1633 /** 1634 * Remove the given predicate for Stream Management acknowledgment request. 1635 * @param predicate the predicate to remove. 1636 * @return true if the predicate was removed. 1637 */ 1638 public boolean removeRequestAckPredicate(StanzaFilter predicate) { 1639 synchronized (requestAckPredicates) { 1640 return requestAckPredicates.remove(predicate); 1641 } 1642 } 1643 1644 /** 1645 * Remove all predicates for Stream Management acknowledgment requests. 1646 */ 1647 public void removeAllRequestAckPredicates() { 1648 synchronized (requestAckPredicates) { 1649 requestAckPredicates.clear(); 1650 } 1651 } 1652 1653 /** 1654 * Send an unconditional Stream Management acknowledgement request to the server. 1655 * 1656 * @throws StreamManagementNotEnabledException if Stream Mangement is not enabled. 1657 * @throws NotConnectedException if the connection is not connected. 1658 * @throws InterruptedException 1659 */ 1660 public void requestSmAcknowledgement() throws StreamManagementNotEnabledException, NotConnectedException, InterruptedException { 1661 if (!isSmEnabled()) { 1662 throw new StreamManagementException.StreamManagementNotEnabledException(); 1663 } 1664 requestSmAcknowledgementInternal(); 1665 } 1666 1667 private void requestSmAcknowledgementInternal() throws NotConnectedException, InterruptedException { 1668 packetWriter.sendStreamElement(AckRequest.INSTANCE); 1669 } 1670 1671 /** 1672 * Send a unconditional Stream Management acknowledgment to the server. 1673 * <p> 1674 * See <a href="http://xmpp.org/extensions/xep-0198.html#acking">XEP-198: Stream Management ยง 4. Acks</a>: 1675 * "Either party MAY send an <a/> element at any time (e.g., after it has received a certain number of stanzas, 1676 * or after a certain period of time), even if it has not received an <r/> element from the other party." 1677 * </p> 1678 * 1679 * @throws StreamManagementNotEnabledException if Stream Management is not enabled. 1680 * @throws NotConnectedException if the connection is not connected. 1681 * @throws InterruptedException 1682 */ 1683 public void sendSmAcknowledgement() throws StreamManagementNotEnabledException, NotConnectedException, InterruptedException { 1684 if (!isSmEnabled()) { 1685 throw new StreamManagementException.StreamManagementNotEnabledException(); 1686 } 1687 sendSmAcknowledgementInternal(); 1688 } 1689 1690 private void sendSmAcknowledgementInternal() throws NotConnectedException, InterruptedException { 1691 packetWriter.sendStreamElement(new AckAnswer(clientHandledStanzasCount)); 1692 } 1693 1694 /** 1695 * Add a Stanza acknowledged listener. 1696 * <p> 1697 * Those listeners will be invoked every time a Stanza has been acknowledged by the server. The will not get 1698 * automatically removed. Consider using {@link #addStanzaIdAcknowledgedListener(String, StanzaListener)} when 1699 * possible. 1700 * </p> 1701 * 1702 * @param listener the listener to add. 1703 */ 1704 public void addStanzaAcknowledgedListener(StanzaListener listener) { 1705 stanzaAcknowledgedListeners.add(listener); 1706 } 1707 1708 /** 1709 * Remove the given Stanza acknowledged listener. 1710 * 1711 * @param listener the listener. 1712 * @return true if the listener was removed. 1713 */ 1714 public boolean removeStanzaAcknowledgedListener(StanzaListener listener) { 1715 return stanzaAcknowledgedListeners.remove(listener); 1716 } 1717 1718 /** 1719 * Remove all stanza acknowledged listeners. 1720 */ 1721 public void removeAllStanzaAcknowledgedListeners() { 1722 stanzaAcknowledgedListeners.clear(); 1723 } 1724 1725 /** 1726 * Add a new Stanza ID acknowledged listener for the given ID. 1727 * <p> 1728 * The listener will be invoked if the stanza with the given ID was acknowledged by the server. It will 1729 * automatically be removed after the listener was run. 1730 * </p> 1731 * 1732 * @param id the stanza ID. 1733 * @param listener the listener to invoke. 1734 * @return the previous listener for this stanza ID or null. 1735 * @throws StreamManagementNotEnabledException if Stream Management is not enabled. 1736 */ 1737 public StanzaListener addStanzaIdAcknowledgedListener(final String id, StanzaListener listener) throws StreamManagementNotEnabledException { 1738 // Prevent users from adding callbacks that will never get removed 1739 if (!smWasEnabledAtLeastOnce) { 1740 throw new StreamManagementException.StreamManagementNotEnabledException(); 1741 } 1742 // Remove the listener after max. 12 hours 1743 final int removeAfterSeconds = Math.min(getMaxSmResumptionTime(), 12 * 60 * 60); 1744 schedule(new Runnable() { 1745 @Override 1746 public void run() { 1747 stanzaIdAcknowledgedListeners.remove(id); 1748 } 1749 }, removeAfterSeconds, TimeUnit.SECONDS); 1750 return stanzaIdAcknowledgedListeners.put(id, listener); 1751 } 1752 1753 /** 1754 * Remove the Stanza ID acknowledged listener for the given ID. 1755 * 1756 * @param id the stanza ID. 1757 * @return true if the listener was found and removed, false otherwise. 1758 */ 1759 public StanzaListener removeStanzaIdAcknowledgedListener(String id) { 1760 return stanzaIdAcknowledgedListeners.remove(id); 1761 } 1762 1763 /** 1764 * Removes all Stanza ID acknowledged listeners. 1765 */ 1766 public void removeAllStanzaIdAcknowledgedListeners() { 1767 stanzaIdAcknowledgedListeners.clear(); 1768 } 1769 1770 /** 1771 * Returns true if Stream Management is supported by the server. 1772 * 1773 * @return true if Stream Management is supported by the server. 1774 */ 1775 public boolean isSmAvailable() { 1776 return hasFeature(StreamManagementFeature.ELEMENT, StreamManagement.NAMESPACE); 1777 } 1778 1779 /** 1780 * Returns true if Stream Management was successfully negotiated with the server. 1781 * 1782 * @return true if Stream Management was negotiated. 1783 */ 1784 public boolean isSmEnabled() { 1785 return smEnabledSyncPoint.wasSuccessful(); 1786 } 1787 1788 /** 1789 * Returns true if the stream was successfully resumed with help of Stream Management. 1790 * 1791 * @return true if the stream was resumed. 1792 */ 1793 public boolean streamWasResumed() { 1794 return smResumedSyncPoint.wasSuccessful(); 1795 } 1796 1797 /** 1798 * Returns true if the connection is disconnected by a Stream resumption via Stream Management is possible. 1799 * 1800 * @return true if disconnected but resumption possible. 1801 */ 1802 public boolean isDisconnectedButSmResumptionPossible() { 1803 return disconnectedButResumeable && isSmResumptionPossible(); 1804 } 1805 1806 /** 1807 * Returns true if the stream is resumable. 1808 * 1809 * @return true if the stream is resumable. 1810 */ 1811 public boolean isSmResumptionPossible() { 1812 // There is no resumable stream available 1813 if (smSessionId == null) 1814 return false; 1815 1816 final Long shutdownTimestamp = packetWriter.shutdownTimestamp; 1817 // Seems like we are already reconnected, report true 1818 if (shutdownTimestamp == null) { 1819 return true; 1820 } 1821 1822 // See if resumption time is over 1823 long current = System.currentTimeMillis(); 1824 long maxResumptionMillies = ((long) getMaxSmResumptionTime()) * 1000; 1825 if (current > shutdownTimestamp + maxResumptionMillies) { 1826 // Stream resumption is *not* possible if the current timestamp is greater then the greatest timestamp where 1827 // resumption is possible 1828 return false; 1829 } else { 1830 return true; 1831 } 1832 } 1833 1834 /** 1835 * Drop the stream management state. Sets {@link #smSessionId} and 1836 * {@link #unacknowledgedStanzas} to <code>null</code>. 1837 */ 1838 private void dropSmState() { 1839 // clientHandledCount and serverHandledCount will be reset on <enable/> and <enabled/> 1840 // respective. No need to reset them here. 1841 smSessionId = null; 1842 unacknowledgedStanzas = null; 1843 } 1844 1845 /** 1846 * Get the maximum resumption time in seconds after which a managed stream can be resumed. 1847 * <p> 1848 * This method will return {@link Integer#MAX_VALUE} if neither the client nor the server specify a maximum 1849 * resumption time. Be aware of integer overflows when using this value, e.g. do not add arbitrary values to it 1850 * without checking for overflows before. 1851 * </p> 1852 * 1853 * @return the maximum resumption time in seconds or {@link Integer#MAX_VALUE} if none set. 1854 */ 1855 public int getMaxSmResumptionTime() { 1856 int clientResumptionTime = smClientMaxResumptionTime > 0 ? smClientMaxResumptionTime : Integer.MAX_VALUE; 1857 int serverResumptionTime = smServerMaxResumptimTime > 0 ? smServerMaxResumptimTime : Integer.MAX_VALUE; 1858 return Math.min(clientResumptionTime, serverResumptionTime); 1859 } 1860 1861 private void processHandledCount(long handledCount) throws StreamManagementCounterError { 1862 long ackedStanzasCount = SMUtils.calculateDelta(handledCount, serverHandledStanzasCount); 1863 final List<Stanza> ackedStanzas = new ArrayList<Stanza>( 1864 ackedStanzasCount <= Integer.MAX_VALUE ? (int) ackedStanzasCount 1865 : Integer.MAX_VALUE); 1866 for (long i = 0; i < ackedStanzasCount; i++) { 1867 Stanza ackedStanza = unacknowledgedStanzas.poll(); 1868 // If the server ack'ed a stanza, then it must be in the 1869 // unacknowledged stanza queue. There can be no exception. 1870 if (ackedStanza == null) { 1871 throw new StreamManagementCounterError(handledCount, serverHandledStanzasCount, 1872 ackedStanzasCount, ackedStanzas); 1873 } 1874 ackedStanzas.add(ackedStanza); 1875 } 1876 1877 boolean atLeastOneStanzaAcknowledgedListener = false; 1878 if (!stanzaAcknowledgedListeners.isEmpty()) { 1879 // If stanzaAcknowledgedListeners is not empty, the we have at least one 1880 atLeastOneStanzaAcknowledgedListener = true; 1881 } 1882 else { 1883 // Otherwise we look for a matching id in the stanza *id* acknowledged listeners 1884 for (Stanza ackedStanza : ackedStanzas) { 1885 String id = ackedStanza.getStanzaId(); 1886 if (id != null && stanzaIdAcknowledgedListeners.containsKey(id)) { 1887 atLeastOneStanzaAcknowledgedListener = true; 1888 break; 1889 } 1890 } 1891 } 1892 1893 // Only spawn a new thread if there is a chance that some listener is invoked 1894 if (atLeastOneStanzaAcknowledgedListener) { 1895 asyncGo(new Runnable() { 1896 @Override 1897 public void run() { 1898 for (Stanza ackedStanza : ackedStanzas) { 1899 for (StanzaListener listener : stanzaAcknowledgedListeners) { 1900 try { 1901 listener.processStanza(ackedStanza); 1902 } 1903 catch (InterruptedException | NotConnectedException | NotLoggedInException e) { 1904 LOGGER.log(Level.FINER, "Received exception", e); 1905 } 1906 } 1907 String id = ackedStanza.getStanzaId(); 1908 if (StringUtils.isNullOrEmpty(id)) { 1909 continue; 1910 } 1911 StanzaListener listener = stanzaIdAcknowledgedListeners.remove(id); 1912 if (listener != null) { 1913 try { 1914 listener.processStanza(ackedStanza); 1915 } 1916 catch (InterruptedException | NotConnectedException | NotLoggedInException e) { 1917 LOGGER.log(Level.FINER, "Received exception", e); 1918 } 1919 } 1920 } 1921 } 1922 }); 1923 } 1924 1925 serverHandledStanzasCount = handledCount; 1926 } 1927 1928 /** 1929 * Set the default bundle and defer callback used for new connections. 1930 * 1931 * @param defaultBundleAndDeferCallback 1932 * @see BundleAndDeferCallback 1933 * @since 4.1 1934 */ 1935 public static void setDefaultBundleAndDeferCallback(BundleAndDeferCallback defaultBundleAndDeferCallback) { 1936 XMPPTCPConnection.defaultBundleAndDeferCallback = defaultBundleAndDeferCallback; 1937 } 1938 1939 /** 1940 * Set the bundle and defer callback used for this connection. 1941 * <p> 1942 * You can use <code>null</code> as argument to reset the callback. Outgoing stanzas will then 1943 * no longer get deferred. 1944 * </p> 1945 * 1946 * @param bundleAndDeferCallback the callback or <code>null</code>. 1947 * @see BundleAndDeferCallback 1948 * @since 4.1 1949 */ 1950 public void setBundleandDeferCallback(BundleAndDeferCallback bundleAndDeferCallback) { 1951 this.bundleAndDeferCallback = bundleAndDeferCallback; 1952 } 1953 1954}