package com.sun.jndi.ldap; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; import javax.naming.NamingException; import javax.naming.spi.ldap.LdapDnsProvider; import javax.naming.spi.ldap.LdapDnsProviderResult; public class DefaultLdapDnsProvider extends LdapDnsProvider { @Override public LdapDnsProviderResult lookupEndpoints(String url, Hashtable env) { String domainName = ""; List urls = new ArrayList<>(); try { LdapURL ldapUrl = new LdapURL(url); String dn = ldapUrl.getDN(); String host = ldapUrl.getHost(); int port = ldapUrl.getPort(); String[] hostports; // handle a URL with no hostport (ldap:/// or ldaps:///) // locate the LDAP service using the URL's distinguished name if (host == null && port == -1 && dn != null && (domainName = ServiceLocator.mapDnToDomainName(dn)) != null && (hostports = ServiceLocator.getLdapService(domainName, env)) != null) { // Generate new URLs that include the discovered hostports. // Reuse the original URL scheme. String scheme = ldapUrl.getScheme() + "://"; String query = ldapUrl.getQuery(); String urlSuffix = ldapUrl.getPath() + (query != null ? query : ""); for (String hostPort : hostports) { // the hostports come from the DNS SRV records // we assume the SRV record is scheme aware urls.add(scheme + hostPort + urlSuffix); } } else { // we don't have enough information to set the domain name // correctly domainName = ""; urls.add(url); } } catch (NamingException e) { // leave list of resolved urls empty } return new LdapDnsProviderResult(domainName, urls); } }