1 package com.sun.jndi.ldap; 2 3 import java.util.ArrayList; 4 import java.util.Hashtable; 5 import java.util.List; 6 import javax.naming.NamingException; 7 import javax.naming.spi.ldap.LdapDnsProvider; 8 import javax.naming.spi.ldap.LdapDnsProviderResult; 9 10 public class DefaultLdapDnsProvider extends LdapDnsProvider { 11 @Override 12 public LdapDnsProviderResult lookupEndpoints(String url, Hashtable<?,?> env) { 13 String domainName = ""; 14 List<String> urls = new ArrayList<>(); 15 try { 16 LdapURL ldapUrl = new LdapURL(url); 17 String dn = ldapUrl.getDN(); 18 String host = ldapUrl.getHost(); 19 int port = ldapUrl.getPort(); 20 String[] hostports; 21 22 // handle a URL with no hostport (ldap:/// or ldaps:///) 23 // locate the LDAP service using the URL's distinguished name 24 if (host == null 25 && port == -1 26 && dn != null 27 && (domainName = ServiceLocator.mapDnToDomainName(dn)) != null 28 && (hostports = ServiceLocator.getLdapService(domainName, env)) != null) 29 { 30 // Generate new URLs that include the discovered hostports. 31 // Reuse the original URL scheme. 32 String scheme = ldapUrl.getScheme() + "://"; 33 String query = ldapUrl.getQuery(); 34 String urlSuffix = ldapUrl.getPath() + (query != null ? query : ""); 35 for (String hostPort : hostports) { 36 // the hostports come from the DNS SRV records 37 // we assume the SRV record is scheme aware 38 urls.add(scheme + hostPort + urlSuffix); 39 } 40 } else { 41 // we don't have enough information to set the domain name 42 // correctly 43 domainName = ""; 44 urls.add(url); 45 } 46 } catch (NamingException e) { 47 // leave list of resolved urls empty 48 } 49 return new LdapDnsProviderResult(domainName, urls); 50 } 51 52 }