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
This commit is contained in:
Florian Schmaus 2015-01-09 11:02:11 +01:00
parent bb8dcc9874
commit 8e74f7faed
7 changed files with 81 additions and 48 deletions

View File

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

View File

@ -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;

View File

@ -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;

View File

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

View File

@ -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 <a href="http://xmpp.org/extensions/xep-0297.html">XEP-0297</a>: Stanza Forwarding.
* Packet extension for >XEP-0297: Stanza Forwarding.
*
* @author Georg Lukas
* @see <a href="http://xmpp.org/extensions/xep-0297.html">XEP-0297: Stanza Forwarding</a>
*/
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);
}
}

View File

@ -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<Forwarded> {
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);

View File

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