1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-25 21:42:07 +01:00

Add filter information to NoResponseException

in order to aid debugging errors. This made it necessary to override
'toString()' for all filters in Smack.
This commit is contained in:
Florian Schmaus 2015-02-19 12:11:19 +01:00
parent d415661e35
commit 887c6114b7
24 changed files with 210 additions and 91 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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);
}
}

View file

@ -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 <code>null</code>.
*/
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 {

View file

@ -189,7 +189,7 @@ public class SynchronizationPoint<E extends Exception> {
case Initial:
case NoResponse:
case RequestSent:
throw new NoResponseException(connection);
throw NoResponseException.newWith(connection);
default:
// Do nothing
break;

View file

@ -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<PacketFilter> filters;
/**
* Creates an empty filter.
*/
protected AbstractListFilter() {
filters = new ArrayList<PacketFilter>();
}
/**
* 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<PacketFilter>(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();
}
}

View file

@ -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<PacketFilter> 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<PacketFilter>();
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<PacketFilter>(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();
}
}

View file

@ -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<P extends Stanza> implements Pack
protected final Class<P> packetType;
public FlexiblePacketTypeFilter(Class<P> packetType) {
this.packetType = packetType;
this.packetType = Objects.requireNonNull(packetType, "Type must not be null");
}
@SuppressWarnings("unchecked")
@ -49,4 +50,12 @@ public abstract class FlexiblePacketTypeFilter<P extends Stanza> 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();
}
}

View file

@ -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;
}
}

View file

@ -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();
}
}

View file

@ -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();
}
}

View file

@ -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<IQ> {
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;
}
}

View file

@ -55,4 +55,8 @@ public class MessageTypeFilter extends FlexiblePacketTypeFilter<Message> {
return message.getType() == type;
}
@Override
public String toString() {
return getClass().getSimpleName() + ": type=" + type;
}
}

View file

@ -36,4 +36,8 @@ public class MessageWithBodiesFilter extends FlexiblePacketTypeFilter<Message> {
return !message.getBodies().isEmpty();
}
@Override
public String toString() {
return getClass().getSimpleName();
}
}

View file

@ -36,4 +36,8 @@ public class MessageWithSubjectFilter extends FlexiblePacketTypeFilter<Message>
return message.getSubject() != null;
}
@Override
public String toString() {
return getClass().getSimpleName();
}
}

View file

@ -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) {

View file

@ -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<PacketFilter> 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<PacketFilter>();
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<PacketFilter>(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();
}
}

View file

@ -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;
}
}

View file

@ -53,6 +53,6 @@ public class PacketIDFilter implements PacketFilter {
}
public String toString() {
return "PacketIDFilter by id: " + packetID;
return getClass().getSimpleName() + ": id=" + packetID;
}
}

View file

@ -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();
}
}

View file

@ -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<Presence> {
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;
}
}

View file

@ -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<Message> 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;
}
}

View file

@ -38,4 +38,8 @@ public class ToFilter implements PacketFilter {
return packetTo.equals(to);
}
@Override
public String toString() {
return getClass().getSimpleName() + ": to=" + to;
}
}

View file

@ -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);
}