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.NamedElement; 021import org.jivesoftware.smack.packet.ExtensionElement; 022import org.jivesoftware.smack.util.XmlStringBuilder; 023import org.jxmpp.jid.Jid; 024 025import java.util.ArrayList; 026import java.util.List; 027 028/** 029 * Stanza(/Packet) extension that contains the list of addresses that a stanza(/packet) should be sent or was sent. 030 * 031 * @author Gaston Dombiak 032 */ 033public class MultipleAddresses implements ExtensionElement { 034 035 public static final String NAMESPACE = "http://jabber.org/protocol/address"; 036 public static final String ELEMENT = "addresses"; 037 038 public enum Type { 039 bcc, 040 cc, 041 noreply, 042 replyroom, 043 replyto, 044 to, 045 046 /** 047 * The "original from" type used to indicate the real originator of the stanza. 048 * <p> 049 * This Extended Stanza Addressing type is not specified in XEP-33, but in XEP-45 ยง 7.2.14 (Example 36). 050 * </p> 051 */ 052 ofrom, 053 } 054 055 private List<Address> addresses = new ArrayList<Address>(); 056 057 /** 058 * Adds a new address to which the stanza(/packet) is going to be sent or was sent. 059 * 060 * @param type on of the static type (BCC, CC, NO_REPLY, REPLY_ROOM, etc.) 061 * @param jid the JID address of the recipient. 062 * @param node used to specify a sub-addressable unit at a particular JID, corresponding to 063 * a Service Discovery node. 064 * @param desc used to specify human-readable information for this address. 065 * @param delivered true when the stanza(/packet) was already delivered to this address. 066 * @param uri used to specify an external system address, such as a sip:, sips:, or im: URI. 067 */ 068 public void addAddress(Type type, Jid jid, String node, String desc, boolean delivered, 069 String uri) { 070 // Create a new address with the specificed configuration 071 Address address = new Address(type); 072 address.setJid(jid); 073 address.setNode(node); 074 address.setDescription(desc); 075 address.setDelivered(delivered); 076 address.setUri(uri); 077 // Add the new address to the list of multiple recipients 078 addresses.add(address); 079 } 080 081 /** 082 * Indicate that the stanza(/packet) being sent should not be replied. 083 */ 084 public void setNoReply() { 085 // Create a new address with the specificed configuration 086 Address address = new Address(Type.noreply); 087 // Add the new address to the list of multiple recipients 088 addresses.add(address); 089 } 090 091 /** 092 * Returns the list of addresses that matches the specified type. Examples of address 093 * type are: TO, CC, BCC, etc.. 094 * 095 * @param type Examples of address type are: TO, CC, BCC, etc. 096 * @return the list of addresses that matches the specified type. 097 */ 098 public List<Address> getAddressesOfType(Type type) { 099 List<Address> answer = new ArrayList<Address>(addresses.size()); 100 for (Address address : addresses) { 101 if (address.getType().equals(type)) { 102 answer.add(address); 103 } 104 } 105 106 return answer; 107 } 108 109 @Override 110 public String getElementName() { 111 return ELEMENT; 112 } 113 114 @Override 115 public String getNamespace() { 116 return NAMESPACE; 117 } 118 119 @Override 120 public XmlStringBuilder toXML() { 121 XmlStringBuilder buf = new XmlStringBuilder(this); 122 buf.rightAngleBracket(); 123 // Loop through all the addresses and append them to the string buffer 124 for (Address address : addresses) { 125 buf.append(address.toXML()); 126 } 127 buf.closeElement(this); 128 return buf; 129 } 130 131 public static final class Address implements NamedElement { 132 133 public static final String ELEMENT = "address"; 134 135 private final Type type; 136 private Jid jid; 137 private String node; 138 private String description; 139 private boolean delivered; 140 private String uri; 141 142 private Address(Type type) { 143 this.type = type; 144 } 145 146 public Type getType() { 147 return type; 148 } 149 150 public Jid getJid() { 151 return jid; 152 } 153 154 private void setJid(Jid jid) { 155 this.jid = jid; 156 } 157 158 public String getNode() { 159 return node; 160 } 161 162 private void setNode(String node) { 163 this.node = node; 164 } 165 166 public String getDescription() { 167 return description; 168 } 169 170 private void setDescription(String description) { 171 this.description = description; 172 } 173 174 public boolean isDelivered() { 175 return delivered; 176 } 177 178 private void setDelivered(boolean delivered) { 179 this.delivered = delivered; 180 } 181 182 public String getUri() { 183 return uri; 184 } 185 186 private void setUri(String uri) { 187 this.uri = uri; 188 } 189 190 @Override 191 public String getElementName() { 192 return ELEMENT; 193 } 194 195 @Override 196 public XmlStringBuilder toXML() { 197 XmlStringBuilder buf = new XmlStringBuilder(); 198 buf.halfOpenElement(this).attribute("type", type); 199 buf.optAttribute("jid", jid); 200 buf.optAttribute("node", node); 201 buf.optAttribute("desc", description); 202 if (description != null && description.trim().length() > 0) { 203 buf.append(" desc=\""); 204 buf.append(description).append('"'); 205 } 206 buf.optBooleanAttribute("delivered", delivered); 207 buf.optAttribute("uri", uri); 208 buf.closeEmptyElement(); 209 return buf; 210 } 211 } 212}