Path: blob/master/src/java.naming/share/classes/com/sun/jndi/ldap/DefaultLdapDnsProvider.java
41161 views
/*1* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jndi.ldap;2627import java.util.ArrayList;28import java.util.List;29import java.util.Map;30import java.util.Optional;3132import javax.naming.NamingException;33import javax.naming.ldap.spi.LdapDnsProviderResult;3435public class DefaultLdapDnsProvider {3637public Optional<LdapDnsProviderResult> lookupEndpoints(String url,38Map<?,?> env)39throws NamingException40{41if (url == null || env == null) {42throw new NullPointerException();43}4445String domainName;46List<String> endpoints = new ArrayList<>();47LdapURL ldapUrl = new LdapURL(url);48String dn = ldapUrl.getDN();49String host = ldapUrl.getHost();50int port = ldapUrl.getPort();51String[] hostports;5253// handle a URL with no hostport (ldap:/// or ldaps:///)54// locate the LDAP service using the URL's distinguished name55if (host == null56&& port == -157&& dn != null58&& (domainName = ServiceLocator.mapDnToDomainName(dn)) != null59&& (hostports = ServiceLocator.getLdapService(domainName, env)) != null) {60// Generate new URLs that include the discovered hostports.61// Reuse the original URL scheme.62String scheme = ldapUrl.getScheme() + "://";63String query = ldapUrl.getQuery();64String urlSuffix = ldapUrl.getPath() + (query != null ? query : "");65for (String hostPort : hostports) {66// the hostports come from the DNS SRV records67// we assume the SRV record is scheme aware68endpoints.add(scheme + hostPort + urlSuffix);69}70} else {71// we don't have enough information to set the domain name72// correctly73domainName = "";74endpoints.add(url);75}7677LdapDnsProviderResult res = new LdapDnsProviderResult(domainName, endpoints);78if (res.getEndpoints().isEmpty() && res.getDomainName().isEmpty()) {79return Optional.empty();80} else {81return Optional.of(res);82}83}84}858687