use of org.jivesoftware.smackx.disco.packet.DiscoverItems in project Smack by igniterealtime.
the class DiscoverItemsProvider method parse.
@Override
public DiscoverItems parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
DiscoverItems discoverItems = new DiscoverItems();
boolean done = false;
DiscoverItems.Item item;
Jid jid = null;
String name = "";
String action = "";
String node = "";
discoverItems.setNode(parser.getAttributeValue("", "node"));
while (!done) {
int eventType = parser.next();
if (eventType == XmlPullParser.START_TAG && "item".equals(parser.getName())) {
// Initialize the variables from the parsed XML
jid = ParserUtils.getJidAttribute(parser);
name = parser.getAttributeValue("", "name");
node = parser.getAttributeValue("", "node");
action = parser.getAttributeValue("", "action");
} else if (eventType == XmlPullParser.END_TAG && "item".equals(parser.getName())) {
// Create a new Item and add it to DiscoverItems.
item = new DiscoverItems.Item(jid);
item.setName(name);
item.setNode(node);
item.setAction(action);
discoverItems.addItem(item);
} else if (eventType == XmlPullParser.END_TAG && "query".equals(parser.getName())) {
done = true;
}
}
return discoverItems;
}
use of org.jivesoftware.smackx.disco.packet.DiscoverItems in project Smack by igniterealtime.
the class AdHocCommandManager method publishCommands.
/**
* Publish the commands to an specific JID.
*
* @param jid the full JID to publish the commands to.
* @throws XMPPException if the operation failed for some reason.
* @throws SmackException if there was no response from the server.
* @throws InterruptedException
*/
public void publishCommands(Jid jid) throws XMPPException, SmackException, InterruptedException {
// Collects the commands to publish as items
DiscoverItems discoverItems = new DiscoverItems();
Collection<AdHocCommandInfo> xCommandsList = getRegisteredCommands();
for (AdHocCommandInfo info : xCommandsList) {
DiscoverItems.Item item = new DiscoverItems.Item(info.getOwnerJID());
item.setName(info.getName());
item.setNode(info.getNode());
discoverItems.addItem(item);
}
serviceDiscoveryManager.publishItems(jid, NAMESPACE, discoverItems);
}
use of org.jivesoftware.smackx.disco.packet.DiscoverItems in project Smack by igniterealtime.
the class ServiceDiscoveryManager method findServicesDiscoverInfo.
/**
* Find all services under the users service that provide a given feature.
*
* @param feature the feature to search for
* @param stopOnFirst if true, stop searching after the first service was found
* @param useCache if true, query a cache first to avoid network I/O
* @return a possible empty list of services providing the given feature
* @throws NoResponseException
* @throws XMPPErrorException
* @throws NotConnectedException
* @throws InterruptedException
*/
public List<DiscoverInfo> findServicesDiscoverInfo(String feature, boolean stopOnFirst, boolean useCache) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
List<DiscoverInfo> serviceDiscoInfo = null;
DomainBareJid serviceName = connection().getXMPPServiceDomain();
if (useCache) {
serviceDiscoInfo = services.lookup(feature);
if (serviceDiscoInfo != null) {
return serviceDiscoInfo;
}
}
serviceDiscoInfo = new LinkedList<>();
// Send the disco packet to the server itself
DiscoverInfo info;
try {
info = discoverInfo(serviceName);
} catch (XMPPErrorException e) {
// Be extra robust here: Return the empty linked list and log this situation
LOGGER.log(Level.WARNING, "Could not discover information about service", e);
return serviceDiscoInfo;
}
// Check if the server supports the feature
if (info.containsFeature(feature)) {
serviceDiscoInfo.add(info);
if (stopOnFirst) {
if (useCache) {
// Cache the discovered information
services.put(feature, serviceDiscoInfo);
}
return serviceDiscoInfo;
}
}
DiscoverItems items;
try {
// Get the disco items and send the disco packet to each server item
items = discoverItems(serviceName);
} catch (XMPPErrorException e) {
LOGGER.log(Level.WARNING, "Could not discover items about service", e);
return serviceDiscoInfo;
}
for (DiscoverItems.Item item : items.getItems()) {
try {
// TODO is it OK here in all cases to query without the node attribute?
// MultipleRecipientManager queried initially also with the node attribute, but this
// could be simply a fault instead of intentional.
info = discoverInfo(item.getEntityID());
} catch (XMPPErrorException | NoResponseException e) {
// Don't throw this exceptions if one of the server's items fail
LOGGER.log(Level.WARNING, "Exception while discovering info for feature " + feature + " of " + item.getEntityID() + " node: " + item.getNode(), e);
continue;
}
if (info.containsFeature(feature)) {
serviceDiscoInfo.add(info);
//serviceAddresses.add(item.getEntityID().asDomainBareJid());
if (stopOnFirst) {
break;
}
}
}
if (useCache) {
// Cache the discovered information
services.put(feature, serviceDiscoInfo);
}
return serviceDiscoInfo;
}
use of org.jivesoftware.smackx.disco.packet.DiscoverItems in project Smack by igniterealtime.
the class STUN method serviceAvailable.
/**
* Check if the server support STUN Service.
*
* @param connection the connection
* @return true if the server support STUN
* @throws SmackException
* @throws XMPPException
* @throws InterruptedException
*/
public static boolean serviceAvailable(XMPPConnection connection) throws XMPPException, SmackException, InterruptedException {
if (!connection.isConnected()) {
return false;
}
LOGGER.fine("Service listing");
ServiceDiscoveryManager disco = ServiceDiscoveryManager.getInstanceFor(connection);
DiscoverItems items = disco.discoverItems(connection.getXMPPServiceDomain());
for (DiscoverItems.Item item : items.getItems()) {
DiscoverInfo info = disco.discoverInfo(item.getEntityID());
for (DiscoverInfo.Identity identity : info.getIdentities()) {
if (identity.getCategory().equals("proxy") && identity.getType().equals("stun"))
if (info.containsFeature(NAMESPACE))
return true;
}
LOGGER.fine(item.getName() + "-" + info.getType());
}
return false;
}
use of org.jivesoftware.smackx.disco.packet.DiscoverItems in project Smack by igniterealtime.
the class OfflineMessageManager method getHeaders.
/**
* Returns a List of <tt>OfflineMessageHeader</tt> that keep information about the
* offline message. The OfflineMessageHeader includes a stamp that could be used to retrieve
* the complete message or delete the specific message.
*
* @return a List of <tt>OfflineMessageHeader</tt> that keep information about the offline
* message.
* @throws XMPPErrorException If the user is not allowed to make this request or the server does
* not support offline message retrieval.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
* @throws InterruptedException
*/
public List<OfflineMessageHeader> getHeaders() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
List<OfflineMessageHeader> answer = new ArrayList<OfflineMessageHeader>();
DiscoverItems items = ServiceDiscoveryManager.getInstanceFor(connection).discoverItems(null, namespace);
for (DiscoverItems.Item item : items.getItems()) {
answer.add(new OfflineMessageHeader(item));
}
return answer;
}
Aggregations