From d64a9d9029e20169cb215c1084cdb47bc5833643 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 12 Jan 2021 09:41:46 +0100 Subject: [PATCH 1/3] [xdata] Fix NPE in DataForm.Builder.addItem() Fixes SMACK-900. --- .../java/org/jivesoftware/smackx/xdata/packet/DataForm.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java index d56214d31..0457e69f3 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java @@ -494,6 +494,9 @@ public final class DataForm implements ExtensionElement { * @return a reference to this builder. */ public Builder addItem(Item item) { + if (items == null) { + items = new ArrayList<>(); + } items.add(item); return this; } From 84d73e962385cdf12cf2cc239189eda8e6a771bf Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 24 Jan 2021 19:23:18 +0100 Subject: [PATCH 2/3] [core] Fix XmlEnvironment namespace: Use default namespace (not element's) Assume the element then the element's namespace is 'foo-namespace', but the default namespace is 'namespace'. And this is the namespace that scopes into inner elements. --- .../smack/packet/XmlEnvironment.java | 4 +-- .../smack/xml/XmlPullParserTest.java | 33 ++++++++++++++++++- .../jivesoftware/smack/xml/XmlPullParser.java | 6 +++- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/XmlEnvironment.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/XmlEnvironment.java index cf4e8f9fd..4b6f76b8b 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/XmlEnvironment.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/XmlEnvironment.java @@ -1,6 +1,6 @@ /** * - * Copyright 2019 Florian Schmaus + * Copyright 2019-2021 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -133,7 +133,7 @@ public class XmlEnvironment { } public static XmlEnvironment from(XmlPullParser parser, XmlEnvironment outerXmlEnvironment) { - String namespace = parser.getNamespace(); + String namespace = parser.getDefaultNamespace(); String xmlLang = ParserUtils.getXmlLang(parser); return new XmlEnvironment(namespace, xmlLang, outerXmlEnvironment); } diff --git a/smack-core/src/test/java/org/jivesoftware/smack/xml/XmlPullParserTest.java b/smack-core/src/test/java/org/jivesoftware/smack/xml/XmlPullParserTest.java index ff82064c9..e78962ac2 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/xml/XmlPullParserTest.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/xml/XmlPullParserTest.java @@ -1,6 +1,6 @@ /** * - * Copyright 2019 Florian Schmaus + * Copyright 2019-2021 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,9 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import java.io.IOException; import java.io.Reader; import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; import java.util.stream.Stream; import javax.xml.XMLConstants; @@ -207,6 +210,34 @@ public class XmlPullParserTest { } } + @ParameterizedTest + @EnumSource(SmackTestUtil.XmlPullParserKind.class) + public void testGetNamespace(SmackTestUtil.XmlPullParserKind parserKind) + throws XmlPullParserException, IOException { + String xml = ""; + XmlPullParser parser = SmackTestUtil.getParserFor(xml, parserKind); + + String parsedName = parser.getName(); + assertEquals("bar", parsedName); + + String parsedPrefix = parser.getPrefix(); + assertEquals("foo", parsedPrefix); + + String parsedPrefixNamespace = parser.getNamespace(parsedPrefix); + assertEquals("foo-namespace", parsedPrefixNamespace); + + String parsedNamespace = parser.getNamespace(); + assertEquals("foo-namespace", parsedNamespace); + + List> defaultNamespaceRetrievers = new ArrayList<>(); + defaultNamespaceRetrievers.add(p -> p.getNamespace(null)); + defaultNamespaceRetrievers.add(p -> p.getDefaultNamespace()); + for (Function defaultNamespaceRetriever : defaultNamespaceRetrievers) { + String defaultNamespace = defaultNamespaceRetriever.apply(parser); + assertEquals("namespace", defaultNamespace); + } + } + private static void assertAttributeHolds(XmlPullParser parser, int attributeIndex, String expectedLocalpart, String expectedPrefix, String expectedNamespace) { QName qname = parser.getAttributeQName(attributeIndex); diff --git a/smack-xmlparser/src/main/java/org/jivesoftware/smack/xml/XmlPullParser.java b/smack-xmlparser/src/main/java/org/jivesoftware/smack/xml/XmlPullParser.java index ebb8f07e9..c49ac66ae 100644 --- a/smack-xmlparser/src/main/java/org/jivesoftware/smack/xml/XmlPullParser.java +++ b/smack-xmlparser/src/main/java/org/jivesoftware/smack/xml/XmlPullParser.java @@ -1,6 +1,6 @@ /** * - * Copyright 2019 Florian Schmaus. + * Copyright 2019-2021 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -75,6 +75,10 @@ public interface XmlPullParser { String getNamespace(String prefix); + default String getDefaultNamespace() { + return getNamespace(null); + } + int getDepth(); String getPositionDescription(); From 9ab50c374deff4aac968769efedf23676907bacc Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 25 Jan 2021 14:29:21 +0100 Subject: [PATCH 3/3] [extensions] Use StanzaView as parameter type in BoBDataExtension.from() BoBDataExtensions can also appear in IQs (Registration IQ), and potentially also Presence stanzas. Fixes SMACK-901. --- .../jivesoftware/smackx/bob/element/BoBDataExtension.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBDataExtension.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBDataExtension.java index 7b7f81709..1bea87b82 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBDataExtension.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBDataExtension.java @@ -1,6 +1,6 @@ /** * - * Copyright 2020 Florian Schmaus + * Copyright 2020-2021 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,7 @@ package org.jivesoftware.smackx.bob.element; import org.jivesoftware.smack.packet.ExtensionElement; -import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.packet.StanzaView; import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smackx.bob.BoBData; @@ -94,8 +94,8 @@ public class BoBDataExtension implements ExtensionElement { return xml; } - public static BoBDataExtension from(Message message) { - return message.getExtension(BoBDataExtension.class); + public static BoBDataExtension from(StanzaView stanza) { + return stanza.getExtension(BoBDataExtension.class); } }