From a3fcbdbf5af531c6ead3b310b6b9d69e321269eb Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 1 Aug 2018 13:16:20 +0200 Subject: [PATCH] Make Stanza.appendErrorIfExists() aware of the enclosing XML namespace Originally discovered by Paul Schaub: Fixes an inconvenience, where an IQ with the implicit namespace `jabber:client` would append the namespace to an error child element like this: ``` ... /> `` --- .../java/org/jivesoftware/smack/packet/IQ.java | 16 ++++++++++++++-- .../org/jivesoftware/smack/packet/Message.java | 2 +- .../org/jivesoftware/smack/packet/Presence.java | 2 +- .../org/jivesoftware/smack/packet/Stanza.java | 4 ++-- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java index 92ef0a964..09fa9178e 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/IQ.java @@ -137,7 +137,7 @@ public abstract class IQ extends Stanza { buf.attribute("type", type.toString()); } buf.rightAngleBracket(); - buf.append(getChildElementXML()); + buf.append(getChildElementXML(enclosingNamespace)); buf.closeElement(IQ_ELEMENT); return buf; } @@ -149,10 +149,22 @@ public abstract class IQ extends Stanza { * @return the child element section of the IQ XML. */ public final XmlStringBuilder getChildElementXML() { + return getChildElementXML(null); + } + + /** + * Returns the sub-element XML section of the IQ packet, or the empty String if there + * isn't one. + * + * @param enclosingNamespace the enclosing XML namespace. + * @return the child element section of the IQ XML. + * @since 4.3.0 + */ + public final XmlStringBuilder getChildElementXML(String enclosingNamespace) { XmlStringBuilder xml = new XmlStringBuilder(); if (type == Type.error) { // Add the error sub-packet, if there is one. - appendErrorIfExists(xml); + appendErrorIfExists(xml, enclosingNamespace); } else if (childElementName != null) { // Add the query section if there is one. diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java index cb87251c7..ee22034c5 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java @@ -501,7 +501,7 @@ public final class Message extends Stanza implements TypedCloneable { buf.optElement("thread", thread); // Append the error subpacket if the message type is an error. if (type == Type.error) { - appendErrorIfExists(buf); + appendErrorIfExists(buf, enclosingNamespace); } // Add extension elements, if any are defined. diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java index abf4e39c5..99a0b8ae1 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Presence.java @@ -292,7 +292,7 @@ public final class Presence extends Stanza implements TypedCloneable { buf.append(getExtensions(), enclosingNamespace); // Add the error sub-packet, if there is one. - appendErrorIfExists(buf); + appendErrorIfExists(buf, enclosingNamespace); buf.closeElement(ELEMENT); diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Stanza.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Stanza.java index 2dff229ab..e9c9072fa 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Stanza.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Stanza.java @@ -541,10 +541,10 @@ public abstract class Stanza implements TopLevelStreamElement { * * @param xml the XmlStringBuilder to append the error to. */ - protected void appendErrorIfExists(XmlStringBuilder xml) { + protected void appendErrorIfExists(XmlStringBuilder xml, String enclosingNamespace) { StanzaError error = getError(); if (error != null) { - xml.append(error.toXML()); + xml.append(error.toXML(enclosingNamespace)); } } }