tags:

views:

37

answers:

2

I list users from LDAP with the following code:

    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, CONTEXT);
    env.put(Context.PROVIDER_URL, HOST); 
    env.put(Context.SECURITY_PRINCIPAL,USER); 
    env.put(Context.SECURITY_CREDENTIALS,PASSWORD);


    DirContext ctx = new InitialDirContext(env);

    SearchControls sc = new SearchControls();
    sc.setSearchScope(SearchControls.SUBTREE_SCOPE);

    NamingEnumeration items = ctx.search(BASE, filter, sc);
    while (items != null && items.hasMore()) {
        SearchResult sr = (SearchResult)items.next();
        System.out.println("SR : " + sr) ;
    }

Now I get an output of;

SR : cn=smith: null:null:{objectclass=objectClass: person, sn=sn: smith, cn=cn: smith}
SR : cn=king: null:null:{objectclass=objectClass: person, sn=sn: king, cn=cn: king}

How can I get from SearchControls just the output like:

sn = smith |  cn = smith
sn = king  |  cn = king
+2  A: 

Wouldn't a simple change in your sysout as below suffice?

System.out.println("SR : " 
+ sr.getAttributes().get("sn")
+ " | " 
+ sr.getAttributes().get("cn")
) ;

Or, am I reading something incorrectly here?

VJ
That is exactly what I needed, thank you @VJ
Adnan
A: 

You could try matching the SR string to a pair of regular expressions to get the value of sn and cn:

sn=sn: ([^,]),

cn=cn: ([^,]),

VeeArr