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.text.DateFormat;
020import java.text.SimpleDateFormat;
021import java.util.Date;
022import java.util.TimeZone;
023
024import org.jivesoftware.smack.packet.PacketExtension;
025
026/**
027 * Represents timestamp information about data stored for later delivery. A DelayInformation will 
028 * always includes the timestamp when the packet was originally sent and may include more 
029 * information such as the JID of the entity that originally sent the packet as well as the reason
030 * for the delay.<p>
031 * 
032 * For more information see <a href="http://xmpp.org/extensions/xep-0091.html">XEP-0091</a>
033 * and <a href="http://xmpp.org/extensions/xep-0203.html">XEP-0203</a>.
034 * 
035 * @author Gaston Dombiak
036 */
037public class DelayInformation implements PacketExtension {
038
039    /**
040     * Date format according to the obsolete XEP-0091 specification.
041     * XEP-0091 recommends to use this old format for date-time instead of
042     * the one specified in XEP-0082.
043     * <p>
044     * Date formats are not synchronized. Since multiple threads access the format concurrently,
045     * it must be synchronized externally. 
046     */
047    public static final DateFormat XEP_0091_UTC_FORMAT = new SimpleDateFormat(
048            "yyyyMMdd'T'HH:mm:ss");
049    static {
050        XEP_0091_UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
051    }
052
053    private Date stamp;
054    private String from;
055    private String reason;
056
057    /**
058     * Creates a new instance with the specified timestamp. 
059     * @param stamp the timestamp
060     */
061    public DelayInformation(Date stamp) {
062        super();
063        this.stamp = stamp;
064    }
065
066    /**
067     * Returns the JID of the entity that originally sent the packet or that delayed the 
068     * delivery of the packet or <tt>null</tt> if this information is not available.
069     * 
070     * @return the JID of the entity that originally sent the packet or that delayed the 
071     *         delivery of the packet.
072     */
073    public String getFrom() {
074        return from;
075    }
076
077    /**
078     * Sets the JID of the entity that originally sent the packet or that delayed the 
079     * delivery of the packet or <tt>null</tt> if this information is not available.
080     * 
081     * @param from the JID of the entity that originally sent the packet.
082     */
083    public void setFrom(String from) {
084        this.from = from;
085    }
086
087    /**
088     * Returns the timestamp when the packet was originally sent. The returned Date is 
089     * be understood as UTC.
090     * 
091     * @return the timestamp when the packet was originally sent.
092     */
093    public Date getStamp() {
094        return stamp;
095    }
096
097    /**
098     * Returns a natural-language description of the reason for the delay or <tt>null</tt> if 
099     * this information is not available.
100     * 
101     * @return a natural-language description of the reason for the delay or <tt>null</tt>.
102     */
103    public String getReason() {
104        return reason;
105    }
106
107    /**
108     * Sets a natural-language description of the reason for the delay or <tt>null</tt> if 
109     * this information is not available.
110     * 
111     * @param reason a natural-language description of the reason for the delay or <tt>null</tt>.
112     */
113    public void setReason(String reason) {
114        this.reason = reason;
115    }
116
117    public String getElementName() {
118        return "x";
119    }
120
121    public String getNamespace() {
122        return "jabber:x:delay";
123    }
124
125    public String toXML() {
126        StringBuilder buf = new StringBuilder();
127        buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
128                "\"");
129        buf.append(" stamp=\"");
130        synchronized (XEP_0091_UTC_FORMAT) {
131            buf.append(XEP_0091_UTC_FORMAT.format(stamp));
132        }
133        buf.append("\"");
134        if (from != null && from.length() > 0) {
135            buf.append(" from=\"").append(from).append("\"");
136        }
137        buf.append(">");
138        if (reason != null && reason.length() > 0) {
139            buf.append(reason);
140        }
141        buf.append("</").append(getElementName()).append(">");
142        return buf.toString();
143    }
144
145}