/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright 2003-2007 Jive Software.
*
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.packet;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.StringUtils;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* A DiscoverInfo IQ packet, which is used by XMPP clients to request and receive information
* to/from other XMPP entities.
*
* The received information may contain one or more identities of the requested XMPP entity, and
* a list of supported features by the requested XMPP entity.
*
* @author Gaston Dombiak
*/
public class DiscoverInfo extends IQ {
public static final String NAMESPACE = "http://jabber.org/protocol/disco#info";
private final List features = new CopyOnWriteArrayList();
private final List identities = new CopyOnWriteArrayList();
private String node;
public DiscoverInfo() {
super();
}
/**
* Copy constructor
*
* @param d
*/
public DiscoverInfo(DiscoverInfo d) {
super(d);
// Set node
setNode(d.getNode());
// Copy features
synchronized (d.features) {
for (Feature f : d.features) {
addFeature(f);
}
}
// Copy identities
synchronized (d.identities) {
for (Identity i : d.identities) {
addIdentity(i);
}
}
}
/**
* Adds a new feature to the discovered information.
*
* @param feature the discovered feature
*/
public void addFeature(String feature) {
addFeature(new Feature(feature));
}
/**
* Adds a collection of features to the packet. Does noting if featuresToAdd is null.
*
* @param featuresToAdd
*/
public void addFeatures(Collection featuresToAdd) {
if (featuresToAdd == null) return;
for (String feature : featuresToAdd) {
addFeature(feature);
}
}
private void addFeature(Feature feature) {
synchronized (features) {
features.add(feature);
}
}
/**
* Returns the discovered features of an XMPP entity.
*
* @return an Iterator on the discovered features of an XMPP entity
*/
public Iterator getFeatures() {
synchronized (features) {
return Collections.unmodifiableList(features).iterator();
}
}
/**
* Adds a new identity of the requested entity to the discovered information.
*
* @param identity the discovered entity's identity
*/
public void addIdentity(Identity identity) {
synchronized (identities) {
identities.add(identity);
}
}
/**
* Adds identities to the DiscoverInfo stanza
*
* @param identitiesToAdd
*/
public void addIdentities(Collection identitiesToAdd) {
if (identitiesToAdd == null) return;
synchronized (identities) {
identities.addAll(identitiesToAdd);
}
}
/**
* Returns the discovered identities of an XMPP entity.
*
* @return an Iterator on the discoveted identities
*/
public Iterator getIdentities() {
synchronized (identities) {
return Collections.unmodifiableList(identities).iterator();
}
}
/**
* Returns the node attribute that supplements the 'jid' attribute. A node is merely
* something that is associated with a JID and for which the JID can provide information.
*
* Node attributes SHOULD be used only when trying to provide or query information which
* is not directly addressable.
*
* @return the node attribute that supplements the 'jid' attribute
*/
public String getNode() {
return node;
}
/**
* Sets the node attribute that supplements the 'jid' attribute. A node is merely
* something that is associated with a JID and for which the JID can provide information.
*
* Node attributes SHOULD be used only when trying to provide or query information which
* is not directly addressable.
*
* @param node the node attribute that supplements the 'jid' attribute
*/
public void setNode(String node) {
this.node = node;
}
/**
* Returns true if the specified feature is part of the discovered information.
*
* @param feature the feature to check
* @return true if the requestes feature has been discovered
*/
public boolean containsFeature(String feature) {
for (Iterator it = getFeatures(); it.hasNext();) {
if (feature.equals(it.next().getVar()))
return true;
}
return false;
}
public String getChildElementXML() {
StringBuilder buf = new StringBuilder();
buf.append("");
synchronized (identities) {
for (Identity identity : identities) {
buf.append(identity.toXML());
}
}
synchronized (features) {
for (Feature feature : features) {
buf.append(feature.toXML());
}
}
// Add packet extensions, if any are defined.
buf.append(getExtensionsXML());
buf.append("");
return buf.toString();
}
/**
* Test if a DiscoverInfo response contains duplicate identities.
*
* @return true if duplicate identities where found, otherwise false
*/
public boolean containsDuplicateIdentities() {
List checkedIdentities = new LinkedList();
for (Identity i : identities) {
for (Identity i2 : checkedIdentities) {
if (i.equals(i2))
return true;
}
checkedIdentities.add(i);
}
return false;
}
/**
* Test if a DiscoverInfo response contains duplicate features.
*
* @return true if duplicate identities where found, otherwise false
*/
public boolean containsDuplicateFeatures() {
List checkedFeatures = new LinkedList();
for (Feature f : features) {
for (Feature f2 : checkedFeatures) {
if (f.equals(f2))
return true;
}
checkedFeatures.add(f);
}
return false;
}
/**
* Represents the identity of a given XMPP entity. An entity may have many identities but all
* the identities SHOULD have the same name.
*
* Refer to Jabber::Registrar
* in order to get the official registry of values for the category and type
* attributes.
*
*/
public static class Identity implements Comparable