001/**
002 *
003 * Copyright © 2014 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.delay;
018
019import java.util.Date;
020
021import org.jivesoftware.smack.packet.Stanza;
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smackx.delay.packet.DelayInformation;
024
025/**
026 * Delayed Delivery (XEP-203)
027 *
028 * @author Florian Schmaus
029 * @see <a href="http://xmpp.org/extensions/xep-0203.html">Delayed Delivery (XEP-203)</a>
030 *
031 */
032public class DelayInformationManager {
033
034    public static final String LEGACY_DELAYED_DELIVERY_NAMESPACE = "jabber:x:delay";
035    public static final String LEGACY_DELAYED_DELIVERY_ELEMENT = "x";
036
037
038    /**
039     * Get Delayed Delivery information as defined in XEP-203
040     * <p>
041     * Prefer {@link #getDelayInformation(Stanza)} over this method for backwards compatibility.
042     * </p>
043     * @param packet
044     * @return the Delayed Delivery information or <code>null</code>
045     */
046    public static DelayInformation getXep203DelayInformation(Stanza packet) {
047        return DelayInformation.from(packet);
048    }
049
050    /**
051     * Get Delayed Delivery information as defined in XEP-91
052     * <p>
053     * Prefer {@link #getDelayInformation(Stanza)} over this method for backwards compatibility.
054     * </p>
055     * @param packet
056     * @return the Delayed Delivery information or <code>null</code>
057     */
058    public static DelayInformation getLegacyDelayInformation(Stanza packet) {
059        return packet.getExtension(LEGACY_DELAYED_DELIVERY_ELEMENT, LEGACY_DELAYED_DELIVERY_NAMESPACE);
060    }
061
062    /**
063     * Get Delayed Delivery information. This method first looks for a PacketExtension with the
064     * XEP-203 namespace and falls back to the XEP-91 namespace.
065     *
066     * @param packet
067     * @return the Delayed Delivery information or <code>null</code>
068     */
069    public static DelayInformation getDelayInformation(Stanza packet) {
070        DelayInformation delayInformation = getXep203DelayInformation(packet);
071        if (delayInformation != null) {
072            return delayInformation;
073        }
074        return getLegacyDelayInformation(packet);
075    }
076
077    /**
078     * Get the Delayed Delivery timestamp or <code>null</code>
079     *
080     * @param packet
081     * @return the Delayed Delivery timestamp or <code>null</code>
082     */
083    public static Date getDelayTimestamp(Stanza packet) {
084        DelayInformation delayInformation = getDelayInformation(packet);
085        if (delayInformation == null) {
086            return null;
087        }
088        return delayInformation.getStamp();
089    }
090
091     /**
092     * Check if the given stanza is a delayed stanza as of XEP-203.
093     *
094     * @param packet
095     * @return true if the stanza got delayed.
096     */
097    public static boolean isDelayedStanza(Stanza packet) {
098        ExtensionElement packetExtension = getDelayInformation(packet);
099        return packetExtension != null;
100    }
101}