My application searches an LDAP server for people.
return ldapTemplate.search("", "(objectclass=person)", new AttributesMapper() {
public Object mapFromAttributes(Attributes attrs)
throws NamingException {
return attrs.get("cn").getAll();
}
});
It returns list of NamingEnumeration
object, which contains vectors in it. Each vector may contain one or more values.
I can print person names by this code
for(NamingEnumeration ne : list){
while (ne.hasMore()) {
System.out.println("name is : " + ne.next().toString());
}
}
As my ldap search can contain mutiple values so that comes in vector inside NamingEnumeration
object. How can I get multiple values out of it.
While iterating a list using the
for
syntax introduced with Java5You shouldn't call
hasMore()
In case your list does not support the Iterator interface you need to use the old form:
As you are using a
java.util.List
ofjavax.naming.NamingEnumeration<java.util.Vector>
such as this,You should be able to iterate over the
Vector
in eachNamingEnumeration
:Note that
Vector
is considered by many to be obsolescent, although not deprecated. Also, the enclosed collection could use a type parameter. If you have a choice, consider one of these alternatives:This code snippet will do the work for unknown attribute names and single or multiple values (such as multiple object classes):
Using spring-ldap 2.3.1 and AttributesMapper implementation:
In this sample code the searchResultList contains all entries, each one is represented as a map of attributes (with one or more values):
Now, working with the searchResultList looks like this: