|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package com.sun.jndi.ldap; |
|
|
|
import javax.naming.*; |
|
import java.net.MalformedURLException; |
|
import java.io.UnsupportedEncodingException; |
|
import java.util.StringTokenizer; |
|
import com.sun.jndi.toolkit.url.Uri; |
|
import com.sun.jndi.toolkit.url.UrlUtil; |
|
|
|
/* |
|
* Extract components of an LDAP URL. |
|
* |
|
* The format of an LDAP URL is defined in RFC 2255 as follows: |
|
* |
|
* ldapurl = scheme "://" [hostport] ["/" |
|
* [dn ["?" [attributes] ["?" [scope] |
|
* ["?" [filter] ["?" extensions]]]]]] |
|
* scheme = "ldap" |
|
* attributes = attrdesc *("," attrdesc) |
|
* scope = "base" / "one" / "sub" |
|
* dn = distinguishedName from Section 3 of [1] |
|
* hostport = hostport from Section 5 of RFC 1738 [5] |
|
* attrdesc = AttributeDescription from Section 4.1.5 of [2] |
|
* filter = filter from Section 4 of [4] |
|
* extensions = extension *("," extension) |
|
* extension = ["!"] extype ["=" exvalue] |
|
* extype = token / xtoken |
|
* exvalue = LDAPString from section 4.1.2 of [2] |
|
* token = oid from section 4.1 of [3] |
|
* xtoken = ("X-" / "x-") token |
|
* |
|
* For example, |
|
* |
|
* ldap://ldap.itd.umich.edu/o=University%20of%20Michigan,c=US |
|
* ldap://host.com:6666/o=IMC,c=US??sub?(cn=Babs%20Jensen) |
|
* |
|
* This class also supports ldaps URLs. |
|
*/ |
|
|
|
final public class LdapURL extends Uri { |
|
|
|
private boolean useSsl = false; |
|
private String DN = null; |
|
private String attributes = null; |
|
private String scope = null; |
|
private String filter = null; |
|
private String extensions = null; |
|
|
|
|
|
|
|
*/ |
|
public LdapURL(String url) throws NamingException { |
|
|
|
super(); |
|
|
|
try { |
|
init(url); |
|
useSsl = scheme.equalsIgnoreCase("ldaps"); |
|
|
|
if (! (scheme.equalsIgnoreCase("ldap") || useSsl)) { |
|
throw new MalformedURLException("Not an LDAP URL: " + url); |
|
} |
|
|
|
parsePathAndQuery(); |
|
|
|
} catch (MalformedURLException e) { |
|
NamingException ne = new NamingException("Cannot parse url: " + url); |
|
ne.setRootCause(e); |
|
throw ne; |
|
} catch (UnsupportedEncodingException e) { |
|
NamingException ne = new NamingException("Cannot parse url: " + url); |
|
ne.setRootCause(e); |
|
throw ne; |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
public boolean useSsl() { |
|
return useSsl; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public String getDN() { |
|
return DN; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public String getAttributes() { |
|
return attributes; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public String getScope() { |
|
return scope; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public String getFilter() { |
|
return filter; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public String getExtensions() { |
|
return extensions; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public static String[] fromList(String urlList) throws NamingException { |
|
|
|
String[] urls = new String[(urlList.length() + 1) / 2]; |
|
int i = 0; |
|
StringTokenizer st = new StringTokenizer(urlList, " "); |
|
|
|
while (st.hasMoreTokens()) { |
|
urls[i++] = st.nextToken(); |
|
} |
|
String[] trimmed = new String[i]; |
|
System.arraycopy(urls, 0, trimmed, 0, i); |
|
return trimmed; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public static boolean hasQueryComponents(String url) { |
|
return (url.lastIndexOf('?') != -1); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static String toUrlString(String host, int port, String dn, boolean useSsl) |
|
{ |
|
|
|
try { |
|
String h = (host != null) ? host : ""; |
|
if ((h.indexOf(':') != -1) && (h.charAt(0) != '[')) { |
|
h = "[" + h + "]"; |
|
} |
|
String p = (port != -1) ? (":" + port) : ""; |
|
String d = (dn != null) ? ("/" + UrlUtil.encode(dn, "UTF8")) : ""; |
|
|
|
return useSsl ? "ldaps://" + h + p + d : "ldap://" + h + p + d; |
|
} catch (UnsupportedEncodingException e) { |
|
|
|
throw new IllegalStateException("UTF-8 encoding unavailable"); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
private void parsePathAndQuery() throws MalformedURLException, |
|
UnsupportedEncodingException { |
|
|
|
// path begins with a '/' or is empty |
|
|
|
if (path.equals("")) { |
|
return; |
|
} |
|
|
|
DN = path.startsWith("/") ? path.substring(1) : path; |
|
if (DN.length() > 0) { |
|
DN = UrlUtil.decode(DN, "UTF8"); |
|
} |
|
|
|
// query begins with a '?' or is null |
|
|
|
if (query == null || query.length() < 2) { |
|
return; |
|
} |
|
|
|
int currentIndex = 1; |
|
int nextQmark; |
|
int endIndex; |
|
|
|
|
|
nextQmark = query.indexOf('?', currentIndex); |
|
endIndex = nextQmark == -1 ? query.length() : nextQmark; |
|
if (endIndex - currentIndex > 0) { |
|
attributes = query.substring(currentIndex, endIndex); |
|
} |
|
currentIndex = endIndex + 1; |
|
if (currentIndex >= query.length()) { |
|
return; |
|
} |
|
|
|
|
|
nextQmark = query.indexOf('?', currentIndex); |
|
endIndex = nextQmark == -1 ? query.length() : nextQmark; |
|
if (endIndex - currentIndex > 0) { |
|
scope = query.substring(currentIndex, endIndex); |
|
} |
|
currentIndex = endIndex + 1; |
|
if (currentIndex >= query.length()) { |
|
return; |
|
} |
|
|
|
|
|
nextQmark = query.indexOf('?', currentIndex); |
|
endIndex = nextQmark == -1 ? query.length() : nextQmark; |
|
if (endIndex - currentIndex > 0) { |
|
filter = query.substring(currentIndex, endIndex); |
|
filter = UrlUtil.decode(filter, "UTF8"); |
|
} |
|
currentIndex = endIndex + 1; |
|
if (currentIndex >= query.length()) { |
|
return; |
|
} |
|
|
|
|
|
if (query.length() - currentIndex > 0) { |
|
extensions = query.substring(currentIndex); |
|
extensions = UrlUtil.decode(extensions, "UTF8"); |
|
} |
|
} |
|
|
|
/* |
|
public static void main(String[] args) throws Exception { |
|
|
|
LdapURL url = new LdapURL(args[0]); |
|
|
|
System.out.println("Example LDAP URL: " + url.toString()); |
|
System.out.println(" scheme: " + url.getScheme()); |
|
System.out.println(" host: " + url.getHost()); |
|
System.out.println(" port: " + url.getPort()); |
|
System.out.println(" DN: " + url.getDN()); |
|
System.out.println(" attrs: " + url.getAttributes()); |
|
System.out.println(" scope: " + url.getScope()); |
|
System.out.println(" filter: " + url.getFilter()); |
|
System.out.println(" extens: " + url.getExtensions()); |
|
System.out.println(""); |
|
} |
|
*/ |
|
} |