MultipleAddresses.java

  1. /**
  2.  *
  3.  * Copyright 2003-2006 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.address.packet;

  18. import org.jivesoftware.smack.packet.NamedElement;
  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;
  21. import org.jxmpp.jid.Jid;

  22. import java.util.ArrayList;
  23. import java.util.List;

  24. /**
  25.  * Packet extension that contains the list of addresses that a packet should be sent or was sent.
  26.  *
  27.  * @author Gaston Dombiak
  28.  */
  29. public class MultipleAddresses implements ExtensionElement {

  30.     public static final String NAMESPACE = "http://jabber.org/protocol/address";
  31.     public static final String ELEMENT = "addresses";

  32.     public enum Type {
  33.         bcc,
  34.         cc,
  35.         noreply,
  36.         replyroom,
  37.         replyto,
  38.         to,

  39.         /**
  40.          * The "original from" type used to indicate the real originator of the stanza.
  41.          * <p>
  42.          * This Extended Stanza Addressing type is not specified in XEP-33, but in XEP-45 ยง 7.2.14 (Example 36).
  43.          * </p>
  44.          */
  45.         ofrom,
  46.     }

  47.     private List<Address> addresses = new ArrayList<Address>();

  48.     /**
  49.      * Adds a new address to which the packet is going to be sent or was sent.
  50.      *
  51.      * @param type on of the static type (BCC, CC, NO_REPLY, REPLY_ROOM, etc.)
  52.      * @param jid the JID address of the recipient.
  53.      * @param node used to specify a sub-addressable unit at a particular JID, corresponding to
  54.      *             a Service Discovery node.
  55.      * @param desc used to specify human-readable information for this address.
  56.      * @param delivered true when the packet was already delivered to this address.
  57.      * @param uri used to specify an external system address, such as a sip:, sips:, or im: URI.
  58.      */
  59.     public void addAddress(Type type, Jid jid, String node, String desc, boolean delivered,
  60.             String uri) {
  61.         // Create a new address with the specificed configuration
  62.         Address address = new Address(type);
  63.         address.setJid(jid);
  64.         address.setNode(node);
  65.         address.setDescription(desc);
  66.         address.setDelivered(delivered);
  67.         address.setUri(uri);
  68.         // Add the new address to the list of multiple recipients
  69.         addresses.add(address);
  70.     }

  71.     /**
  72.      * Indicate that the packet being sent should not be replied.
  73.      */
  74.     public void setNoReply() {
  75.         // Create a new address with the specificed configuration
  76.         Address address = new Address(Type.noreply);
  77.         // Add the new address to the list of multiple recipients
  78.         addresses.add(address);
  79.     }

  80.     /**
  81.      * Returns the list of addresses that matches the specified type. Examples of address
  82.      * type are: TO, CC, BCC, etc..
  83.      *
  84.      * @param type Examples of address type are: TO, CC, BCC, etc.
  85.      * @return the list of addresses that matches the specified type.
  86.      */
  87.     public List<Address> getAddressesOfType(Type type) {
  88.         List<Address> answer = new ArrayList<Address>(addresses.size());
  89.         for (Address address : addresses) {
  90.             if (address.getType().equals(type)) {
  91.                 answer.add(address);
  92.             }
  93.         }

  94.         return answer;
  95.     }

  96.     @Override
  97.     public String getElementName() {
  98.         return ELEMENT;
  99.     }

  100.     @Override
  101.     public String getNamespace() {
  102.         return NAMESPACE;
  103.     }

  104.     @Override
  105.     public XmlStringBuilder toXML() {
  106.         XmlStringBuilder buf = new XmlStringBuilder(this);
  107.         buf.rightAngleBracket();
  108.         // Loop through all the addresses and append them to the string buffer
  109.         for (Address address : addresses) {
  110.             buf.append(address.toXML());
  111.         }
  112.         buf.closeElement(this);
  113.         return buf;
  114.     }

  115.     public static class Address implements NamedElement {

  116.         public static final String ELEMENT = "address";

  117.         private final Type type;
  118.         private Jid jid;
  119.         private String node;
  120.         private String description;
  121.         private boolean delivered;
  122.         private String uri;

  123.         private Address(Type type) {
  124.             this.type = type;
  125.         }

  126.         public Type getType() {
  127.             return type;
  128.         }

  129.         public Jid getJid() {
  130.             return jid;
  131.         }

  132.         private void setJid(Jid jid) {
  133.             this.jid = jid;
  134.         }

  135.         public String getNode() {
  136.             return node;
  137.         }

  138.         private void setNode(String node) {
  139.             this.node = node;
  140.         }

  141.         public String getDescription() {
  142.             return description;
  143.         }

  144.         private void setDescription(String description) {
  145.             this.description = description;
  146.         }

  147.         public boolean isDelivered() {
  148.             return delivered;
  149.         }

  150.         private void setDelivered(boolean delivered) {
  151.             this.delivered = delivered;
  152.         }

  153.         public String getUri() {
  154.             return uri;
  155.         }

  156.         private void setUri(String uri) {
  157.             this.uri = uri;
  158.         }

  159.         @Override
  160.         public String getElementName() {
  161.             return ELEMENT;
  162.         }

  163.         @Override
  164.         public XmlStringBuilder toXML() {
  165.             XmlStringBuilder buf = new XmlStringBuilder();
  166.             buf.halfOpenElement(this).attribute("type", type);
  167.             buf.optAttribute("jid", jid);
  168.             buf.optAttribute("node", node);
  169.             buf.optAttribute("desc", description);
  170.             if (description != null && description.trim().length() > 0) {
  171.                 buf.append(" desc=\"");
  172.                 buf.append(description).append("\"");
  173.             }
  174.             buf.optBooleanAttribute("delivered", delivered);
  175.             buf.optAttribute("uri", uri);
  176.             buf.closeEmptyElement();
  177.             return buf;
  178.         }
  179.     }
  180. }