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:

```
<iq (xmlns='jabber:client) <!-- in parenthesis since the NS is implicit --> ... >
    <error xmlns='jabber:client' <!-- this NS is too much --> ... />
</iq>
``
This commit is contained in:
Florian Schmaus 2018-08-01 13:16:20 +02:00
parent 9596ca8943
commit a3fcbdbf5a
4 changed files with 18 additions and 6 deletions

View File

@ -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.

View File

@ -501,7 +501,7 @@ public final class Message extends Stanza implements TypedCloneable<Message> {
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.

View File

@ -292,7 +292,7 @@ public final class Presence extends Stanza implements TypedCloneable<Presence> {
buf.append(getExtensions(), enclosingNamespace);
// Add the error sub-packet, if there is one.
appendErrorIfExists(buf);
appendErrorIfExists(buf, enclosingNamespace);
buf.closeElement(ELEMENT);

View File

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