From 9c8e52bcbb2a583f8e83c6390b960e9d9edefd53 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Mon, 4 Jul 2022 07:50:50 +0200 Subject: [PATCH] Remove naming exceptions from DNS browser --- .../argeo/util/directory/ldap/IpaUtils.java | 5 +- .../org/argeo/util/naming/dns/DnsBrowser.java | 96 +++++++++++-------- 2 files changed, 58 insertions(+), 43 deletions(-) diff --git a/org.argeo.util/src/org/argeo/util/directory/ldap/IpaUtils.java b/org.argeo.util/src/org/argeo/util/directory/ldap/IpaUtils.java index 53c30848b..68b40868a 100644 --- a/org.argeo.util/src/org/argeo/util/directory/ldap/IpaUtils.java +++ b/org.argeo.util/src/org/argeo/util/directory/ldap/IpaUtils.java @@ -10,7 +10,6 @@ import java.util.Hashtable; import java.util.List; import javax.naming.InvalidNameException; -import javax.naming.NamingException; import javax.naming.ldap.LdapName; import javax.naming.ldap.Rdn; @@ -95,7 +94,7 @@ public class IpaUtils { String dnsZone = hostname.substring(hostname.indexOf('.') + 1); kerberosDomain = dnsBrowser.getRecord("_kerberos." + dnsZone, "TXT"); return kerberosDomain; - } catch (NamingException | IOException e) { + } catch (IOException e) { throw new IllegalStateException("Cannot determine Kerberos domain from DNS", e); } @@ -126,7 +125,7 @@ public class IpaUtils { } else { ldapHostsStr = ldapHosts.get(0); } - } catch (NamingException | IOException e) { + } catch (IOException e) { throw new IllegalStateException("Cannot convert IPA uri " + uri, e); } } else { diff --git a/org.argeo.util/src/org/argeo/util/naming/dns/DnsBrowser.java b/org.argeo.util/src/org/argeo/util/naming/dns/DnsBrowser.java index 376c51edc..4fba43405 100644 --- a/org.argeo.util/src/org/argeo/util/naming/dns/DnsBrowser.java +++ b/org.argeo.util/src/org/argeo/util/naming/dns/DnsBrowser.java @@ -28,41 +28,49 @@ import javax.naming.directory.InitialDirContext; public class DnsBrowser implements Closeable { private final DirContext initialCtx; - public DnsBrowser() throws NamingException { + public DnsBrowser() { this(new ArrayList<>()); } - public DnsBrowser(List dnsServerUrls) throws NamingException { - Objects.requireNonNull(dnsServerUrls); - Hashtable env = new Hashtable<>(); - env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); - if (!dnsServerUrls.isEmpty()) { - boolean specified = false; - StringJoiner providerUrl = new StringJoiner(" "); - for (String dnsUrl : dnsServerUrls) { - if (dnsUrl != null) { - providerUrl.add(dnsUrl); - specified = true; + public DnsBrowser(List dnsServerUrls) { + try { + Objects.requireNonNull(dnsServerUrls); + Hashtable env = new Hashtable<>(); + env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); + if (!dnsServerUrls.isEmpty()) { + boolean specified = false; + StringJoiner providerUrl = new StringJoiner(" "); + for (String dnsUrl : dnsServerUrls) { + if (dnsUrl != null) { + providerUrl.add(dnsUrl); + specified = true; + } } + if (specified) + env.put(Context.PROVIDER_URL, providerUrl.toString()); } - if (specified) - env.put(Context.PROVIDER_URL, providerUrl.toString()); + initialCtx = new InitialDirContext(env); + } catch (NamingException e) { + throw new IllegalStateException("Cannot initialise DNS borowser.", e); } - initialCtx = new InitialDirContext(env); } - public Map> getAllRecords(String name) throws NamingException { - Map> res = new TreeMap<>(); - Attributes attrs = initialCtx.getAttributes(name); - NamingEnumeration ids = attrs.getIDs(); - while (ids.hasMore()) { - String recordType = ids.next(); - List lst = new ArrayList(); - res.put(recordType, lst); - Attribute attr = attrs.get(recordType); - addValues(attr, lst); + public Map> getAllRecords(String name) { + try { + Map> res = new TreeMap<>(); + Attributes attrs = initialCtx.getAttributes(name); + NamingEnumeration ids = attrs.getIDs(); + while (ids.hasMore()) { + String recordType = ids.next(); + List lst = new ArrayList(); + res.put(recordType, lst); + Attribute attr = attrs.get(recordType); + addValues(attr, lst); + } + return Collections.unmodifiableMap(res); + } catch (NamingException e) { + throw new IllegalStateException("Cannot get allrecords of " + name, e); } - return Collections.unmodifiableMap(res); } /** @@ -91,16 +99,20 @@ public class DnsBrowser implements Closeable { /** * Return records of a given type. */ - public List getRecords(String name, String recordType) throws NamingException { - List res = new ArrayList(); - Attributes attrs = initialCtx.getAttributes(name, new String[] { recordType }); - Attribute attr = attrs.get(recordType); - addValues(attr, res); - return res; + public List getRecords(String name, String recordType) { + try { + List res = new ArrayList(); + Attributes attrs = initialCtx.getAttributes(name, new String[] { recordType }); + Attribute attr = attrs.get(recordType); + addValues(attr, res); + return res; + } catch (NamingException e) { + throw new IllegalStateException("Cannot get records " + recordType + " of " + name, e); + } } /** Ordered, with preferred first. */ - public List getSrvRecordsAsHosts(String name, boolean withPort) throws NamingException { + public List getSrvRecordsAsHosts(String name, boolean withPort) { List raw = getRecords(name, "SRV"); if (raw.size() == 0) return null; @@ -137,14 +149,18 @@ public class DnsBrowser implements Closeable { } - public List listEntries(String name) throws NamingException { - List res = new ArrayList(); - NamingEnumeration ne = initialCtx.listBindings(name); - while (ne.hasMore()) { - Binding b = ne.next(); - res.add(b.getName()); + public List listEntries(String name) { + try { + List res = new ArrayList(); + NamingEnumeration ne = initialCtx.listBindings(name); + while (ne.hasMore()) { + Binding b = ne.next(); + res.add(b.getName()); + } + return Collections.unmodifiableList(res); + } catch (NamingException e) { + throw new IllegalStateException("Cannot list entries of " + name, e); } - return Collections.unmodifiableList(res); } @Override -- 2.30.2