package com.sun.jndi.ldap; import javax.naming.NamingException; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; import java.util.function.BiFunction; public class DefaultLdapDnsProvider implements BiFunction, List> { @Override public List apply(String url, Hashtable env) { List urls = new ArrayList<>(); try { LdapURL ldapUrl = new LdapURL(url); String dn = ldapUrl.getDN(); String host = ldapUrl.getHost(); int port = ldapUrl.getPort(); String[] hostports; String domainName = null; // 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 && 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 (int i = 0; i < hostports.length; i++) { urls.add(scheme + hostports[i] + urlSuffix); } } else { urls.add(url); } } catch (NamingException e) { // leave list of resolved urls empty } return urls; } }