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