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.ExtensionElement;
022import org.jivesoftware.smack.packet.Stanza;
023
024import org.jivesoftware.smackx.delay.packet.DelayInformation;
025
026/**
027 * Delayed Delivery (XEP-203).
028 *
029 * @author Florian Schmaus
030 * @see <a href="http://xmpp.org/extensions/xep-0203.html">Delayed Delivery (XEP-203)</a>
031 *
032 */
033public class DelayInformationManager {
034
035    public static final String LEGACY_DELAYED_DELIVERY_NAMESPACE = "jabber:x:delay";
036    public static final String LEGACY_DELAYED_DELIVERY_ELEMENT = "x";
037
038
039    /**
040     * Get Delayed Delivery information as defined in XEP-203
041     * <p>
042     * Prefer {@link #getDelayInformation(Stanza)} over this method for backwards compatibility.
043     * </p>
044     * @param packet
045     * @return the Delayed Delivery information or <code>null</code>
046     */
047    public static DelayInformation getXep203DelayInformation(Stanza packet) {
048        return DelayInformation.from(packet);
049    }
050
051    /**
052     * Get Delayed Delivery information as defined in XEP-91
053     * <p>
054     * Prefer {@link #getDelayInformation(Stanza)} over this method for backwards compatibility.
055     * </p>
056     * @param packet
057     * @return the Delayed Delivery information or <code>null</code>
058     */
059    public static DelayInformation getLegacyDelayInformation(Stanza packet) {
060        return packet.getExtension(LEGACY_DELAYED_DELIVERY_ELEMENT, LEGACY_DELAYED_DELIVERY_NAMESPACE);
061    }
062
063    /**
064     * Get Delayed Delivery information. This method first looks for a PacketExtension with the
065     * XEP-203 namespace and falls back to the XEP-91 namespace.
066     *
067     * @param packet
068     * @return the Delayed Delivery information or <code>null</code>
069     */
070    public static DelayInformation getDelayInformation(Stanza packet) {
071        DelayInformation delayInformation = getXep203DelayInformation(packet);
072        if (delayInformation != null) {
073            return delayInformation;
074        }
075        return getLegacyDelayInformation(packet);
076    }
077
078    /**
079     * Get the Delayed Delivery timestamp or <code>null</code>.
080     *
081     * @param packet
082     * @return the Delayed Delivery timestamp or <code>null</code>
083     */
084    public static Date getDelayTimestamp(Stanza packet) {
085        DelayInformation delayInformation = getDelayInformation(packet);
086        if (delayInformation == null) {
087            return null;
088        }
089        return delayInformation.getStamp();
090    }
091
092     /**
093     * Check if the given stanza is a delayed stanza as of XEP-203.
094     *
095     * @param packet
096     * @return true if the stanza got delayed.
097     */
098    public static boolean isDelayedStanza(Stanza packet) {
099        ExtensionElement packetExtension = getDelayInformation(packet);
100        return packetExtension != null;
101    }
102}