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