2003-10-14 01:05:06 +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 01:05:06 +02:00
|
|
|
|
|
|
|
package org.jivesoftware.smackx;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.*;
|
2006-07-10 18:56:14 +02:00
|
|
|
import org.jivesoftware.smack.filter.PacketFilter;
|
|
|
|
import org.jivesoftware.smack.filter.PacketIDFilter;
|
|
|
|
import org.jivesoftware.smack.filter.PacketTypeFilter;
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.packet.Packet;
|
2013-03-18 09:50:48 +01:00
|
|
|
import org.jivesoftware.smack.packet.PacketExtension;
|
2006-07-10 18:56:14 +02:00
|
|
|
import org.jivesoftware.smack.packet.XMPPError;
|
2013-03-18 09:50:48 +01:00
|
|
|
import org.jivesoftware.smackx.entitycaps.EntityCapsManager;
|
2006-07-10 18:56:14 +02:00
|
|
|
import org.jivesoftware.smackx.packet.DiscoverInfo;
|
2013-03-18 09:50:48 +01:00
|
|
|
import org.jivesoftware.smackx.packet.DiscoverInfo.Identity;
|
2006-07-10 18:56:14 +02:00
|
|
|
import org.jivesoftware.smackx.packet.DiscoverItems;
|
2008-02-15 17:32:46 +01:00
|
|
|
import org.jivesoftware.smackx.packet.DataForm;
|
2006-07-10 18:56:14 +02:00
|
|
|
|
|
|
|
import java.util.*;
|
2006-07-18 07:14:33 +02:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2003-10-14 01:05:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages discovery of services in XMPP entities. This class provides:
|
|
|
|
* <ol>
|
|
|
|
* <li>A registry of supported features in this XMPP entity.
|
|
|
|
* <li>Automatic response when this XMPP entity is queried for information.
|
|
|
|
* <li>Ability to discover items and information of remote XMPP entities.
|
|
|
|
* <li>Ability to publish publicly available items.
|
|
|
|
* </ol>
|
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
|
|
|
public class ServiceDiscoveryManager {
|
2004-05-09 21:18:02 +02:00
|
|
|
|
2013-03-18 09:50:48 +01:00
|
|
|
private static final String DEFAULT_IDENTITY_NAME = "Smack";
|
|
|
|
private static final String DEFAULT_IDENTITY_CATEGORY = "client";
|
|
|
|
private static final String DEFAULT_IDENTITY_TYPE = "pc";
|
|
|
|
|
|
|
|
private static List<DiscoverInfo.Identity> identities = new LinkedList<DiscoverInfo.Identity>();
|
|
|
|
|
|
|
|
private EntityCapsManager capsManager;
|
2004-08-10 00:09:20 +02:00
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
private static Map<Connection, ServiceDiscoveryManager> instances =
|
|
|
|
new ConcurrentHashMap<Connection, ServiceDiscoveryManager>();
|
2004-05-09 21:18:02 +02:00
|
|
|
|
2010-02-09 12:55:56 +01:00
|
|
|
private Connection connection;
|
2013-02-11 18:22:42 +01:00
|
|
|
private final Set<String> features = new HashSet<String>();
|
2008-02-15 17:32:46 +01:00
|
|
|
private DataForm extendedInfo = null;
|
2006-07-18 07:14:33 +02:00
|
|
|
private Map<String, NodeInformationProvider> nodeInformationProviders =
|
|
|
|
new ConcurrentHashMap<String, NodeInformationProvider>();
|
2004-05-09 21:18:02 +02:00
|
|
|
|
2003-12-19 18:59:55 +01:00
|
|
|
// Create a new ServiceDiscoveryManager on every established connection
|
|
|
|
static {
|
2010-02-09 12:55:56 +01:00
|
|
|
Connection.addConnectionCreationListener(new ConnectionCreationListener() {
|
|
|
|
public void connectionCreated(Connection connection) {
|
2003-12-19 18:59:55 +01:00
|
|
|
new ServiceDiscoveryManager(connection);
|
|
|
|
}
|
|
|
|
});
|
2013-03-18 09:50:48 +01:00
|
|
|
identities.add(new Identity(DEFAULT_IDENTITY_CATEGORY, DEFAULT_IDENTITY_NAME, DEFAULT_IDENTITY_TYPE));
|
2003-12-19 18:59:55 +01:00
|
|
|
}
|
|
|
|
|
2003-10-14 01:05:06 +02:00
|
|
|
/**
|
2010-02-09 12:55:56 +01:00
|
|
|
* Creates a new ServiceDiscoveryManager for a given Connection. This means that the
|
2003-10-14 01:05:06 +02:00
|
|
|
* service manager will respond to any service discovery request that the connection may
|
|
|
|
* receive.
|
|
|
|
*
|
2004-08-05 18:57:28 +02:00
|
|
|
* @param connection the connection to which a ServiceDiscoveryManager is going to be created.
|
2003-10-14 01:05:06 +02:00
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
public ServiceDiscoveryManager(Connection connection) {
|
2003-10-14 01:05:06 +02:00
|
|
|
this.connection = connection;
|
2013-03-18 09:50:48 +01:00
|
|
|
|
2003-10-14 01:05:06 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-09 12:55:56 +01:00
|
|
|
* Returns the ServiceDiscoveryManager instance associated with a given Connection.
|
2003-10-14 01:05:06 +02:00
|
|
|
*
|
2004-08-05 18:57:28 +02:00
|
|
|
* @param connection the connection used to look for the proper ServiceDiscoveryManager.
|
2010-02-09 12:55:56 +01:00
|
|
|
* @return the ServiceDiscoveryManager associated with a given Connection.
|
2003-10-14 01:05:06 +02:00
|
|
|
*/
|
2010-02-09 12:55:56 +01:00
|
|
|
public static ServiceDiscoveryManager getInstanceFor(Connection connection) {
|
2006-07-18 07:14:33 +02:00
|
|
|
return instances.get(connection);
|
2003-10-14 01:05:06 +02:00
|
|
|
}
|
2004-05-09 21:18:02 +02:00
|
|
|
|
2004-08-10 00:09:20 +02:00
|
|
|
/**
|
|
|
|
* Returns the name of the client that will be returned when asked for the client identity
|
|
|
|
* in a disco request. The name could be any value you need to identity this client.
|
|
|
|
*
|
|
|
|
* @return the name of the client that will be returned when asked for the client identity
|
|
|
|
* in a disco request.
|
|
|
|
*/
|
|
|
|
public static String getIdentityName() {
|
2013-03-18 09:50:48 +01:00
|
|
|
DiscoverInfo.Identity identity = identities.get(0);
|
|
|
|
if (identity != null) {
|
|
|
|
return identity.getName();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2004-08-10 00:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the name of the client that will be returned when asked for the client identity
|
|
|
|
* in a disco request. The name could be any value you need to identity this client.
|
|
|
|
*
|
|
|
|
* @param name the name of the client that will be returned when asked for the client identity
|
|
|
|
* in a disco request.
|
|
|
|
*/
|
|
|
|
public static void setIdentityName(String name) {
|
2013-03-18 09:50:48 +01:00
|
|
|
DiscoverInfo.Identity identity = identities.remove(0);
|
|
|
|
identity = new DiscoverInfo.Identity(DEFAULT_IDENTITY_CATEGORY, name, DEFAULT_IDENTITY_TYPE);
|
|
|
|
identities.add(identity);
|
2004-08-10 00:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the type of client that will be returned when asked for the client identity in a
|
|
|
|
* disco request. The valid types are defined by the category client. Follow this link to learn
|
2011-08-18 05:44:41 +02:00
|
|
|
* the possible types: <a href="http://xmpp.org/registrar/disco-categories.html#client">Jabber::Registrar</a>.
|
2004-08-10 00:09:20 +02:00
|
|
|
*
|
|
|
|
* @return the type of client that will be returned when asked for the client identity in a
|
|
|
|
* disco request.
|
|
|
|
*/
|
|
|
|
public static String getIdentityType() {
|
2013-03-18 09:50:48 +01:00
|
|
|
DiscoverInfo.Identity identity = identities.get(0);
|
|
|
|
if (identity != null) {
|
|
|
|
return identity.getType();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2004-08-10 00:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the type of client that will be returned when asked for the client identity in a
|
|
|
|
* disco request. The valid types are defined by the category client. Follow this link to learn
|
2011-08-18 05:44:41 +02:00
|
|
|
* the possible types: <a href="http://xmpp.org/registrar/disco-categories.html#client">Jabber::Registrar</a>.
|
2004-08-10 00:09:20 +02:00
|
|
|
*
|
|
|
|
* @param type the type of client that will be returned when asked for the client identity in a
|
|
|
|
* disco request.
|
|
|
|
*/
|
|
|
|
public static void setIdentityType(String type) {
|
2013-03-18 09:50:48 +01:00
|
|
|
DiscoverInfo.Identity identity = identities.get(0);
|
|
|
|
if (identity != null) {
|
|
|
|
identity.setType(type);
|
|
|
|
} else {
|
|
|
|
identity = new DiscoverInfo.Identity(DEFAULT_IDENTITY_CATEGORY, DEFAULT_IDENTITY_NAME, type);
|
|
|
|
identities.add(identity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all identities of this client as unmodifiable Collection
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static List<DiscoverInfo.Identity> getIdentities() {
|
|
|
|
return Collections.unmodifiableList(identities);
|
2004-08-10 00:09:20 +02:00
|
|
|
}
|
|
|
|
|
2003-10-14 01:05:06 +02:00
|
|
|
/**
|
|
|
|
* Initializes the packet listeners of the connection that will answer to any
|
|
|
|
* service discovery request.
|
|
|
|
*/
|
|
|
|
private void init() {
|
|
|
|
// Register the new instance and associate it with the connection
|
|
|
|
instances.put(connection, this);
|
2013-02-11 18:22:31 +01:00
|
|
|
|
|
|
|
addFeature(DiscoverInfo.NAMESPACE);
|
|
|
|
addFeature(DiscoverItems.NAMESPACE);
|
|
|
|
|
2003-10-14 01:05:06 +02:00
|
|
|
// Add a listener to the connection that removes the registered instance when
|
|
|
|
// the connection is closed
|
|
|
|
connection.addConnectionListener(new ConnectionListener() {
|
|
|
|
public void connectionClosed() {
|
|
|
|
// Unregister this instance since the connection has been closed
|
|
|
|
instances.remove(connection);
|
|
|
|
}
|
2004-05-09 21:18:02 +02:00
|
|
|
|
2003-10-14 01:05:06 +02:00
|
|
|
public void connectionClosedOnError(Exception e) {
|
2006-09-14 21:16:40 +02:00
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectionFailed(Exception e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reconnectingIn(int seconds) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
|
2006-11-14 22:58:03 +01:00
|
|
|
public void reconnectionSuccessful() {
|
2006-09-14 21:16:40 +02:00
|
|
|
// ignore
|
2003-10-14 01:05:06 +02:00
|
|
|
}
|
|
|
|
});
|
2004-05-09 21:18:02 +02:00
|
|
|
|
2003-10-14 01:05:06 +02:00
|
|
|
// Listen for disco#items requests and answer with an empty result
|
|
|
|
PacketFilter packetFilter = new PacketTypeFilter(DiscoverItems.class);
|
|
|
|
PacketListener packetListener = new PacketListener() {
|
|
|
|
public void processPacket(Packet packet) {
|
|
|
|
DiscoverItems discoverItems = (DiscoverItems) packet;
|
2004-05-09 21:18:02 +02:00
|
|
|
// Send back the items defined in the client if the request is of type GET
|
2003-10-14 01:05:06 +02:00
|
|
|
if (discoverItems != null && discoverItems.getType() == IQ.Type.GET) {
|
|
|
|
DiscoverItems response = new DiscoverItems();
|
|
|
|
response.setType(IQ.Type.RESULT);
|
|
|
|
response.setTo(discoverItems.getFrom());
|
|
|
|
response.setPacketID(discoverItems.getPacketID());
|
2006-07-10 18:56:14 +02:00
|
|
|
response.setNode(discoverItems.getNode());
|
2004-05-09 21:18:02 +02:00
|
|
|
|
|
|
|
// Add the defined items related to the requested node. Look for
|
|
|
|
// the NodeInformationProvider associated with the requested node.
|
2006-07-10 18:56:14 +02:00
|
|
|
NodeInformationProvider nodeInformationProvider =
|
|
|
|
getNodeInformationProvider(discoverItems.getNode());
|
|
|
|
if (nodeInformationProvider != null) {
|
2013-03-18 09:50:48 +01:00
|
|
|
// Specified node was found, add node items
|
|
|
|
response.addItems(nodeInformationProvider.getNodeItems());
|
|
|
|
// Add packet extensions
|
|
|
|
response.addExtensions(nodeInformationProvider.getNodePacketExtensions());
|
2006-02-28 19:52:17 +01:00
|
|
|
} else if(discoverItems.getNode() != null) {
|
2006-07-10 18:56:14 +02:00
|
|
|
// Return <item-not-found/> error since client doesn't contain
|
|
|
|
// the specified node
|
2006-02-28 19:52:17 +01:00
|
|
|
response.setType(IQ.Type.ERROR);
|
2006-07-19 21:24:00 +02:00
|
|
|
response.setError(new XMPPError(XMPPError.Condition.item_not_found));
|
2004-05-09 21:18:02 +02:00
|
|
|
}
|
2003-10-14 01:05:06 +02:00
|
|
|
connection.sendPacket(response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
connection.addPacketListener(packetListener, packetFilter);
|
|
|
|
|
|
|
|
// Listen for disco#info requests and answer the client's supported features
|
|
|
|
// To add a new feature as supported use the #addFeature message
|
|
|
|
packetFilter = new PacketTypeFilter(DiscoverInfo.class);
|
|
|
|
packetListener = new PacketListener() {
|
|
|
|
public void processPacket(Packet packet) {
|
|
|
|
DiscoverInfo discoverInfo = (DiscoverInfo) packet;
|
|
|
|
// Answer the client's supported features if the request is of the GET type
|
|
|
|
if (discoverInfo != null && discoverInfo.getType() == IQ.Type.GET) {
|
|
|
|
DiscoverInfo response = new DiscoverInfo();
|
|
|
|
response.setType(IQ.Type.RESULT);
|
|
|
|
response.setTo(discoverInfo.getFrom());
|
|
|
|
response.setPacketID(discoverInfo.getPacketID());
|
2006-07-10 18:56:14 +02:00
|
|
|
response.setNode(discoverInfo.getNode());
|
2013-03-18 09:50:48 +01:00
|
|
|
// Add the client's identity and features only if "node" is null
|
|
|
|
// and if the request was not send to a node. If Entity Caps are
|
|
|
|
// enabled the client's identity and features are may also added
|
|
|
|
// if the right node is chosen
|
2004-10-31 04:35:20 +01:00
|
|
|
if (discoverInfo.getNode() == null) {
|
2013-03-18 09:50:48 +01:00
|
|
|
addDiscoverInfoTo(response);
|
2003-10-14 01:05:06 +02:00
|
|
|
}
|
2004-10-31 04:35:20 +01:00
|
|
|
else {
|
2006-07-10 18:56:14 +02:00
|
|
|
// Disco#info was sent to a node. Check if we have information of the
|
|
|
|
// specified node
|
|
|
|
NodeInformationProvider nodeInformationProvider =
|
|
|
|
getNodeInformationProvider(discoverInfo.getNode());
|
|
|
|
if (nodeInformationProvider != null) {
|
|
|
|
// Node was found. Add node features
|
2013-03-18 09:50:48 +01:00
|
|
|
response.addFeatures(nodeInformationProvider.getNodeFeatures());
|
2006-10-16 19:56:48 +02:00
|
|
|
// Add node identities
|
2013-03-18 09:50:48 +01:00
|
|
|
response.addIdentities(nodeInformationProvider.getNodeIdentities());
|
|
|
|
// Add packet extensions
|
|
|
|
response.addExtensions(nodeInformationProvider.getNodePacketExtensions());
|
2006-07-10 18:56:14 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Return <item-not-found/> error since specified node was not found
|
|
|
|
response.setType(IQ.Type.ERROR);
|
2006-07-19 21:24:00 +02:00
|
|
|
response.setError(new XMPPError(XMPPError.Condition.item_not_found));
|
2006-07-10 18:56:14 +02:00
|
|
|
}
|
2004-10-31 04:35:20 +01:00
|
|
|
}
|
2003-10-14 01:05:06 +02:00
|
|
|
connection.sendPacket(response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
connection.addPacketListener(packetListener, packetFilter);
|
|
|
|
}
|
|
|
|
|
2013-03-18 09:50:48 +01:00
|
|
|
/**
|
|
|
|
* Add discover info response data.
|
|
|
|
*
|
|
|
|
* @see <a href="http://xmpp.org/extensions/xep-0030.html#info-basic">XEP-30 Basic Protocol; Example 2</a>
|
|
|
|
*
|
|
|
|
* @param response the discover info response packet
|
|
|
|
*/
|
|
|
|
public void addDiscoverInfoTo(DiscoverInfo response) {
|
|
|
|
// First add the identities of the connection
|
|
|
|
response.addIdentities(identities);
|
|
|
|
|
|
|
|
// Add the registered features to the response
|
|
|
|
synchronized (features) {
|
|
|
|
for (Iterator<String> it = getFeatures(); it.hasNext();) {
|
|
|
|
response.addFeature(it.next());
|
|
|
|
}
|
|
|
|
response.addExtension(extendedInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-09 21:18:02 +02:00
|
|
|
/**
|
|
|
|
* Returns the NodeInformationProvider responsible for providing information
|
2004-08-09 23:15:42 +02:00
|
|
|
* (ie items) related to a given node or <tt>null</null> if none.<p>
|
2004-05-09 21:18:02 +02:00
|
|
|
*
|
|
|
|
* In MUC, a node could be 'http://jabber.org/protocol/muc#rooms' which means that the
|
|
|
|
* NodeInformationProvider will provide information about the rooms where the user has joined.
|
|
|
|
*
|
2004-08-05 18:57:28 +02:00
|
|
|
* @param node the node that contains items associated with an entity not addressable as a JID.
|
2004-05-09 21:18:02 +02:00
|
|
|
* @return the NodeInformationProvider responsible for providing information related
|
2004-08-05 18:57:28 +02:00
|
|
|
* to a given node.
|
2004-05-09 21:18:02 +02:00
|
|
|
*/
|
|
|
|
private NodeInformationProvider getNodeInformationProvider(String node) {
|
2004-08-10 15:03:15 +02:00
|
|
|
if (node == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2006-07-18 07:14:33 +02:00
|
|
|
return nodeInformationProviders.get(node);
|
2004-05-09 21:18:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the NodeInformationProvider responsible for providing information
|
2004-08-09 23:15:42 +02:00
|
|
|
* (ie items) related to a given node. Every time this client receives a disco request
|
2004-05-09 21:18:02 +02:00
|
|
|
* regarding the items of a given node, the provider associated to that node will be the
|
|
|
|
* responsible for providing the requested information.<p>
|
|
|
|
*
|
|
|
|
* In MUC, a node could be 'http://jabber.org/protocol/muc#rooms' which means that the
|
|
|
|
* NodeInformationProvider will provide information about the rooms where the user has joined.
|
|
|
|
*
|
2004-08-05 18:57:28 +02:00
|
|
|
* @param node the node whose items will be provided by the NodeInformationProvider.
|
2004-08-09 23:15:42 +02:00
|
|
|
* @param listener the NodeInformationProvider responsible for providing items related
|
|
|
|
* to the node.
|
2004-05-09 21:18:02 +02:00
|
|
|
*/
|
|
|
|
public void setNodeInformationProvider(String node, NodeInformationProvider listener) {
|
|
|
|
nodeInformationProviders.put(node, listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the NodeInformationProvider responsible for providing information
|
2004-08-09 23:15:42 +02:00
|
|
|
* (ie items) related to a given node. This means that no more information will be
|
2004-05-09 21:18:02 +02:00
|
|
|
* available for the specified node.
|
|
|
|
*
|
|
|
|
* In MUC, a node could be 'http://jabber.org/protocol/muc#rooms' which means that the
|
|
|
|
* NodeInformationProvider will provide information about the rooms where the user has joined.
|
|
|
|
*
|
2004-08-05 18:57:28 +02:00
|
|
|
* @param node the node to remove the associated NodeInformationProvider.
|
2004-05-09 21:18:02 +02:00
|
|
|
*/
|
|
|
|
public void removeNodeInformationProvider(String node) {
|
|
|
|
nodeInformationProviders.remove(node);
|
|
|
|
}
|
|
|
|
|
2003-10-14 01:05:06 +02:00
|
|
|
/**
|
|
|
|
* Returns the supported features by this XMPP entity.
|
|
|
|
*
|
2004-08-05 18:57:28 +02:00
|
|
|
* @return an Iterator on the supported features by this XMPP entity.
|
2003-10-14 01:05:06 +02:00
|
|
|
*/
|
2006-07-18 07:14:33 +02:00
|
|
|
public Iterator<String> getFeatures() {
|
2003-10-14 01:05:06 +02:00
|
|
|
synchronized (features) {
|
2006-07-18 07:14:33 +02:00
|
|
|
return Collections.unmodifiableList(new ArrayList<String>(features)).iterator();
|
2003-10-14 01:05:06 +02:00
|
|
|
}
|
|
|
|
}
|
2004-05-09 21:18:02 +02:00
|
|
|
|
2013-03-18 09:50:48 +01:00
|
|
|
/**
|
|
|
|
* Returns the supported features by this XMPP entity.
|
|
|
|
*
|
|
|
|
* @return a copy of the List on the supported features by this XMPP entity.
|
|
|
|
*/
|
|
|
|
public List<String> getFeaturesList() {
|
|
|
|
synchronized (features) {
|
|
|
|
return new LinkedList<String>(features);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-14 01:05:06 +02:00
|
|
|
/**
|
|
|
|
* Registers that a new feature is supported by this XMPP entity. When this client is
|
2005-04-04 21:50:09 +02:00
|
|
|
* queried for its information the registered features will be answered.<p>
|
|
|
|
*
|
|
|
|
* Since no packet is actually sent to the server it is safe to perform this operation
|
|
|
|
* before logging to the server. In fact, you may want to configure the supported features
|
|
|
|
* before logging to the server so that the information is already available if it is required
|
|
|
|
* upon login.
|
2003-10-14 01:05:06 +02:00
|
|
|
*
|
2004-08-05 18:57:28 +02:00
|
|
|
* @param feature the feature to register as supported.
|
2003-10-14 01:05:06 +02:00
|
|
|
*/
|
|
|
|
public void addFeature(String feature) {
|
|
|
|
synchronized (features) {
|
|
|
|
features.add(feature);
|
2013-03-18 09:50:48 +01:00
|
|
|
renewEntityCapsVersion();
|
2003-10-14 01:05:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-04-04 21:50:09 +02:00
|
|
|
* Removes the specified feature from the supported features by this XMPP entity.<p>
|
|
|
|
*
|
|
|
|
* Since no packet is actually sent to the server it is safe to perform this operation
|
|
|
|
* before logging to the server.
|
2003-10-14 01:05:06 +02:00
|
|
|
*
|
2004-08-05 18:57:28 +02:00
|
|
|
* @param feature the feature to remove from the supported features.
|
2003-10-14 01:05:06 +02:00
|
|
|
*/
|
|
|
|
public void removeFeature(String feature) {
|
|
|
|
synchronized (features) {
|
|
|
|
features.remove(feature);
|
2013-03-18 09:50:48 +01:00
|
|
|
renewEntityCapsVersion();
|
2003-10-14 01:05:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the specified feature is registered in the ServiceDiscoveryManager.
|
|
|
|
*
|
2004-08-05 18:57:28 +02:00
|
|
|
* @param feature the feature to look for.
|
|
|
|
* @return a boolean indicating if the specified featured is registered or not.
|
2003-10-14 01:05:06 +02:00
|
|
|
*/
|
|
|
|
public boolean includesFeature(String feature) {
|
|
|
|
synchronized (features) {
|
|
|
|
return features.contains(feature);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-15 17:32:46 +01:00
|
|
|
/**
|
|
|
|
* Registers extended discovery information of this XMPP entity. When this
|
|
|
|
* client is queried for its information this data form will be returned as
|
|
|
|
* specified by XEP-0128.
|
|
|
|
* <p>
|
|
|
|
*
|
|
|
|
* Since no packet is actually sent to the server it is safe to perform this
|
|
|
|
* operation before logging to the server. In fact, you may want to
|
|
|
|
* configure the extended info before logging to the server so that the
|
|
|
|
* information is already available if it is required upon login.
|
|
|
|
*
|
|
|
|
* @param info
|
|
|
|
* the data form that contains the extend service discovery
|
|
|
|
* information.
|
|
|
|
*/
|
|
|
|
public void setExtendedInfo(DataForm info) {
|
|
|
|
extendedInfo = info;
|
2013-03-18 09:50:48 +01:00
|
|
|
renewEntityCapsVersion();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the data form that is set as extended information for this Service Discovery instance (XEP-0128)
|
|
|
|
*
|
|
|
|
* @see <a href="http://xmpp.org/extensions/xep-0128.html">XEP-128: Service Discovery Extensions</a>
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public DataForm getExtendedInfo() {
|
|
|
|
return extendedInfo;
|
2008-02-15 17:32:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-18 09:50:48 +01:00
|
|
|
* Returns the data form as List of PacketExtensions, or null if no data form is set.
|
|
|
|
* This representation is needed by some classes (e.g. EntityCapsManager, NodeInformationProvider)
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public List<PacketExtension> getExtendedInfoAsList() {
|
|
|
|
List<PacketExtension> res = null;
|
|
|
|
if (extendedInfo != null) {
|
|
|
|
res = new ArrayList<PacketExtension>(1);
|
|
|
|
res.add(extendedInfo);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the data form containing extended service discovery information
|
2008-02-15 17:32:46 +01:00
|
|
|
* from the information returned by this XMPP entity.<p>
|
|
|
|
*
|
|
|
|
* Since no packet is actually sent to the server it is safe to perform this
|
|
|
|
* operation before logging to the server.
|
|
|
|
*/
|
|
|
|
public void removeExtendedInfo() {
|
|
|
|
extendedInfo = null;
|
2013-03-18 09:50:48 +01:00
|
|
|
renewEntityCapsVersion();
|
2008-02-15 17:32:46 +01:00
|
|
|
}
|
|
|
|
|
2003-10-14 01:05:06 +02:00
|
|
|
/**
|
|
|
|
* Returns the discovered information of a given XMPP entity addressed by its JID.
|
2013-03-18 09:50:48 +01:00
|
|
|
* Use null as entityID to query the server
|
2003-10-14 01:05:06 +02:00
|
|
|
*
|
2013-03-18 09:50:48 +01:00
|
|
|
* @param entityID the address of the XMPP entity or null.
|
2004-08-05 18:57:28 +02:00
|
|
|
* @return the discovered information.
|
|
|
|
* @throws XMPPException if the operation failed for some reason.
|
2003-10-14 01:05:06 +02:00
|
|
|
*/
|
|
|
|
public DiscoverInfo discoverInfo(String entityID) throws XMPPException {
|
2013-03-18 09:50:48 +01:00
|
|
|
if (entityID == null)
|
|
|
|
return discoverInfo(null, null);
|
|
|
|
|
|
|
|
// Check if the have it cached in the Entity Capabilities Manager
|
|
|
|
DiscoverInfo info = EntityCapsManager.getDiscoverInfoByUser(entityID);
|
|
|
|
|
|
|
|
if (info != null) {
|
|
|
|
// We were able to retrieve the information from Entity Caps and
|
|
|
|
// avoided a disco request, hurray!
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to get the newest node#version if it's known, otherwise null is
|
|
|
|
// returned
|
|
|
|
EntityCapsManager.NodeVerHash nvh = EntityCapsManager.getNodeVerHashByJid(entityID);
|
|
|
|
|
|
|
|
// Discover by requesting the information from the remote entity
|
|
|
|
// Note that wee need to use NodeVer as argument for Node if it exists
|
|
|
|
info = discoverInfo(entityID, nvh != null ? nvh.getNodeVer() : null);
|
|
|
|
|
|
|
|
// If the node version is known, store the new entry.
|
|
|
|
if (nvh != null) {
|
|
|
|
if (EntityCapsManager.verifyDiscvoerInfoVersion(nvh.getVer(), nvh.getHash(), info))
|
|
|
|
EntityCapsManager.addDiscoverInfoByNode(nvh.getNodeVer(), info);
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
2003-10-14 01:05:06 +02:00
|
|
|
}
|
2004-05-09 21:18:02 +02:00
|
|
|
|
2003-10-14 01:05:06 +02:00
|
|
|
/**
|
|
|
|
* Returns the discovered information of a given XMPP entity addressed by its JID and
|
|
|
|
* note attribute. Use this message only when trying to query information which is not
|
|
|
|
* directly addressable.
|
|
|
|
*
|
2013-03-18 09:50:48 +01:00
|
|
|
* @see <a href="http://xmpp.org/extensions/xep-0030.html#info-basic">XEP-30 Basic Protocol</a>
|
|
|
|
* @see <a href="http://xmpp.org/extensions/xep-0030.html#info-nodes">XEP-30 Info Nodes</a>
|
|
|
|
*
|
2004-08-05 18:57:28 +02:00
|
|
|
* @param entityID the address of the XMPP entity.
|
2013-03-18 09:50:48 +01:00
|
|
|
* @param node the optional attribute that supplements the 'jid' attribute.
|
2004-08-05 18:57:28 +02:00
|
|
|
* @return the discovered information.
|
|
|
|
* @throws XMPPException if the operation failed for some reason.
|
2003-10-14 01:05:06 +02:00
|
|
|
*/
|
|
|
|
public DiscoverInfo discoverInfo(String entityID, String node) throws XMPPException {
|
|
|
|
// Discover the entity's info
|
|
|
|
DiscoverInfo disco = new DiscoverInfo();
|
|
|
|
disco.setType(IQ.Type.GET);
|
|
|
|
disco.setTo(entityID);
|
|
|
|
disco.setNode(node);
|
|
|
|
|
|
|
|
// Create a packet collector to listen for a response.
|
|
|
|
PacketCollector collector =
|
|
|
|
connection.createPacketCollector(new PacketIDFilter(disco.getPacketID()));
|
|
|
|
|
|
|
|
connection.sendPacket(disco);
|
|
|
|
|
|
|
|
// Wait up to 5 seconds for a result.
|
2004-05-03 02:43:51 +02:00
|
|
|
IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
2004-07-16 22:46:16 +02:00
|
|
|
// Stop queuing results
|
|
|
|
collector.cancel();
|
2003-10-14 01:05:06 +02:00
|
|
|
if (result == null) {
|
|
|
|
throw new XMPPException("No response from the server.");
|
|
|
|
}
|
|
|
|
if (result.getType() == IQ.Type.ERROR) {
|
|
|
|
throw new XMPPException(result.getError());
|
|
|
|
}
|
|
|
|
return (DiscoverInfo) result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the discovered items of a given XMPP entity addressed by its JID.
|
|
|
|
*
|
2004-08-05 18:57:28 +02:00
|
|
|
* @param entityID the address of the XMPP entity.
|
|
|
|
* @return the discovered information.
|
|
|
|
* @throws XMPPException if the operation failed for some reason.
|
2003-10-14 01:05:06 +02:00
|
|
|
*/
|
|
|
|
public DiscoverItems discoverItems(String entityID) throws XMPPException {
|
|
|
|
return discoverItems(entityID, null);
|
|
|
|
}
|
2004-05-09 21:18:02 +02:00
|
|
|
|
2003-10-14 01:05:06 +02:00
|
|
|
/**
|
|
|
|
* Returns the discovered items of a given XMPP entity addressed by its JID and
|
|
|
|
* note attribute. Use this message only when trying to query information which is not
|
|
|
|
* directly addressable.
|
|
|
|
*
|
2004-08-05 18:57:28 +02:00
|
|
|
* @param entityID the address of the XMPP entity.
|
2013-03-18 09:50:48 +01:00
|
|
|
* @param node the optional attribute that supplements the 'jid' attribute.
|
2004-08-05 18:57:28 +02:00
|
|
|
* @return the discovered items.
|
|
|
|
* @throws XMPPException if the operation failed for some reason.
|
2003-10-14 01:05:06 +02:00
|
|
|
*/
|
|
|
|
public DiscoverItems discoverItems(String entityID, String node) throws XMPPException {
|
|
|
|
// Discover the entity's items
|
|
|
|
DiscoverItems disco = new DiscoverItems();
|
|
|
|
disco.setType(IQ.Type.GET);
|
|
|
|
disco.setTo(entityID);
|
|
|
|
disco.setNode(node);
|
|
|
|
|
|
|
|
// Create a packet collector to listen for a response.
|
|
|
|
PacketCollector collector =
|
|
|
|
connection.createPacketCollector(new PacketIDFilter(disco.getPacketID()));
|
|
|
|
|
|
|
|
connection.sendPacket(disco);
|
|
|
|
|
|
|
|
// Wait up to 5 seconds for a result.
|
2004-05-03 02:43:51 +02:00
|
|
|
IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
2004-07-16 22:46:16 +02:00
|
|
|
// Stop queuing results
|
|
|
|
collector.cancel();
|
2003-10-14 01:05:06 +02:00
|
|
|
if (result == null) {
|
|
|
|
throw new XMPPException("No response from the server.");
|
|
|
|
}
|
|
|
|
if (result.getType() == IQ.Type.ERROR) {
|
|
|
|
throw new XMPPException(result.getError());
|
|
|
|
}
|
|
|
|
return (DiscoverItems) result;
|
|
|
|
}
|
|
|
|
|
2004-08-05 18:57:28 +02:00
|
|
|
/**
|
|
|
|
* Returns true if the server supports publishing of items. A client may wish to publish items
|
|
|
|
* to the server so that the server can provide items associated to the client. These items will
|
|
|
|
* be returned by the server whenever the server receives a disco request targeted to the bare
|
|
|
|
* address of the client (i.e. user@host.com).
|
|
|
|
*
|
|
|
|
* @param entityID the address of the XMPP entity.
|
|
|
|
* @return true if the server supports publishing of items.
|
|
|
|
* @throws XMPPException if the operation failed for some reason.
|
|
|
|
*/
|
|
|
|
public boolean canPublishItems(String entityID) throws XMPPException {
|
|
|
|
DiscoverInfo info = discoverInfo(entityID);
|
2013-03-18 09:50:48 +01:00
|
|
|
return canPublishItems(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the server supports publishing of items. A client may wish to publish items
|
|
|
|
* to the server so that the server can provide items associated to the client. These items will
|
|
|
|
* be returned by the server whenever the server receives a disco request targeted to the bare
|
|
|
|
* address of the client (i.e. user@host.com).
|
|
|
|
*
|
|
|
|
* @param DiscoverInfo the discover info packet to check.
|
|
|
|
* @return true if the server supports publishing of items.
|
|
|
|
*/
|
|
|
|
public static boolean canPublishItems(DiscoverInfo info) {
|
|
|
|
return info.containsFeature("http://jabber.org/protocol/disco#publish");
|
|
|
|
}
|
2004-08-05 18:57:28 +02:00
|
|
|
|
2003-10-14 01:05:06 +02:00
|
|
|
/**
|
|
|
|
* Publishes new items to a parent entity. The item elements to publish MUST have at least
|
|
|
|
* a 'jid' attribute specifying the Entity ID of the item, and an action attribute which
|
|
|
|
* specifies the action being taken for that item. Possible action values are: "update" and
|
|
|
|
* "remove".
|
|
|
|
*
|
2004-08-05 18:57:28 +02:00
|
|
|
* @param entityID the address of the XMPP entity.
|
|
|
|
* @param discoverItems the DiscoveryItems to publish.
|
|
|
|
* @throws XMPPException if the operation failed for some reason.
|
|
|
|
*/
|
|
|
|
public void publishItems(String entityID, DiscoverItems discoverItems)
|
|
|
|
throws XMPPException {
|
|
|
|
publishItems(entityID, null, discoverItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Publishes new items to a parent entity and node. The item elements to publish MUST have at
|
|
|
|
* least a 'jid' attribute specifying the Entity ID of the item, and an action attribute which
|
|
|
|
* specifies the action being taken for that item. Possible action values are: "update" and
|
|
|
|
* "remove".
|
|
|
|
*
|
|
|
|
* @param entityID the address of the XMPP entity.
|
|
|
|
* @param node the attribute that supplements the 'jid' attribute.
|
|
|
|
* @param discoverItems the DiscoveryItems to publish.
|
|
|
|
* @throws XMPPException if the operation failed for some reason.
|
2003-10-14 01:05:06 +02:00
|
|
|
*/
|
2004-08-05 18:57:28 +02:00
|
|
|
public void publishItems(String entityID, String node, DiscoverItems discoverItems)
|
|
|
|
throws XMPPException {
|
2003-10-14 01:05:06 +02:00
|
|
|
discoverItems.setType(IQ.Type.SET);
|
|
|
|
discoverItems.setTo(entityID);
|
2004-08-05 18:57:28 +02:00
|
|
|
discoverItems.setNode(node);
|
2004-05-09 21:18:02 +02:00
|
|
|
|
2003-10-14 01:05:06 +02:00
|
|
|
// Create a packet collector to listen for a response.
|
|
|
|
PacketCollector collector =
|
|
|
|
connection.createPacketCollector(new PacketIDFilter(discoverItems.getPacketID()));
|
|
|
|
|
|
|
|
connection.sendPacket(discoverItems);
|
|
|
|
|
|
|
|
// Wait up to 5 seconds for a result.
|
2004-05-03 02:43:51 +02:00
|
|
|
IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
2004-07-16 22:46:16 +02:00
|
|
|
// Stop queuing results
|
|
|
|
collector.cancel();
|
2003-10-14 01:05:06 +02:00
|
|
|
if (result == null) {
|
|
|
|
throw new XMPPException("No response from the server.");
|
|
|
|
}
|
|
|
|
if (result.getType() == IQ.Type.ERROR) {
|
|
|
|
throw new XMPPException(result.getError());
|
|
|
|
}
|
|
|
|
}
|
2013-03-18 09:50:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Entity Capabilities
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the ServiceDiscoveryManager with an EntityCapsManger
|
|
|
|
* that speeds up certain lookups
|
|
|
|
* @param manager
|
|
|
|
*/
|
|
|
|
public void setEntityCapsManager(EntityCapsManager manager) {
|
|
|
|
capsManager = manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the Entity Capabilities Verification String
|
|
|
|
* if EntityCaps is enabled
|
|
|
|
*/
|
|
|
|
private void renewEntityCapsVersion() {
|
|
|
|
if (capsManager != null && capsManager.entityCapsEnabled())
|
|
|
|
capsManager.updateLocalEntityCaps();
|
|
|
|
}
|
|
|
|
}
|