From c1b32f8e11e865aa95eb96f266a3cafe28fc3ee0 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 23 Sep 2020 17:23:33 +0200 Subject: [PATCH 1/5] [carbons] Throw SmackParsingException instead of IOException --- .../smackx/carbons/provider/CarbonManagerProvider.java | 3 +-- .../test/java/org/jivesoftware/smackx/carbons/CarbonTest.java | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/provider/CarbonManagerProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/provider/CarbonManagerProvider.java index e224d6e3d..a534e4749 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/provider/CarbonManagerProvider.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/provider/CarbonManagerProvider.java @@ -55,8 +55,7 @@ public class CarbonManagerProvider extends ExtensionElementProvider tag"); + throw new SmackParsingException("sent/received must contain exactly one element"); } return new CarbonExtension(dir, fwd); } diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/carbons/CarbonTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/carbons/CarbonTest.java index fb88f6e95..6f2836dff 100644 --- a/smack-experimental/src/test/java/org/jivesoftware/smackx/carbons/CarbonTest.java +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/carbons/CarbonTest.java @@ -19,9 +19,9 @@ package org.jivesoftware.smackx.carbons; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import java.io.IOException; import java.util.Properties; +import org.jivesoftware.smack.parsing.SmackParsingException; import org.jivesoftware.smack.test.util.SmackTestUtil; import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smack.xml.XmlPullParser; @@ -107,6 +107,6 @@ public class CarbonTest extends ExperimentalInitializerTest { .a("xmlns", "urn:xmpp:forwarded:0") .asString(outputProperties); - assertThrows(IOException.class, () -> SmackTestUtil.parse(control, CarbonManagerProvider.class, parserKind)); + assertThrows(SmackParsingException.class, () -> SmackTestUtil.parse(control, CarbonManagerProvider.class, parserKind)); } } From fe7d3bec3026c122eb75b371bac6202788fa4a5e Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 23 Sep 2020 17:39:53 +0200 Subject: [PATCH 2/5] Make Forwarded a generic type Fixes SMACK-821. --- .../smack/provider/AbstractProvider.java | 13 ++++- .../smackx/carbons/CarbonManager.java | 4 +- .../carbons/packet/CarbonExtension.java | 8 +-- .../provider/CarbonManagerProvider.java | 9 ++- .../jivesoftware/smackx/mam/MamManager.java | 6 +- .../smackx/mam/element/MamElements.java | 7 ++- .../mam/provider/MamResultProvider.java | 7 ++- .../smackx/carbons/CarbonTest.java | 3 +- .../smackx/mam/MamResultProviderTest.java | 8 +-- .../smackx/mam/QueryArchiveTest.java | 4 +- .../smackx/forward/packet/Forwarded.java | 56 +++++++++++++------ .../forward/provider/ForwardedProvider.java | 23 ++++++-- .../smackx/forward/ForwardedTest.java | 4 +- 13 files changed, 101 insertions(+), 51 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/provider/AbstractProvider.java b/smack-core/src/main/java/org/jivesoftware/smack/provider/AbstractProvider.java index 5eda3b494..2897ef820 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/provider/AbstractProvider.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/provider/AbstractProvider.java @@ -1,6 +1,6 @@ /** * - * Copyright 2019 Florian Schmaus + * Copyright 2019-2020 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,7 +36,16 @@ public class AbstractProvider { Type[] actualTypeArguments = parameterizedGenericSuperclass.getActualTypeArguments(); Type elementType = actualTypeArguments[0]; - elementClass = (Class) elementType; + + if (elementType instanceof Class) { + elementClass = (Class) elementType; + } else if (elementType instanceof ParameterizedType) { + ParameterizedType parameteriezedElementType = (ParameterizedType) elementType; + elementClass = (Class) parameteriezedElementType.getRawType(); + } else { + throw new AssertionError( + "Element type '" + elementType + "' is neither of type Class or ParameterizedType"); + } } public final Class getElementClass() { diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/CarbonManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/CarbonManager.java index e63d37452..196484c97 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/CarbonManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/CarbonManager.java @@ -132,8 +132,8 @@ public final class CarbonManager extends Manager { final Message wrappingMessage = (Message) stanza; final CarbonExtension carbonExtension = CarbonExtension.from(wrappingMessage); final Direction direction = carbonExtension.getDirection(); - final Forwarded forwarded = carbonExtension.getForwarded(); - final Message carbonCopy = (Message) forwarded.getForwardedStanza(); + final Forwarded forwarded = carbonExtension.getForwarded(); + final Message carbonCopy = forwarded.getForwardedStanza(); final BareJid from = carbonCopy.getFrom().asBareJid(); carbonsListenerAsyncButOrdered.performAsyncButOrdered(from, new Runnable() { diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/packet/CarbonExtension.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/packet/CarbonExtension.java index 2b7220ba1..028ec56b2 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/packet/CarbonExtension.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/packet/CarbonExtension.java @@ -1,6 +1,6 @@ /** * - * Copyright 2013-2014 Georg Lukas + * Copyright 2013-2014 Georg Lukas, 2020 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +38,7 @@ public class CarbonExtension implements ExtensionElement { public static final String NAMESPACE = Carbon.NAMESPACE; private final Direction dir; - private final Forwarded fwd; + private final Forwarded fwd; /** * Construct a Carbon message extension. @@ -46,7 +46,7 @@ public class CarbonExtension implements ExtensionElement { * @param dir Determines if the carbon is being sent/received * @param fwd The forwarded message. */ - public CarbonExtension(Direction dir, Forwarded fwd) { + public CarbonExtension(Direction dir, Forwarded fwd) { this.dir = dir; this.fwd = fwd; } @@ -65,7 +65,7 @@ public class CarbonExtension implements ExtensionElement { * * @return the {@link Forwarded} message contained in this Carbon. */ - public Forwarded getForwarded() { + public Forwarded getForwarded() { return fwd; } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/provider/CarbonManagerProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/provider/CarbonManagerProvider.java index a534e4749..a51827f2d 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/provider/CarbonManagerProvider.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/carbons/provider/CarbonManagerProvider.java @@ -1,6 +1,6 @@ /** * - * Copyright 2013-2014 Georg Lukas + * Copyright 2013-2014 Georg Lukas, 2020 Florian Schmaus. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.jivesoftware.smackx.carbons.provider; import java.io.IOException; +import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.XmlEnvironment; import org.jivesoftware.smack.parsing.SmackParsingException; import org.jivesoftware.smack.provider.ExtensionElementProvider; @@ -38,18 +39,16 @@ import org.jivesoftware.smackx.forward.provider.ForwardedProvider; */ public class CarbonManagerProvider extends ExtensionElementProvider { - private static final ForwardedProvider FORWARDED_PROVIDER = new ForwardedProvider(); - @Override public CarbonExtension parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException { Direction dir = Direction.valueOf(parser.getName()); - Forwarded fwd = null; + Forwarded fwd = null; boolean done = false; while (!done) { XmlPullParser.Event eventType = parser.next(); if (eventType == XmlPullParser.Event.START_ELEMENT && parser.getName().equals("forwarded")) { - fwd = FORWARDED_PROVIDER.parse(parser); + fwd = ForwardedProvider.parseForwardedMessage(parser, xmlEnvironment); } else if (eventType == XmlPullParser.Event.END_ELEMENT && dir == Direction.valueOf(parser.getName())) done = true; diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/MamManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/MamManager.java index 3398df23c..2d87daba6 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/MamManager.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/MamManager.java @@ -632,7 +632,7 @@ public final class MamManager extends Manager { private final MamFinIQ mamFin; private final List mamResultCarrierMessages; private final List mamResultExtensions; - private final List forwardedMessages; + private final List> forwardedMessages; private final List messages; private MamQueryPage(StanzaCollector stanzaCollector, MamFinIQ mamFin) { @@ -642,7 +642,7 @@ public final class MamManager extends Manager { List mamResultCarrierMessages = new ArrayList<>(mamResultCarrierStanzas.size()); List mamResultExtensions = new ArrayList<>(mamResultCarrierStanzas.size()); - List forwardedMessages = new ArrayList<>(mamResultCarrierStanzas.size()); + List> forwardedMessages = new ArrayList<>(mamResultCarrierStanzas.size()); for (Stanza mamResultStanza : mamResultCarrierStanzas) { Message resultMessage = (Message) mamResultStanza; @@ -665,7 +665,7 @@ public final class MamManager extends Manager { return messages; } - public List getForwarded() { + public List> getForwarded() { return forwardedMessages; } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamElements.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamElements.java index 2423b0c1e..24c0753a6 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamElements.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/element/MamElements.java @@ -22,6 +22,7 @@ import javax.xml.namespace.QName; import org.jivesoftware.smack.packet.Element; import org.jivesoftware.smack.packet.ExtensionElement; +import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.MessageView; import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smack.util.XmlStringBuilder; @@ -69,7 +70,7 @@ public class MamElements { /** * the forwarded element. */ - private final Forwarded forwarded; + private final Forwarded forwarded; /** * the query id. @@ -83,7 +84,7 @@ public class MamElements { * @param id TODO javadoc me please * @param forwarded TODO javadoc me please */ - public MamResultExtension(String queryId, String id, Forwarded forwarded) { + public MamResultExtension(String queryId, String id, Forwarded forwarded) { if (StringUtils.isEmpty(id)) { throw new IllegalArgumentException("id must not be null or empty"); } @@ -109,7 +110,7 @@ public class MamElements { * * @return the forwarded element */ - public Forwarded getForwarded() { + public Forwarded getForwarded() { return forwarded; } diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamResultProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamResultProvider.java index 61d07edf2..7999a0e13 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamResultProvider.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/mam/provider/MamResultProvider.java @@ -1,6 +1,6 @@ /** * - * Copyright 2016 Fernando Ramirez + * Copyright 2016 Fernando Ramirez, 2020 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ package org.jivesoftware.smackx.mam.provider; import java.io.IOException; +import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.XmlEnvironment; import org.jivesoftware.smack.parsing.SmackParsingException; import org.jivesoftware.smack.provider.ExtensionElementProvider; @@ -40,7 +41,7 @@ public class MamResultProvider extends ExtensionElementProvider forwarded = null; String queryId = parser.getAttributeValue("", "queryid"); String id = parser.getAttributeValue("", "id"); @@ -51,7 +52,7 @@ public class MamResultProvider extends ExtensionElementProvider fwd; control = XMLBuilder.create("sent") .e("forwarded") diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/MamResultProviderTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/MamResultProviderTest.java index f7a0a28b4..dcc6939b0 100644 --- a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/MamResultProviderTest.java +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/MamResultProviderTest.java @@ -59,10 +59,10 @@ public class MamResultProviderTest { calendar.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = calendar.getTime(); - Forwarded forwarded = mamResultExtension.getForwarded(); + Forwarded forwarded = mamResultExtension.getForwarded(); assertEquals(forwarded.getDelayInformation().getStamp(), date); - Message message = (Message) forwarded.getForwardedStanza(); + Message message = forwarded.getForwardedStanza(); assertEquals(message.getFrom().toString(), "romeo@montague.lit/orchard"); assertEquals(message.getTo().toString(), "juliet@capulet.lit/balcony"); assertEquals(message.getBody(), @@ -81,10 +81,10 @@ public class MamResultProviderTest { calendar.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = calendar.getTime(); - Forwarded forwarded = mamResultExtension.getForwarded(); + Forwarded forwarded = mamResultExtension.getForwarded(); assertEquals(forwarded.getDelayInformation().getStamp(), date); - Message forwardedMessage = (Message) forwarded.getForwardedStanza(); + Message forwardedMessage = forwarded.getForwardedStanza(); assertEquals(forwardedMessage.getFrom().toString(), "witch@shakespeare.lit"); assertEquals(forwardedMessage.getTo().toString(), "macbeth@shakespeare.lit"); assertEquals(forwardedMessage.getBody(), "Hail to thee"); diff --git a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/QueryArchiveTest.java b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/QueryArchiveTest.java index 1603fb99f..54e2caadf 100644 --- a/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/QueryArchiveTest.java +++ b/smack-experimental/src/test/java/org/jivesoftware/smackx/mam/QueryArchiveTest.java @@ -79,7 +79,7 @@ public class QueryArchiveTest extends MamTest { .setBody("Thrice the brinded cat hath mew.") .build(); - Forwarded forwarded = new Forwarded(delay, forwardedMessage); + Forwarded forwarded = new Forwarded<>(forwardedMessage, delay); message.addExtension(new MamResultExtension("g27", "34482-21985-73620", forwarded)); @@ -90,7 +90,7 @@ public class QueryArchiveTest extends MamTest { assertEquals(mamResultExtension.getId(), "34482-21985-73620"); assertEquals(mamResultExtension.getForwarded().getDelayInformation().getStamp(), date); - Message resultMessage = (Message) mamResultExtension.getForwarded().getForwardedStanza(); + Message resultMessage = mamResultExtension.getForwarded().getForwardedStanza(); assertEquals(resultMessage.getFrom(), JidCreate.from("coven@chat.shakespeare.lit/firstwitch")); assertEquals(resultMessage.getStanzaId(), "162BEBB1-F6DB-4D9A-9BD8-CFDCC801A0B2"); assertEquals(resultMessage.getType(), Type.chat); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/packet/Forwarded.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/packet/Forwarded.java index f3887d758..a65559e01 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/packet/Forwarded.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/packet/Forwarded.java @@ -1,6 +1,6 @@ /** * - * Copyright 2013-2014 Georg Lukas + * Copyright 2013-2014 Georg Lukas, 2020 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import javax.xml.namespace.QName; import org.jivesoftware.smack.packet.ExtensionElement; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Stanza; +import org.jivesoftware.smack.util.Objects; import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smackx.delay.packet.DelayInformation; @@ -35,23 +36,24 @@ import org.jivesoftware.smackx.delay.packet.DelayInformation; * @author Georg Lukas * @see XEP-0297: Stanza Forwarding */ -public class Forwarded implements ExtensionElement { +public class Forwarded implements ExtensionElement { public static final String NAMESPACE = "urn:xmpp:forward:0"; public static final String ELEMENT = "forwarded"; public static final QName QNAME = new QName(NAMESPACE, ELEMENT); private final DelayInformation delay; - private final Stanza forwardedPacket; + private final S forwardedStanza; /** * Creates a new Forwarded stanza extension. * * @param delay an optional {@link DelayInformation} timestamp of the packet. - * @param fwdPacket the stanza that is forwarded (required). + * @param forwardedStanza the stanza that is forwarded (required). + * @deprecated use {@link #Forwarded(Stanza, DelayInformation)} instead. */ - public Forwarded(DelayInformation delay, Stanza fwdPacket) { - this.delay = delay; - this.forwardedPacket = fwdPacket; + @Deprecated + public Forwarded(DelayInformation delay, S forwardedStanza) { + this(forwardedStanza, delay); } /** @@ -59,8 +61,19 @@ public class Forwarded implements ExtensionElement { * * @param fwdPacket the stanza that is forwarded (required). */ - public Forwarded(Stanza fwdPacket) { - this(null, fwdPacket); + public Forwarded(S fwdPacket) { + this(fwdPacket, null); + } + + /** + * Creates a new Forwarded stanza extension. + * + * @param forwardedStanza the stanza that is forwarded (required). + * @param delay an optional {@link DelayInformation} timestamp of the packet. + */ + public Forwarded(S forwardedStanza, DelayInformation delay) { + this.forwardedStanza = Objects.requireNonNull(forwardedStanza); + this.delay = delay; } @Override @@ -78,7 +91,7 @@ public class Forwarded implements ExtensionElement { XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace); xml.rightAngleBracket(); xml.optElement(getDelayInformation()); - xml.append(forwardedPacket); + xml.append(forwardedStanza); xml.closeElement(this); return xml; } @@ -88,8 +101,8 @@ public class Forwarded implements ExtensionElement { * * @return the {@link Stanza} (typically a message) that was forwarded. */ - public Stanza getForwardedStanza() { - return forwardedPacket; + public S getForwardedStanza() { + return forwardedStanza; } /** @@ -101,12 +114,23 @@ public class Forwarded implements ExtensionElement { return delay; } + /** + * Check if this is forwarding a stanza of the provided class. + * + * @param stanzaClass the class to check for. + * @return true if this is forwarding a stanza of the provided class. + * @since 4.4 + */ + public boolean isForwarded(Class stanzaClass) { + return stanzaClass.isAssignableFrom(forwardedStanza.getClass()); + } + /** * Get the forwarded extension. * @param packet TODO javadoc me please * @return the Forwarded extension or null */ - public static Forwarded from(Stanza packet) { + public static Forwarded from(Stanza packet) { return packet.getExtension(Forwarded.class); } @@ -118,10 +142,10 @@ public class Forwarded implements ExtensionElement { * @return a list a the extracted messages. * @since 4.3.0 */ - public static List extractMessagesFrom(Collection forwardedCollection) { + public static List extractMessagesFrom(Collection> forwardedCollection) { List res = new ArrayList<>(forwardedCollection.size()); - for (Forwarded forwarded : forwardedCollection) { - Message message = (Message) forwarded.forwardedPacket; + for (Forwarded forwarded : forwardedCollection) { + Message message = forwarded.getForwardedStanza(); res.add(message); } return res; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/provider/ForwardedProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/provider/ForwardedProvider.java index e8bd8b4fe..e81c06c5e 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/provider/ForwardedProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/provider/ForwardedProvider.java @@ -1,6 +1,6 @@ /** * - * Copyright 2013-2014 Georg Lukas + * Copyright 2013-2014 Georg Lukas, 2020 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,14 +38,14 @@ import org.jivesoftware.smackx.forward.packet.Forwarded; * * @author Georg Lukas */ -public class ForwardedProvider extends ExtensionElementProvider { +public class ForwardedProvider extends ExtensionElementProvider> { public static final ForwardedProvider INSTANCE = new ForwardedProvider(); private static final Logger LOGGER = Logger.getLogger(ForwardedProvider.class.getName()); @Override - public Forwarded parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException { + public Forwarded parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException { DelayInformation di = null; Stanza packet = null; @@ -86,6 +86,21 @@ public class ForwardedProvider extends ExtensionElementProvider { // TODO: Should be SmackParseException. throw new IOException("forwarded extension must contain a packet"); } - return new Forwarded(di, packet); + return new Forwarded<>(packet, di); + } + + public static Forwarded parseForwardedMessage(XmlPullParser parser, XmlEnvironment xmlEnvironment) + throws XmlPullParserException, IOException, SmackParsingException { + return parseForwardedMessage(parser, parser.getDepth(), xmlEnvironment); + } + + @SuppressWarnings("unchecked") + public static Forwarded parseForwardedMessage(XmlPullParser parser, int initialDepth, + XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException { + Forwarded forwarded = INSTANCE.parse(parser, initialDepth, xmlEnvironment); + if (!forwarded.isForwarded(Message.class)) { + throw new SmackParsingException("Expecting a forwarded message, but got " + forwarded); + } + return (Forwarded) forwarded; } } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/forward/ForwardedTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/forward/ForwardedTest.java index a7a8bc553..d303d85ed 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/forward/ForwardedTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/forward/ForwardedTest.java @@ -45,7 +45,7 @@ public class ForwardedTest { public void forwardedTest() throws Exception { XmlPullParser parser; String control; - Forwarded fwd; + Forwarded fwd; control = XMLBuilder.create("forwarded") .a("xmlns", "urn:xmpp:forwarded:0") @@ -71,7 +71,7 @@ public class ForwardedTest { public void forwardedWithDelayTest() throws Exception { XmlPullParser parser; String control; - Forwarded fwd; + Forwarded fwd; // @formatter:off control = XMLBuilder.create("forwarded").a("xmlns", "urn:xmpp:forwarded:0") From 048226960b9360d85089a9f3c973ea8b0446efbf Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 23 Sep 2020 17:50:11 +0200 Subject: [PATCH 3/5] Rename smack-java7 to smack-java8 Fixes SMACK-854. --- build.gradle | 2 +- settings.gradle | 2 +- smack-java8-full/build.gradle | 2 +- {smack-java7 => smack-java8}/build.gradle | 0 .../org/jivesoftware/smack/java7/Java7SmackInitializer.java | 2 +- .../java/org/jivesoftware/smack/java7/XmppHostnameVerifier.java | 0 .../main/java/org/jivesoftware/smack/java7/package-info.java | 0 .../smack/util/stringencoder/java7/Java7Base64Encoder.java | 0 .../util/stringencoder/java7/Java7Base64UrlSafeEncoder.java | 0 .../smack/util/stringencoder/java7/package-info.java | 0 .../org/jivesoftware/smack/util/stringencoder/package-info.java | 0 11 files changed, 4 insertions(+), 4 deletions(-) rename {smack-java7 => smack-java8}/build.gradle (100%) rename {smack-java7 => smack-java8}/src/main/java/org/jivesoftware/smack/java7/Java7SmackInitializer.java (96%) rename {smack-java7 => smack-java8}/src/main/java/org/jivesoftware/smack/java7/XmppHostnameVerifier.java (100%) rename {smack-java7 => smack-java8}/src/main/java/org/jivesoftware/smack/java7/package-info.java (100%) rename {smack-java7 => smack-java8}/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64Encoder.java (100%) rename {smack-java7 => smack-java8}/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64UrlSafeEncoder.java (100%) rename {smack-java7 => smack-java8}/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/package-info.java (100%) rename {smack-java7 => smack-java8}/src/main/java/org/jivesoftware/smack/util/stringencoder/package-info.java (100%) diff --git a/build.gradle b/build.gradle index 47e8f24f6..a39bc7439 100644 --- a/build.gradle +++ b/build.gradle @@ -62,7 +62,7 @@ allprojects { ':smack-bosh', ':smack-debug', ':smack-debug-slf4j', - ':smack-java7', + ':smack-java8', ':smack-jingle-old', ':smack-resolver-dnsjava', ':smack-resolver-javax', diff --git a/settings.gradle b/settings.gradle index 875a6e88c..42e83712c 100644 --- a/settings.gradle +++ b/settings.gradle @@ -22,7 +22,7 @@ include 'smack-core', 'smack-bosh', 'smack-android', 'smack-android-extensions', - 'smack-java7', + 'smack-java8', 'smack-java8-full', 'smack-integration-test', 'smack-omemo', diff --git a/smack-java8-full/build.gradle b/smack-java8-full/build.gradle index 8586a0005..5292c5cff 100644 --- a/smack-java8-full/build.gradle +++ b/smack-java8-full/build.gradle @@ -6,7 +6,7 @@ dependencies { api project(':smack-debug') api project(':smack-experimental') api project(':smack-extensions') - api project(':smack-java7') + api project(':smack-java8') api project(':smack-legacy') api project(':smack-omemo') api project(':smack-openpgp') diff --git a/smack-java7/build.gradle b/smack-java8/build.gradle similarity index 100% rename from smack-java7/build.gradle rename to smack-java8/build.gradle diff --git a/smack-java7/src/main/java/org/jivesoftware/smack/java7/Java7SmackInitializer.java b/smack-java8/src/main/java/org/jivesoftware/smack/java7/Java7SmackInitializer.java similarity index 96% rename from smack-java7/src/main/java/org/jivesoftware/smack/java7/Java7SmackInitializer.java rename to smack-java8/src/main/java/org/jivesoftware/smack/java7/Java7SmackInitializer.java index 8fbb8c644..e6e2a3d46 100644 --- a/smack-java7/src/main/java/org/jivesoftware/smack/java7/Java7SmackInitializer.java +++ b/smack-java8/src/main/java/org/jivesoftware/smack/java7/Java7SmackInitializer.java @@ -33,7 +33,7 @@ public class Java7SmackInitializer implements SmackInitializer { if (SystemUtil.onAndroid()) { // @formatter:off throw new RuntimeException( - "You need to remove the smack-java7 dependency/jar from your build, " + + "You need to remove the smack-java8 dependency/jar from your build, " + "as it does not run on Android. " + "Use smack-android instead."); // @formatter:on diff --git a/smack-java7/src/main/java/org/jivesoftware/smack/java7/XmppHostnameVerifier.java b/smack-java8/src/main/java/org/jivesoftware/smack/java7/XmppHostnameVerifier.java similarity index 100% rename from smack-java7/src/main/java/org/jivesoftware/smack/java7/XmppHostnameVerifier.java rename to smack-java8/src/main/java/org/jivesoftware/smack/java7/XmppHostnameVerifier.java diff --git a/smack-java7/src/main/java/org/jivesoftware/smack/java7/package-info.java b/smack-java8/src/main/java/org/jivesoftware/smack/java7/package-info.java similarity index 100% rename from smack-java7/src/main/java/org/jivesoftware/smack/java7/package-info.java rename to smack-java8/src/main/java/org/jivesoftware/smack/java7/package-info.java diff --git a/smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64Encoder.java b/smack-java8/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64Encoder.java similarity index 100% rename from smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64Encoder.java rename to smack-java8/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64Encoder.java diff --git a/smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64UrlSafeEncoder.java b/smack-java8/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64UrlSafeEncoder.java similarity index 100% rename from smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64UrlSafeEncoder.java rename to smack-java8/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/Java7Base64UrlSafeEncoder.java diff --git a/smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/package-info.java b/smack-java8/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/package-info.java similarity index 100% rename from smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/package-info.java rename to smack-java8/src/main/java/org/jivesoftware/smack/util/stringencoder/java7/package-info.java diff --git a/smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/package-info.java b/smack-java8/src/main/java/org/jivesoftware/smack/util/stringencoder/package-info.java similarity index 100% rename from smack-java7/src/main/java/org/jivesoftware/smack/util/stringencoder/package-info.java rename to smack-java8/src/main/java/org/jivesoftware/smack/util/stringencoder/package-info.java From 15e3d267f612f83f2a8ed9d2c444fd34a7dfcea9 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 20 Sep 2020 18:17:44 +0200 Subject: [PATCH 4/5] Add Pair utility class --- .../org/jivesoftware/smack/util/Pair.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 smack-core/src/main/java/org/jivesoftware/smack/util/Pair.java diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/Pair.java b/smack-core/src/main/java/org/jivesoftware/smack/util/Pair.java new file mode 100644 index 000000000..08bf41234 --- /dev/null +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/Pair.java @@ -0,0 +1,64 @@ +/** + * + * Copyright 2020 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.smack.util; + +public final class Pair { + + private final F first; + private final S second; + + private Pair(F first, S second) { + this.first = first; + this.second = second; + } + + public static Pair create(F first, S second) { + return new Pair<>(first, second); + } + + public static Pair createAndInitHashCode(F first, S second) { + Pair pair = new Pair<>(first, second); + pair.hashCode(); + return pair; + } + + public F getFirst() { + return first; + } + + public S getSecond() { + return second; + } + + private final HashCode.Cache hashCodeCache = new HashCode.Cache(); + + @Override + public int hashCode() { + return hashCodeCache.getHashCode(c -> + c.append(first) + .append(second) + ); + } + + @Override + public boolean equals(Object object) { + return EqualsUtil.equals(this, object, (e, o) -> + e.append(first, o.first) + .append(second, o.second) + ); + } +} From 6d39a4e3ac8abbef11b4a6f8f5d89363c2424980 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 23 Sep 2020 19:57:13 +0200 Subject: [PATCH 5/5] [bob] Add BoBDataExtension, remove BoBExtension BoBExtension extending XHTMLExtension was poorly designed and only worked for a single paragraphy. Fixes SMACK-770. --- .../org/jivesoftware/smackx/bob/BoBData.java | 23 +++-- .../org/jivesoftware/smackx/bob/BoBInfo.java | 6 +- .../jivesoftware/smackx/bob/BoBManager.java | 21 ++-- .../bob/{BoBHash.java => ContentId.java} | 31 +++--- .../smackx/bob/element/BoBDataExtension.java | 81 ++++++++++++++++ .../smackx/bob/element/BoBExtension.java | 97 ------------------- .../smackx/bob/element/BoBIQ.java | 36 ++++--- .../provider/BoBDataExtensionProvider.java | 41 ++++++++ .../smackx/bob/provider/BoBIQProvider.java | 22 +---- .../smackx/bob/provider/BoBProviderUtil.java | 48 +++++++++ .../smackx/xhtmlim/packet/XHTMLExtension.java | 2 +- .../extensions.providers | 6 ++ .../jivesoftware/smackx/bob/BoBIQTest.java | 8 +- 13 files changed, 254 insertions(+), 168 deletions(-) rename smack-extensions/src/main/java/org/jivesoftware/smackx/bob/{BoBHash.java => ContentId.java} (79%) create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBDataExtension.java delete mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBExtension.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBDataExtensionProvider.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBProviderUtil.java diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBData.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBData.java index 654e120d8..6ec45af34 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBData.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBData.java @@ -29,14 +29,19 @@ import org.jivesoftware.smack.util.stringencoder.Base64; */ public class BoBData { - private final int maxAge; + private final Integer maxAge; private final String type; private byte[] contentBinary; private String contentString; + private BoBData(String type, Integer maxAge) { + this.type = type; + this.maxAge = maxAge; + } + public BoBData(String type, byte[] content) { - this(type, content, -1); + this(type, content, null); } /** @@ -46,20 +51,18 @@ public class BoBData { * @param content TODO javadoc me please * @param maxAge TODO javadoc me please */ - public BoBData(String type, byte[] content, int maxAge) { - this.type = type; + public BoBData(String type, byte[] content, Integer maxAge) { + this(type, maxAge); this.contentBinary = content; - this.maxAge = maxAge; } public BoBData(String type, String content) { - this(type, content, -1); + this(type, content, null); } - public BoBData(String type, String content, int maxAge) { - this.type = type; + public BoBData(String type, String content, Integer maxAge) { + this(type, maxAge); this.contentString = content; - this.maxAge = maxAge; } /** @@ -67,7 +70,7 @@ public class BoBData { * * @return the max age */ - public int getMaxAge() { + public Integer getMaxAge() { return maxAge; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBInfo.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBInfo.java index a4db4aeae..becb5322f 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBInfo.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBInfo.java @@ -20,15 +20,15 @@ import java.util.Set; public class BoBInfo { - private final Set hashes; + private final Set hashes; private final BoBData data; - BoBInfo(Set hashes, BoBData data) { + BoBInfo(Set hashes, BoBData data) { this.hashes = hashes; this.data = data; } - public Set getHashes() { + public Set getHashes() { return hashes; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBManager.java index b3ac970d2..e368ab366 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBManager.java @@ -1,6 +1,6 @@ /** * - * Copyright 2016-2017 Fernando Ramirez, Florian Schmaus + * Copyright 2016-2020 Fernando Ramirez, Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -81,9 +81,9 @@ public final class BoBManager extends Manager { return bobManager; } - private static final LruCache BOB_CACHE = new LruCache<>(128); + private static final LruCache BOB_CACHE = new LruCache<>(128); - private final Map bobs = new ConcurrentHashMap<>(); + private final Map bobs = new ConcurrentHashMap<>(); private BoBManager(XMPPConnection connection) { super(connection); @@ -95,15 +95,16 @@ public final class BoBManager extends Manager { @Override public IQ handleIQRequest(IQ iqRequest) { BoBIQ bobIQRequest = (BoBIQ) iqRequest; + ContentId contentId = bobIQRequest.getContentId(); - BoBInfo bobInfo = bobs.get(bobIQRequest.getBoBHash()); + BoBInfo bobInfo = bobs.get(contentId); if (bobInfo == null) { // TODO return item-not-found return null; } BoBData bobData = bobInfo.getData(); - BoBIQ responseBoBIQ = new BoBIQ(bobIQRequest.getBoBHash(), bobData); + BoBIQ responseBoBIQ = new BoBIQ(contentId, bobData); responseBoBIQ.setType(Type.result); responseBoBIQ.setTo(bobIQRequest.getFrom()); return responseBoBIQ; @@ -137,7 +138,7 @@ public final class BoBManager extends Manager { * @throws NotConnectedException if the XMPP connection is not connected. * @throws InterruptedException if the calling thread was interrupted. */ - public BoBData requestBoB(Jid to, BoBHash bobHash) throws NotLoggedInException, NoResponseException, + public BoBData requestBoB(Jid to, ContentId bobHash) throws NotLoggedInException, NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { BoBData bobData = BOB_CACHE.lookup(bobHash); if (bobData != null) { @@ -159,9 +160,9 @@ public final class BoBManager extends Manager { public BoBInfo addBoB(BoBData bobData) { // We only support SHA-1 for now. - BoBHash bobHash = new BoBHash(SHA1.hex(bobData.getContent()), "sha1"); + ContentId bobHash = new ContentId(SHA1.hex(bobData.getContent()), "sha1"); - Set bobHashes = Collections.singleton(bobHash); + Set bobHashes = Collections.singleton(bobHash); bobHashes = Collections.unmodifiableSet(bobHashes); BoBInfo bobInfo = new BoBInfo(bobHashes, bobData); @@ -171,12 +172,12 @@ public final class BoBManager extends Manager { return bobInfo; } - public BoBInfo removeBoB(BoBHash bobHash) { + public BoBInfo removeBoB(ContentId bobHash) { BoBInfo bobInfo = bobs.remove(bobHash); if (bobInfo == null) { return null; } - for (BoBHash otherBobHash : bobInfo.getHashes()) { + for (ContentId otherBobHash : bobInfo.getHashes()) { bobs.remove(otherBobHash); } return bobInfo; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBHash.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/ContentId.java similarity index 79% rename from smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBHash.java rename to smack-extensions/src/main/java/org/jivesoftware/smackx/bob/ContentId.java index 8175de8be..5b5265bd9 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/BoBHash.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/ContentId.java @@ -19,29 +19,32 @@ package org.jivesoftware.smackx.bob; import org.jivesoftware.smack.util.StringUtils; /** - * Bits of Binary hash class. + * Content-ID class. * * @author Fernando Ramirez * @author Florian Schmaus - * @see XEP-0231: Bits of - * Binary + * @see RFC 2392: Content-ID and Message-ID Uniform Resource Locators */ -public class BoBHash { +public class ContentId { private final String hash; private final String hashType; private final String cid; + private ContentId(String hash, String hashType, String cid) { + this.hash = StringUtils.requireNotNullNorEmpty(hash, "hash must not be null nor empty"); + this.hashType = StringUtils.requireNotNullNorEmpty(hashType, "hashType must not be null nor empty"); + this.cid = cid; + } + /** * BoB hash constructor. * * @param hash TODO javadoc me please * @param hashType TODO javadoc me please */ - public BoBHash(String hash, String hashType) { - this.hash = StringUtils.requireNotNullNorEmpty(hash, "hash must not be null nor empty"); - this.hashType = StringUtils.requireNotNullNorEmpty(hashType, "hashType must not be null nor empty"); - this.cid = this.hashType + '+' + this.hash + "@bob.xmpp.org"; + public ContentId(String hash, String hashType) { + this(hash, hashType, hashType + '+' + hash + "@bob.xmpp.org"); } /** @@ -82,8 +85,8 @@ public class BoBHash { @Override public boolean equals(Object other) { - if (other instanceof BoBHash) { - BoBHash otherBob = (BoBHash) other; + if (other instanceof ContentId) { + ContentId otherBob = (ContentId) other; return cid.equals(otherBob.cid); } return false; @@ -100,10 +103,10 @@ public class BoBHash { * @param src TODO javadoc me please * @return the BoB hash */ - public static BoBHash fromSrc(String src) { + public static ContentId fromSrc(String src) { String hashType = src.substring(src.lastIndexOf("cid:") + 4, src.indexOf("+")); String hash = src.substring(src.indexOf("+") + 1, src.indexOf("@bob.xmpp.org")); - return new BoBHash(hash, hashType); + return new ContentId(hash, hashType); } /** @@ -112,10 +115,10 @@ public class BoBHash { * @param cid TODO javadoc me please * @return the BoB hash */ - public static BoBHash fromCid(String cid) { + public static ContentId fromCid(String cid) { String hashType = cid.substring(0, cid.indexOf("+")); String hash = cid.substring(cid.indexOf("+") + 1, cid.indexOf("@bob.xmpp.org")); - return new BoBHash(hash, hashType); + return new ContentId(hash, hashType, cid); } } 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 new file mode 100644 index 000000000..76414d9bc --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBDataExtension.java @@ -0,0 +1,81 @@ +/** + * + * Copyright 2020 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.bob.element; + +import org.jivesoftware.smack.packet.ExtensionElement; +import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smack.util.Objects; +import org.jivesoftware.smack.util.XmlStringBuilder; +import org.jivesoftware.smackx.bob.BoBData; +import org.jivesoftware.smackx.bob.BoBManager; +import org.jivesoftware.smackx.bob.ContentId; + +/** + * Bits of Binary data extension element. + * + * @author Florian Schmaus + * @see XEP-0231: Bits of + * Binary + */ +public class BoBDataExtension implements ExtensionElement { + + public static final String ELEMENT = "data"; + public static final String NAMESPACE = BoBManager.NAMESPACE; + + private final ContentId cid; + private final BoBData bobData; + + /** + * Bits of Binary data extension constructor. + * + * @param cid TODO javadoc me please + * @param bobData TODO javadoc me please + */ + public BoBDataExtension(ContentId cid, BoBData bobData) { + this.cid = Objects.requireNonNull(cid); + this.bobData = Objects.requireNonNull(bobData); + } + + @Override + public String getElementName() { + return ELEMENT; + } + + @Override + public String getNamespace() { + return NAMESPACE; + } + + @Override + public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.attribute("cid", cid.getCid()); + xml.attribute("type", bobData.getType()); + xml.optAttribute("max-age", bobData.getMaxAge()); + xml.rightAngleBracket(); + + xml.append(bobData.getContentBase64Encoded()); + + xml.closeElement(this); + return xml; + } + + public static BoBDataExtension from(Message message) { + return message.getExtension(BoBDataExtension.class); + } + +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBExtension.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBExtension.java deleted file mode 100644 index 442474fa1..000000000 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBExtension.java +++ /dev/null @@ -1,97 +0,0 @@ -/** - * - * Copyright 2016 Fernando Ramirez - * - * 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.bob.element; - -import org.jivesoftware.smack.packet.Message; -import org.jivesoftware.smack.util.XmlStringBuilder; - -import org.jivesoftware.smackx.bob.BoBHash; -import org.jivesoftware.smackx.xhtmlim.XHTMLText; -import org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension; - -/** - * Bits of Binary extension element. - * - * @author Fernando Ramirez - * @see XEP-0231: Bits of - * Binary - */ -public class BoBExtension extends XHTMLExtension { - - private final BoBHash bobHash; - private final String alt; - private final String paragraph; - - /** - * Bits of Binary extension constructor. - * - * @param bobHash TODO javadoc me please - * @param alt TODO javadoc me please - * @param paragraph TODO javadoc me please - */ - public BoBExtension(BoBHash bobHash, String alt, String paragraph) { - this.bobHash = bobHash; - this.alt = alt; - this.paragraph = paragraph; - } - - /** - * Get the BoB hash. - * - * @return the BoB hash - */ - public BoBHash getBoBHash() { - return bobHash; - } - - /** - * Get the alt field. - * - * @return the alt field - */ - public String getAlt() { - return alt; - } - - @Override - public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { - XmlStringBuilder xml = new XmlStringBuilder(this); - xml.rightAngleBracket(); - - xml.halfOpenElement(Message.BODY); - xml.xmlnsAttribute(XHTMLText.NAMESPACE); - xml.rightAngleBracket(); - - xml.openElement(XHTMLText.P); - xml.optEscape(paragraph); - - xml.halfOpenElement(XHTMLText.IMG); - xml.optAttribute("alt", alt); - xml.attribute("src", bobHash.toSrc()); - xml.closeEmptyElement(); - - xml.closeElement(XHTMLText.P); - xml.closeElement(Message.BODY); - xml.closeElement(this); - return xml; - } - - public static BoBExtension from(Message message) { - return (BoBExtension) message.getExtensionElement(ELEMENT, NAMESPACE); - } - -} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBIQ.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBIQ.java index b79d5d145..085df5fd5 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBIQ.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/element/BoBIQ.java @@ -1,6 +1,6 @@ /** * - * Copyright 2016 Fernando Ramirez + * Copyright 2016 Fernando Ramirez, 2020 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,8 @@ package org.jivesoftware.smackx.bob.element; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smackx.bob.BoBData; -import org.jivesoftware.smackx.bob.BoBHash; import org.jivesoftware.smackx.bob.BoBManager; +import org.jivesoftware.smackx.bob.ContentId; /** * Bits of Binary IQ class. @@ -41,28 +41,40 @@ public class BoBIQ extends IQ { */ public static final String NAMESPACE = BoBManager.NAMESPACE; - private final BoBHash bobHash; + private final ContentId cid; private final BoBData bobData; /** * Bits of Binary IQ constructor. * - * @param bobHash TODO javadoc me please + * @param cid TODO javadoc me please * @param bobData TODO javadoc me please */ - public BoBIQ(BoBHash bobHash, BoBData bobData) { + public BoBIQ(ContentId cid, BoBData bobData) { super(ELEMENT, NAMESPACE); - this.bobHash = bobHash; + this.cid = cid; this.bobData = bobData; } /** * Bits of Binary IQ constructor. * - * @param bobHash TODO javadoc me please + * @param cid TODO javadoc me please */ - public BoBIQ(BoBHash bobHash) { - this(bobHash, null); + public BoBIQ(ContentId cid) { + this(cid, null); + } + + /** + * Get the BoB hash. + * + * @return the BoB hash + * @deprecated use {@link #getContentId()} instead. + */ + // TODO: Remove in Smack 4.5. + @Deprecated + public ContentId getBoBHash() { + return cid; } /** @@ -70,8 +82,8 @@ public class BoBIQ extends IQ { * * @return the BoB hash */ - public BoBHash getBoBHash() { - return bobHash; + public ContentId getContentId() { + return cid; } /** @@ -85,7 +97,7 @@ public class BoBIQ extends IQ { @Override protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) { - xml.attribute("cid", bobHash.getCid()); + xml.attribute("cid", cid.getCid()); if (bobData != null) { xml.optIntAttribute("max_age", bobData.getMaxAge()); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBDataExtensionProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBDataExtensionProvider.java new file mode 100644 index 000000000..a633c8b07 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBDataExtensionProvider.java @@ -0,0 +1,41 @@ +/** + * + * Copyright 2020 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.bob.provider; + +import java.io.IOException; + +import org.jivesoftware.smack.packet.XmlEnvironment; +import org.jivesoftware.smack.provider.ExtensionElementProvider; +import org.jivesoftware.smack.util.Pair; +import org.jivesoftware.smack.xml.XmlPullParser; +import org.jivesoftware.smack.xml.XmlPullParserException; +import org.jivesoftware.smackx.bob.BoBData; +import org.jivesoftware.smackx.bob.ContentId; +import org.jivesoftware.smackx.bob.element.BoBDataExtension; + +public class BoBDataExtensionProvider extends ExtensionElementProvider { + + @Override + public BoBDataExtension parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) + throws XmlPullParserException, IOException { + Pair parserResult = BoBProviderUtil.parseContentIdAndBobData(parser, initialDepth, + xmlEnvironment); + + return new BoBDataExtension(parserResult.getFirst(), parserResult.getSecond()); + } + +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBIQProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBIQProvider.java index 36f820256..a1e9f92f3 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBIQProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBIQProvider.java @@ -20,12 +20,12 @@ import java.io.IOException; import org.jivesoftware.smack.packet.XmlEnvironment; import org.jivesoftware.smack.provider.IQProvider; -import org.jivesoftware.smack.util.ParserUtils; +import org.jivesoftware.smack.util.Pair; import org.jivesoftware.smack.xml.XmlPullParser; import org.jivesoftware.smack.xml.XmlPullParserException; import org.jivesoftware.smackx.bob.BoBData; -import org.jivesoftware.smackx.bob.BoBHash; +import org.jivesoftware.smackx.bob.ContentId; import org.jivesoftware.smackx.bob.element.BoBIQ; /** @@ -39,22 +39,10 @@ public class BoBIQProvider extends IQProvider { @Override public BoBIQ parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException { - String cid = parser.getAttributeValue("", "cid"); - BoBHash bobHash = BoBHash.fromCid(cid); + Pair parserResult = BoBProviderUtil.parseContentIdAndBobData(parser, initialDepth, + xmlEnvironment); - String dataType = parser.getAttributeValue("", "type"); - int maxAge = ParserUtils.getIntegerAttribute(parser, "max-age", -1); - - String base64EncodedData = parser.nextText(); - - BoBData bobData; - if (dataType != null) { - bobData = new BoBData(dataType, base64EncodedData, maxAge); - } else { - bobData = null; - } - - return new BoBIQ(bobHash, bobData); + return new BoBIQ(parserResult.getFirst(), parserResult.getSecond()); } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBProviderUtil.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBProviderUtil.java new file mode 100644 index 000000000..81c166b45 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bob/provider/BoBProviderUtil.java @@ -0,0 +1,48 @@ +/** + * + * Copyright 2020 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.bob.provider; + +import java.io.IOException; + +import org.jivesoftware.smack.packet.XmlEnvironment; +import org.jivesoftware.smack.util.Pair; +import org.jivesoftware.smack.util.ParserUtils; +import org.jivesoftware.smack.xml.XmlPullParser; +import org.jivesoftware.smack.xml.XmlPullParserException; +import org.jivesoftware.smackx.bob.BoBData; +import org.jivesoftware.smackx.bob.ContentId; + +public class BoBProviderUtil { + + public static Pair parseContentIdAndBobData(XmlPullParser parser, int initialDepth, + XmlEnvironment xmlEnvironment) throws IOException, XmlPullParserException { + String cid = parser.getAttributeValue("", "cid"); + ContentId contentId = ContentId.fromCid(cid); + + String dataType = parser.getAttributeValue("", "type"); + Integer maxAge = ParserUtils.getIntegerAttribute(parser, "max-age"); + + String base64EncodedData = parser.nextText(); + + BoBData bobData = null; + if (dataType != null) { + bobData = new BoBData(dataType, base64EncodedData, maxAge); + } + + return Pair.create(contentId, bobData); + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/packet/XHTMLExtension.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/packet/XHTMLExtension.java index 75ebd5bdf..a49020f68 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/packet/XHTMLExtension.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xhtmlim/packet/XHTMLExtension.java @@ -37,7 +37,7 @@ import org.jivesoftware.smack.util.XmlStringBuilder; * * @author Gaston Dombiak */ -public class XHTMLExtension implements ExtensionElement { +public final class XHTMLExtension implements ExtensionElement { public static final String ELEMENT = "html"; public static final String NAMESPACE = "http://jabber.org/protocol/xhtml-im"; diff --git a/smack-extensions/src/main/resources/org.jivesoftware.smack.extensions/extensions.providers b/smack-extensions/src/main/resources/org.jivesoftware.smack.extensions/extensions.providers index 6d7ebc38c..19c19a305 100644 --- a/smack-extensions/src/main/resources/org.jivesoftware.smack.extensions/extensions.providers +++ b/smack-extensions/src/main/resources/org.jivesoftware.smack.extensions/extensions.providers @@ -500,6 +500,12 @@ + + data + urn:xmpp:bob + org.jivesoftware.smackx.bob.provider.BoBDataExtensionProvider + + data urn:xmpp:bob diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/bob/BoBIQTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/bob/BoBIQTest.java index bc17dd23e..63c2d0183 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/bob/BoBIQTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/bob/BoBIQTest.java @@ -41,7 +41,7 @@ public class BoBIQTest extends SmackTestSuite { @Test public void checkBoBIQRequest() throws Exception { - BoBHash bobHash = new BoBHash("8f35fef110ffc5df08d579a50083ff9308fb6242", "sha1"); + ContentId bobHash = new ContentId("8f35fef110ffc5df08d579a50083ff9308fb6242", "sha1"); BoBIQ createdBoBIQ = new BoBIQ(bobHash); createdBoBIQ.setStanzaId("sarasa"); @@ -55,7 +55,7 @@ public class BoBIQTest extends SmackTestSuite { public void checkBoBIQResponse() throws Exception { BoBIQ bobIQ = PacketParserUtils.parseStanza(sampleBoBIQResponse); - BoBHash bobHash = new BoBHash("8f35fef110ffc5df08d579a50083ff9308fb6242", "sha1"); + ContentId bobHash = new ContentId("8f35fef110ffc5df08d579a50083ff9308fb6242", "sha1"); BoBData bobData = new BoBData("image/png", "sarasade2354j2".getBytes(StandardCharsets.UTF_8), 86400); BoBIQ createdBoBIQ = new BoBIQ(bobHash, bobData); @@ -63,8 +63,8 @@ public class BoBIQTest extends SmackTestSuite { createdBoBIQ.setTo(JidCreate.from("doctor@shakespeare.lit/pda")); createdBoBIQ.setType(Type.result); - assertEquals(bobIQ.getBoBHash().getHash(), createdBoBIQ.getBoBHash().getHash()); - assertEquals(bobIQ.getBoBHash().getHashType(), createdBoBIQ.getBoBHash().getHashType()); + assertEquals(bobIQ.getContentId().getHash(), createdBoBIQ.getContentId().getHash()); + assertEquals(bobIQ.getContentId().getHashType(), createdBoBIQ.getContentId().getHashType()); assertEquals(bobIQ.getBoBData().getMaxAge(), createdBoBIQ.getBoBData().getMaxAge()); assertEquals(bobIQ.getBoBData().getType(), createdBoBIQ.getBoBData().getType()); assertEquals(bobIQ.getBoBData().getContentBase64Encoded(), createdBoBIQ.getBoBData().getContentBase64Encoded());