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        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
138
139        private final Type type;
140        private Jid jid;
141        private String node;
142        private String description;
143        private boolean delivered;
144        private String uri;
145
146        private Address(Type type) {
147            this.type = type;
148        }
149
150        public Type getType() {
151            return type;
152        }
153
154        public Jid getJid() {
155            return jid;
156        }
157
158        private void setJid(Jid jid) {
159            this.jid = jid;
160        }
161
162        public String getNode() {
163            return node;
164        }
165
166        private void setNode(String node) {
167            this.node = node;
168        }
169
170        public String getDescription() {
171            return description;
172        }
173
174        private void setDescription(String description) {
175            this.description = description;
176        }
177
178        public boolean isDelivered() {
179            return delivered;
180        }
181
182        private void setDelivered(boolean delivered) {
183            this.delivered = delivered;
184        }
185
186        public String getUri() {
187            return uri;
188        }
189
190        private void setUri(String uri) {
191            this.uri = uri;
192        }
193
194        @Override
195        public String getElementName() {
196            return QNAME.getLocalPart();
197        }
198
199        @Override
200        public String getNamespace() {
201            return QNAME.getNamespaceURI();
202        }
203
204        @Override
205        public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
206            XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
207            buf.attribute("type", type);
208            buf.optAttribute("jid", jid);
209            buf.optAttribute("node", node);
210            buf.optAttribute("desc", description);
211            if (description != null && description.trim().length() > 0) {
212                buf.append(" desc=\"");
213                buf.append(description).append('"');
214            }
215            buf.optBooleanAttribute("delivered", delivered);
216            buf.optAttribute("uri", uri);
217            buf.closeEmptyElement();
218            return buf;
219        }
220
221    }
222}