DiscoverInfo.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.StringUtils;
  20. import org.jivesoftware.smack.util.TypedCloneable;
  21. import org.jivesoftware.smack.util.XmlStringBuilder;
  22. import org.jxmpp.util.XmppStringUtils;

  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.HashSet;
  27. import java.util.LinkedList;
  28. import java.util.List;
  29. import java.util.Set;

  30. /**
  31.  * A DiscoverInfo IQ packet, which is used by XMPP clients to request and receive information
  32.  * to/from other XMPP entities.<p>
  33.  *
  34.  * The received information may contain one or more identities of the requested XMPP entity, and
  35.  * a list of supported features by the requested XMPP entity.
  36.  *
  37.  * @author Gaston Dombiak
  38.  */
  39. public class DiscoverInfo extends IQ implements TypedCloneable<DiscoverInfo> {

  40.     public static final String ELEMENT = QUERY_ELEMENT;
  41.     public static final String NAMESPACE = "http://jabber.org/protocol/disco#info";

  42.     private final List<Feature> features = new LinkedList<Feature>();
  43.     private final Set<Feature> featuresSet = new HashSet<Feature>();
  44.     private final List<Identity> identities = new LinkedList<Identity>();
  45.     private final Set<String> identitiesSet = new HashSet<String>();
  46.     private String node;
  47.     private boolean containsDuplicateFeatures;

  48.     public DiscoverInfo() {
  49.         super(ELEMENT, NAMESPACE);
  50.     }

  51.     /**
  52.      * Copy constructor
  53.      *
  54.      * @param d
  55.      */
  56.     public DiscoverInfo(DiscoverInfo d) {
  57.         super(d);

  58.         // Set node
  59.         setNode(d.getNode());

  60.         // Copy features
  61.         for (Feature f : d.features) {
  62.             addFeature(f.clone());
  63.         }

  64.         // Copy identities
  65.         for (Identity i : d.identities) {
  66.             addIdentity(i.clone());
  67.         }
  68.     }

  69.     /**
  70.      * Adds a new feature to the discovered information.
  71.      *
  72.      * @param feature the discovered feature
  73.      * @return true if the feature did not already exist.
  74.      */
  75.     public boolean addFeature(String feature) {
  76.         return addFeature(new Feature(feature));
  77.     }

  78.     /**
  79.      * Adds a collection of features to the packet. Does noting if featuresToAdd is null.
  80.      *
  81.      * @param featuresToAdd
  82.      */
  83.     public void addFeatures(Collection<String> featuresToAdd) {
  84.         if (featuresToAdd == null) return;
  85.         for (String feature : featuresToAdd) {
  86.             addFeature(feature);
  87.         }
  88.     }

  89.     public boolean addFeature(Feature feature) {
  90.         features.add(feature);
  91.         boolean featureIsNew = featuresSet.add(feature);
  92.         if (!featureIsNew) {
  93.             containsDuplicateFeatures = true;
  94.         }
  95.         return featureIsNew;
  96.     }

  97.     /**
  98.      * Returns the discovered features of an XMPP entity.
  99.      *
  100.      * @return an unmodifiable list of the discovered features of an XMPP entity
  101.      */
  102.     public List<Feature> getFeatures() {
  103.         return Collections.unmodifiableList(features);
  104.     }

  105.     /**
  106.      * Adds a new identity of the requested entity to the discovered information.
  107.      *
  108.      * @param identity the discovered entity's identity
  109.      */
  110.     public void addIdentity(Identity identity) {
  111.         identities.add(identity);
  112.         identitiesSet.add(identity.getKey());
  113.     }

  114.     /**
  115.      * Adds identities to the DiscoverInfo stanza
  116.      *
  117.      * @param identitiesToAdd
  118.      */
  119.     public void addIdentities(Collection<Identity> identitiesToAdd) {
  120.         if (identitiesToAdd == null) return;
  121.         for (Identity identity : identitiesToAdd) {
  122.             addIdentity(identity);
  123.         }
  124.     }

  125.     /**
  126.      * Returns the discovered identities of an XMPP entity.
  127.      *
  128.      * @return an unmodifiable list of the discovered identities
  129.      */
  130.     public List<Identity> getIdentities() {
  131.         return Collections.unmodifiableList(identities);
  132.     }

  133.     /**
  134.      * Returns true if this DiscoverInfo contains at least one Identity of the given category and type.
  135.      *
  136.      * @param category the category to look for.
  137.      * @param type the type to look for.
  138.      * @return true if this DiscoverInfo contains a Identity of the given category and type.
  139.      */
  140.     public boolean hasIdentity(String category, String type) {
  141.         String key = XmppStringUtils.generateKey(category, type);
  142.         return identitiesSet.contains(key);
  143.     }

  144.     /**
  145.      * Returns all Identities of the given category and type of this DiscoverInfo.
  146.      *
  147.      * @param category category the category to look for.
  148.      * @param type type the type to look for.
  149.      * @return a list of Identites with the given category and type.
  150.      */
  151.     public List<Identity> getIdentities(String category, String type) {
  152.         List<Identity> res = new ArrayList<Identity>(identities.size());
  153.         for (Identity identity : identities) {
  154.             if (identity.getCategory().equals(category) && identity.getType().equals(type)) {
  155.                 res.add(identity);
  156.             }
  157.         }
  158.         return res;
  159.     }

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

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

  184.     /**
  185.      * Returns true if the specified feature is part of the discovered information.
  186.      *
  187.      * @param feature the feature to check
  188.      * @return true if the requestes feature has been discovered
  189.      */
  190.     public boolean containsFeature(String feature) {
  191.         return features.contains(new Feature(feature));
  192.     }

  193.     @Override
  194.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  195.         xml.optAttribute("node", getNode());
  196.         xml.rightAngleBracket();
  197.         for (Identity identity : identities) {
  198.             xml.append(identity.toXML());
  199.         }
  200.         for (Feature feature : features) {
  201.             xml.append(feature.toXML());
  202.         }

  203.         return xml;
  204.     }

  205.     /**
  206.      * Test if a DiscoverInfo response contains duplicate identities.
  207.      *
  208.      * @return true if duplicate identities where found, otherwise false
  209.      */
  210.     public boolean containsDuplicateIdentities() {
  211.         List<Identity> checkedIdentities = new LinkedList<Identity>();
  212.         for (Identity i : identities) {
  213.             for (Identity i2 : checkedIdentities) {
  214.                 if (i.equals(i2))
  215.                     return true;
  216.             }
  217.             checkedIdentities.add(i);
  218.         }
  219.         return false;
  220.     }

  221.     /**
  222.      * Test if a DiscoverInfo response contains duplicate features.
  223.      *
  224.      * @return true if duplicate identities where found, otherwise false
  225.      */
  226.     public boolean containsDuplicateFeatures() {
  227.         return containsDuplicateFeatures;
  228.     }

  229.     @Override
  230.     public DiscoverInfo clone() {
  231.         return new DiscoverInfo(this);
  232.     }

  233.     /**
  234.      * Represents the identity of a given XMPP entity. An entity may have many identities but all
  235.      * the identities SHOULD have the same name.<p>
  236.      *
  237.      * Refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a>
  238.      * in order to get the official registry of values for the <i>category</i> and <i>type</i>
  239.      * attributes.
  240.      *
  241.      */
  242.     public static class Identity implements Comparable<Identity>, TypedCloneable<Identity> {

  243.         private final String category;
  244.         private final String type;
  245.         private final String key;
  246.         private final String name;
  247.         private final String lang; // 'xml:lang;

  248.         public Identity(Identity identity) {
  249.             this.category = identity.category;
  250.             this.type = identity.type;
  251.             this.key = identity.type;
  252.             this.name = identity.name;
  253.             this.lang = identity.lang;
  254.         }

  255.         /**
  256.          * Creates a new identity for an XMPP entity.
  257.          *
  258.          * @param category the entity's category (required as per XEP-30).
  259.          * @param type the entity's type (required as per XEP-30).
  260.          */
  261.         public Identity(String category, String type) {
  262.             this(category, type, null, null);
  263.         }

  264.         /**
  265.          * Creates a new identity for an XMPP entity.
  266.          * 'category' and 'type' are required by
  267.          * <a href="http://xmpp.org/extensions/xep-0030.html#schemas">XEP-30 XML Schemas</a>
  268.          *
  269.          * @param category the entity's category (required as per XEP-30).
  270.          * @param name the entity's name.
  271.          * @param type the entity's type (required as per XEP-30).
  272.          */
  273.         public Identity(String category, String name, String type) {
  274.             this(category, type, name, null);
  275.         }

  276.         /**
  277.          * Creates a new identity for an XMPP entity.
  278.          * 'category' and 'type' are required by
  279.          * <a href="http://xmpp.org/extensions/xep-0030.html#schemas">XEP-30 XML Schemas</a>
  280.          *
  281.          * @param category the entity's category (required as per XEP-30).
  282.          * @param type the entity's type (required as per XEP-30).
  283.          * @param name the entity's name.
  284.          * @param lang the entity's lang.
  285.          */
  286.         public Identity(String category, String type, String name, String lang) {
  287.             this.category = StringUtils.requireNotNullOrEmpty(category, "category cannot be null");
  288.             this.type = StringUtils.requireNotNullOrEmpty(type, "type cannot be null");
  289.             this.key = XmppStringUtils.generateKey(category, type);
  290.             this.name = name;
  291.             this.lang = lang;
  292.         }

  293.         /**
  294.          * Returns the entity's category. To get the official registry of values for the
  295.          * 'category' attribute refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a>
  296.          *
  297.          * @return the entity's category.
  298.          */
  299.         public String getCategory() {
  300.             return category;
  301.         }

  302.         /**
  303.          * Returns the identity's name.
  304.          *
  305.          * @return the identity's name.
  306.          */
  307.         public String getName() {
  308.             return name;
  309.         }

  310.         /**
  311.          * Returns the entity's type. To get the official registry of values for the
  312.          * 'type' attribute refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a>
  313.          *
  314.          * @return the entity's type.
  315.          */
  316.         public String getType() {
  317.             return type;
  318.         }

  319.         /**
  320.          * Returns the identities natural language if one is set
  321.          *
  322.          * @return the value of xml:lang of this Identity
  323.          */
  324.         public String getLanguage() {
  325.             return lang;
  326.         }

  327.         private String getKey() {
  328.             return key;
  329.         }

  330.         /**
  331.          * Returns true if this identity is of the given category and type.
  332.          *
  333.          * @param category the category.
  334.          * @param type the type.
  335.          * @return true if this identity is of the given category and type.
  336.          */
  337.         public boolean isOfCategoryAndType(String category, String type) {
  338.             return this.category.equals(category) && this.type.equals(type);
  339.         }

  340.         public XmlStringBuilder toXML() {
  341.             XmlStringBuilder xml = new XmlStringBuilder();
  342.             xml.halfOpenElement("identity");
  343.             xml.xmllangAttribute(lang);
  344.             xml.attribute("category", category);
  345.             xml.optAttribute("name", name);
  346.             xml.optAttribute("type", type);
  347.             xml.closeEmptyElement();
  348.             return xml;
  349.         }

  350.         /**
  351.          * Check equality for Identity  for category, type, lang and name
  352.          * in that order as defined by
  353.          * <a href="http://xmpp.org/extensions/xep-0115.html#ver-proc">XEP-0015 5.4 Processing Method (Step 3.3)</a>
  354.          *  
  355.          */
  356.         public boolean equals(Object obj) {
  357.             if (obj == null)
  358.                 return false;
  359.             if (obj == this)
  360.                 return true;
  361.             if (obj.getClass() != getClass())
  362.                 return false;

  363.             DiscoverInfo.Identity other = (DiscoverInfo.Identity) obj;
  364.             if (!this.key.equals(other.key))
  365.                 return false;

  366.             String otherLang = other.lang == null ? "" : other.lang;
  367.             String thisLang = lang == null ? "" : lang;
  368.             if (!otherLang.equals(thisLang))
  369.                 return false;

  370.             String otherName = other.name == null ? "" : other.name;
  371.             String thisName = name == null ? "" : other.name;
  372.             if (!thisName.equals(otherName))
  373.                 return false;

  374.             return true;
  375.         }

  376.         @Override
  377.         public int hashCode() {
  378.             int result = 1;
  379.             result = 37 * result + key.hashCode();
  380.             result = 37 * result + (lang == null ? 0 : lang.hashCode());
  381.             result = 37 * result + (name == null ? 0 : name.hashCode());
  382.             return result;
  383.         }

  384.         /**
  385.          * Compares this identity with another one. The comparison order is: Category, Type, Lang.
  386.          * If all three are identical the other Identity is considered equal. Name is not used for
  387.          * comparison, as defined by XEP-0115
  388.          *
  389.          * @param other
  390.          * @return a negative integer, zero, or a positive integer as this object is less than,
  391.          *         equal to, or greater than the specified object.
  392.          */
  393.         public int compareTo(DiscoverInfo.Identity other) {
  394.             String otherLang = other.lang == null ? "" : other.lang;
  395.             String thisLang = lang == null ? "" : lang;
  396.            
  397.             // This can be removed once the deprecated constructor is removed.
  398.             String otherType = other.type == null ? "" : other.type;
  399.             String thisType = type == null ? "" : type;

  400.             if (category.equals(other.category)) {
  401.                 if (thisType.equals(otherType)) {
  402.                     if (thisLang.equals(otherLang)) {
  403.                         // Don't compare on name, XEP-30 says that name SHOULD
  404.                         // be equals for all identities of an entity
  405.                         return 0;
  406.                     } else {
  407.                         return thisLang.compareTo(otherLang);
  408.                     }
  409.                 } else {
  410.                     return thisType.compareTo(otherType);
  411.                 }
  412.             } else {
  413.                 return category.compareTo(other.category);
  414.             }
  415.         }

  416.         @Override
  417.         public Identity clone() {
  418.             return new Identity(this);
  419.         }
  420.     }

  421.     /**
  422.      * Represents the features offered by the item. This information helps requestors determine
  423.      * what actions are possible with regard to this item (registration, search, join, etc.)
  424.      * as well as specific feature types of interest, if any (e.g., for the purpose of feature
  425.      * negotiation).
  426.      */
  427.     public static class Feature implements TypedCloneable<Feature> {

  428.         private final String variable;

  429.         public Feature(Feature feature) {
  430.             this.variable = feature.variable;
  431.         }

  432.         /**
  433.          * Creates a new feature offered by an XMPP entity or item.
  434.          *
  435.          * @param variable the feature's variable.
  436.          */
  437.         public Feature(String variable) {
  438.             this.variable = StringUtils.requireNotNullOrEmpty(variable, "variable cannot be null");
  439.         }

  440.         /**
  441.          * Returns the feature's variable.
  442.          *
  443.          * @return the feature's variable.
  444.          */
  445.         public String getVar() {
  446.             return variable;
  447.         }

  448.         public XmlStringBuilder toXML() {
  449.             XmlStringBuilder xml = new XmlStringBuilder();
  450.             xml.halfOpenElement("feature");
  451.             xml.attribute("var", variable);
  452.             xml.closeEmptyElement();
  453.             return xml;
  454.         }

  455.         public boolean equals(Object obj) {
  456.             if (obj == null)
  457.                 return false;
  458.             if (obj == this)
  459.                 return true;
  460.             if (obj.getClass() != getClass())
  461.                 return false;

  462.             DiscoverInfo.Feature other = (DiscoverInfo.Feature) obj;
  463.             return variable.equals(other.variable);
  464.         }

  465.         @Override
  466.         public int hashCode() {
  467.             return variable.hashCode();
  468.         }

  469.         @Override
  470.         public Feature clone() {
  471.             return new Feature(this);
  472.         }
  473.     }
  474. }