public static String convertHost(String host) { try { /* * Validate the host name as an IPV4/IPV6 address. * If this is an AWS ENDPOINT it will not parse. * In that case accept host as is. * * Costs: If this is an IPV4/6 encoding, e.g. 127.0.0.1 then no DNS lookup * is done. If it is a name then a DNS lookup is done but it is normally cached. * Secondarily, this class is typically used to create a connection once * at the beginning of processing and then not used again. So even if the DNS * lookup needs to be done then the cost is miniscule. */ InetAddress inetAddress = InetAddress.getByName(host); // isLoopbackAddress() handles both IPV4 and IPV6 if (inetAddress.isLoopbackAddress() || host.equals("0.0.0.0") || host.startsWith("169.254")) return getLocalhost(); else return host; } catch (Exception e) { // Not a valid IP address log.warn("{}.convertHost '" + host + "' is not a valid IP address. ", HostAndPort.class.getName(), e); return host; } }