2003-10-14 00:57:25 +02:00
|
|
|
/**
|
2004-11-03 00:37:00 +01:00
|
|
|
* $RCSfile$
|
|
|
|
* $Revision$
|
|
|
|
* $Date$
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2004-11-03 00:37:00 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2003-10-14 00:57:25 +02:00
|
|
|
|
|
|
|
package org.jivesoftware.smackx.packet;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
2013-01-31 23:30:36 +01:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2003-10-14 00:57:25 +02:00
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2006-11-21 00:09:11 +01:00
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
2006-07-18 07:14:33 +02:00
|
|
|
|
2003-10-14 00:57:25 +02:00
|
|
|
/**
|
|
|
|
* A DiscoverInfo IQ packet, which is used by XMPP clients to request and receive information
|
|
|
|
* to/from other XMPP entities.<p>
|
|
|
|
*
|
|
|
|
* 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 {
|
|
|
|
|
2006-11-21 00:09:11 +01:00
|
|
|
private final List<Feature> features = new CopyOnWriteArrayList<Feature>();
|
|
|
|
private final List<Identity> identities = new CopyOnWriteArrayList<Identity>();
|
2003-10-14 00:57:25 +02:00
|
|
|
private String node;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new feature to the discovered information.
|
|
|
|
*
|
|
|
|
* @param feature the discovered feature
|
|
|
|
*/
|
|
|
|
public void addFeature(String feature) {
|
2006-11-21 00:09:11 +01:00
|
|
|
addFeature(new Feature(feature));
|
2003-10-14 00:57:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2010-12-04 17:28:52 +01:00
|
|
|
public Iterator<Feature> getFeatures() {
|
2003-10-14 00:57:25 +02:00
|
|
|
synchronized (features) {
|
2006-11-21 00:09:11 +01:00
|
|
|
return Collections.unmodifiableList(features).iterator();
|
2003-10-14 00:57:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the discovered identities of an XMPP entity.
|
|
|
|
*
|
|
|
|
* @return an Iterator on the discoveted identities
|
|
|
|
*/
|
2006-07-18 07:14:33 +02:00
|
|
|
public Iterator<Identity> getIdentities() {
|
2003-10-14 00:57:25 +02:00
|
|
|
synchronized (identities) {
|
2006-11-21 00:09:11 +01:00
|
|
|
return Collections.unmodifiableList(identities).iterator();
|
2003-10-14 00:57:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.<p>
|
|
|
|
*
|
|
|
|
* 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.<p>
|
|
|
|
*
|
|
|
|
* 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) {
|
2006-07-18 07:14:33 +02:00
|
|
|
for (Iterator<Feature> it = getFeatures(); it.hasNext();) {
|
|
|
|
if (feature.equals(it.next().getVar()))
|
2003-10-14 00:57:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getChildElementXML() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buf = new StringBuilder();
|
2004-05-13 19:33:21 +02:00
|
|
|
buf.append("<query xmlns=\"http://jabber.org/protocol/disco#info\"");
|
2004-05-09 20:52:26 +02:00
|
|
|
if (getNode() != null) {
|
2004-05-13 19:33:21 +02:00
|
|
|
buf.append(" node=\"");
|
2013-01-31 23:30:36 +01:00
|
|
|
buf.append(StringUtils.escapeForXML(getNode()));
|
2004-05-13 19:33:21 +02:00
|
|
|
buf.append("\"");
|
2004-05-09 20:52:26 +02:00
|
|
|
}
|
2004-05-13 19:33:21 +02:00
|
|
|
buf.append(">");
|
2003-10-14 00:57:25 +02:00
|
|
|
synchronized (identities) {
|
2006-07-18 07:14:33 +02:00
|
|
|
for (Identity identity : identities) {
|
2003-10-14 00:57:25 +02:00
|
|
|
buf.append(identity.toXML());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
synchronized (features) {
|
2006-07-18 07:14:33 +02:00
|
|
|
for (Feature feature : features) {
|
2003-10-14 00:57:25 +02:00
|
|
|
buf.append(feature.toXML());
|
|
|
|
}
|
|
|
|
}
|
2004-09-19 18:20:10 +02:00
|
|
|
// Add packet extensions, if any are defined.
|
|
|
|
buf.append(getExtensionsXML());
|
2003-10-14 00:57:25 +02:00
|
|
|
buf.append("</query>");
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the identity of a given XMPP entity. An entity may have many identities but all
|
|
|
|
* the identities SHOULD have the same name.<p>
|
|
|
|
*
|
|
|
|
* Refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a>
|
|
|
|
* in order to get the official registry of values for the <i>category</i> and <i>type</i>
|
|
|
|
* attributes.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static class Identity {
|
|
|
|
|
|
|
|
private String category;
|
|
|
|
private String name;
|
|
|
|
private String type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new identity for an XMPP entity.
|
|
|
|
*
|
|
|
|
* @param category the entity's category.
|
|
|
|
* @param name the entity's name.
|
|
|
|
*/
|
|
|
|
public Identity(String category, String name) {
|
|
|
|
this.category = category;
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the entity's category. To get the official registry of values for the
|
|
|
|
* 'category' attribute refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a>
|
|
|
|
*
|
|
|
|
* @return the entity's category.
|
|
|
|
*/
|
|
|
|
public String getCategory() {
|
|
|
|
return category;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the identity's name.
|
|
|
|
*
|
|
|
|
* @return the identity's name.
|
|
|
|
*/
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the entity's type. To get the official registry of values for the
|
|
|
|
* 'type' attribute refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a>
|
|
|
|
*
|
|
|
|
* @return the entity's type.
|
|
|
|
*/
|
|
|
|
public String getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the entity's type. To get the official registry of values for the
|
|
|
|
* 'type' attribute refer to <a href="http://www.jabber.org/registrar/disco-categories.html">Jabber::Registrar</a>
|
|
|
|
*
|
2004-03-11 16:55:44 +01:00
|
|
|
* @param type the identity's type.
|
2003-10-14 00:57:25 +02:00
|
|
|
*/
|
|
|
|
public void setType(String type) {
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toXML() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buf = new StringBuilder();
|
2013-01-31 23:30:36 +01:00
|
|
|
buf.append("<identity category=\"").append(StringUtils.escapeForXML(category)).append("\"");
|
|
|
|
buf.append(" name=\"").append(StringUtils.escapeForXML(name)).append("\"");
|
2003-10-14 00:57:25 +02:00
|
|
|
if (type != null) {
|
2013-01-31 23:30:36 +01:00
|
|
|
buf.append(" type=\"").append(StringUtils.escapeForXML(type)).append("\"");
|
2003-10-14 00:57:25 +02:00
|
|
|
}
|
|
|
|
buf.append("/>");
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the features offered by the item. This information helps requestors determine
|
|
|
|
* what actions are possible with regard to this item (registration, search, join, etc.)
|
|
|
|
* as well as specific feature types of interest, if any (e.g., for the purpose of feature
|
|
|
|
* negotiation).
|
|
|
|
*/
|
|
|
|
public static class Feature {
|
|
|
|
|
|
|
|
private String variable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new feature offered by an XMPP entity or item.
|
|
|
|
*
|
|
|
|
* @param variable the feature's variable.
|
|
|
|
*/
|
|
|
|
public Feature(String variable) {
|
|
|
|
this.variable = variable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the feature's variable.
|
|
|
|
*
|
|
|
|
* @return the feature's variable.
|
|
|
|
*/
|
|
|
|
public String getVar() {
|
|
|
|
return variable;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toXML() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buf = new StringBuilder();
|
2013-01-31 23:30:36 +01:00
|
|
|
buf.append("<feature var=\"").append(StringUtils.escapeForXML(variable)).append("\"/>");
|
2003-10-14 00:57:25 +02:00
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
}
|
2013-01-31 23:30:36 +01:00
|
|
|
}
|