1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-12-08 12:01:09 +01:00

Remove decorators for "Legacy Delayed Delivery"

(aka. XEP-91) in favor of Delayed Delivery (XEP-203)

SMACK-578
This commit is contained in:
Florian Schmaus 2014-06-30 13:36:49 +02:00
parent 37279eaa06
commit fdaf7940fb
13 changed files with 259 additions and 365 deletions

View file

@ -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 <a href="http://xmpp.org/extensions/xep-0203.html">Delayed Delivery (XEP-203)</a>
*
*/
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
* <p>
* Prefer {@link #getDelayInformation(Packet)} over this method for backwards compatibility.
* </p>
* @param packet
* @return the Delayed Delivery information or <code>null</code>
*/
public static DelayInformation getXep203DelayInformation(Packet packet) {
return (DelayInformation) packet.getExtension(DelayInformation.ELEMENT, DelayInformation.NAMESPACE);
}
/**
* Get Delayed Delivery information as defined in XEP-91
* <p>
* Prefer {@link #getDelayInformation(Packet)} over this method for backwards compatibility.
* </p>
* @param packet
* @return the Delayed Delivery information or <code>null</code>
*/
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 <code>null</code>
*/
public static DelayInformation getDelayInformation(Packet packet) {
DelayInformation delayInformation = getXep203DelayInformation(packet);
if (delayInformation != null) {
return delayInformation;
}
return getLegacyDelayInformation(packet);
}
/**
* Get the Delayed Delivery timestamp or <code>null</code>
*
* @param packet
* @return the Delayed Delivery timestamp or <code>null</code>
*/
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;
}
}

View file

@ -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 <b>Delay Delivery</b> specification <a href="http://xmpp.org/extensions/xep-0203.html">XEP-0203</a> and
* the old one <a href="http://xmpp.org/extensions/xep-0091.html">XEP-0091</a>.
*
* 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("</").append(getElementName()).append(">");
return buf.toString();
}
}

View file

@ -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 <a href="http://xmpp.org/extensions/xep-0203.html">XEP-0203</a>.
*
* @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.
* <p>
* 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 <tt>null</tt> 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 <tt>null</tt> if
* this information is not available.
*
* @param reason a natural-language description of the reason for the delay or <tt>null</tt>.
*/
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("</").append(getElementName()).append(">");
return buf.toString();
xml.closeElement(this);
return xml;
}
}

View file

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

View file

@ -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
* <a href="http://xmpp.org/extensions/xep-0203.html">Delay Information XEP-0203</a> to be
* backward compatible with <a href="http://xmpp.org/extensions/xep-0091.html">XEP-0091</a>.
*
* <p>This provider must be registered in the <b>smack.properties</b> file for the element
* <b>delay</b> with namespace <b>urn:xmpp:delay</b></p>
*
* @author Robin Collier
*/
public class DelayInfoProvider extends DelayInformationProvider
{
@Override
public PacketExtension parseExtension(XmlPullParser parser) throws Exception
{
return new DelayInfo((DelayInformation)super.parseExtension(parser));
}
}

View file

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

View file

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

View file

@ -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 <a href="http://xmpp.org/extensions/xep-0297.html">XEP-0297</a>: 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;
}
}

View file

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

View file

@ -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<Item>)itemsElem.getItems(), getSubscriptionIds(packet), (delay == null ? null : delay.getStamp()));
ItemPublishEvent eventItems = new ItemPublishEvent(itemsElem.getNode(), (List<Item>)itemsElem.getItems(), getSubscriptionIds(packet), DelayInformationManager.getDelayTimestamp(packet));
listener.handlePublishedItems(eventItems);
}
}

View file

@ -114,13 +114,13 @@
<extensionProvider>
<elementName>x</elementName>
<namespace>jabber:x:delay</namespace>
<className>org.jivesoftware.smackx.delay.provider.DelayInformationProvider</className>
<className>org.jivesoftware.smackx.delay.provider.LegacyDelayInformationProvider</className>
</extensionProvider>
<extensionProvider>
<elementName>delay</elementName>
<namespace>urn:xmpp:delay</namespace>
<className>org.jivesoftware.smackx.delay.provider.DelayInfoProvider</className>
<className>org.jivesoftware.smackx.delay.provider.DelayInformationProvider</className>
</extensionProvider>
<!-- Version -->