Shuyi Chen | d7955ce | 2013-05-22 14:51:55 -0700 | [diff] [blame] | 1 | /** |
| 2 | * $RCSfile$ |
| 3 | * $Revision$ |
| 4 | * $Date$ |
| 5 | * |
| 6 | * Copyright 2003-2007 Jive Software. |
| 7 | * |
| 8 | * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | package org.jivesoftware.smackx.packet; |
| 22 | |
| 23 | import java.text.DateFormat; |
| 24 | import java.text.SimpleDateFormat; |
| 25 | import java.util.Date; |
| 26 | import java.util.TimeZone; |
| 27 | |
| 28 | import org.jivesoftware.smack.packet.PacketExtension; |
| 29 | |
| 30 | /** |
| 31 | * Represents timestamp information about data stored for later delivery. A DelayInformation will |
| 32 | * always includes the timestamp when the packet was originally sent and may include more |
| 33 | * information such as the JID of the entity that originally sent the packet as well as the reason |
| 34 | * for the delay.<p> |
| 35 | * |
| 36 | * For more information see <a href="http://xmpp.org/extensions/xep-0091.html">XEP-0091</a> |
| 37 | * and <a href="http://xmpp.org/extensions/xep-0203.html">XEP-0203</a>. |
| 38 | * |
| 39 | * @author Gaston Dombiak |
| 40 | */ |
| 41 | public class DelayInformation implements PacketExtension { |
| 42 | |
| 43 | /** |
| 44 | * Date format according to the obsolete XEP-0091 specification. |
| 45 | * XEP-0091 recommends to use this old format for date-time instead of |
| 46 | * the one specified in XEP-0082. |
| 47 | * <p> |
| 48 | * Date formats are not synchronized. Since multiple threads access the format concurrently, |
| 49 | * it must be synchronized externally. |
| 50 | */ |
| 51 | public static final DateFormat XEP_0091_UTC_FORMAT = new SimpleDateFormat( |
| 52 | "yyyyMMdd'T'HH:mm:ss"); |
| 53 | static { |
| 54 | XEP_0091_UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC")); |
| 55 | } |
| 56 | |
| 57 | private Date stamp; |
| 58 | private String from; |
| 59 | private String reason; |
| 60 | |
| 61 | /** |
| 62 | * Creates a new instance with the specified timestamp. |
| 63 | * @param stamp the timestamp |
| 64 | */ |
| 65 | public DelayInformation(Date stamp) { |
| 66 | super(); |
| 67 | this.stamp = stamp; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns the JID of the entity that originally sent the packet or that delayed the |
| 72 | * delivery of the packet or <tt>null</tt> if this information is not available. |
| 73 | * |
| 74 | * @return the JID of the entity that originally sent the packet or that delayed the |
| 75 | * delivery of the packet. |
| 76 | */ |
| 77 | public String getFrom() { |
| 78 | return from; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Sets the JID of the entity that originally sent the packet or that delayed the |
| 83 | * delivery of the packet or <tt>null</tt> if this information is not available. |
| 84 | * |
| 85 | * @param from the JID of the entity that originally sent the packet. |
| 86 | */ |
| 87 | public void setFrom(String from) { |
| 88 | this.from = from; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the timestamp when the packet was originally sent. The returned Date is |
| 93 | * be understood as UTC. |
| 94 | * |
| 95 | * @return the timestamp when the packet was originally sent. |
| 96 | */ |
| 97 | public Date getStamp() { |
| 98 | return stamp; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Returns a natural-language description of the reason for the delay or <tt>null</tt> if |
| 103 | * this information is not available. |
| 104 | * |
| 105 | * @return a natural-language description of the reason for the delay or <tt>null</tt>. |
| 106 | */ |
| 107 | public String getReason() { |
| 108 | return reason; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Sets a natural-language description of the reason for the delay or <tt>null</tt> if |
| 113 | * this information is not available. |
| 114 | * |
| 115 | * @param reason a natural-language description of the reason for the delay or <tt>null</tt>. |
| 116 | */ |
| 117 | public void setReason(String reason) { |
| 118 | this.reason = reason; |
| 119 | } |
| 120 | |
| 121 | public String getElementName() { |
| 122 | return "x"; |
| 123 | } |
| 124 | |
| 125 | public String getNamespace() { |
| 126 | return "jabber:x:delay"; |
| 127 | } |
| 128 | |
| 129 | public String toXML() { |
| 130 | StringBuilder buf = new StringBuilder(); |
| 131 | buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append( |
| 132 | "\""); |
| 133 | buf.append(" stamp=\""); |
| 134 | synchronized (XEP_0091_UTC_FORMAT) { |
| 135 | buf.append(XEP_0091_UTC_FORMAT.format(stamp)); |
| 136 | } |
| 137 | buf.append("\""); |
| 138 | if (from != null && from.length() > 0) { |
| 139 | buf.append(" from=\"").append(from).append("\""); |
| 140 | } |
| 141 | buf.append(">"); |
| 142 | if (reason != null && reason.length() > 0) { |
| 143 | buf.append(reason); |
| 144 | } |
| 145 | buf.append("</").append(getElementName()).append(">"); |
| 146 | return buf.toString(); |
| 147 | } |
| 148 | |
| 149 | } |