From bfdcfba092642218165a29c7aa38c54534e2d14f Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 3 Jul 2015 12:57:30 +0200 Subject: [PATCH] Throw NotConnectedException instead of NoResponseException if the connection is not connected in sendStanzaWithResponseCallback and in PacketCollector. Also decrease log level if roster result listener's exeption callback is invoked with a NotConnectedException. --- .../jivesoftware/smack/AbstractXMPPConnection.java | 9 ++++++++- .../java/org/jivesoftware/smack/PacketCollector.java | 12 ++++++++++-- .../java/org/jivesoftware/smack/SmackException.java | 5 +++++ .../java/org/jivesoftware/smack/roster/Roster.java | 8 +++++++- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java index df05dbc61..d2c54e2d1 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java @@ -1466,7 +1466,14 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { // If the packetListener got removed, then it was never run and // we never received a response, inform the exception callback if (removed && exceptionCallback != null) { - exceptionCallback.processException(NoResponseException.newWith(AbstractXMPPConnection.this, replyFilter)); + Exception exception; + if (!isConnected()) { + // If the connection is no longer connected, throw a not connected exception. + exception = new NotConnectedException(AbstractXMPPConnection.this, replyFilter); + } else { + exception = NoResponseException.newWith(AbstractXMPPConnection.this, replyFilter); + } + exceptionCallback.processException(exception); } } }, timeout, TimeUnit.MILLISECONDS); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/PacketCollector.java b/smack-core/src/main/java/org/jivesoftware/smack/PacketCollector.java index 57764c2b8..00d7d904e 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/PacketCollector.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/PacketCollector.java @@ -20,6 +20,7 @@ package org.jivesoftware.smack; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.TimeUnit; +import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.filter.StanzaFilter; @@ -199,8 +200,10 @@ public class PacketCollector { * @throws XMPPErrorException in case an error response. * @throws NoResponseException if there was no response from the server. * @throws InterruptedException + * @throws NotConnectedException */ - public

P nextResultOrThrow() throws NoResponseException, XMPPErrorException, InterruptedException { + public

P nextResultOrThrow() throws NoResponseException, XMPPErrorException, + InterruptedException, NotConnectedException { return nextResultOrThrow(connection.getPacketReplyTimeout()); } @@ -213,11 +216,16 @@ public class PacketCollector { * @throws NoResponseException if there was no response from the server. * @throws XMPPErrorException in case an error response. * @throws InterruptedException + * @throws NotConnectedException */ - public

P nextResultOrThrow(long timeout) throws NoResponseException, XMPPErrorException, InterruptedException { + public

P nextResultOrThrow(long timeout) throws NoResponseException, + XMPPErrorException, InterruptedException, NotConnectedException { P result = nextResult(timeout); cancel(); if (result == null) { + if (!connection.isConnected()) { + throw new NotConnectedException(connection, packetFilter); + } throw NoResponseException.newWith(connection, this); } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java b/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java index 4bbe52f26..7a52710db 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java @@ -171,6 +171,11 @@ public class SmackException extends Exception { super("The connection " + connection.toString() + " is no longer connected. " + details); } + + public NotConnectedException(XMPPConnection connection, StanzaFilter stanzaFilter) { + super("The connection " + connection + + " is no longer connected while waiting for response with " + stanzaFilter); + } } public static class IllegalStateChangeException extends SmackException { diff --git a/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java b/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java index d22c46ac6..8bcfff223 100644 --- a/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java +++ b/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java @@ -341,7 +341,13 @@ public final class Roster extends Manager { connection.sendIqWithResponseCallback(packet, new RosterResultListener(), new ExceptionCallback() { @Override public void processException(Exception exception) { - LOGGER.log(Level.SEVERE, "Exception reloading roster" , exception); + Level logLevel; + if (exception instanceof NotConnectedException) { + logLevel = Level.FINE; + } else { + logLevel = Level.SEVERE; + } + LOGGER.log(logLevel, "Exception reloading roster" , exception); } }); }