From 8e74f7faed4aec624cb1f7a5eb31a43666c3c1e9 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Fri, 9 Jan 2015 11:02:11 +0100 Subject: [PATCH] Improve 'forward' code Move Forwarded into forward.packet and remove deprecated methods. Also make fields final. Improve ForwardedProvider: - use INSTANCE of DelayInformationProvider - use loop label - don't throw exceptions in certain cases, instead log --- .../carbons/packet/CarbonExtension.java | 2 +- .../provider/CarbonManagerProvider.java | 2 +- .../smackx/carbons/CarbonTest.java | 2 +- .../provider/DelayInformationProvider.java | 2 + .../forward/{ => packet}/Forwarded.java | 50 ++++++------------- .../forward/provider/ForwardedProvider.java | 41 +++++++++++---- .../smackx/forward/ForwardedTest.java | 30 +++++++++++ 7 files changed, 81 insertions(+), 48 deletions(-) rename smack-extensions/src/main/java/org/jivesoftware/smackx/forward/{ => packet}/Forwarded.java (76%) 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 1864194c2..f890d5631 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 @@ -19,7 +19,7 @@ package org.jivesoftware.smackx.carbons.packet; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.util.XmlStringBuilder; -import org.jivesoftware.smackx.forward.Forwarded; +import org.jivesoftware.smackx.forward.packet.Forwarded; /** * Packet extension for XEP-0280: Message Carbons. The extension 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 1c9890271..2d3c86c6b 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 @@ -22,7 +22,7 @@ import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.provider.PacketExtensionProvider; import org.jivesoftware.smackx.carbons.packet.CarbonExtension; import org.jivesoftware.smackx.carbons.packet.CarbonExtension.Direction; -import org.jivesoftware.smackx.forward.Forwarded; +import org.jivesoftware.smackx.forward.packet.Forwarded; import org.jivesoftware.smackx.forward.provider.ForwardedProvider; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; 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 df180ea44..7f9b97f05 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 @@ -24,7 +24,7 @@ import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smackx.ExperimentalInitializerTest; import org.jivesoftware.smackx.carbons.packet.CarbonExtension; import org.jivesoftware.smackx.carbons.provider.CarbonManagerProvider; -import org.jivesoftware.smackx.forward.Forwarded; +import org.jivesoftware.smackx.forward.packet.Forwarded; import org.junit.Test; import org.xmlpull.v1.XmlPullParser; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/provider/DelayInformationProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/provider/DelayInformationProvider.java index 1a6010343..a9ed78498 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/provider/DelayInformationProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/provider/DelayInformationProvider.java @@ -28,6 +28,8 @@ import org.jxmpp.util.XmppDateTime; */ public class DelayInformationProvider extends AbstractDelayInformationProvider { + public static final DelayInformationProvider INSTANCE = new DelayInformationProvider(); + @Override protected Date parseDate(String string) throws ParseException { return XmppDateTime.parseXEP0082Date(string); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/Forwarded.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/packet/Forwarded.java similarity index 76% rename from smack-extensions/src/main/java/org/jivesoftware/smackx/forward/Forwarded.java rename to smack-extensions/src/main/java/org/jivesoftware/smackx/forward/packet/Forwarded.java index 4e3570dcf..844d6d8e0 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/Forwarded.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/packet/Forwarded.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jivesoftware.smackx.forward; +package org.jivesoftware.smackx.forward.packet; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.PacketExtension; @@ -22,16 +22,17 @@ import org.jivesoftware.smack.util.XmlStringBuilder; import org.jivesoftware.smackx.delay.packet.DelayInformation; /** - * Packet extension for XEP-0297: Stanza Forwarding. + * Packet extension for >XEP-0297: Stanza Forwarding. * * @author Georg Lukas + * @see XEP-0297: Stanza Forwarding */ public class Forwarded implements PacketExtension { public static final String NAMESPACE = "urn:xmpp:forward:0"; public static final String ELEMENT = "forwarded"; - private DelayInformation delay; - private Packet forwardedPacket; + private final DelayInformation delay; + private final Packet forwardedPacket; /** * Creates a new Forwarded packet extension. @@ -50,7 +51,7 @@ public class Forwarded implements PacketExtension { * @param fwdPacket the packet that is forwarded (required). */ public Forwarded(Packet fwdPacket) { - this.forwardedPacket = fwdPacket; + this(null, fwdPacket); } @Override @@ -73,26 +74,6 @@ public class Forwarded implements PacketExtension { return xml; } - /** - * - * @param packet - * @return the Forwarded extension or null - * @deprecated use {@link #from(Packet)} instead - */ - @Deprecated - public static Forwarded getFrom(Packet packet) { - return from(packet); - } - - /** - * - * @param packet - * @return the Forwarded extension or null - */ - public static Forwarded from(Packet packet) { - return packet.getExtension(ELEMENT, NAMESPACE); - } - /** * get the packet forwarded by this stanza. * @@ -102,16 +83,6 @@ public class Forwarded implements PacketExtension { return forwardedPacket; } - /** - * get the timestamp of the forwarded packet. - * - * @return the {@link DelayInformation} representing the time when the original packet was sent. May be null. - * @deprecated Use {@link #getDelayInformation} instead. - */ - public DelayInformation getDelayInfo() { - return getDelayInformation(); - } - /** * get the timestamp of the forwarded packet. * @@ -120,4 +91,13 @@ public class Forwarded implements PacketExtension { public DelayInformation getDelayInformation() { return delay; } + + /** + * + * @param packet + * @return the Forwarded extension or null + */ + public static Forwarded from(Packet packet) { + return packet.getExtension(ELEMENT, NAMESPACE); + } } 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 25ffb10e8..e061b4177 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 @@ -17,13 +17,16 @@ package org.jivesoftware.smackx.forward.provider; import java.io.IOException; +import java.util.logging.Logger; import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.provider.PacketExtensionProvider; import org.jivesoftware.smack.util.PacketParserUtils; import org.jivesoftware.smackx.delay.packet.DelayInformation; -import org.jivesoftware.smackx.forward.Forwarded; +import org.jivesoftware.smackx.delay.provider.DelayInformationProvider; +import org.jivesoftware.smackx.forward.packet.Forwarded; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; @@ -35,24 +38,42 @@ import org.xmlpull.v1.XmlPullParserException; */ public class ForwardedProvider extends PacketExtensionProvider { + private static final Logger LOGGER = Logger.getLogger(ForwardedProvider.class.getName()); + @Override public Forwarded parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException { DelayInformation di = null; Packet packet = null; - boolean done = false; - while (!done) { + outerloop: while (true) { int eventType = parser.next(); - if (eventType == XmlPullParser.START_TAG) { - if (parser.getName().equals("delay")) - di = (DelayInformation)PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser); - else if (parser.getName().equals("message")) + switch (eventType) { + case XmlPullParser.START_TAG: + String name = parser.getName(); + String namespace = parser.getNamespace(); + switch (name) { + case DelayInformation.ELEMENT: + if (DelayInformation.NAMESPACE.equals(namespace)) { + di = DelayInformationProvider.INSTANCE.parse(parser, parser.getDepth()); + } else { + LOGGER.warning("Namespace '" + namespace + "' does not match expected namespace '" + + DelayInformation.NAMESPACE + "'"); + } + break; + case Message.ELEMENT: packet = PacketParserUtils.parseMessage(parser); - else throw new SmackException("Unsupported forwarded packet type: " + parser.getName()); + break; + default: + LOGGER.warning("Unsupported forwarded packet type: " + name); + } + case XmlPullParser.END_TAG: + if (parser.getDepth() == initialDepth) { + break outerloop; + } + break; } - else if (eventType == XmlPullParser.END_TAG && parser.getName().equals(Forwarded.ELEMENT)) - done = true; } + if (packet == null) throw new SmackException("forwarded extension must contain a packet"); return new Forwarded(di, packet); 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 2b354d0f7..30ea25fa3 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 @@ -17,10 +17,13 @@ package org.jivesoftware.smackx.forward; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import java.util.Properties; import org.jivesoftware.smack.util.PacketParserUtils; +import org.jivesoftware.smackx.delay.packet.DelayInformation; +import org.jivesoftware.smackx.forward.packet.Forwarded; import org.jivesoftware.smackx.forward.provider.ForwardedProvider; import org.junit.Test; import org.xmlpull.v1.XmlPullParser; @@ -58,7 +61,34 @@ public class ForwardedTest { // check end of tag assertEquals(XmlPullParser.END_TAG, parser.getEventType()); assertEquals("forwarded", parser.getName()); + } + @Test + public void forwardedWithDelayTest() throws Exception { + XmlPullParser parser; + String control; + Forwarded fwd; + + // @formatter:off + control = XMLBuilder.create("forwarded").a("xmlns", "urn:xmpp:forwarded:0") + .e("message").a("from", "romeo@montague.com").up() + .e("delay").ns(DelayInformation.NAMESPACE).a("stamp", "2010-07-10T23:08:25Z") + .asString(outputProperties); + // @formatter:on + + parser = PacketParserUtils.getParserFor(control); + fwd = (Forwarded) new ForwardedProvider().parse(parser); + + // assert there is delay information in packet + DelayInformation delay = fwd.getDelayInformation(); + assertNotNull(delay); + + // check message + assertEquals("romeo@montague.com", fwd.getForwardedPacket().getFrom()); + + // check end of tag + assertEquals(XmlPullParser.END_TAG, parser.getEventType()); + assertEquals("forwarded", parser.getName()); } @Test(expected=Exception.class)