简体   繁体   中英

Extract data from NamingEnumeration

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.

As you are using a java.util.List of javax.naming.NamingEnumeration<java.util.Vector> such as this,

List<NamingEnumeration<Vector>> list

You should be able to iterate over the Vector in each NamingEnumeration :

for (NamingEnumeration<Vector> ne : list) {
    while (ne.hasMore()) {
        Vector vector = ne.next();
        for (Object object : vector) {
            System.out.println(object);
        }
    }
}

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:

List<NamingEnumeration<Vector<T>>>
List<NamingEnumeration<List<T>>>

While iterating a list using the for syntax introduced with Java5

You shouldn't call hasMore()

for(NamingEnumeration ne : list){   
    System.out.println("name is : " + ne.toString());     
}

In case your list does not support the Iterator interface you need to use the old form:

for ( Enumeration e = v.elements() ; e.hasMoreElements() ; ) {
    String a = (String) e.nextElement();
    System.out.println( a );
}

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:

<!-- https://mvnrepository.com/artifact/org.springframework.ldap/spring-ldap-core -->
<dependency>
    <groupId>org.springframework.ldap</groupId>
    <artifactId>spring-ldap-core</artifactId>
    <version>2.3.1.RELEASE</version>
</dependency>

In this sample code the searchResultList contains all entries, each one is represented as a map of attributes (with one or more values):

List<Map<String, List<String>>> searchResultList = sourceLdapTemplate.search(searchBase, filter.encode(), SearchControls.ONELEVEL_SCOPE, new AttributesMapper<Map<String, List<String>>>() {
            @Override
            public Map<String, List<String>> mapFromAttributes(Attributes attributes) throws NamingException {
                Map<String, List<String>> attrsMap = new HashMap<>();
                NamingEnumeration<String> attrIdEnum = attributes.getIDs();
                while (attrIdEnum.hasMoreElements()) {
                    // Get attribute id:
                    String attrId = attrIdEnum.next();
                    // Get all attribute values:
                    Attribute attr = attributes.get(attrId);
                    NamingEnumeration<?> attrValuesEnum = attr.getAll();
                    while (attrValuesEnum.hasMore()) {
                        if (!attrsMap.containsKey(attrId))
                            attrsMap.put(attrId, new ArrayList<String>()); 
                        attrsMap.get(attrId).add(attrValuesEnum.next().toString());
                    }
                }
                return attrsMap;
            }
        });

Now, working with the searchResultList looks like this:

for (Map<String, List<String>> attrsMap : searchResultList) {
    for (String objectClass : m.get("objectClass")) {
        // do something with this objectClass...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM