001/** 002 * 003 * Copyright 2003-2006 Jive Software. 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 */ 017 018package org.jivesoftware.smackx.address.packet; 019 020import org.jivesoftware.smack.packet.PacketExtension; 021 022import java.util.ArrayList; 023import java.util.Iterator; 024import java.util.List; 025 026/** 027 * Packet extension that contains the list of addresses that a packet should be sent or was sent. 028 * 029 * @author Gaston Dombiak 030 */ 031public class MultipleAddresses implements PacketExtension { 032 033 public static final String NAMESPACE = "http://jabber.org/protocol/address"; 034 public static final String ELEMENT = "addresses"; 035 036 public static final String BCC = "bcc"; 037 public static final String CC = "cc"; 038 public static final String NO_REPLY = "noreply"; 039 public static final String REPLY_ROOM = "replyroom"; 040 public static final String REPLY_TO = "replyto"; 041 public static final String TO = "to"; 042 043 044 private List<Address> addresses = new ArrayList<Address>(); 045 046 /** 047 * Adds a new address to which the packet is going to be sent or was sent. 048 * 049 * @param type on of the static type (BCC, CC, NO_REPLY, REPLY_ROOM, etc.) 050 * @param jid the JID address of the recipient. 051 * @param node used to specify a sub-addressable unit at a particular JID, corresponding to 052 * a Service Discovery node. 053 * @param desc used to specify human-readable information for this address. 054 * @param delivered true when the packet was already delivered to this address. 055 * @param uri used to specify an external system address, such as a sip:, sips:, or im: URI. 056 */ 057 public void addAddress(String type, String jid, String node, String desc, boolean delivered, 058 String uri) { 059 // Create a new address with the specificed configuration 060 Address address = new Address(type); 061 address.setJid(jid); 062 address.setNode(node); 063 address.setDescription(desc); 064 address.setDelivered(delivered); 065 address.setUri(uri); 066 // Add the new address to the list of multiple recipients 067 addresses.add(address); 068 } 069 070 /** 071 * Indicate that the packet being sent should not be replied. 072 */ 073 public void setNoReply() { 074 // Create a new address with the specificed configuration 075 Address address = new Address(NO_REPLY); 076 // Add the new address to the list of multiple recipients 077 addresses.add(address); 078 } 079 080 /** 081 * Returns the list of addresses that matches the specified type. Examples of address 082 * type are: TO, CC, BCC, etc.. 083 * 084 * @param type Examples of address type are: TO, CC, BCC, etc. 085 * @return the list of addresses that matches the specified type. 086 */ 087 public List<Address> getAddressesOfType(String type) { 088 List<Address> answer = new ArrayList<Address>(addresses.size()); 089 for (Iterator<Address> it = addresses.iterator(); it.hasNext();) { 090 Address address = (Address) it.next(); 091 if (address.getType().equals(type)) { 092 answer.add(address); 093 } 094 } 095 096 return answer; 097 } 098 099 public String getElementName() { 100 return ELEMENT; 101 } 102 103 public String getNamespace() { 104 return NAMESPACE; 105 } 106 107 public String toXML() { 108 StringBuilder buf = new StringBuilder(); 109 buf.append("<").append(getElementName()); 110 buf.append(" xmlns=\"").append(NAMESPACE).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}