Java Reference
In-Depth Information
11
N OTE
Many attributes in an LDAP server can hold multiple values. For example, a person
could have two telephone numbers, both of which are stored in the attribute
telephoneNumber . If this is the case, both values are returned as values for the single
Attribute object holding the telephone number.
All of the code written for this chapter allows for only one value for each attribute.
This is done for simplicity reasons; however, multiple values are possible.
Now let's take a look at the client code that uses this method to search the LDAP server:
LDAPManager lm = new LDAPManager();
lm.setSearchBase(“o=virtuas.com”);
String[] returnAtts = {“cn”, “sn”, “mail”, “telephoneNumber”};
String searchFilter = “uid=proy”;
Vector results = null;
try {
results = lm.search(returnAtts, searchFilter);
}
catch (NamingException e) {
e.printStackTrace();
}
Iterator iter = results.iterator();
while (iter.hasNext()) {
LDAPObject lobj = (LDAPObject)iter.next();
System.out.println(lobj.getDn());
System.out.println(lobj.getAttribute(“cn”));
System.out.println(lobj.getAttribute(“sn”));
System.out.println(lobj.getAttribute(“mail”));
System.out.println(lobj.getAttribute(“telephoneNumber”));
}
This client first creates a new LDAPManager and then sets the appropriate search base. It then
defines the required return attributes and the search filter. After all of this information is in
order, it uses the LDAPManager to perform the search. It can then easily iterate through the
results, using a simple container object, and print out the information.
 
Search WWH ::




Custom Search