From 7f851d806cb80636cb93548c2c1f7b2820bef7ae Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 30 Jul 2017 19:15:56 +0200 Subject: [PATCH] Use StandardExtensionElement in JingleProvider and some further minor jingle fixes: - deprecate getJingleTransport() in favor of getTransport() - Jingle.Builder now checks if the session ID is not empty --- .../StandardExtensionElementParserTest.java | 5 +- .../jingle/JingleTransportMethodManager.java | 2 +- .../smackx/jingle/element/Jingle.java | 1 + .../smackx/jingle/element/JingleContent.java | 11 ++ .../element/JingleContentDescription.java | 9 +- .../element/JingleContentTransport.java | 2 +- .../UnknownJingleContentDescription.java | 49 +++++++ .../UnknownJingleContentTransport.java | 61 +++++++++ .../jingle/provider/JingleProvider.java | 22 +++- .../transports/JingleTransportSession.java | 2 +- .../jingle_s5b/JingleS5BTransportSession.java | 4 +- .../JingleContentProviderManagerTest.java | 3 +- .../jingle/provider/JingleProviderTest.java | 122 ++++++++++++++++++ 13 files changed, 275 insertions(+), 18 deletions(-) create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/UnknownJingleContentDescription.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/UnknownJingleContentTransport.java rename smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/{ => provider}/JingleContentProviderManagerTest.java (95%) create mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/jingle/provider/JingleProviderTest.java diff --git a/smack-core/src/test/java/org/jivesoftware/smack/parsing/StandardExtensionElementParserTest.java b/smack-core/src/test/java/org/jivesoftware/smack/parsing/StandardExtensionElementParserTest.java index b00844092..8c38f08b4 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/parsing/StandardExtensionElementParserTest.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/parsing/StandardExtensionElementParserTest.java @@ -1,6 +1,6 @@ /** * - * Copyright 2015 Florian Schmaus. + * Copyright 2015-2017 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +46,9 @@ public class StandardExtensionElementParserTest { assertEquals("attr2-value", barNs2Element.getAttributeValue("attr2")); assertEquals("another-element-text", parsedElement.getFirstElement("another-element").getText()); + + String parsedElementString = parsedElement.toXML().toString(); + assertEquals(elementString, parsedElementString); } @Test diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleTransportMethodManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleTransportMethodManager.java index 19206384b..32c9648df 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleTransportMethodManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/JingleTransportMethodManager.java @@ -79,7 +79,7 @@ public final class JingleTransportMethodManager extends Manager { return null; } - JingleContentTransport transport = content.getJingleTransport(); + JingleContentTransport transport = content.getTransport(); if (transport == null) { return null; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/Jingle.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/Jingle.java index 3f7ac01c9..12c2e4d5e 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/Jingle.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/Jingle.java @@ -190,6 +190,7 @@ public final class Jingle extends IQ { } public Builder setSessionId(String sessionId) { + StringUtils.requireNotNullOrEmpty(sessionId, "Session ID must not be null or empty"); this.sid = sessionId; return this; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContent.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContent.java index d5a3741bc..d18005024 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContent.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContent.java @@ -110,8 +110,19 @@ public final class JingleContent implements NamedElement { * Returns an Iterator for the JingleTransports in the packet. * * @return an Iterator for the JingleTransports in the packet. + * @deprecated use {@link #getTransport()} instead. */ + @Deprecated public JingleContentTransport getJingleTransport() { + return getTransport(); + } + + /** + * Returns an Iterator for the JingleTransports in the packet. + * + * @return an Iterator for the JingleTransports in the packet. + */ + public JingleContentTransport getTransport() { return transport; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentDescription.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentDescription.java index 90bb96867..d7e5c7933 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentDescription.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentDescription.java @@ -20,6 +20,7 @@ import java.util.Collections; import java.util.List; import org.jivesoftware.smack.packet.ExtensionElement; +import org.jivesoftware.smack.packet.NamedElement; import org.jivesoftware.smack.util.XmlStringBuilder; /** @@ -30,9 +31,9 @@ public abstract class JingleContentDescription implements ExtensionElement { public static final String ELEMENT = "description"; - private final List payloads; + private final List payloads; - protected JingleContentDescription(List payloads) { + protected JingleContentDescription(List payloads) { if (payloads != null) { this.payloads = Collections.unmodifiableList(payloads); } @@ -46,7 +47,7 @@ public abstract class JingleContentDescription implements ExtensionElement { return ELEMENT; } - public List getJingleContentDescriptionChildren() { + public List getJingleContentDescriptionChildren() { return payloads; } @@ -55,7 +56,7 @@ public abstract class JingleContentDescription implements ExtensionElement { } @Override - public final XmlStringBuilder toXML() { + public XmlStringBuilder toXML() { XmlStringBuilder xml = new XmlStringBuilder(this); addExtraAttributes(xml); xml.rightAngleBracket(); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentTransport.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentTransport.java index a21f14163..5b641480f 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentTransport.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/JingleContentTransport.java @@ -66,7 +66,7 @@ public abstract class JingleContentTransport implements ExtensionElement { } @Override - public final XmlStringBuilder toXML() { + public XmlStringBuilder toXML() { XmlStringBuilder xml = new XmlStringBuilder(this); addExtraAttributes(xml); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/UnknownJingleContentDescription.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/UnknownJingleContentDescription.java new file mode 100644 index 000000000..1f4994fc6 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/UnknownJingleContentDescription.java @@ -0,0 +1,49 @@ +/** + * + * Copyright 2017 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.smackx.jingle.element; + +import org.jivesoftware.smack.packet.StandardExtensionElement; +import org.jivesoftware.smack.util.XmlStringBuilder; + +public final class UnknownJingleContentDescription extends JingleContentDescription { + + private final StandardExtensionElement standardExtensionElement; + + public UnknownJingleContentDescription(StandardExtensionElement standardExtensionElement) { + super(standardExtensionElement.getElements()); + this.standardExtensionElement = standardExtensionElement; + } + + @Override + public String getElementName() { + return standardExtensionElement.getElementName(); + } + + @Override + public String getNamespace() { + return standardExtensionElement.getNamespace(); + } + + @Override + public XmlStringBuilder toXML() { + return standardExtensionElement.toXML(); + } + + public StandardExtensionElement getStandardExtensionElement() { + return standardExtensionElement; + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/UnknownJingleContentTransport.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/UnknownJingleContentTransport.java new file mode 100644 index 000000000..8a98f6bae --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/element/UnknownJingleContentTransport.java @@ -0,0 +1,61 @@ +/** + * + * Copyright 2017 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.smackx.jingle.element; + +import java.util.List; + +import org.jivesoftware.smack.packet.StandardExtensionElement; +import org.jivesoftware.smack.util.XmlStringBuilder; + +public final class UnknownJingleContentTransport extends JingleContentTransport { + + private final StandardExtensionElement standardExtensionElement; + + public UnknownJingleContentTransport(StandardExtensionElement standardExtensionElement) { + super(null, null); + this.standardExtensionElement = standardExtensionElement; + } + + @Override + public String getElementName() { + return standardExtensionElement.getElementName(); + } + + @Override + public String getNamespace() { + return standardExtensionElement.getNamespace(); + } + + @Override + public XmlStringBuilder toXML() { + return standardExtensionElement.toXML(); + } + + @Override + public List getCandidates() { + throw new UnsupportedOperationException(); + } + + @Override + public JingleContentTransportInfo getInfo() { + throw new UnsupportedOperationException(); + } + + public StandardExtensionElement getStandardExtensionElement() { + return standardExtensionElement; + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/provider/JingleProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/provider/JingleProvider.java index b920f4a35..a040d0ffc 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/provider/JingleProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/provider/JingleProvider.java @@ -18,6 +18,8 @@ package org.jivesoftware.smackx.jingle.provider; import java.util.logging.Logger; +import org.jivesoftware.smack.packet.StandardExtensionElement; +import org.jivesoftware.smack.parsing.StandardExtensionElementProvider; import org.jivesoftware.smack.provider.IQProvider; import org.jivesoftware.smack.util.ParserUtils; @@ -28,6 +30,8 @@ import org.jivesoftware.smackx.jingle.element.JingleContentDescription; import org.jivesoftware.smackx.jingle.element.JingleContentTransport; import org.jivesoftware.smackx.jingle.element.JingleReason; import org.jivesoftware.smackx.jingle.element.JingleReason.Reason; +import org.jivesoftware.smackx.jingle.element.UnknownJingleContentDescription; +import org.jivesoftware.smackx.jingle.element.UnknownJingleContentTransport; import org.jxmpp.jid.FullJid; import org.xmlpull.v1.XmlPullParser; @@ -122,22 +126,28 @@ public class JingleProvider extends IQProvider { String namespace = parser.getNamespace(); switch (tagName) { case JingleContentDescription.ELEMENT: { + JingleContentDescription description; JingleContentDescriptionProvider provider = JingleContentProviderManager.getJingleContentDescriptionProvider(namespace); if (provider == null) { - // TODO handle this case (DefaultExtensionElement wrapped in something?) - break; + StandardExtensionElement standardExtensionElement = StandardExtensionElementProvider.INSTANCE.parse(parser); + description = new UnknownJingleContentDescription(standardExtensionElement); + } + else { + description = provider.parse(parser); } - JingleContentDescription description = provider.parse(parser); builder.setDescription(description); break; } case JingleContentTransport.ELEMENT: { + JingleContentTransport transport; JingleContentTransportProvider provider = JingleContentProviderManager.getJingleContentTransportProvider(namespace); if (provider == null) { - // TODO handle this case (DefaultExtensionElement wrapped in something?) - break; + StandardExtensionElement standardExtensionElement = StandardExtensionElementProvider.INSTANCE.parse(parser); + transport = new UnknownJingleContentTransport(standardExtensionElement); + } + else { + transport = provider.parse(parser); } - JingleContentTransport transport = provider.parse(parser); builder.setTransport(transport); break; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/JingleTransportSession.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/JingleTransportSession.java index fab36419d..0776bc6b2 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/JingleTransportSession.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/JingleTransportSession.java @@ -41,7 +41,7 @@ public abstract class JingleTransportSession { } JingleContent content = jingle.getContents().get(0); - JingleContentTransport t = content.getJingleTransport(); + JingleContentTransport t = content.getTransport(); if (t != null && t.getNamespace().equals(getNamespace())) { setTheirProposal(t); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_s5b/JingleS5BTransportSession.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_s5b/JingleS5BTransportSession.java index ba03e1d58..a74bc276e 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_s5b/JingleS5BTransportSession.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jingle/transports/jingle_s5b/JingleS5BTransportSession.java @@ -189,7 +189,7 @@ public class JingleS5BTransportSession extends JingleTransportSession" + + "" + + "1969-07-21T02:56:15Z" + + "This is a test. If this were a real file..." + + "text/plain" + + "test.txt" + + "" + + "6144" + + "w0mcJylzCn+AfvuGdqkty2+KP48=" + + "" + + ""; + // @formatter:on + XmlPullParser parser = createTestJingle(unknownJingleContentDescription); + Jingle jingle = (Jingle) PacketParserUtils.parseIQ(parser); + + JingleContentDescription jingleContentDescription = jingle.getSoleContentOrThrow().getDescription(); + + String parsedUnknownJingleContentDescrptionNamespace = jingleContentDescription.getNamespace(); + assertEquals(unknownJingleContentDescriptionNamespace, parsedUnknownJingleContentDescrptionNamespace); + } + + @Test + public void testParseUnknownJingleContentTransport() throws Exception { + final String unknownJingleContentTransportNamespace = "urn:xmpp:jingle:unknown-transport:foo:1"; + final String unknownJingleContentTransport = + // @formatter:off + "" + + "" + + "" + + ""; + // @formatter:on + XmlPullParser parser = createTestJingle(unknownJingleContentTransport); + Jingle jingle = (Jingle) PacketParserUtils.parseIQ(parser); + + JingleContentTransport jingleContentTransport = jingle.getSoleContentOrThrow().getTransport(); + + String parsedUnknownJingleContentTransportNamespace = jingleContentTransport.getNamespace(); + assertEquals(unknownJingleContentTransportNamespace, parsedUnknownJingleContentTransportNamespace); + } + + private static XmlPullParser createTestJingle(String... childs) throws XmlPullParserException, IOException { + StringBuilder sb = new StringBuilder(); + sb.append(// @formatter:off + "" + + "" + + "" + // @formatter:on + ); + for (String child : childs) { + sb.append(child); + } + sb.append(// @formatter:off + "" + + "" + + "" + // @formatter:on + ); + + String jingleStanza = sb.toString(); + + XmlPullParser parser = PacketParserUtils.getParserFor(jingleStanza); + return parser; + } +}