开发者

SearchResult in Java

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开发者_Go百科.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


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?


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: ([^,]),

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜