Search code examples
javaldap

Parse LDAP-URL to the hostname and Base-DN


I try to parse an LDAP-URL in Java.

import java.net.URL;

String url = "ldaps://my.ldap.site/o=test,dc=testdc,dc=us";
URL url = new URL (this.url);

But that brings an MalformedURLException up. Seems that URL does not know LDAP.

What I need is to find the host (my.ldap.site) and the base-DN (o=test,dc=testdc,dc=us).

Is there any class that does that for me or do I need to start a RegExp?


Solution

  • Proper way:

    import com.unboundid.ldap.sdk.migrate.ldapjdk.LDAPUrl;
    
    public class App {
        public static void main(String[] args) throws Exception {
            LDAPUrl u = new LDAPUrl(args[0]);
            System.out.println(u.getHost());
            System.out.println(u.getDN());
        }
    }