Path: blob/master/src/java.naming/share/classes/com/sun/jndi/ldap/LdapURL.java
41161 views
/*1* Copyright (c) 1999, 2002, 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 javax.naming.*;28import java.net.MalformedURLException;29import java.io.UnsupportedEncodingException;30import java.util.StringTokenizer;31import com.sun.jndi.toolkit.url.Uri;32import com.sun.jndi.toolkit.url.UrlUtil;3334/*35* Extract components of an LDAP URL.36*37* The format of an LDAP URL is defined in RFC 2255 as follows:38*39* ldapurl = scheme "://" [hostport] ["/"40* [dn ["?" [attributes] ["?" [scope]41* ["?" [filter] ["?" extensions]]]]]]42* scheme = "ldap"43* attributes = attrdesc *("," attrdesc)44* scope = "base" / "one" / "sub"45* dn = distinguishedName from Section 3 of [1]46* hostport = hostport from Section 5 of RFC 1738 [5]47* attrdesc = AttributeDescription from Section 4.1.5 of [2]48* filter = filter from Section 4 of [4]49* extensions = extension *("," extension)50* extension = ["!"] extype ["=" exvalue]51* extype = token / xtoken52* exvalue = LDAPString from section 4.1.2 of [2]53* token = oid from section 4.1 of [3]54* xtoken = ("X-" / "x-") token55*56* For example,57*58* ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US59* ldap://host.com:6666/o=IMC,c=US??sub?(cn=Babs%20Jensen)60*61* This class also supports ldaps URLs.62*/6364public final class LdapURL extends Uri {6566private boolean useSsl = false;67private String DN = null;68private String attributes = null;69private String scope = null;70private String filter = null;71private String extensions = null;7273/**74* Creates an LdapURL object from an LDAP URL string.75*/76public LdapURL(String url) throws NamingException {7778super();7980try {81init(url); // scheme, host, port, path, query82useSsl = scheme.equalsIgnoreCase("ldaps");8384if (! (scheme.equalsIgnoreCase("ldap") || useSsl)) {85throw new MalformedURLException("Not an LDAP URL: " + url);86}8788parsePathAndQuery(); // DN, attributes, scope, filter, extensions8990} catch (MalformedURLException e) {91NamingException ne = new NamingException("Cannot parse url: " + url);92ne.setRootCause(e);93throw ne;94} catch (UnsupportedEncodingException e) {95NamingException ne = new NamingException("Cannot parse url: " + url);96ne.setRootCause(e);97throw ne;98}99}100101/**102* Returns true if the URL is an LDAPS URL.103*/104public boolean useSsl() {105return useSsl;106}107108/**109* Returns the LDAP URL's distinguished name.110*/111public String getDN() {112return DN;113}114115/**116* Returns the LDAP URL's attributes.117*/118public String getAttributes() {119return attributes;120}121122/**123* Returns the LDAP URL's scope.124*/125public String getScope() {126return scope;127}128129/**130* Returns the LDAP URL's filter.131*/132public String getFilter() {133return filter;134}135136/**137* Returns the LDAP URL's extensions.138*/139public String getExtensions() {140return extensions;141}142143/**144* Given a space-separated list of LDAP URLs, returns an array of strings.145*/146public static String[] fromList(String urlList) throws NamingException {147148String[] urls = new String[(urlList.length() + 1) / 2];149int i = 0; // next available index in urls150StringTokenizer st = new StringTokenizer(urlList, " ");151152while (st.hasMoreTokens()) {153urls[i++] = st.nextToken();154}155String[] trimmed = new String[i];156System.arraycopy(urls, 0, trimmed, 0, i);157return trimmed;158}159160/**161* Determines whether an LDAP URL has query components.162*/163public static boolean hasQueryComponents(String url) {164return (url.lastIndexOf('?') != -1);165}166167/*168* Assembles an LDAP or LDAPS URL string from its components.169* If "host" is an IPv6 literal, it may optionally include delimiting170* brackets.171*/172static String toUrlString(String host, int port, String dn, boolean useSsl)173{174175try {176String h = (host != null) ? host : "";177if ((h.indexOf(':') != -1) && (h.charAt(0) != '[')) {178h = "[" + h + "]"; // IPv6 literal179}180String p = (port != -1) ? (":" + port) : "";181String d = (dn != null) ? ("/" + UrlUtil.encode(dn, "UTF8")) : "";182183return useSsl ? "ldaps://" + h + p + d : "ldap://" + h + p + d;184} catch (UnsupportedEncodingException e) {185// UTF8 should always be supported186throw new IllegalStateException("UTF-8 encoding unavailable");187}188}189190/*191* Parses the path and query components of an URL and sets this192* object's fields accordingly.193*/194private void parsePathAndQuery() throws MalformedURLException,195UnsupportedEncodingException {196197// path begins with a '/' or is empty198199if (path.isEmpty()) {200return;201}202203DN = path.startsWith("/") ? path.substring(1) : path;204if (DN.length() > 0) {205DN = UrlUtil.decode(DN, "UTF8");206}207208// query begins with a '?' or is null209210if (query == null || query.length() < 2) {211return;212}213214int currentIndex = 1;215int nextQmark;216int endIndex;217218// attributes:219nextQmark = query.indexOf('?', currentIndex);220endIndex = nextQmark == -1 ? query.length() : nextQmark;221if (endIndex - currentIndex > 0) {222attributes = query.substring(currentIndex, endIndex);223}224currentIndex = endIndex + 1;225if (currentIndex >= query.length()) {226return;227}228229// scope:230nextQmark = query.indexOf('?', currentIndex);231endIndex = nextQmark == -1 ? query.length() : nextQmark;232if (endIndex - currentIndex > 0) {233scope = query.substring(currentIndex, endIndex);234}235currentIndex = endIndex + 1;236if (currentIndex >= query.length()) {237return;238}239240// filter:241nextQmark = query.indexOf('?', currentIndex);242endIndex = nextQmark == -1 ? query.length() : nextQmark;243if (endIndex - currentIndex > 0) {244filter = query.substring(currentIndex, endIndex);245filter = UrlUtil.decode(filter, "UTF8");246}247currentIndex = endIndex + 1;248if (currentIndex >= query.length()) {249return;250}251252// extensions:253if (query.length() - currentIndex > 0) {254extensions = query.substring(currentIndex);255extensions = UrlUtil.decode(extensions, "UTF8");256}257}258259/*260public static void main(String[] args) throws Exception {261262LdapURL url = new LdapURL(args[0]);263264System.out.println("Example LDAP URL: " + url.toString());265System.out.println(" scheme: " + url.getScheme());266System.out.println(" host: " + url.getHost());267System.out.println(" port: " + url.getPort());268System.out.println(" DN: " + url.getDN());269System.out.println(" attrs: " + url.getAttributes());270System.out.println(" scope: " + url.getScope());271System.out.println(" filter: " + url.getFilter());272System.out.println(" extens: " + url.getExtensions());273System.out.println("");274}275*/276}277278279