From fe7d3bec3026c122eb75b371bac6202788fa4a5e Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 23 Sep 2020 17:39:53 +0200 Subject: [PATCH] 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")