📄 iqdiscoinfohandler.java
字号:
/**
* $RCSfile: IQDiscoInfoHandler.java,v $
* $Revision: 2859 $
* $Date: 2005-09-22 02:30:39 -0300 (Thu, 22 Sep 2005) $
*
* Copyright (C) 2008 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution, or a commercial license
* agreement with Jive.
*/
package org.jivesoftware.openfire.disco;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName;
import org.jivesoftware.openfire.IQHandlerInfo;
import org.jivesoftware.openfire.SessionManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.cluster.ClusterEventListener;
import org.jivesoftware.openfire.cluster.ClusterManager;
import org.jivesoftware.openfire.cluster.NodeID;
import org.jivesoftware.openfire.forms.spi.XDataFormImpl;
import org.jivesoftware.openfire.handler.IQHandler;
import org.jivesoftware.openfire.resultsetmanager.ResultSet;
import org.jivesoftware.openfire.user.UserManager;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.cache.Cache;
import org.jivesoftware.util.cache.CacheFactory;
import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.PacketError;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.locks.Lock;
/**
* IQDiscoInfoHandler is responsible for handling disco#info requests. This class holds a map with
* the main entities and the associated DiscoInfoProvider. We are considering the host of the
* recipient JIDs as main entities. It's the DiscoInfoProvider responsibility to provide information
* about the JID's name together with any possible requested node.<p>
* <p/>
* For example, let's have in the entities map the following entries: "localhost" and
* "conference.localhost". Associated with each entry we have different DiscoInfoProviders. Now we
* receive a disco#info request for the following JID: "room@conference.localhost" which is a disco
* request for a MUC room. So IQDiscoInfoHandler will look for the DiscoInfoProvider associated
* with the JID's host which in this case is "conference.localhost". Once we have located the
* provider we will delegate to the provider the responsibility to provide the info specific to
* the JID's name which in this case is "room". Among the information that a room could provide we
* could find its identity and the features it supports (e.g. 'muc_passwordprotected',
* 'muc_unmoderated', etc.). Finally, after we have collected all the information provided by the
* provider we will add it to the reply. On the other hand, if no provider was found or the provider
* has no information for the requested name/node then a not-found error will be returned.
*
* @author Gaston Dombiak
*/
public class IQDiscoInfoHandler extends IQHandler implements ClusterEventListener {
public static final String NAMESPACE_DISCO_INFO = "http://jabber.org/protocol/disco#info";
private Map<String, DiscoInfoProvider> entities = new HashMap<String, DiscoInfoProvider>();
private Set<String> localServerFeatures = new CopyOnWriteArraySet<String>();
private Cache<String, Set<NodeID>> serverFeatures;
private List<Element> serverIdentities = new ArrayList<Element>();
private Map<String, DiscoInfoProvider> serverNodeProviders = new ConcurrentHashMap<String, DiscoInfoProvider>();
private IQHandlerInfo info;
private List<Element> anonymousUserIdentities = new ArrayList<Element>();
private List<Element> registeredUserIdentities = new ArrayList<Element>();
private List<String> userFeatures = new ArrayList<String>();
public IQDiscoInfoHandler() {
super("XMPP Disco Info Handler");
info = new IQHandlerInfo("query", NAMESPACE_DISCO_INFO);
// Initialize the user identity and features collections (optimization to avoid creating
// the same objects for each response)
Element userIdentity = DocumentHelper.createElement("identity");
userIdentity.addAttribute("category", "account");
userIdentity.addAttribute("type", "anonymous");
anonymousUserIdentities.add(userIdentity);
userIdentity = DocumentHelper.createElement("identity");
userIdentity.addAttribute("category", "account");
userIdentity.addAttribute("type", "registered");
registeredUserIdentities.add(userIdentity);
userFeatures.add(NAMESPACE_DISCO_INFO);
}
public IQHandlerInfo getInfo() {
return info;
}
public IQ handleIQ(IQ packet) {
// Create a copy of the sent pack that will be used as the reply
// we only need to add the requested info to the reply if any otherwise add
// a not found error
IQ reply = IQ.createResultIQ(packet);
// Look for a DiscoInfoProvider associated with the requested entity.
// We consider the host of the recipient JID of the packet as the entity. It's the
// DiscoInfoProvider responsibility to provide information about the JID's name together
// with any possible requested node.
DiscoInfoProvider infoProvider = getProvider(packet.getTo() == null ?
XMPPServer.getInstance().getServerInfo().getXMPPDomain() : packet.getTo().getDomain());
if (infoProvider != null) {
// Get the JID's name
String name = packet.getTo() == null ? null : packet.getTo().getNode();
if (name == null || name.trim().length() == 0) {
name = null;
}
// Get the requested node
Element iq = packet.getChildElement();
String node = iq.attributeValue("node");
//String node = metaData.getProperty("query:node");
// Check if we have information about the requested name and node
if (infoProvider.hasInfo(name, node, packet.getFrom())) {
reply.setChildElement(iq.createCopy());
Element queryElement = reply.getChildElement();
// Add to the reply all the identities provided by the DiscoInfoProvider
Element identity;
Iterator<Element> identities = infoProvider.getIdentities(name, node, packet.getFrom());
while (identities.hasNext()) {
identity = identities.next();
identity.setQName(new QName(identity.getName(), queryElement.getNamespace()));
queryElement.add((Element)identity.clone());
}
// Add to the reply all the features provided by the DiscoInfoProvider
Iterator<String> features = infoProvider.getFeatures(name, node, packet.getFrom());
boolean hasDiscoInfoFeature = false;
boolean hasDiscoItemsFeature = false;
boolean hasResultSetManagementFeature = false;
while (features.hasNext()) {
final String feature = features.next();
queryElement.addElement("feature").addAttribute("var", feature);
if (feature.equals(NAMESPACE_DISCO_INFO)) {
hasDiscoInfoFeature = true;
} else if (feature.equals("http://jabber.org/protocol/disco#items")) {
hasDiscoItemsFeature = true;
} else if (feature.equals(ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT)) {
hasResultSetManagementFeature = true;
}
}
if (hasDiscoItemsFeature && !hasResultSetManagementFeature) {
// IQDiscoItemsHandler provides result set management
// support.
queryElement.addElement("feature").addAttribute("var",
ResultSet.NAMESPACE_RESULT_SET_MANAGEMENT);
}
if (!hasDiscoInfoFeature) {
// XEP-0030 requires that every entity that supports service
// discovery broadcasts the disco#info feature.
queryElement.addElement("feature").addAttribute("var", NAMESPACE_DISCO_INFO);
}
// Add to the reply the extended info (XDataForm) provided by the DiscoInfoProvider
XDataFormImpl dataForm = infoProvider.getExtendedInfo(name, node, packet.getFrom());
if (dataForm != null) {
queryElement.add(dataForm.asXMLElement());
}
}
else {
// If the DiscoInfoProvider has no information for the requested name and node
// then answer a not found error
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(PacketError.Condition.item_not_found);
}
}
else {
// If we didn't find a DiscoInfoProvider then answer a not found error
reply.setChildElement(packet.getChildElement().createCopy());
reply.setError(PacketError.Condition.item_not_found);
}
return reply;
}
/**
* Sets the DiscoInfoProvider to use when a disco#info packet is sent to the server itself
* and the specified node. For instance, if node matches "http://jabber.org/protocol/offline"
* then a special DiscoInfoProvider should be use to return information about offline messages.
*
* @param node the node that the provider will handle.
* @param provider the DiscoInfoProvider that will handle disco#info packets sent with the
* specified node.
*/
public void setServerNodeInfoProvider(String node, DiscoInfoProvider provider) {
serverNodeProviders.put(node, provider);
}
/**
* Removes the DiscoInfoProvider to use when a disco#info packet is sent to the server itself
* and the specified node.
*
* @param node the node that the provider was handling.
*/
public void removeServerNodeInfoProvider(String node) {
serverNodeProviders.remove(node);
}
/**
* Returns the DiscoInfoProvider responsible for providing information about a given entity or
* null if none was found.
*
* @param name the name of the identity.
* @return the DiscoInfoProvider responsible for providing information about a given entity or
* null if none was found.
*/
private DiscoInfoProvider getProvider(String name) {
return entities.get(name);
}
/**
* Sets that a given DiscoInfoProvider will provide information about a given entity. This
* message must be used when new modules (e.g. MUC) are implemented and need to provide
* information about them.
*
* @param name the name of the entity.
* @param provider the DiscoInfoProvider that will provide the entity's information.
*/
protected void setProvider(String name, DiscoInfoProvider provider) {
entities.put(name, provider);
}
/**
* Removes the DiscoInfoProvider related to a given entity.
*
* @param name the name of the entity.
*/
protected void removeProvider(String name) {
entities.remove(name);
}
/**
* Adds the features provided by the new service that implements the ServerFeaturesProvider
* interface. This information will be used whenever a disco for information is made against
* the server (i.e. the packet's target is the server).
* Example of features are: jabber:iq:agents, jabber:iq:time, etc.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -