001/**
002 *
003 * Copyright the original author or authors
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.packet;
018
019import java.util.Date;
020
021import org.jivesoftware.smack.packet.ExtensionElement;
022import org.jivesoftware.smack.packet.Stanza;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025import org.jxmpp.util.XmppDateTime;
026
027/**
028 * Represents timestamp information about data stored for later delivery. A DelayInformation will 
029 * always includes the timestamp when the stanza(/packet) was originally sent and may include more 
030 * information such as the JID of the entity that originally sent the stanza(/packet) as well as the reason
031 * for the delay.<p>
032 * 
033 * For more information see <a href="http://xmpp.org/extensions/xep-0091.html">XEP-0091</a>
034 * and <a href="http://xmpp.org/extensions/xep-0203.html">XEP-0203</a>.
035 * 
036 * @author Gaston Dombiak
037 * @author Florian Schmaus
038 */
039public class DelayInformation implements ExtensionElement {
040    public static final String ELEMENT = "delay";
041    public static final String NAMESPACE = "urn:xmpp:delay";
042
043    private final Date stamp;
044    private final String from;
045    private final String reason;
046
047    /**
048     * Creates a new instance with the specified timestamp. 
049     * @param stamp the timestamp
050     * @param from sender
051     * @param reason reason of delay.
052     */
053    public DelayInformation(Date stamp, String from, String reason) {
054        this.stamp = stamp;
055        this.from = from;
056        this.reason = reason;
057    }
058
059    public DelayInformation(Date stamp) {
060        this(stamp, null, null);
061    }
062
063    /**
064     * Returns the JID of the entity that originally sent the stanza(/packet) or that delayed the 
065     * delivery of the stanza(/packet) or <tt>null</tt> if this information is not available.
066     * 
067     * @return the JID of the entity that originally sent the stanza(/packet) or that delayed the 
068     *         delivery of the packet.
069     */
070    public String getFrom() {
071        return from;
072    }
073
074    /**
075     * Returns the timestamp when the stanza(/packet) was originally sent. The returned Date is 
076     * be understood as UTC.
077     * 
078     * @return the timestamp when the stanza(/packet) was originally sent.
079     */
080    public Date getStamp() {
081        return stamp;
082    }
083
084    /**
085     * Returns a natural-language description of the reason for the delay or <tt>null</tt> if 
086     * this information is not available.
087     * 
088     * @return a natural-language description of the reason for the delay or <tt>null</tt>.
089     */
090    public String getReason() {
091        return reason;
092    }
093
094    @Override
095    public String getElementName() {
096        return ELEMENT;
097    }
098
099    @Override
100    public String getNamespace() {
101        return NAMESPACE;
102    }
103
104    @Override
105    public XmlStringBuilder toXML() {
106        XmlStringBuilder xml = new XmlStringBuilder(this);
107        xml.attribute("stamp", XmppDateTime.formatXEP0082Date(stamp));
108        xml.optAttribute("from", from);
109        xml.rightAngleBracket();
110        xml.optAppend(reason);
111        xml.closeElement(this);
112        return xml;
113    }
114
115    /**
116     * Return delay information from the given stanza.
117     *
118     * @param packet
119     * @return the DelayInformation or null
120     * @deprecated use {@link #from(Stanza)} instead
121     */
122    @Deprecated
123    public static DelayInformation getFrom(Stanza packet) {
124        return from(packet);
125    }
126
127    /**
128     * Return delay information from the given stanza.
129     *
130     * @param packet
131     * @return the DelayInformation or null
132     */
133    public static DelayInformation from(Stanza packet) {
134        return packet.getExtension(ELEMENT, NAMESPACE);
135    }
136}