📄 iqdiscoitemshandler.java
字号:
public void addServerItemsProvider(ServerItemsProvider provider) {
DiscoServerItem discoItem;
Iterator<DiscoServerItem> items = provider.getItems();
if (items == null) {
// Do nothing
return;
}
while (items.hasNext()) {
discoItem = items.next();
// Add the element to the list of items related to the server
addComponentItem(discoItem.getJID(), discoItem.getNode(), discoItem.getName());
// Add the new item as a valid entity that could receive info and items disco requests
String host = new JID(discoItem.getJID()).getDomain();
infoHandler.setProvider(host, discoItem.getDiscoInfoProvider());
setProvider(host, discoItem.getDiscoItemsProvider());
}
}
/**
* Removes the provided items as a service of the service.
*
* @param provider The provider that is being removed.
*/
public void removeServerItemsProvider(ServerItemsProvider provider) {
DiscoServerItem discoItem;
Iterator<DiscoServerItem> items = provider.getItems();
if (items == null) {
// Do nothing
return;
}
while (items.hasNext()) {
discoItem = items.next();
// Remove the item from the server items list
removeComponentItem(discoItem.getJID());
// Remove the item as a valid entity that could receive info and items disco requests
String host = new JID(discoItem.getJID()).getDomain();
infoHandler.removeProvider(host);
removeProvider(host);
}
}
/**
* Sets the DiscoItemsProvider to use when a disco#items packet is sent to the server itself
* and the specified node. For instance, if node matches "http://jabber.org/protocol/offline"
* then a special DiscoItemsProvider should be use to return information about offline messages.
*
* @param node the node that the provider will handle.
* @param provider the DiscoItemsProvider that will handle disco#items packets sent with the
* specified node.
*/
public void setServerNodeInfoProvider(String node, DiscoItemsProvider provider) {
serverNodeProviders.put(node, provider);
}
/**
* Removes the DiscoItemsProvider to use when a disco#items 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);
}
/**
* Registers a new disco item for a component. The jid attribute of the item will match the jid
* of the component and the name should be the name of the component discovered using disco.
*
* @param jid the jid of the component.
* @param name the discovered name of the component.
*/
public void addComponentItem(String jid, String name) {
addComponentItem(jid, null, name);
}
/**
* Registers a new disco item for a component. The jid attribute of the item will match the jid
* of the component and the name should be the name of the component discovered using disco.
*
* @param jid the jid of the component.
* @param node the node that complements the jid address.
* @param name the discovered name of the component.
*/
public synchronized void addComponentItem(String jid, String node, String name) {
// A component may send his disco#info many times and we only want to have one item
// for the component so remove any element under the requested jid
removeComponentItem(jid);
// Create a new element based on the provided DiscoItem
Element element = DocumentHelper.createElement("item");
element.addAttribute("jid", jid);
element.addAttribute("node", node);
element.addAttribute("name", name);
// Add the element to the list of items related to the server
serverItems.add(element);
}
/**
* Removes a disco item for a component that has been removed from the server.
*
* @param jid the jid of the component being removed.
*/
public synchronized void removeComponentItem(String jid) {
for (Iterator<Element> it = serverItems.iterator(); it.hasNext();) {
if (jid.equals(it.next().attributeValue("jid"))) {
it.remove();
}
}
}
public void initialize(XMPPServer server) {
super.initialize(server);
// Track the implementors of ServerItemsProvider so that we can collect the items
// provided by the server
infoHandler = server.getIQDiscoInfoHandler();
setProvider(server.getServerInfo().getName(), getServerItemsProvider());
}
public void start() throws IllegalStateException {
super.start();
for (ServerItemsProvider provider : XMPPServer.getInstance().getServerItemsProviders()) {
addServerItemsProvider(provider);
}
}
public Iterator<String> getFeatures() {
List<String> features = new ArrayList<String>();
features.add("http://jabber.org/protocol/disco#items");
// TODO Comment out this line when publishing of client items is implemented
//features.add("http://jabber.org/protocol/disco#publish");
return features.iterator();
}
private DiscoItemsProvider getServerItemsProvider() {
return new DiscoItemsProvider() {
public Iterator<Element> getItems(String name, String node, JID senderJID) {
if (node != null) {
// Check if there is a provider for the requested node
if (serverNodeProviders.get(node) != null) {
return serverNodeProviders.get(node).getItems(name, node, senderJID);
}
return null;
}
if (name == null) {
return serverItems.iterator();
}
else {
List<Element> answer = new ArrayList<Element>();
try {
User user = UserManager.getInstance().getUser(name);
RosterItem item = user.getRoster().getRosterItem(senderJID);
// If the requesting entity is subscribed to the account's presence then
// answer the user's "available resources"
if (item.getSubStatus() == RosterItem.SUB_FROM ||
item.getSubStatus() == RosterItem.SUB_BOTH) {
for (Session session : SessionManager.getInstance().getSessions(name)) {
Element element = DocumentHelper.createElement("item");
element.addAttribute("jid", session.getAddress().toString());
answer.add(element);
}
}
return answer.iterator();
}
catch (UserNotFoundException e) {
return answer.iterator();
}
}
}
};
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -