diff --git a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/muc/MultiUserChatTest.java b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/muc/MultiUserChatTest.java index ba20a119b..f0dff8dc3 100644 --- a/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/muc/MultiUserChatTest.java +++ b/smack-extensions/src/integration-test/java/org/jivesoftware/smackx/muc/MultiUserChatTest.java @@ -41,6 +41,7 @@ import org.jivesoftware.smack.test.SmackTestCase; import org.jivesoftware.smackx.Form; import org.jivesoftware.smackx.FormField; import org.jivesoftware.smackx.ServiceDiscoveryManager; +import org.jivesoftware.smackx.delay.DelayInformationManager; import org.jivesoftware.smackx.packet.DelayInformation; import org.jivesoftware.smackx.packet.DiscoverInfo; import org.jivesoftware.smackx.packet.XHTMLExtension; @@ -124,7 +125,7 @@ public class MultiUserChatTest extends SmackTestCase { // Get first historic message msg = muc2.nextMessage(1000); assertNotNull("First message is null", msg); - DelayInformation delay = (DelayInformation) msg.getExtension("x", "jabber:x:delay"); + DelayInformation delay = DelayInformationManager.getDelayInformation(msg); assertNotNull("Message contains no delay information", delay); SimpleDateFormat UTC_FORMAT = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ss"); UTC_FORMAT.setTimeZone(TimeZone.getDefault()); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/DelayInformationManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/DelayInformationManager.java new file mode 100644 index 000000000..debc46e0f --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/DelayInformationManager.java @@ -0,0 +1,101 @@ +/** + * + * Copyright © 2014 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.delay; + +import java.util.Date; + +import org.jivesoftware.smack.packet.Packet; +import org.jivesoftware.smack.packet.PacketExtension; +import org.jivesoftware.smackx.delay.packet.DelayInformation; + +/** + * Delayed Delivery (XEP-203) + * + * @author Florian Schmaus + * @see Delayed Delivery (XEP-203) + * + */ +public class DelayInformationManager { + + public static final String LEGACY_DELAYED_DELIVERY_NAMESPACE = "jabber:x:delay"; + public static final String LEGACY_DELAYED_DELIVERY_ELEMENT = "x"; + + + /** + * Get Delayed Delivery information as defined in XEP-203 + *

+ * Prefer {@link #getDelayInformation(Packet)} over this method for backwards compatibility. + *

+ * @param packet + * @return the Delayed Delivery information or null + */ + public static DelayInformation getXep203DelayInformation(Packet packet) { + return (DelayInformation) packet.getExtension(DelayInformation.ELEMENT, DelayInformation.NAMESPACE); + } + + /** + * Get Delayed Delivery information as defined in XEP-91 + *

+ * Prefer {@link #getDelayInformation(Packet)} over this method for backwards compatibility. + *

+ * @param packet + * @return the Delayed Delivery information or null + */ + public static DelayInformation getLegacyDelayInformation(Packet packet) { + return (DelayInformation) packet.getExtension(LEGACY_DELAYED_DELIVERY_ELEMENT, LEGACY_DELAYED_DELIVERY_NAMESPACE); + } + + /** + * Get Delayed Delivery information. This method first looks for a PacketExtension with the + * XEP-203 namespace and falls back to the XEP-91 namespace. + * + * @param packet + * @return the Delayed Delivery information or null + */ + public static DelayInformation getDelayInformation(Packet packet) { + DelayInformation delayInformation = getXep203DelayInformation(packet); + if (delayInformation != null) { + return delayInformation; + } + return getLegacyDelayInformation(packet); + } + + /** + * Get the Delayed Delivery timestamp or null + * + * @param packet + * @return the Delayed Delivery timestamp or null + */ + public static Date getDelayTimestamp(Packet packet) { + DelayInformation delayInformation = getDelayInformation(packet); + if (delayInformation == null) { + return null; + } + return delayInformation.getStamp(); + } + + /** + * Check if the given stanza is a delayed stanza as of XEP-203. + * + * @param packet + * @return true if the stanza got delayed. + */ + public static boolean isDelayedStanza(Packet packet) { + PacketExtension packetExtension = getDelayInformation(packet); + return packetExtension != null; + } +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/packet/DelayInfo.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/packet/DelayInfo.java deleted file mode 100644 index 3264db6e0..000000000 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/packet/DelayInfo.java +++ /dev/null @@ -1,108 +0,0 @@ -/** - * - * Copyright the original author or authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jivesoftware.smackx.delay.packet; - -import java.util.Date; - -import org.jxmpp.util.XmppDateTime; - -/** - * A decorator for the {@link DelayInformation} class to transparently support - * both the new Delay Delivery specification XEP-0203 and - * the old one XEP-0091. - * - * Existing code can be backward compatible. - * - * @author Robin Collier - */ -public class DelayInfo extends DelayInformation -{ - - DelayInformation wrappedInfo; - - /** - * Creates a new instance with given delay information. - * @param delay the delay information - */ - public DelayInfo(DelayInformation delay) - { - super(delay.getStamp()); - wrappedInfo = delay; - } - - @Override - public String getFrom() - { - return wrappedInfo.getFrom(); - } - - @Override - public String getReason() - { - return wrappedInfo.getReason(); - } - - @Override - public Date getStamp() - { - return wrappedInfo.getStamp(); - } - - @Override - public void setFrom(String from) - { - wrappedInfo.setFrom(from); - } - - @Override - public void setReason(String reason) - { - wrappedInfo.setReason(reason); - } - - @Override - public String getElementName() - { - return "delay"; - } - - @Override - public String getNamespace() - { - return "urn:xmpp:delay"; - } - - @Override - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append( - "\""); - buf.append(" stamp=\""); - buf.append(XmppDateTime.formatXEP0082Date(getStamp())); - buf.append("\""); - if (getFrom() != null && getFrom().length() > 0) { - buf.append(" from=\"").append(getFrom()).append("\""); - } - buf.append(">"); - if (getReason() != null && getReason().length() > 0) { - buf.append(getReason()); - } - buf.append(""); - return buf.toString(); - } - -} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/packet/DelayInformation.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/packet/DelayInformation.java index 34277a37a..04d9e0f7b 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/packet/DelayInformation.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/packet/DelayInformation.java @@ -16,12 +16,11 @@ */ package org.jivesoftware.smackx.delay.packet; -import java.text.DateFormat; -import java.text.SimpleDateFormat; import java.util.Date; -import java.util.TimeZone; import org.jivesoftware.smack.packet.PacketExtension; +import org.jivesoftware.smack.util.XmlStringBuilder; +import org.jxmpp.util.XmppDateTime; /** * Represents timestamp information about data stored for later delivery. A DelayInformation will @@ -33,34 +32,28 @@ import org.jivesoftware.smack.packet.PacketExtension; * and XEP-0203. * * @author Gaston Dombiak + * @author Florian Schmaus */ public class DelayInformation implements PacketExtension { + public static final String ELEMENT = "delay"; + public static final String NAMESPACE = "urn:xmpp:delay"; - /** - * Date format according to the obsolete XEP-0091 specification. - * XEP-0091 recommends to use this old format for date-time instead of - * the one specified in XEP-0082. - *

- * Date formats are not synchronized. Since multiple threads access the format concurrently, - * it must be synchronized externally. - */ - public static final DateFormat XEP_0091_UTC_FORMAT = new SimpleDateFormat( - "yyyyMMdd'T'HH:mm:ss"); - static { - XEP_0091_UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC")); - } - - private Date stamp; - private String from; - private String reason; + private final Date stamp; + private final String from; + private final String reason; /** * Creates a new instance with the specified timestamp. * @param stamp the timestamp */ - public DelayInformation(Date stamp) { - super(); + public DelayInformation(Date stamp, String from, String reason) { this.stamp = stamp; + this.from = from; + this.reason = reason; + } + + public DelayInformation(Date stamp) { + this(stamp, null, null); } /** @@ -74,16 +67,6 @@ public class DelayInformation implements PacketExtension { return from; } - /** - * Sets the JID of the entity that originally sent the packet or that delayed the - * delivery of the packet or null if this information is not available. - * - * @param from the JID of the entity that originally sent the packet. - */ - public void setFrom(String from) { - this.from = from; - } - /** * Returns the timestamp when the packet was originally sent. The returned Date is * be understood as UTC. @@ -104,42 +87,25 @@ public class DelayInformation implements PacketExtension { return reason; } - /** - * Sets a natural-language description of the reason for the delay or null if - * this information is not available. - * - * @param reason a natural-language description of the reason for the delay or null. - */ - public void setReason(String reason) { - this.reason = reason; - } - public String getElementName() { - return "x"; + return ELEMENT; } public String getNamespace() { - return "jabber:x:delay"; + return NAMESPACE; } - public String toXML() { - StringBuilder buf = new StringBuilder(); - buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append( - "\""); - buf.append(" stamp=\""); - synchronized (XEP_0091_UTC_FORMAT) { - buf.append(XEP_0091_UTC_FORMAT.format(stamp)); + @Override + public CharSequence toXML() { + XmlStringBuilder xml = new XmlStringBuilder(this); + xml.attribute("stamp", XmppDateTime.formatXEP0082Date(stamp)); + xml.optAttribute("from", from); + xml.rightAngelBracket(); + if (reason != null) { + xml.escape(reason); } - buf.append("\""); - if (from != null && from.length() > 0) { - buf.append(" from=\"").append(from).append("\""); - } - buf.append(">"); - if (reason != null && reason.length() > 0) { - buf.append(reason); - } - buf.append(""); - return buf.toString(); + xml.closeElement(this); + return xml; } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/provider/AbstractDelayInformationProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/provider/AbstractDelayInformationProvider.java new file mode 100644 index 000000000..082b81eb0 --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/provider/AbstractDelayInformationProvider.java @@ -0,0 +1,45 @@ +/** + * + * Copyright © 2014 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.delay.provider; + +import java.util.Date; + +import org.jivesoftware.smack.packet.PacketExtension; +import org.jivesoftware.smack.provider.PacketExtensionProvider; +import org.jivesoftware.smackx.delay.packet.DelayInformation; +import org.xmlpull.v1.XmlPullParser; + +public abstract class AbstractDelayInformationProvider implements PacketExtensionProvider { + + @Override + public final PacketExtension parseExtension(XmlPullParser parser) throws Exception { + String stampString = (parser.getAttributeValue("", "stamp")); + String from = parser.getAttributeValue("", "from"); + String reason = null; + if (!parser.isEmptyElementTag()) { + parser.next(); + assert(parser.getEventType() == XmlPullParser.TEXT); + reason = parser.getText(); + } + parser.next(); + assert(parser.getEventType() == XmlPullParser.END_TAG); + Date stamp = parseDate(stampString); + return new DelayInformation(stamp, from, reason); + } + + protected abstract Date parseDate(String string) throws Exception; +} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/provider/DelayInfoProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/provider/DelayInfoProvider.java deleted file mode 100644 index 727f7f7c1..000000000 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/provider/DelayInfoProvider.java +++ /dev/null @@ -1,44 +0,0 @@ -/** - * - * Copyright the original author or authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jivesoftware.smackx.delay.provider; - -import org.jivesoftware.smack.packet.PacketExtension; -import org.jivesoftware.smackx.delay.packet.DelayInfo; -import org.jivesoftware.smackx.delay.packet.DelayInformation; -import org.xmlpull.v1.XmlPullParser; - -/** - * This provider simply creates a {@link DelayInfo} decorator for the {@link DelayInformation} that - * is returned by the superclass. This allows the new code using - * Delay Information XEP-0203 to be - * backward compatible with XEP-0091. - * - *

This provider must be registered in the smack.properties file for the element - * delay with namespace urn:xmpp:delay

- * - * @author Robin Collier - */ -public class DelayInfoProvider extends DelayInformationProvider -{ - - @Override - public PacketExtension parseExtension(XmlPullParser parser) throws Exception - { - return new DelayInfo((DelayInformation)super.parseExtension(parser)); - } - -} 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 60ff6f093..4876e856e 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 @@ -1,6 +1,6 @@ /** * - * Copyright the original author or authors + * Copyright © 2014 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,51 +19,19 @@ package org.jivesoftware.smackx.delay.provider; import java.text.ParseException; import java.util.Date; -import org.jivesoftware.smack.packet.PacketExtension; -import org.jivesoftware.smack.provider.PacketExtensionProvider; import org.jxmpp.util.XmppDateTime; -import org.jivesoftware.smackx.delay.packet.DelayInformation; -import org.xmlpull.v1.XmlPullParser; /** * The DelayInformationProvider parses DelayInformation packets. * - * @author Gaston Dombiak - * @author Henning Staib + * @author Florian Schmaus */ -public class DelayInformationProvider implements PacketExtensionProvider { - - public PacketExtension parseExtension(XmlPullParser parser) throws Exception { - String stampString = (parser.getAttributeValue("", "stamp")); - Date stamp = null; - - try { - stamp = XmppDateTime.parseDate(stampString); - } - catch (ParseException parseExc) { - /* - * if date could not be parsed but XML is valid, don't shutdown - * connection by throwing an exception instead set timestamp to epoch - * so that it is obviously wrong. - */ - if (stamp == null) { - stamp = new Date(0); - } - } - - - DelayInformation delayInformation = new DelayInformation(stamp); - delayInformation.setFrom(parser.getAttributeValue("", "from")); - String reason = parser.nextText(); +public class DelayInformationProvider extends AbstractDelayInformationProvider { - /* - * parser.nextText() returns empty string if there is no reason. - * DelayInformation API specifies that null should be returned in that - * case. - */ - reason = "".equals(reason) ? null : reason; - delayInformation.setReason(reason); - - return delayInformation; + @SuppressWarnings("deprecation") + @Override + protected Date parseDate(String string) throws ParseException { + return XmppDateTime.parseXEP0082Date(string); } + } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/provider/LegacyDelayInformationProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/provider/LegacyDelayInformationProvider.java new file mode 100644 index 000000000..8e469200d --- /dev/null +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/delay/provider/LegacyDelayInformationProvider.java @@ -0,0 +1,36 @@ +/** + * + * Copyright © 2014 Florian Schmaus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.delay.provider; + +import java.text.ParseException; +import java.util.Date; + +import org.jxmpp.util.XmppDateTime; + +/** + * The DelayInformationProvider parses DelayInformation packets. + * + * @author Florian Schmaus + */ +public class LegacyDelayInformationProvider extends AbstractDelayInformationProvider { + + @Override + protected Date parseDate(String string) throws ParseException { + return XmppDateTime.parseDate(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/Forwarded.java index 133a7d44a..53f3c5c9a 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/Forwarded.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/forward/Forwarded.java @@ -18,7 +18,7 @@ package org.jivesoftware.smackx.forward; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.PacketExtension; -import org.jivesoftware.smackx.delay.packet.DelayInfo; +import org.jivesoftware.smackx.delay.packet.DelayInformation; /** * Packet extension for XEP-0297: Stanza Forwarding. @@ -29,16 +29,16 @@ public class Forwarded implements PacketExtension { public static final String NAMESPACE = "urn:xmpp:forward:0"; public static final String ELEMENT_NAME = "forwarded"; - private DelayInfo delay; + private DelayInformation delay; private Packet forwardedPacket; /** * Creates a new Forwarded packet extension. * - * @param delay an optional {@link DelayInfo} timestamp of the packet. + * @param delay an optional {@link DelayInformation} timestamp of the packet. * @param fwdPacket the packet that is forwarded (required). */ - public Forwarded(DelayInfo delay, Packet fwdPacket) { + public Forwarded(DelayInformation delay, Packet fwdPacket) { this.delay = delay; this.forwardedPacket = fwdPacket; } @@ -88,9 +88,19 @@ public class Forwarded implements PacketExtension { /** * get the timestamp of the forwarded packet. * - * @return the {@link DelayInfo} representing the time when the original packet was sent. May be null. + * @return the {@link DelayInformation} representing the time when the original packet was sent. May be null. + * @deprecated Use {@link #getDelayInformation} instead. */ - public DelayInfo getDelayInfo() { + public DelayInformation getDelayInfo() { + return getDelayInformation(); + } + + /** + * get the timestamp of the forwarded packet. + * + * @return the {@link DelayInformation} representing the time when the original packet was sent. May be null. + */ + public DelayInformation getDelayInformation() { return delay; } } 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 06bddac0e..6293a2b4d 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 @@ -20,7 +20,7 @@ import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.provider.PacketExtensionProvider; import org.jivesoftware.smack.util.PacketParserUtils; -import org.jivesoftware.smackx.delay.packet.DelayInfo; +import org.jivesoftware.smackx.delay.packet.DelayInformation; import org.jivesoftware.smackx.forward.Forwarded; import org.xmlpull.v1.XmlPullParser; @@ -32,7 +32,7 @@ import org.xmlpull.v1.XmlPullParser; */ public class ForwardedProvider implements PacketExtensionProvider { public PacketExtension parseExtension(XmlPullParser parser) throws Exception { - DelayInfo di = null; + DelayInformation di = null; Packet packet = null; boolean done = false; @@ -40,7 +40,7 @@ public class ForwardedProvider implements PacketExtensionProvider { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG) { if (parser.getName().equals("delay")) - di = (DelayInfo)PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser); + di = (DelayInformation)PacketParserUtils.parsePacketExtension(parser.getName(), parser.getNamespace(), parser); else if (parser.getName().equals("message")) packet = PacketParserUtils.parseMessage(parser); else throw new Exception("Unsupported forwarded packet type: " + parser.getName()); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java index 125bede91..228fa77d7 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java @@ -32,7 +32,7 @@ import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.PacketExtension; import org.jivesoftware.smack.packet.IQ.Type; -import org.jivesoftware.smackx.delay.packet.DelayInformation; +import org.jivesoftware.smackx.delay.DelayInformationManager; import org.jivesoftware.smackx.disco.packet.DiscoverInfo; import org.jivesoftware.smackx.pubsub.listener.ItemDeleteListener; import org.jivesoftware.smackx.pubsub.listener.ItemEventListener; @@ -409,14 +409,7 @@ abstract public class Node { EventElement event = (EventElement)packet.getExtension("event", PubSubNamespace.EVENT.getXmlns()); ItemsExtension itemsElem = (ItemsExtension)event.getEvent(); - DelayInformation delay = (DelayInformation)packet.getExtension("delay", "urn:xmpp:delay"); - - // If there was no delay based on XEP-0203, then try XEP-0091 for backward compatibility - if (delay == null) - { - delay = (DelayInformation)packet.getExtension("x", "jabber:x:delay"); - } - ItemPublishEvent eventItems = new ItemPublishEvent(itemsElem.getNode(), (List)itemsElem.getItems(), getSubscriptionIds(packet), (delay == null ? null : delay.getStamp())); + ItemPublishEvent eventItems = new ItemPublishEvent(itemsElem.getNode(), (List)itemsElem.getItems(), getSubscriptionIds(packet), DelayInformationManager.getDelayTimestamp(packet)); listener.handlePublishedItems(eventItems); } } diff --git a/smack-extensions/src/main/resources/org.jivesoftware.smackx/extensions.providers b/smack-extensions/src/main/resources/org.jivesoftware.smackx/extensions.providers index 5388ede2c..9fcb25df6 100644 --- a/smack-extensions/src/main/resources/org.jivesoftware.smackx/extensions.providers +++ b/smack-extensions/src/main/resources/org.jivesoftware.smackx/extensions.providers @@ -114,13 +114,13 @@ x jabber:x:delay - org.jivesoftware.smackx.delay.provider.DelayInformationProvider + org.jivesoftware.smackx.delay.provider.LegacyDelayInformationProvider delay urn:xmpp:delay - org.jivesoftware.smackx.delay.provider.DelayInfoProvider + org.jivesoftware.smackx.delay.provider.DelayInformationProvider diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/delay/provider/DelayInformationTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/delay/provider/DelayInformationTest.java index ccef79fbd..ecad2c7d0 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/delay/provider/DelayInformationTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/delay/provider/DelayInformationTest.java @@ -31,9 +31,8 @@ import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.util.PacketParserUtils; import org.jxmpp.util.XmppDateTime; import org.jivesoftware.smackx.InitExtensions; -import org.jivesoftware.smackx.delay.packet.DelayInfo; +import org.jivesoftware.smackx.delay.DelayInformationManager; import org.jivesoftware.smackx.delay.packet.DelayInformation; -import org.jivesoftware.smackx.delay.provider.DelayInfoProvider; import org.jivesoftware.smackx.delay.provider.DelayInformationProvider; import org.junit.Test; import org.xmlpull.v1.XmlPullParser; @@ -92,55 +91,10 @@ public class DelayInformationTest extends InitExtensions { } - @Test - public void delayInfoTest() throws Exception { - DelayInformationProvider p = new DelayInfoProvider(); - DelayInfo delayInfo; - XmlPullParser parser; - String control; - GregorianCalendar calendar = new GregorianCalendar(2002, 9 - 1, 10, 23, 8, 25); - calendar.setTimeZone(TimeZone.getTimeZone("UTC")); - Date date = calendar.getTime(); - - control = XMLBuilder.create("delay") - .a("xmlns", "urn:xmpp:delay") - .a("from", "capulet.com") - .a("stamp", "2002-09-10T23:08:25Z") - .t("Offline Storage") - .asString(outputProperties); - - parser = PacketParserUtils.getParserFor(control); - delayInfo = (DelayInfo) p.parseExtension(parser); - - assertEquals("capulet.com", delayInfo.getFrom()); - assertEquals(date, delayInfo.getStamp()); - assertEquals("Offline Storage", delayInfo.getReason()); - - assertEquals(XmlPullParser.END_TAG, parser.getEventType()); - assertEquals("delay", parser.getName()); - - control = XMLBuilder.create("delay") - .a("xmlns", "urn:xmpp:delay") - .a("from", "capulet.com") - .a("stamp", "2002-09-10T23:08:25Z") - .asString(outputProperties); - - parser = PacketParserUtils.getParserFor(control); - delayInfo = (DelayInfo) p.parseExtension(parser); - - assertEquals("capulet.com", delayInfo.getFrom()); - assertEquals(date, delayInfo.getStamp()); - assertNull(delayInfo.getReason()); - - assertEquals(XmlPullParser.END_TAG, parser.getEventType()); - assertEquals("delay", parser.getName()); - - } - @Test public void dateFormatsTest() throws Exception { - DelayInformationProvider p = new DelayInfoProvider(); - DelayInfo delayInfo; + DelayInformationProvider p = new DelayInformationProvider(); + DelayInformation delayInfo; String control; GregorianCalendar calendar = new GregorianCalendar(2002, 9 - 1, 10, 23, 8, 25); calendar.setTimeZone(TimeZone.getTimeZone("UTC")); @@ -152,7 +106,7 @@ public class DelayInformationTest extends InitExtensions { .a("stamp", "2002-09-10T23:08:25.12Z") .asString(outputProperties); - delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control)); + delayInfo = (DelayInformation) p.parseExtension(PacketParserUtils.getParserFor(control)); GregorianCalendar cal = (GregorianCalendar) calendar.clone(); cal.add(Calendar.MILLISECOND, 12); @@ -165,7 +119,7 @@ public class DelayInformationTest extends InitExtensions { .a("stamp", "2002-09-10T23:08:25Z") .asString(outputProperties); - delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control)); + delayInfo = (DelayInformation) p.parseExtension(PacketParserUtils.getParserFor(control)); assertEquals(calendar.getTime(), delayInfo.getStamp()); @@ -176,18 +130,18 @@ public class DelayInformationTest extends InitExtensions { .a("stamp", "2002-9-10T23:08:25Z") .asString(outputProperties); - delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control)); + delayInfo = (DelayInformation) p.parseExtension(PacketParserUtils.getParserFor(control)); assertEquals(calendar.getTime(), delayInfo.getStamp()); // XEP-0091 date format - control = XMLBuilder.create("delay") - .a("xmlns", "urn:xmpp:delay") + control = XMLBuilder.create("x") + .a("xmlns", "jabber:x:delay") .a("from", "capulet.com") .a("stamp", "20020910T23:08:25") .asString(outputProperties); - delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control)); + delayInfo = (DelayInformation) p.parseExtension(PacketParserUtils.getParserFor(control)); assertEquals(calendar.getTime(), delayInfo.getStamp()); @@ -201,39 +155,27 @@ public class DelayInformationTest extends InitExtensions { dateInPast.add(Calendar.DAY_OF_MONTH, -3); dateInPast.set(Calendar.MILLISECOND, 0); - control = XMLBuilder.create("delay") - .a("xmlns", "urn:xmpp:delay") + control = XMLBuilder.create("x") + .a("xmlns", "jabber:x:delay") .a("from", "capulet.com") .a("stamp", dateFormat.format(dateInPast.getTime())) .asString(outputProperties); - delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control)); + delayInfo = (DelayInformation) p.parseExtension(PacketParserUtils.getParserFor(control)); assertEquals(dateInPast.getTime(), delayInfo.getStamp()); // XEP-0091 date format from SMACK-243 - control = XMLBuilder.create("delay") - .a("xmlns", "urn:xmpp:delay") + control = XMLBuilder.create("x") + .a("xmlns", "jabber:x:delay") .a("from", "capulet.com") .a("stamp", "200868T09:16:20") .asString(outputProperties); - delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control)); + delayInfo = (DelayInformation) p.parseExtension(PacketParserUtils.getParserFor(control)); Date controlDate = XmppDateTime.parseDate("2008-06-08T09:16:20.0Z"); assertEquals(controlDate, delayInfo.getStamp()); - - // invalid date format - control = XMLBuilder.create("delay") - .a("xmlns", "urn:xmpp:delay") - .a("from", "capulet.com") - .a("stamp", "yesterday") - .asString(outputProperties); - - delayInfo = (DelayInfo) p.parseExtension(PacketParserUtils.getParserFor(control)); - - assertNotNull(delayInfo.getStamp()); - } @Test @@ -243,35 +185,19 @@ public class DelayInformationTest extends InitExtensions { Presence presence = PacketParserUtils.parsePresence(PacketParserUtils.getParserFor(stanza)); - DelayInformation delay = (DelayInformation) presence.getExtension("urn:xmpp:delay"); + DelayInformation delay = DelayInformationManager.getXep203DelayInformation(presence); assertNotNull(delay); Date date = XmppDateTime.parseDate("2002-09-10T23:41:07Z"); assertEquals(date, delay.getStamp()); } - @Test - public void validatePresenceWithLegacyDelayed() throws Exception { - String stanza = "" - + ""; - - Presence presence = PacketParserUtils.parsePresence(PacketParserUtils.getParserFor(stanza)); - - DelayInformation delay = (DelayInformation) presence.getExtension("jabber:x:delay"); - assertNotNull(delay); - Date date = XmppDateTime.parseDate("20020910T23:41:07"); - Calendar cal = Calendar.getInstance(); - cal.setTimeZone(TimeZone.getTimeZone("GMT")); - cal.setTime(date); - assertEquals(cal.getTime(), delay.getStamp()); - } - @Test public void parsePresenceWithInvalidLegacyDelayed() throws Exception { String stanza = "" + ""; Presence presence = PacketParserUtils.parsePresence(PacketParserUtils.getParserFor(stanza)); - DelayInformation delay = (DelayInformation) presence.getExtension("urn:xmpp:delay"); + DelayInformation delay = DelayInformationManager.getXep203DelayInformation(presence); assertNull((Object)delay); } }