DiscoverItems.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 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.disco.packet;

  18. import org.jivesoftware.smack.packet.IQ;
  19. import org.jivesoftware.smack.util.XmlStringBuilder;
  20. import org.jxmpp.jid.Jid;

  21. import java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.LinkedList;
  24. import java.util.List;

  25. /**
  26.  * A DiscoverItems IQ packet, which is used by XMPP clients to request and receive items
  27.  * associated with XMPP entities.<p>
  28.  *
  29.  * The items could also be queried in order to discover if they contain items inside. Some items
  30.  * may be addressable by its JID and others may require to be addressed by a JID and a node name.
  31.  *
  32.  * @author Gaston Dombiak
  33.  */
  34. public class DiscoverItems extends IQ {

  35.     public static final String ELEMENT = QUERY_ELEMENT;
  36.     public static final String NAMESPACE = "http://jabber.org/protocol/disco#items";

  37.     private final List<Item> items = new LinkedList<Item>();
  38.     private String node;

  39.     public DiscoverItems() {
  40.         super(ELEMENT, NAMESPACE);
  41.     }

  42.     /**
  43.      * Adds a new item to the discovered information.
  44.      *
  45.      * @param item the discovered entity's item
  46.      */
  47.     public void addItem(Item item) {
  48.         items.add(item);
  49.     }

  50.     /**
  51.      * Adds a collection of items to the discovered information. Does nothing if itemsToAdd is null
  52.      *
  53.      * @param itemsToAdd
  54.      */
  55.     public void addItems(Collection<Item> itemsToAdd) {
  56.         if (itemsToAdd == null) return;
  57.         for (Item i : itemsToAdd) {
  58.             addItem(i);
  59.         }
  60.     }


  61.     /**
  62.      * Returns the discovered items of the queried XMPP entity.
  63.      *
  64.      * @return an unmodifiable list of the discovered entity's items
  65.      */
  66.     public List<DiscoverItems.Item> getItems() {
  67.         return Collections.unmodifiableList(items);
  68.     }

  69.     /**
  70.      * Returns the node attribute that supplements the 'jid' attribute. A node is merely
  71.      * something that is associated with a JID and for which the JID can provide information.<p>
  72.      *
  73.      * Node attributes SHOULD be used only when trying to provide or query information which
  74.      * is not directly addressable.
  75.      *
  76.      * @return the node attribute that supplements the 'jid' attribute
  77.      */
  78.     public String getNode() {
  79.         return node;
  80.     }

  81.     /**
  82.      * Sets the node attribute that supplements the 'jid' attribute. A node is merely
  83.      * something that is associated with a JID and for which the JID can provide information.<p>
  84.      *
  85.      * Node attributes SHOULD be used only when trying to provide or query information which
  86.      * is not directly addressable.
  87.      *
  88.      * @param node the node attribute that supplements the 'jid' attribute
  89.      */
  90.     public void setNode(String node) {
  91.         this.node = node;
  92.     }

  93.     @Override
  94.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  95.         xml.optAttribute("node", getNode());
  96.         xml.rightAngleBracket();

  97.         for (Item item : items) {
  98.             xml.append(item.toXML());
  99.         }

  100.         return xml;
  101.     }

  102.     /**
  103.      * An item is associated with an XMPP Entity, usually thought of a children of the parent
  104.      * entity and normally are addressable as a JID.<p>
  105.      *
  106.      * An item associated with an entity may not be addressable as a JID. In order to handle
  107.      * such items, Service Discovery uses an optional 'node' attribute that supplements the
  108.      * 'jid' attribute.
  109.      */
  110.     public static class Item {

  111.         /**
  112.          * Request to create or update the item.
  113.          */
  114.         public static final String UPDATE_ACTION = "update";

  115.         /**
  116.          * Request to remove the item.
  117.          */
  118.         public static final String REMOVE_ACTION = "remove";

  119.         private final Jid entityID;
  120.         private String name;
  121.         private String node;
  122.         private String action;

  123.         /**
  124.          * Create a new Item associated with a given entity.
  125.          *
  126.          * @param entityID the id of the entity that contains the item
  127.          */
  128.         public Item(Jid entityID) {
  129.             this.entityID = entityID;
  130.         }

  131.         /**
  132.          * Returns the entity's ID.
  133.          *
  134.          * @return the entity's ID.
  135.          */
  136.         public Jid getEntityID() {
  137.             return entityID;
  138.         }

  139.         /**
  140.          * Returns the entity's name.
  141.          *
  142.          * @return the entity's name.
  143.          */
  144.         public String getName() {
  145.             return name;
  146.         }

  147.         /**
  148.          * Sets the entity's name.
  149.          *
  150.          * @param name the entity's name.
  151.          */
  152.         public void setName(String name) {
  153.             this.name = name;
  154.         }

  155.         /**
  156.          * Returns the node attribute that supplements the 'jid' attribute. A node is merely
  157.          * something that is associated with a JID and for which the JID can provide information.<p>
  158.          *
  159.          * Node attributes SHOULD be used only when trying to provide or query information which
  160.          * is not directly addressable.
  161.          *
  162.          * @return the node attribute that supplements the 'jid' attribute
  163.          */
  164.         public String getNode() {
  165.             return node;
  166.         }

  167.         /**
  168.          * Sets the node attribute that supplements the 'jid' attribute. A node is merely
  169.          * something that is associated with a JID and for which the JID can provide information.<p>
  170.          *
  171.          * Node attributes SHOULD be used only when trying to provide or query information which
  172.          * is not directly addressable.
  173.          *
  174.          * @param node the node attribute that supplements the 'jid' attribute
  175.          */
  176.         public void setNode(String node) {
  177.             this.node = node;
  178.         }

  179.         /**
  180.          * Returns the action that specifies the action being taken for this item. Possible action
  181.          * values are: "update" and "remove". Update should either create a new entry if the node
  182.          * and jid combination does not already exist, or simply update an existing entry. If
  183.          * "remove" is used as the action, the item should be removed from persistent storage.
  184.          *  
  185.          * @return the action being taken for this item
  186.          */
  187.         public String getAction() {
  188.             return action;
  189.         }

  190.         /**
  191.          * Sets the action that specifies the action being taken for this item. Possible action
  192.          * values are: "update" and "remove". Update should either create a new entry if the node
  193.          * and jid combination does not already exist, or simply update an existing entry. If
  194.          * "remove" is used as the action, the item should be removed from persistent storage.
  195.          *
  196.          * @param action the action being taken for this item
  197.          */
  198.         public void setAction(String action) {
  199.             this.action = action;
  200.         }

  201.         public XmlStringBuilder toXML() {
  202.             XmlStringBuilder xml = new XmlStringBuilder();
  203.             xml.halfOpenElement("item");
  204.             xml.attribute("jid", entityID);
  205.             xml.optAttribute("name", name);
  206.             xml.optAttribute("node", node);
  207.             xml.optAttribute("action", action);
  208.             xml.closeEmptyElement();
  209.             return xml;
  210.         }
  211.     }
  212. }