From 5f13ece9bd75bc20e4ad21e26c81f48d9042810d Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Wed, 7 Feb 2018 22:02:51 +0100 Subject: [PATCH] Suggested improvements (SMACK-799) --- .../smackx/reference/ReferenceManager.java | 47 ------------- .../reference/element/ReferenceElement.java | 68 ++++++++++++++++--- .../reference/provider/ReferenceProvider.java | 11 ++- .../smackx/reference/ReferenceTest.java | 31 +++++---- 4 files changed, 85 insertions(+), 72 deletions(-) diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/reference/ReferenceManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/reference/ReferenceManager.java index a93bf6bad..d3bdfce12 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/reference/ReferenceManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/reference/ReferenceManager.java @@ -16,8 +16,6 @@ */ package org.jivesoftware.smackx.reference; -import java.util.ArrayList; -import java.util.List; import java.util.Map; import java.util.WeakHashMap; @@ -25,12 +23,7 @@ import org.jivesoftware.smack.ConnectionCreationListener; import org.jivesoftware.smack.Manager; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnectionRegistry; -import org.jivesoftware.smack.packet.ExtensionElement; -import org.jivesoftware.smack.packet.Stanza; import org.jivesoftware.smackx.disco.ServiceDiscoveryManager; -import org.jivesoftware.smackx.reference.element.ReferenceElement; - -import org.jxmpp.jid.BareJid; public final class ReferenceManager extends Manager { @@ -66,44 +59,4 @@ public final class ReferenceManager extends Manager { } return manager; } - - /** - * Add a reference to another users bare jid to a stanza. - * - * @param stanza stanza. - * @param begin start index of the mention in the messages body. - * @param end end index of the mention in the messages body. - * @param jid referenced jid. - */ - public static void addMention(Stanza stanza, int begin, int end, BareJid jid) { - ReferenceElement reference = new ReferenceElement(begin, end, ReferenceElement.Type.mention, null, - "xmpp:" + jid.toString()); - stanza.addExtension(reference); - } - - /** - * Return a list of all reference extensions contained in a stanza. - * If there are no reference elements, return an empty list. - * - * @param stanza stanza - * @return list of all references contained in the stanza - */ - public static List getReferencesFromStanza(Stanza stanza) { - List references = new ArrayList<>(); - List extensions = stanza.getExtensions(ReferenceElement.ELEMENT, NAMESPACE); - for (ExtensionElement e : extensions) { - references.add((ReferenceElement) e); - } - return references; - } - - /** - * Return true, if the stanza contains at least one reference extension. - * - * @param stanza stanza - * @return true if stanza contains references - */ - public static boolean containsReferences(Stanza stanza) { - return getReferencesFromStanza(stanza).size() > 0; - } } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/reference/element/ReferenceElement.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/reference/element/ReferenceElement.java index e7de2645d..f3490d175 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/reference/element/ReferenceElement.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/reference/element/ReferenceElement.java @@ -16,10 +16,19 @@ */ package org.jivesoftware.smackx.reference.element; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; + import org.jivesoftware.smack.packet.ExtensionElement; +import org.jivesoftware.smack.packet.Stanza; +import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smackx.reference.ReferenceManager; +import org.jxmpp.jid.BareJid; + public class ReferenceElement implements ExtensionElement { public static final String ELEMENT = "reference"; @@ -38,7 +47,7 @@ public class ReferenceElement implements ExtensionElement { private final Integer end; private final Type type; private final String anchor; - private final String uri; + private final URI uri; // Non-XEP-compliant, but needed for SIMS private final ExtensionElement child; @@ -53,7 +62,7 @@ public class ReferenceElement implements ExtensionElement { * @param uri * @param child */ - public ReferenceElement(Integer begin, Integer end, Type type, String anchor, String uri, ExtensionElement child) { + public ReferenceElement(Integer begin, Integer end, Type type, String anchor, URI uri, ExtensionElement child) { if (begin != null && begin < 0) { throw new IllegalArgumentException("Attribute 'begin' MUST NOT be smaller than 0."); } @@ -63,9 +72,7 @@ public class ReferenceElement implements ExtensionElement { if (begin != null && end != null && begin >= end) { throw new IllegalArgumentException("Attribute 'begin' MUST be smaller than attribute 'end'."); } - if (type == null) { - throw new NullPointerException("Attribute 'type' MUST NOT be null."); - } + Objects.requireNonNull(type); // TODO: The uri attribute is not mandatory according to SIMS, but it is according to references. /*if (uri == null) { throw new NullPointerException("Attribute 'uri' MUST NOT be null."); @@ -87,7 +94,7 @@ public class ReferenceElement implements ExtensionElement { * @param anchor * @param uri */ - public ReferenceElement(Integer begin, Integer end, Type type, String anchor, String uri) { + public ReferenceElement(Integer begin, Integer end, Type type, String anchor, URI uri) { this(begin, end, type, anchor, uri, null); } @@ -107,10 +114,55 @@ public class ReferenceElement implements ExtensionElement { return anchor; } - public String getUri() { + public URI getUri() { return uri; } + /** + * Add a reference to another users bare jid to a stanza. + * + * @param stanza stanza. + * @param begin start index of the mention in the messages body. + * @param end end index of the mention in the messages body. + * @param jid referenced jid. + */ + public static void addMention(Stanza stanza, int begin, int end, BareJid jid) { + URI uri; + try { + uri = new URI("xmpp:" + jid.toString()); + } catch (URISyntaxException e) { + throw new AssertionError("Cannot create URI from bareJid."); + } + ReferenceElement reference = new ReferenceElement(begin, end, ReferenceElement.Type.mention, null, uri); + stanza.addExtension(reference); + } + + /** + * Return a list of all reference extensions contained in a stanza. + * If there are no reference elements, return an empty list. + * + * @param stanza stanza + * @return list of all references contained in the stanza + */ + public static List getReferencesFromStanza(Stanza stanza) { + List references = new ArrayList<>(); + List extensions = stanza.getExtensions(ReferenceElement.ELEMENT, ReferenceManager.NAMESPACE); + for (ExtensionElement e : extensions) { + references.add((ReferenceElement) e); + } + return references; + } + + /** + * Return true, if the stanza contains at least one reference extension. + * + * @param stanza stanza + * @return true if stanza contains references + */ + public static boolean containsReferences(Stanza stanza) { + return getReferencesFromStanza(stanza).size() > 0; + } + @Override public String getNamespace() { return ReferenceManager.NAMESPACE; @@ -128,7 +180,7 @@ public class ReferenceElement implements ExtensionElement { .optIntAttribute(ATTR_END, end != null ? end : -1) .attribute(ATTR_TYPE, type.toString()) .optAttribute(ATTR_ANCHOR, anchor) - .optAttribute(ATTR_URI, uri); + .optAttribute(ATTR_URI, uri != null ? uri.toString() : null); if (child == null) { return xml.closeEmptyElement(); diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/reference/provider/ReferenceProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/reference/provider/ReferenceProvider.java index 0b05d0e9b..92474a48c 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/reference/provider/ReferenceProvider.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/reference/provider/ReferenceProvider.java @@ -16,6 +16,8 @@ */ package org.jivesoftware.smackx.reference.provider; +import java.net.URI; + import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.provider.ExtensionElementProvider; import org.jivesoftware.smack.provider.ProviderManager; @@ -35,9 +37,10 @@ public class ReferenceProvider extends ExtensionElementProvider"; - ReferenceElement element = new ReferenceElement(72, 78, ReferenceElement.Type.mention, null, - "xmpp:juliet@capulet.lit"); + URI uri = new URI("xmpp:juliet@capulet.lit"); + ReferenceElement element = new ReferenceElement(72, 78, ReferenceElement.Type.mention, null, uri); assertXMLEqual(xml, element.toXML().toString()); assertEquals(72, (int) element.getBegin()); assertEquals(78, (int) element.getEnd()); assertEquals(ReferenceElement.Type.mention, element.getType()); assertNull(element.getAnchor()); - assertEquals("xmpp:juliet@capulet.lit", element.getUri()); + assertEquals(uri, element.getUri()); ReferenceElement parsed = ReferenceProvider.TEST_PROVIDER.parse(TestUtils.getParser(xml)); assertXMLEqual(xml, parsed.toXML().toString()); @@ -59,38 +62,38 @@ public class ReferenceTest extends SmackTestSuite { String xml = ""; - ReferenceElement element = new ReferenceElement(null, null, ReferenceElement.Type.data, null, - "xmpp:fdp.shakespeare.lit?;node=fdp/submitted/stan.isode.net/accidentreport;item=ndina872be"); + URI uri = new URI("xmpp:fdp.shakespeare.lit?;node=fdp/submitted/stan.isode.net/accidentreport;item=ndina872be"); + ReferenceElement element = new ReferenceElement(null, null, ReferenceElement.Type.data, null, uri); assertXMLEqual(xml, element.toXML().toString()); assertNull(element.getBegin()); assertNull(element.getEnd()); assertNull(element.getAnchor()); assertEquals(ReferenceElement.Type.data, element.getType()); - assertEquals("xmpp:fdp.shakespeare.lit?;node=fdp/submitted/stan.isode.net/accidentreport;item=ndina872be", element.getUri()); + assertEquals(uri, element.getUri()); ReferenceElement parsed = ReferenceProvider.TEST_PROVIDER.parse(TestUtils.getParser(xml)); assertXMLEqual(xml, parsed.toXML().toString()); } @Test(expected = IllegalArgumentException.class) - public void beginGreaterEndIllegalTest() { - new ReferenceElement(100, 10, ReferenceElement.Type.mention, null, "test@test.test"); + public void beginGreaterEndIllegalTest() throws URISyntaxException { + new ReferenceElement(100, 10, ReferenceElement.Type.mention, null, new URI("xmpp:test@test.test")); } @Test(expected = IllegalArgumentException.class) - public void beginSmallerZeroTest() { - new ReferenceElement(-1, 12, ReferenceElement.Type.data, null, "test@test.test"); + public void beginSmallerZeroTest() throws URISyntaxException { + new ReferenceElement(-1, 12, ReferenceElement.Type.data, null, new URI("xmpp:test@test.test")); } @Test(expected = IllegalArgumentException.class) - public void endSmallerZeroTest() { - new ReferenceElement(12, -2, ReferenceElement.Type.mention, null, "test@test.test"); + public void endSmallerZeroTest() throws URISyntaxException { + new ReferenceElement(12, -2, ReferenceElement.Type.mention, null, new URI("xmpp:test@test.test")); } @Test(expected = NullPointerException.class) - public void typeArgumentNullTest() { - new ReferenceElement(1, 2, null, null, "test@test.test"); + public void typeArgumentNullTest() throws URISyntaxException { + new ReferenceElement(1, 2, null, null, new URI("xmpp:test@test.test")); } /*