diff options
author | Vitaly Takmazov | 2017-01-23 22:15:51 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-01-23 22:46:01 +0300 |
commit | 4e3b2b1f110843f639b6060b9bd7cdee35868dc7 (patch) | |
tree | d6eadcc58091f407b5e3eb0b0163cf4c8cfee2e1 /juick-xmpp/src/main | |
parent | f2b6ec29e8e933c93239a3d114ca6013b3c06c0e (diff) |
juick-xmpp: fallback to A record when SRV record is not found
Diffstat (limited to 'juick-xmpp/src/main')
-rw-r--r-- | juick-xmpp/src/main/java/com/juick/components/s2s/DNSQueries.java | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/juick-xmpp/src/main/java/com/juick/components/s2s/DNSQueries.java b/juick-xmpp/src/main/java/com/juick/components/s2s/DNSQueries.java index 887dfc5c..ea4585a1 100644 --- a/juick-xmpp/src/main/java/com/juick/components/s2s/DNSQueries.java +++ b/juick-xmpp/src/main/java/com/juick/components/s2s/DNSQueries.java @@ -1,6 +1,8 @@ package com.juick.components.s2s; import org.apache.commons.lang3.math.NumberUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.net.InetSocketAddress; import java.net.UnknownHostException; @@ -17,25 +19,31 @@ import javax.naming.directory.InitialDirContext; */ public class DNSQueries { + private static final Logger logger = LoggerFactory.getLogger(DNSQueries.class); + private static Random rand = new Random(); - public static InetSocketAddress getServerAddress(String hostname) throws UnknownHostException, NamingException { + public static InetSocketAddress getServerAddress(String hostname) { String host = hostname; int port = 5269; Hashtable<String, String> env = new Hashtable<>(5); env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); - DirContext ctx = new InitialDirContext(env); - Attribute att = ctx.getAttributes("_xmpp-server._tcp." + hostname, new String[]{"SRV"}).get("SRV"); - - if (att != null && att.size() > 0) { - int i = rand.nextInt(att.size()); - String srv[] = att.get(i).toString().split(" "); - port = NumberUtils.toInt(srv[2], 5269); - host = srv[3]; + try { + DirContext ctx = new InitialDirContext(env); + Attribute att = ctx.getAttributes("_xmpp-server._tcp." + hostname, new String[]{"SRV"}).get("SRV"); + + if (att != null && att.size() > 0) { + int i = rand.nextInt(att.size()); + String srv[] = att.get(i).toString().split(" "); + port = NumberUtils.toInt(srv[2], 5269); + host = srv[3]; + } + ctx.close(); + } catch (NamingException e) { + logger.info("SRV record for {} is not resolved, falling back to A record", hostname); } - ctx.close(); return new InetSocketAddress(host, port); } } |