DelayInformationManager.java

  1. /**
  2.  *
  3.  * Copyright © 2014 Florian Schmaus
  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;

  18. import java.util.Date;

  19. import org.jivesoftware.smack.packet.Stanza;
  20. import org.jivesoftware.smack.packet.ExtensionElement;
  21. import org.jivesoftware.smackx.delay.packet.DelayInformation;

  22. /**
  23.  * Delayed Delivery (XEP-203)
  24.  *
  25.  * @author Florian Schmaus
  26.  * @see <a href="http://xmpp.org/extensions/xep-0203.html">Delayed Delivery (XEP-203)</a>
  27.  *
  28.  */
  29. public class DelayInformationManager {

  30.     public static final String LEGACY_DELAYED_DELIVERY_NAMESPACE = "jabber:x:delay";
  31.     public static final String LEGACY_DELAYED_DELIVERY_ELEMENT = "x";


  32.     /**
  33.      * Get Delayed Delivery information as defined in XEP-203
  34.      * <p>
  35.      * Prefer {@link #getDelayInformation(Stanza)} over this method for backwards compatibility.
  36.      * </p>
  37.      * @param packet
  38.      * @return the Delayed Delivery information or <code>null</code>
  39.      */
  40.     public static DelayInformation getXep203DelayInformation(Stanza packet) {
  41.         return DelayInformation.from(packet);
  42.     }

  43.     /**
  44.      * Get Delayed Delivery information as defined in XEP-91
  45.      * <p>
  46.      * Prefer {@link #getDelayInformation(Stanza)} over this method for backwards compatibility.
  47.      * </p>
  48.      * @param packet
  49.      * @return the Delayed Delivery information or <code>null</code>
  50.      */
  51.     public static DelayInformation getLegacyDelayInformation(Stanza packet) {
  52.         return packet.getExtension(LEGACY_DELAYED_DELIVERY_ELEMENT, LEGACY_DELAYED_DELIVERY_NAMESPACE);
  53.     }

  54.     /**
  55.      * Get Delayed Delivery information. This method first looks for a PacketExtension with the
  56.      * XEP-203 namespace and falls back to the XEP-91 namespace.
  57.      *
  58.      * @param packet
  59.      * @return the Delayed Delivery information or <code>null</code>
  60.      */
  61.     public static DelayInformation getDelayInformation(Stanza packet) {
  62.         DelayInformation delayInformation = getXep203DelayInformation(packet);
  63.         if (delayInformation != null) {
  64.             return delayInformation;
  65.         }
  66.         return getLegacyDelayInformation(packet);
  67.     }

  68.     /**
  69.      * Get the Delayed Delivery timestamp or <code>null</code>
  70.      *
  71.      * @param packet
  72.      * @return the Delayed Delivery timestamp or <code>null</code>
  73.      */
  74.     public static Date getDelayTimestamp(Stanza packet) {
  75.         DelayInformation delayInformation = getDelayInformation(packet);
  76.         if (delayInformation == null) {
  77.             return null;
  78.         }
  79.         return delayInformation.getStamp();
  80.     }

  81.      /**
  82.      * Check if the given stanza is a delayed stanza as of XEP-203.
  83.      *
  84.      * @param packet
  85.      * @return true if the stanza got delayed.
  86.      */
  87.     public static boolean isDelayedStanza(Stanza packet) {
  88.         ExtensionElement packetExtension = getDelayInformation(packet);
  89.         return packetExtension != null;
  90.     }
  91. }