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