1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-26 14:17:57 +01:00

Make Forwarded a generic type

Fixes SMACK-821.
This commit is contained in:
Florian Schmaus 2020-09-23 17:39:53 +02:00
parent c1b32f8e11
commit fe7d3bec30
13 changed files with 101 additions and 51 deletions

View file

@ -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<E extends Element> {
Type[] actualTypeArguments = parameterizedGenericSuperclass.getActualTypeArguments();
Type elementType = actualTypeArguments[0];
elementClass = (Class<E>) elementType;
if (elementType instanceof Class) {
elementClass = (Class<E>) elementType;
} else if (elementType instanceof ParameterizedType) {
ParameterizedType parameteriezedElementType = (ParameterizedType) elementType;
elementClass = (Class<E>) parameteriezedElementType.getRawType();
} else {
throw new AssertionError(
"Element type '" + elementType + "' is neither of type Class or ParameterizedType");
}
}
public final Class<E> getElementClass() {

View file

@ -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<Message> forwarded = carbonExtension.getForwarded();
final Message carbonCopy = forwarded.getForwardedStanza();
final BareJid from = carbonCopy.getFrom().asBareJid();
carbonsListenerAsyncButOrdered.performAsyncButOrdered(from, new Runnable() {

View file

@ -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<Message> 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<Message> 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<Message> getForwarded() {
return fwd;
}

View file

@ -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<CarbonExtension> {
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<Message> 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;

View file

@ -632,7 +632,7 @@ public final class MamManager extends Manager {
private final MamFinIQ mamFin;
private final List<Message> mamResultCarrierMessages;
private final List<MamResultExtension> mamResultExtensions;
private final List<Forwarded> forwardedMessages;
private final List<Forwarded<Message>> forwardedMessages;
private final List<Message> messages;
private MamQueryPage(StanzaCollector stanzaCollector, MamFinIQ mamFin) {
@ -642,7 +642,7 @@ public final class MamManager extends Manager {
List<Message> mamResultCarrierMessages = new ArrayList<>(mamResultCarrierStanzas.size());
List<MamResultExtension> mamResultExtensions = new ArrayList<>(mamResultCarrierStanzas.size());
List<Forwarded> forwardedMessages = new ArrayList<>(mamResultCarrierStanzas.size());
List<Forwarded<Message>> 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<Forwarded> getForwarded() {
public List<Forwarded<Message>> getForwarded() {
return forwardedMessages;
}

View file

@ -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<Message> 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<Message> 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<Message> getForwarded() {
return forwarded;
}

View file

@ -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<MamResultExtensi
@Override
public MamResultExtension parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
Forwarded forwarded = null;
Forwarded<Message> forwarded = null;
String queryId = parser.getAttributeValue("", "queryid");
String id = parser.getAttributeValue("", "id");
@ -51,7 +52,7 @@ public class MamResultProvider extends ExtensionElementProvider<MamResultExtensi
case START_ELEMENT:
switch (name) {
case Forwarded.ELEMENT:
forwarded = ForwardedProvider.INSTANCE.parse(parser);
forwarded = ForwardedProvider.parseForwardedMessage(parser, xmlEnvironment);
break;
}
break;

View file

@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Properties;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.parsing.SmackParsingException;
import org.jivesoftware.smack.test.util.SmackTestUtil;
import org.jivesoftware.smack.util.PacketParserUtils;
@ -48,7 +49,7 @@ public class CarbonTest extends ExperimentalInitializerTest {
XmlPullParser parser;
String control;
CarbonExtension cc;
Forwarded fwd;
Forwarded<Message> fwd;
control = XMLBuilder.create("sent")
.e("forwarded")

View file

@ -59,10 +59,10 @@ public class MamResultProviderTest {
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = calendar.getTime();
Forwarded forwarded = mamResultExtension.getForwarded();
Forwarded<Message> 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<Message> 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");

View file

@ -79,7 +79,7 @@ public class QueryArchiveTest extends MamTest {
.setBody("Thrice the brinded cat hath mew.")
.build();
Forwarded forwarded = new Forwarded(delay, forwardedMessage);
Forwarded<Message> 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);

View file

@ -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 <a href="http://xmpp.org/extensions/xep-0297.html">XEP-0297: Stanza Forwarding</a>
*/
public class Forwarded implements ExtensionElement {
public class Forwarded<S extends Stanza> 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 <code>true</code> if this is forwarding a stanza of the provided class.
* @since 4.4
*/
public boolean isForwarded(Class<? extends Stanza> 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<Message> extractMessagesFrom(Collection<Forwarded> forwardedCollection) {
public static List<Message> extractMessagesFrom(Collection<Forwarded<Message>> forwardedCollection) {
List<Message> res = new ArrayList<>(forwardedCollection.size());
for (Forwarded forwarded : forwardedCollection) {
Message message = (Message) forwarded.forwardedPacket;
for (Forwarded<Message> forwarded : forwardedCollection) {
Message message = forwarded.getForwardedStanza();
res.add(message);
}
return res;

View file

@ -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<Forwarded> {
public class ForwardedProvider extends ExtensionElementProvider<Forwarded<?>> {
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<Forwarded> {
// 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<Message> parseForwardedMessage(XmlPullParser parser, XmlEnvironment xmlEnvironment)
throws XmlPullParserException, IOException, SmackParsingException {
return parseForwardedMessage(parser, parser.getDepth(), xmlEnvironment);
}
@SuppressWarnings("unchecked")
public static Forwarded<Message> 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<Message>) forwarded;
}
}

View file

@ -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")