From cbb477ebebb1f253209f5512d5c89fe4afb97b17 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 23 Jan 2015 20:12:21 +0100 Subject: [PATCH] Add AbstractXMPPConnection.setReplyToUnkownIq(boolean) and some javadoc related to IQ request handlers. --- .../smack/AbstractXMPPConnection.java | 37 ++++++++++++++++--- .../iqrequest/AbstractIqRequestHandler.java | 3 ++ .../smack/iqrequest/IQRequestHandler.java | 6 +++ 3 files changed, 41 insertions(+), 5 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 48fca605f..fef413dd3 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java @@ -881,6 +881,32 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { packetReplyTimeout = timeout; } + private static boolean replyToUnknownIqDefault = true; + + /** + * Set the default value used to determine if new connection will reply to unknown IQ requests. The pre-configured + * default is 'true'. + * + * @param replyToUnkownIqDefault + * @see #setReplyToUnknownIq(boolean) + */ + public static void setReplyToUnknownIqDefault(boolean replyToUnkownIqDefault) { + AbstractXMPPConnection.replyToUnknownIqDefault = replyToUnkownIqDefault; + } + + private boolean replyToUnkownIq = replyToUnknownIqDefault; + + /** + * Set if Smack will automatically send + * {@link org.jivesoftware.smack.packet.XMPPError.Condition#feature_not_implemented} when a request IQ without a + * registered {@link IQRequestHandler} is received. + * + * @param replyToUnknownIq + */ + public void setReplyToUnknownIq(boolean replyToUnknownIq) { + this.replyToUnkownIq = replyToUnknownIq; + } + protected void parseAndProcessStanza(XmlPullParser parser) throws Exception { ParserUtils.assertAtStartTag(parser); int parserDepth = parser.getDepth(); @@ -933,7 +959,6 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { } } - /** * Invoke {@link PacketCollector#processPacket(Packet)} for every * PacketCollector with the given packet. Also notify the receive listeners with a matching packet filter about the packet. @@ -964,6 +989,9 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { throw new IllegalStateException("Should only encounter IQ type 'get' or 'set'"); } if (iqRequestHandler == null) { + if (!replyToUnkownIq) { + return; + } // If the IQ stanza is of type "get" or "set" with no registered IQ request handler, then answer an // IQ of type "error" with code 501 ("feature-not-implemented") ErrorIQ errorIQ = IQ.createErrorResponse(iq, new XMPPError( @@ -992,10 +1020,9 @@ public abstract class AbstractXMPPConnection implements XMPPConnection { if (response == null) { // It is not ideal if the IQ request handler does not return an IQ response, because RFC // 6120 ยง 8.1.2 does specify that a response is mandatory. But some APIs, mostly the - // file transfer one, does not always return a result, so we need to handle this case - // gracefully for now - // TODO Add a warning if response is null once all APIs using handleIQRequest return an - // IQ response. Later consider throwing an IllegalStateException + // file transfer one, does not always return a result, so we need to handle this case. + // Also sometimes a request handler may decide that it's better to not send a response, + // e.g. to avoid presence leaks. return; } try { diff --git a/smack-core/src/main/java/org/jivesoftware/smack/iqrequest/AbstractIqRequestHandler.java b/smack-core/src/main/java/org/jivesoftware/smack/iqrequest/AbstractIqRequestHandler.java index 6aee0fec0..da4cb66a0 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/iqrequest/AbstractIqRequestHandler.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/iqrequest/AbstractIqRequestHandler.java @@ -19,6 +19,9 @@ package org.jivesoftware.smack.iqrequest; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ.Type; +/** + * Convenience class to create IQ requests handlers. + */ public abstract class AbstractIqRequestHandler implements IQRequestHandler { private final String element; diff --git a/smack-core/src/main/java/org/jivesoftware/smack/iqrequest/IQRequestHandler.java b/smack-core/src/main/java/org/jivesoftware/smack/iqrequest/IQRequestHandler.java index b6f38b45d..59391cf98 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/iqrequest/IQRequestHandler.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/iqrequest/IQRequestHandler.java @@ -18,6 +18,12 @@ package org.jivesoftware.smack.iqrequest; import org.jivesoftware.smack.packet.IQ; +/** + * IQ request handler are responsible for handling incoming IQ requests. They can be registered with + * {@link org.jivesoftware.smack.XMPPConnection#registerIQRequestHandler(IQRequestHandler)}. + * + * @see AbstractIqRequestHandler + */ public interface IQRequestHandler { public enum Mode {