blob: 522250a0fce4e2fc848fde7dd1df34d323b3693c [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
2 * $RCSfile$
3 * $Revision$
4 * $Date$
5 *
6 * Copyright 2003-2006 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
21package org.jivesoftware.smackx.packet;
22
23import org.jivesoftware.smack.packet.PacketExtension;
24
25import java.util.ArrayList;
26import java.util.Iterator;
27import java.util.List;
28
29/**
30 * Packet extension that contains the list of addresses that a packet should be sent or was sent.
31 *
32 * @author Gaston Dombiak
33 */
34public class MultipleAddresses implements PacketExtension {
35
36 public static final String BCC = "bcc";
37 public static final String CC = "cc";
38 public static final String NO_REPLY = "noreply";
39 public static final String REPLY_ROOM = "replyroom";
40 public static final String REPLY_TO = "replyto";
41 public static final String TO = "to";
42
43
44 private List<Address> addresses = new ArrayList<Address>();
45
46 /**
47 * Adds a new address to which the packet is going to be sent or was sent.
48 *
49 * @param type on of the static type (BCC, CC, NO_REPLY, REPLY_ROOM, etc.)
50 * @param jid the JID address of the recipient.
51 * @param node used to specify a sub-addressable unit at a particular JID, corresponding to
52 * a Service Discovery node.
53 * @param desc used to specify human-readable information for this address.
54 * @param delivered true when the packet was already delivered to this address.
55 * @param uri used to specify an external system address, such as a sip:, sips:, or im: URI.
56 */
57 public void addAddress(String type, String jid, String node, String desc, boolean delivered,
58 String uri) {
59 // Create a new address with the specificed configuration
60 Address address = new Address(type);
61 address.setJid(jid);
62 address.setNode(node);
63 address.setDescription(desc);
64 address.setDelivered(delivered);
65 address.setUri(uri);
66 // Add the new address to the list of multiple recipients
67 addresses.add(address);
68 }
69
70 /**
71 * Indicate that the packet being sent should not be replied.
72 */
73 public void setNoReply() {
74 // Create a new address with the specificed configuration
75 Address address = new Address(NO_REPLY);
76 // Add the new address to the list of multiple recipients
77 addresses.add(address);
78 }
79
80 /**
81 * Returns the list of addresses that matches the specified type. Examples of address
82 * type are: TO, CC, BCC, etc..
83 *
84 * @param type Examples of address type are: TO, CC, BCC, etc.
85 * @return the list of addresses that matches the specified type.
86 */
87 public List<Address> getAddressesOfType(String type) {
88 List<Address> answer = new ArrayList<Address>(addresses.size());
89 for (Iterator<Address> it = addresses.iterator(); it.hasNext();) {
90 Address address = (Address) it.next();
91 if (address.getType().equals(type)) {
92 answer.add(address);
93 }
94 }
95
96 return answer;
97 }
98
99 public String getElementName() {
100 return "addresses";
101 }
102
103 public String getNamespace() {
104 return "http://jabber.org/protocol/address";
105 }
106
107 public String toXML() {
108 StringBuilder buf = new StringBuilder();
109 buf.append("<").append(getElementName());
110 buf.append(" xmlns=\"").append(getNamespace()).append("\">");
111 // Loop through all the addresses and append them to the string buffer
112 for (Iterator<Address> i = addresses.iterator(); i.hasNext();) {
113 Address address = (Address) i.next();
114 buf.append(address.toXML());
115 }
116 buf.append("</").append(getElementName()).append(">");
117 return buf.toString();
118 }
119
120 public static class Address {
121
122 private String type;
123 private String jid;
124 private String node;
125 private String description;
126 private boolean delivered;
127 private String uri;
128
129 private Address(String type) {
130 this.type = type;
131 }
132
133 public String getType() {
134 return type;
135 }
136
137 public String getJid() {
138 return jid;
139 }
140
141 private void setJid(String jid) {
142 this.jid = jid;
143 }
144
145 public String getNode() {
146 return node;
147 }
148
149 private void setNode(String node) {
150 this.node = node;
151 }
152
153 public String getDescription() {
154 return description;
155 }
156
157 private void setDescription(String description) {
158 this.description = description;
159 }
160
161 public boolean isDelivered() {
162 return delivered;
163 }
164
165 private void setDelivered(boolean delivered) {
166 this.delivered = delivered;
167 }
168
169 public String getUri() {
170 return uri;
171 }
172
173 private void setUri(String uri) {
174 this.uri = uri;
175 }
176
177 private String toXML() {
178 StringBuilder buf = new StringBuilder();
179 buf.append("<address type=\"");
180 // Append the address type (e.g. TO/CC/BCC)
181 buf.append(type).append("\"");
182 if (jid != null) {
183 buf.append(" jid=\"");
184 buf.append(jid).append("\"");
185 }
186 if (node != null) {
187 buf.append(" node=\"");
188 buf.append(node).append("\"");
189 }
190 if (description != null && description.trim().length() > 0) {
191 buf.append(" desc=\"");
192 buf.append(description).append("\"");
193 }
194 if (delivered) {
195 buf.append(" delivered=\"true\"");
196 }
197 if (uri != null) {
198 buf.append(" uri=\"");
199 buf.append(uri).append("\"");
200 }
201 buf.append("/>");
202 return buf.toString();
203 }
204 }
205}