From 4e3b2b1f110843f639b6060b9bd7cdee35868dc7 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 23 Jan 2017 22:15:51 +0300 Subject: juick-xmpp: fallback to A record when SRV record is not found --- .../java/com/juick/components/s2s/DNSQueries.java | 28 ++++++++++++++-------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'juick-xmpp/src/main/java/com/juick/components/s2s') 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 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); } } -- cgit v1.2.3