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