DelayInformation.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.delay.packet;

  18. import java.util.Date;

  19. import org.jivesoftware.smack.packet.Stanza;
  20. import org.jivesoftware.smack.packet.ExtensionElement;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;
  22. import org.jxmpp.util.XmppDateTime;

  23. /**
  24.  * Represents timestamp information about data stored for later delivery. A DelayInformation will
  25.  * always includes the timestamp when the packet was originally sent and may include more
  26.  * information such as the JID of the entity that originally sent the packet as well as the reason
  27.  * for the delay.<p>
  28.  *
  29.  * For more information see <a href="http://xmpp.org/extensions/xep-0091.html">XEP-0091</a>
  30.  * and <a href="http://xmpp.org/extensions/xep-0203.html">XEP-0203</a>.
  31.  *
  32.  * @author Gaston Dombiak
  33.  * @author Florian Schmaus
  34.  */
  35. public class DelayInformation implements ExtensionElement {
  36.     public static final String ELEMENT = "delay";
  37.     public static final String NAMESPACE = "urn:xmpp:delay";

  38.     private final Date stamp;
  39.     private final String from;
  40.     private final String reason;

  41.     /**
  42.      * Creates a new instance with the specified timestamp.
  43.      * @param stamp the timestamp
  44.      */
  45.     public DelayInformation(Date stamp, String from, String reason) {
  46.         this.stamp = stamp;
  47.         this.from = from;
  48.         this.reason = reason;
  49.     }

  50.     public DelayInformation(Date stamp) {
  51.         this(stamp, null, null);
  52.     }

  53.     /**
  54.      * Returns the JID of the entity that originally sent the packet or that delayed the
  55.      * delivery of the packet or <tt>null</tt> if this information is not available.
  56.      *
  57.      * @return the JID of the entity that originally sent the packet or that delayed the
  58.      *         delivery of the packet.
  59.      */
  60.     public String getFrom() {
  61.         return from;
  62.     }

  63.     /**
  64.      * Returns the timestamp when the packet was originally sent. The returned Date is
  65.      * be understood as UTC.
  66.      *
  67.      * @return the timestamp when the packet was originally sent.
  68.      */
  69.     public Date getStamp() {
  70.         return stamp;
  71.     }

  72.     /**
  73.      * Returns a natural-language description of the reason for the delay or <tt>null</tt> if
  74.      * this information is not available.
  75.      *
  76.      * @return a natural-language description of the reason for the delay or <tt>null</tt>.
  77.      */
  78.     public String getReason() {
  79.         return reason;
  80.     }

  81.     public String getElementName() {
  82.         return ELEMENT;
  83.     }

  84.     public String getNamespace() {
  85.         return NAMESPACE;
  86.     }

  87.     @Override
  88.     public XmlStringBuilder toXML() {
  89.         XmlStringBuilder xml = new XmlStringBuilder(this);
  90.         xml.attribute("stamp", XmppDateTime.formatXEP0082Date(stamp));
  91.         xml.optAttribute("from", from);
  92.         xml.rightAngleBracket();
  93.         xml.optAppend(reason);
  94.         xml.closeElement(this);
  95.         return xml;
  96.     }

  97.     /**
  98.      *
  99.      * @param packet
  100.      * @return the DelayInformation or null
  101.      * @deprecated use {@link #from(Stanza)} instead
  102.      */
  103.     @Deprecated
  104.     public static DelayInformation getFrom(Stanza packet) {
  105.         return from(packet);
  106.     }

  107.     /**
  108.      *
  109.      * @param packet
  110.      * @return the DelayInformation or null
  111.      */
  112.     public static DelayInformation from(Stanza packet) {
  113.         return packet.getExtension(ELEMENT, NAMESPACE);
  114.     }
  115. }