From 2ac0fc72baafe590afeb95f1116863aa5db080ca Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 24 Jan 2017 20:14:01 +0100 Subject: [PATCH] Handle NameNotFoundException in smack-resolver-javax --- .../smack/util/dns/DNSResolver.java | 3 +++ .../smack/util/dns/javax/JavaxResolver.java | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/dns/DNSResolver.java b/smack-core/src/main/java/org/jivesoftware/smack/util/dns/DNSResolver.java index 79de3e08a..f5e4518f9 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/dns/DNSResolver.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/dns/DNSResolver.java @@ -20,6 +20,7 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; import java.util.List; +import java.util.logging.Logger; import org.jivesoftware.smack.ConnectionConfiguration.DnssecMode; @@ -29,6 +30,8 @@ import org.jivesoftware.smack.ConnectionConfiguration.DnssecMode; */ public abstract class DNSResolver { + protected static final Logger LOGGER = Logger.getLogger(DNSResolver.class.getName()); + private final boolean supportsDnssec; protected DNSResolver(boolean supportsDnssec) { diff --git a/smack-resolver-javax/src/main/java/org/jivesoftware/smack/util/dns/javax/JavaxResolver.java b/smack-resolver-javax/src/main/java/org/jivesoftware/smack/util/dns/javax/JavaxResolver.java index 7de7b911e..9ebec30a2 100644 --- a/smack-resolver-javax/src/main/java/org/jivesoftware/smack/util/dns/javax/JavaxResolver.java +++ b/smack-resolver-javax/src/main/java/org/jivesoftware/smack/util/dns/javax/JavaxResolver.java @@ -1,6 +1,6 @@ /** * - * Copyright 2013-2016 Florian Schmaus + * Copyright 2013-2017 Florian Schmaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,9 @@ import java.net.InetAddress; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; +import java.util.logging.Level; +import javax.naming.NameNotFoundException; import javax.naming.NamingEnumeration; import javax.naming.NamingException; import javax.naming.directory.Attribute; @@ -80,15 +82,26 @@ public class JavaxResolver extends DNSResolver implements SmackInitializer { @Override protected List lookupSRVRecords0(String name, List failedAddresses, DnssecMode dnssecMode) { - List res = new ArrayList(); + List res = null; + Attribute srvAttribute; try { Attributes dnsLookup = dirContext.getAttributes(name, new String[] { "SRV" }); - Attribute srvAttribute = dnsLookup.get("SRV"); + srvAttribute = dnsLookup.get("SRV"); if (srvAttribute == null) - return res; + return null; + } catch (NameNotFoundException e) { + LOGGER.log(Level.FINEST, "No DNS SRV RR found for " + name, e); + return null; + } catch (NamingException e) { + LOGGER.log(Level.WARNING, "Exception while resolving DNS SRV RR for " + name, e); + return null; + } + + try { @SuppressWarnings("unchecked") NamingEnumeration srvRecords = (NamingEnumeration) srvAttribute.getAll(); + res = new ArrayList<>(); while (srvRecords.hasMore()) { String srvRecordString = srvRecords.next(); String[] srvRecordEntries = srvRecordString.split(" "); @@ -107,7 +120,7 @@ public class JavaxResolver extends DNSResolver implements SmackInitializer { } } catch (NamingException e) { - throw new IllegalStateException(e); + LOGGER.log(Level.SEVERE, "Exception while resolving DNS SRV RR for" + name, e); } return res;