mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-09 09:09:38 +02:00
Make Forwarded a generic type
Fixes SMACK-821.
This commit is contained in:
parent
c1b32f8e11
commit
fe7d3bec30
13 changed files with 101 additions and 51 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue