mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-12-12 14:01:08 +01:00
Add API to find services for a given feature
It's a common pattern to look for all services hosted under the users service that provide a given feature. Let's provide an API for that and use that API in the methods that benefit from it.
This commit is contained in:
parent
6b7fa4a788
commit
7b3331dda0
5 changed files with 95 additions and 93 deletions
|
|
@ -23,6 +23,7 @@ import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
|||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.provider.IQProvider;
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
import org.jivesoftware.smackx.xdata.Form;
|
||||
import org.jivesoftware.smackx.xdata.FormField;
|
||||
import org.jivesoftware.smackx.xdata.packet.DataForm;
|
||||
|
|
@ -40,18 +41,23 @@ import org.xmlpull.v1.XmlPullParser;
|
|||
*/
|
||||
public class UserSearch extends IQ {
|
||||
|
||||
public static final String NAMESPACE = "jabber:iq:search";
|
||||
|
||||
/**
|
||||
* Creates a new instance of UserSearch.
|
||||
*/
|
||||
public UserSearch() {
|
||||
}
|
||||
|
||||
public String getChildElementXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<query xmlns=\"jabber:iq:search\">");
|
||||
buf.append(getExtensionsXML());
|
||||
buf.append("</query>");
|
||||
return buf.toString();
|
||||
@Override
|
||||
public XmlStringBuilder getChildElementXML() {
|
||||
XmlStringBuilder xml = new XmlStringBuilder();
|
||||
xml.halfOpenElement(IQ.QUERY_ELEMENT);
|
||||
xml.xmlnsAttribute(NAMESPACE);
|
||||
xml.rightAngelBracket();
|
||||
xml.append(getExtensionsXML());
|
||||
xml.closeElement(IQ.QUERY_ELEMENT);
|
||||
return xml;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -19,16 +19,11 @@ package org.jivesoftware.smackx.search;
|
|||
import org.jivesoftware.smack.SmackException.NoResponseException;
|
||||
import org.jivesoftware.smack.SmackException.NotConnectedException;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
||||
import org.jivesoftware.smackx.disco.packet.DiscoverItems;
|
||||
import org.jivesoftware.smackx.xdata.Form;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The UserSearchManager is a facade built upon Jabber Search Services (XEP-055) to allow for searching
|
||||
|
|
@ -101,29 +96,7 @@ public class UserSearchManager {
|
|||
* @throws NotConnectedException
|
||||
*/
|
||||
public Collection<String> getSearchServices() throws NoResponseException, XMPPErrorException, NotConnectedException {
|
||||
final List<String> searchServices = new ArrayList<String>();
|
||||
ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(con);
|
||||
DiscoverItems items = discoManager.discoverItems(con.getServiceName());
|
||||
for (DiscoverItems.Item item : items.getItems()) {
|
||||
try {
|
||||
DiscoverInfo info;
|
||||
try {
|
||||
info = discoManager.discoverInfo(item.getEntityID());
|
||||
}
|
||||
catch (XMPPException e) {
|
||||
// Ignore Case
|
||||
continue;
|
||||
}
|
||||
|
||||
if (info.containsFeature("jabber:iq:search")) {
|
||||
searchServices.add(item.getEntityID());
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
// No info found.
|
||||
break;
|
||||
}
|
||||
}
|
||||
return searchServices;
|
||||
return discoManager.findServices(UserSearch.NAMESPACE, false, false);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue