From 887c6114b727d4fb275dfb2c4c5e6158a5de821a Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 19 Feb 2015 12:11:19 +0100 Subject: [PATCH] Add filter information to NoResponseException in order to aid debugging errors. This made it necessary to override 'toString()' for all filters in Smack. --- .../smack/AbstractXMPPConnection.java | 4 +- .../jivesoftware/smack/PacketCollector.java | 2 +- .../smack/SASLAuthentication.java | 6 +- .../jivesoftware/smack/SmackException.java | 44 ++++++++++- .../smack/SynchronizationPoint.java | 2 +- .../smack/filter/AbstractListFilter.java | 78 +++++++++++++++++++ .../jivesoftware/smack/filter/AndFilter.java | 34 +------- .../filter/FlexiblePacketTypeFilter.java | 11 ++- .../smack/filter/FromMatchesFilter.java | 2 +- .../smack/filter/IQReplyFilter.java | 8 ++ .../smack/filter/IQResultReplyFilter.java | 7 ++ .../smack/filter/IQTypeFilter.java | 8 +- .../smack/filter/MessageTypeFilter.java | 4 + .../smack/filter/MessageWithBodiesFilter.java | 4 + .../filter/MessageWithSubjectFilter.java | 4 + .../jivesoftware/smack/filter/NotFilter.java | 6 +- .../jivesoftware/smack/filter/OrFilter.java | 35 +-------- .../smack/filter/PacketExtensionFilter.java | 5 ++ .../smack/filter/PacketIDFilter.java | 2 +- .../smack/filter/PacketTypeFilter.java | 3 +- .../smack/filter/PresenceTypeFilter.java | 8 +- .../smack/filter/ThreadFilter.java | 13 +++- .../jivesoftware/smack/filter/ToFilter.java | 4 + .../filetransfer/FaultTolerantNegotiator.java | 7 +- 24 files changed, 210 insertions(+), 91 deletions(-) create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractListFilter.java 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 36d570533..e8585ad85 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java @@ -1432,7 +1432,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { } @Override - public void sendStanzaWithResponseCallback(Stanza stanza, PacketFilter replyFilter, + public void sendStanzaWithResponseCallback(Stanza stanza, final PacketFilter replyFilter, final PacketListener callback, final ExceptionCallback exceptionCallback, long timeout) throws NotConnectedException { Objects.requireNonNull(stanza, "stanza must not be null"); @@ -1465,7 +1465,7 @@ 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(new NoResponseException(AbstractXMPPConnection.this)); + exceptionCallback.processException(NoResponseException.newWith(AbstractXMPPConnection.this, replyFilter)); } } }, 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 0908b35c2..49ce54f64 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/PacketCollector.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/PacketCollector.java @@ -214,7 +214,7 @@ public class PacketCollector { P result = nextResult(timeout); cancel(); if (result == null) { - throw new NoResponseException(connection); + throw NoResponseException.newWith(connection, this); } XMPPErrorException.ifHasErrorThenThrow(result); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/SASLAuthentication.java b/smack-core/src/main/java/org/jivesoftware/smack/SASLAuthentication.java index d15e6c446..dade9ae82 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SASLAuthentication.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SASLAuthentication.java @@ -200,7 +200,7 @@ public class SASLAuthentication { maybeThrowException(); if (!authenticationSuccessful) { - throw new NoResponseException(connection); + throw NoResponseException.newWith(connection); } } else { @@ -247,7 +247,7 @@ public class SASLAuthentication { maybeThrowException(); if (!authenticationSuccessful) { - throw new NoResponseException(connection); + throw NoResponseException.newWith(connection); } } else { @@ -286,7 +286,7 @@ public class SASLAuthentication { maybeThrowException(); if (!authenticationSuccessful) { - throw new NoResponseException(connection); + throw NoResponseException.newWith(connection); } } 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 807f68ef1..789f6a0ff 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SmackException.java @@ -19,6 +19,7 @@ package org.jivesoftware.smack; import java.util.ArrayList; import java.util.List; +import org.jivesoftware.smack.filter.PacketFilter; import org.jivesoftware.smack.util.dns.HostAddress; /** @@ -64,10 +65,47 @@ public class SmackException extends Exception { */ private static final long serialVersionUID = -6523363748984543636L; - public NoResponseException(XMPPConnection connection) { - super("No response received within packet reply timeout. Timeout was " + connection.getPacketReplyTimeout() - + "ms (~" + connection.getPacketReplyTimeout() / 1000 + "s)"); + private final PacketFilter filter; + + private NoResponseException(String message, PacketFilter filter) { + super(message); + this.filter = filter; } + + /** + * Get the filter that was used to collect the response. + * + * @return the used filter or null. + */ + public PacketFilter getFilter() { + return filter; + } + + public static NoResponseException newWith(XMPPConnection connection) { + return newWith(connection, (PacketFilter) null); + } + + public static NoResponseException newWith(XMPPConnection connection, + PacketCollector collector) { + return newWith(connection, collector.getPacketFilter()); + } + + public static NoResponseException newWith(XMPPConnection connection, PacketFilter filter) { + final long replyTimeout = connection.getPacketReplyTimeout(); + final StringBuilder sb = new StringBuilder(256); + sb.append("No response received within reply timeout. Timeout was " + + replyTimeout + "ms (~" + + replyTimeout / 1000 + "s). Used filter: "); + if (filter != null) { + sb.append(filter.toString()); + } + else { + sb.append("No filter used or filter was 'null'"); + } + sb.append('.'); + return new NoResponseException(sb.toString(), filter); + } + } public static class NotLoggedInException extends SmackException { diff --git a/smack-core/src/main/java/org/jivesoftware/smack/SynchronizationPoint.java b/smack-core/src/main/java/org/jivesoftware/smack/SynchronizationPoint.java index 7e75a75c1..4cd2070e1 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/SynchronizationPoint.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/SynchronizationPoint.java @@ -189,7 +189,7 @@ public class SynchronizationPoint { case Initial: case NoResponse: case RequestSent: - throw new NoResponseException(connection); + throw NoResponseException.newWith(connection); default: // Do nothing break; diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractListFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractListFilter.java new file mode 100644 index 000000000..d32fb6eef --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/AbstractListFilter.java @@ -0,0 +1,78 @@ +/** + * + * Copyright 2015 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jivesoftware.smack.filter; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.jivesoftware.smack.util.Objects; + +/** + * + */ +public abstract class AbstractListFilter implements PacketFilter { + + /** + * The list of filters. + */ + protected final List filters; + + /** + * Creates an empty filter. + */ + protected AbstractListFilter() { + filters = new ArrayList(); + } + + /** + * Creates an filter using the specified filters. + * + * @param filters the filters to add. + */ + protected AbstractListFilter(PacketFilter... filters) { + Objects.requireNonNull(filters, "Parameter must not be null."); + for(PacketFilter filter : filters) { + Objects.requireNonNull(filter, "Parameter must not be null."); + } + this.filters = new ArrayList(Arrays.asList(filters)); + } + + /** + * Adds a filter to the filter list. A stanza will pass the filter if all of the filters in the + * list accept it. + * + * @param filter a filter to add to the filter list. + */ + public void addFilter(PacketFilter filter) { + Objects.requireNonNull(filter, "Parameter must not be null."); + filters.add(filter); + } + + @Override + public final String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" ("); + for (PacketFilter filter : filters) { + sb.append(' ' + filter.toString() + ','); + } + sb.append(")"); + return sb.toString(); + } +} diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/AndFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/AndFilter.java index 0f7ccb4e0..7006852fe 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/AndFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/AndFilter.java @@ -17,12 +17,7 @@ package org.jivesoftware.smack.filter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - import org.jivesoftware.smack.packet.Stanza; -import org.jivesoftware.smack.util.Objects; /** * Implements the logical AND operation over two or more packet filters. @@ -30,19 +25,14 @@ import org.jivesoftware.smack.util.Objects; * * @author Matt Tucker */ -public class AndFilter implements PacketFilter { - - /** - * The list of filters. - */ - private final List filters; +public class AndFilter extends AbstractListFilter implements PacketFilter { /** * Creates an empty AND filter. Filters should be added using the * {@link #addFilter(PacketFilter)} method. */ public AndFilter() { - filters = new ArrayList(); + super(); } /** @@ -51,22 +41,7 @@ public class AndFilter implements PacketFilter { * @param filters the filters to add. */ public AndFilter(PacketFilter... filters) { - Objects.requireNonNull(filters, "Parameter must not be null."); - for(PacketFilter filter : filters) { - Objects.requireNonNull(filter, "Parameter must not be null."); - } - this.filters = new ArrayList(Arrays.asList(filters)); - } - - /** - * Adds a filter to the filter list for the AND operation. A packet - * will pass the filter if all of the filters in the list accept it. - * - * @param filter a filter to add to the filter list. - */ - public void addFilter(PacketFilter filter) { - Objects.requireNonNull(filter, "Parameter must not be null."); - filters.add(filter); + super(filters); } public boolean accept(Stanza packet) { @@ -78,7 +53,4 @@ public class AndFilter implements PacketFilter { return true; } - public String toString() { - return filters.toString(); - } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/FlexiblePacketTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/FlexiblePacketTypeFilter.java index 54e49c2fe..454d8caa3 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/FlexiblePacketTypeFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/FlexiblePacketTypeFilter.java @@ -20,6 +20,7 @@ package org.jivesoftware.smack.filter; import java.lang.reflect.ParameterizedType; import org.jivesoftware.smack.packet.Stanza; +import org.jivesoftware.smack.util.Objects; /** * Filters for packets of a particular type and allows a custom method to further filter the packets. @@ -31,7 +32,7 @@ public abstract class FlexiblePacketTypeFilter

implements Pack protected final Class

packetType; public FlexiblePacketTypeFilter(Class

packetType) { - this.packetType = packetType; + this.packetType = Objects.requireNonNull(packetType, "Type must not be null"); } @SuppressWarnings("unchecked") @@ -49,4 +50,12 @@ public abstract class FlexiblePacketTypeFilter

implements Pack } protected abstract boolean acceptSpecific(P packet); + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" (" + packetType.toString() + ')'); + return sb.toString(); + } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/FromMatchesFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/FromMatchesFilter.java index ee570aa78..3b48d0f13 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/FromMatchesFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/FromMatchesFilter.java @@ -104,6 +104,6 @@ public class FromMatchesFilter implements PacketFilter { public String toString() { String matchMode = matchBareJID ? "bare" : "full"; - return "FromMatchesFilter (" +matchMode + "): " + address; + return getClass().getSimpleName() + " (" + matchMode + "): " + address; } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/IQReplyFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/IQReplyFilter.java index fc0673548..e7b162356 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/IQReplyFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/IQReplyFilter.java @@ -130,4 +130,12 @@ public class IQReplyFilter implements PacketFilter { } } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(": iqAndIdFilter (").append(iqAndIdFilter.toString()).append("), "); + sb.append(": fromFilter (").append(fromFilter.toString()).append(')'); + return sb.toString(); + } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/IQResultReplyFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/IQResultReplyFilter.java index c77539f56..a8225dcf4 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/IQResultReplyFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/IQResultReplyFilter.java @@ -38,4 +38,11 @@ public class IQResultReplyFilter extends IQReplyFilter { return IQTypeFilter.RESULT.accept(packet); } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName()); + sb.append(" (" + super.toString() + ')'); + return sb.toString(); + } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/IQTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/IQTypeFilter.java index f0cccb245..dc2c38b10 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/IQTypeFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/IQTypeFilter.java @@ -18,6 +18,7 @@ package org.jivesoftware.smack.filter; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ.Type; +import org.jivesoftware.smack.util.Objects; /** * A filter for IQ packet types. Returns true only if the packet is an IQ packet @@ -38,11 +39,16 @@ public class IQTypeFilter extends FlexiblePacketTypeFilter { private IQTypeFilter(IQ.Type type) { super(IQ.class); - this.type = type; + this.type = Objects.requireNonNull(type, "Type must not be null"); } @Override protected boolean acceptSpecific(IQ iq) { return iq.getType() == type; } + + @Override + public String toString() { + return getClass().getSimpleName() + ": type=" + type; + } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/MessageTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/MessageTypeFilter.java index c7eaa5275..74df7bfed 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/MessageTypeFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/MessageTypeFilter.java @@ -55,4 +55,8 @@ public class MessageTypeFilter extends FlexiblePacketTypeFilter { return message.getType() == type; } + @Override + public String toString() { + return getClass().getSimpleName() + ": type=" + type; + } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/MessageWithBodiesFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/MessageWithBodiesFilter.java index a8f796622..201e5546d 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/MessageWithBodiesFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/MessageWithBodiesFilter.java @@ -36,4 +36,8 @@ public class MessageWithBodiesFilter extends FlexiblePacketTypeFilter { return !message.getBodies().isEmpty(); } + @Override + public String toString() { + return getClass().getSimpleName(); + } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/MessageWithSubjectFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/MessageWithSubjectFilter.java index fa29ec497..c484aa2e8 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/MessageWithSubjectFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/MessageWithSubjectFilter.java @@ -36,4 +36,8 @@ public class MessageWithSubjectFilter extends FlexiblePacketTypeFilter return message.getSubject() != null; } + @Override + public String toString() { + return getClass().getSimpleName(); + } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/NotFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/NotFilter.java index 3dd76a7f0..76d63ce9c 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/NotFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/NotFilter.java @@ -18,6 +18,7 @@ package org.jivesoftware.smack.filter; import org.jivesoftware.smack.packet.Stanza; +import org.jivesoftware.smack.util.Objects; /** * Implements the logical NOT operation on a packet filter. In other words, packets @@ -35,10 +36,7 @@ public class NotFilter implements PacketFilter { * @param filter the filter. */ public NotFilter(PacketFilter filter) { - if (filter == null) { - throw new IllegalArgumentException("Parameter must not be null."); - } - this.filter = filter; + this.filter = Objects.requireNonNull(filter, "Parameter must not be null."); } public boolean accept(Stanza packet) { diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/OrFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/OrFilter.java index db583c076..b1d96eeef 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/OrFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/OrFilter.java @@ -17,12 +17,7 @@ package org.jivesoftware.smack.filter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - import org.jivesoftware.smack.packet.Stanza; -import org.jivesoftware.smack.util.Objects; /** * Implements the logical OR operation over two or more packet filters. In @@ -30,19 +25,14 @@ import org.jivesoftware.smack.util.Objects; * * @author Matt Tucker */ -public class OrFilter implements PacketFilter { - - /** - * The list of filters. - */ - private final List filters; +public class OrFilter extends AbstractListFilter implements PacketFilter { /** * Creates an empty OR filter. Filters should be added using the * {@link #addFilter(PacketFilter)} method. */ public OrFilter() { - filters = new ArrayList(); + super(); } /** @@ -51,24 +41,10 @@ public class OrFilter implements PacketFilter { * @param filters the filters to add. */ public OrFilter(PacketFilter... filters) { - Objects.requireNonNull(filters, "Parameter must not be null."); - for(PacketFilter filter : filters) { - Objects.requireNonNull(filter, "Parameter must not be null."); - } - this.filters = new ArrayList(Arrays.asList(filters)); - } - - /** - * Adds a filter to the filter list for the OR operation. A packet - * will pass the filter if any filter in the list accepts it. - * - * @param filter a filter to add to the filter list. - */ - public void addFilter(PacketFilter filter) { - Objects.requireNonNull(filter, "Parameter must not be null."); - filters.add(filter); + super(filters); } + @Override public boolean accept(Stanza packet) { for (PacketFilter filter : filters) { if (filter.accept(packet)) { @@ -78,7 +54,4 @@ public class OrFilter implements PacketFilter { return false; } - public String toString() { - return filters.toString(); - } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/PacketExtensionFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/PacketExtensionFilter.java index 0ba17386f..c0deb6183 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/PacketExtensionFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/PacketExtensionFilter.java @@ -68,4 +68,9 @@ public class PacketExtensionFilter implements PacketFilter { public boolean accept(Stanza packet) { return packet.hasExtension(elementName, namespace); } + + @Override + public String toString() { + return getClass().getSimpleName() + ": element=" + elementName + " namespace=" + namespace; + } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/PacketIDFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/PacketIDFilter.java index 82a2ea904..a01b247c6 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/PacketIDFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/PacketIDFilter.java @@ -53,6 +53,6 @@ public class PacketIDFilter implements PacketFilter { } public String toString() { - return "PacketIDFilter by id: " + packetID; + return getClass().getSimpleName() + ": id=" + packetID; } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/PacketTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/PacketTypeFilter.java index 6c57a4071..5a32e784c 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/PacketTypeFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/PacketTypeFilter.java @@ -53,7 +53,8 @@ public class PacketTypeFilter implements PacketFilter { return packetType.isInstance(packet); } + @Override public String toString() { - return "PacketTypeFilter: " + packetType.getName(); + return getClass().getSimpleName() + ": " + packetType.getName(); } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/PresenceTypeFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/PresenceTypeFilter.java index 045cd2bf8..d67635609 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/PresenceTypeFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/PresenceTypeFilter.java @@ -18,6 +18,7 @@ package org.jivesoftware.smack.filter; import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.Presence.Type; +import org.jivesoftware.smack.util.Objects; /** * A filter for Presence types. Returns true only if the stanza is an Presence packet and it matches the type provided in the @@ -38,11 +39,16 @@ public class PresenceTypeFilter extends FlexiblePacketTypeFilter { private PresenceTypeFilter(Presence.Type type) { super(Presence.class); - this.type = type; + this.type = Objects.requireNonNull(type, "type must not be null"); } @Override protected boolean acceptSpecific(Presence presence) { return presence.getType() == type; } + + @Override + public String toString() { + return getClass().getSimpleName() + ": type=" + type; + } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/ThreadFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/ThreadFilter.java index e053d364b..da1c3d7dd 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/ThreadFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/ThreadFilter.java @@ -17,7 +17,6 @@ package org.jivesoftware.smack.filter; -import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.util.StringUtils; @@ -26,7 +25,7 @@ import org.jivesoftware.smack.util.StringUtils; * * @author Matt Tucker */ -public class ThreadFilter implements PacketFilter { +public class ThreadFilter extends FlexiblePacketTypeFilter implements PacketFilter { private final String thread; @@ -40,7 +39,13 @@ public class ThreadFilter implements PacketFilter { this.thread = thread; } - public boolean accept(Stanza packet) { - return packet instanceof Message && thread.equals(((Message) packet).getThread()); + @Override + protected boolean acceptSpecific(Message message) { + return thread.equals(message.getThread()); + } + + @Override + public String toString() { + return getClass().getSimpleName() + ": thread=" + thread; } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/filter/ToFilter.java b/smack-core/src/main/java/org/jivesoftware/smack/filter/ToFilter.java index cfb0d3ed4..a691537b2 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/filter/ToFilter.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/filter/ToFilter.java @@ -38,4 +38,8 @@ public class ToFilter implements PacketFilter { return packetTo.equals(to); } + @Override + public String toString() { + return getClass().getSimpleName() + ": to=" + to; + } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator.java index b93850279..25ff808c4 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator.java @@ -175,11 +175,8 @@ public class FaultTolerantNegotiator extends StreamNegotiator { this.collector = collector; } - public InputStream call() throws XMPPErrorException, InterruptedException, SmackException { - Stanza streamInitiation = collector.nextResult(); - if (streamInitiation == null) { - throw new NoResponseException(connection); - } + public InputStream call() throws XMPPErrorException, InterruptedException, NoResponseException, SmackException { + Stanza streamInitiation = collector.nextResultOrThrow(); StreamNegotiator negotiator = determineNegotiator(streamInitiation); return negotiator.negotiateIncomingStream(streamInitiation); }