Add findServicesDiscoveryInfo() variant

which does not log some exceptions, but instead comes with an optional
output paramater which returns the encountered exceptions.
This commit is contained in:
Florian Schmaus 2017-11-08 16:57:25 +01:00
parent 5ef6853db6
commit a66c42834f
1 changed files with 30 additions and 8 deletions

View File

@ -27,7 +27,6 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.jivesoftware.smack.ConnectionCreationListener; import org.jivesoftware.smack.ConnectionCreationListener;
@ -766,6 +765,25 @@ public final class ServiceDiscoveryManager extends Manager {
*/ */
public List<DiscoverInfo> findServicesDiscoverInfo(String feature, boolean stopOnFirst, boolean useCache) public List<DiscoverInfo> findServicesDiscoverInfo(String feature, boolean stopOnFirst, boolean useCache)
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException { throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
return findServicesDiscoverInfo(feature, stopOnFirst, useCache, null);
}
/**
* 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
* @param encounteredExceptions an optional map which will be filled with the exceptions encountered
* @return a possible empty list of services providing the given feature
* @throws NoResponseException
* @throws XMPPErrorException
* @throws NotConnectedException
* @throws InterruptedException
* @since 4.2.2
*/
public List<DiscoverInfo> findServicesDiscoverInfo(String feature, boolean stopOnFirst, boolean useCache, Map<? super Jid, Exception> encounteredExceptions)
throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
List<DiscoverInfo> serviceDiscoInfo = null; List<DiscoverInfo> serviceDiscoInfo = null;
DomainBareJid serviceName = connection().getXMPPServiceDomain(); DomainBareJid serviceName = connection().getXMPPServiceDomain();
if (useCache) { if (useCache) {
@ -780,8 +798,9 @@ public final class ServiceDiscoveryManager extends Manager {
try { try {
info = discoverInfo(serviceName); info = discoverInfo(serviceName);
} catch (XMPPErrorException e) { } catch (XMPPErrorException e) {
// Be extra robust here: Return the empty linked list and log this situation if (encounteredExceptions != null) {
LOGGER.log(Level.WARNING, "Could not discover information about service", e); encounteredExceptions.put(serviceName, e);
}
return serviceDiscoInfo; return serviceDiscoInfo;
} }
// Check if the server supports the feature // Check if the server supports the feature
@ -800,20 +819,23 @@ public final class ServiceDiscoveryManager extends Manager {
// Get the disco items and send the disco packet to each server item // Get the disco items and send the disco packet to each server item
items = discoverItems(serviceName); items = discoverItems(serviceName);
} catch (XMPPErrorException e) { } catch (XMPPErrorException e) {
LOGGER.log(Level.WARNING, "Could not discover items about service", e); if (encounteredExceptions != null) {
encounteredExceptions.put(serviceName, e);
}
return serviceDiscoInfo; return serviceDiscoInfo;
} }
for (DiscoverItems.Item item : items.getItems()) { for (DiscoverItems.Item item : items.getItems()) {
Jid address = item.getEntityID();
try { try {
// TODO is it OK here in all cases to query without the node attribute? // TODO is it OK here in all cases to query without the node attribute?
// MultipleRecipientManager queried initially also with the node attribute, but this // MultipleRecipientManager queried initially also with the node attribute, but this
// could be simply a fault instead of intentional. // could be simply a fault instead of intentional.
info = discoverInfo(item.getEntityID()); info = discoverInfo(address);
} }
catch (XMPPErrorException | NoResponseException e) { catch (XMPPErrorException | NoResponseException e) {
// Don't throw this exceptions if one of the server's items fail if (encounteredExceptions != null) {
LOGGER.log(Level.WARNING, "Exception while discovering info for feature " + feature encounteredExceptions.put(address, e);
+ " of " + item.getEntityID() + " node: " + item.getNode(), e); }
continue; continue;
} }
if (info.containsFeature(feature)) { if (info.containsFeature(feature)) {