Announcement Announcement Module
Collapse
No announcement yet.
Search Multivalue attributes Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Search Multivalue attributes

    Hi,
    I want to search multivalue attrs using ldapTemplate. For single values I am using,

    public List getAllPersonRoles() {
    System.out.println("Inside getAllPersonRoles");
    EqualsFilter filter = new EqualsFilter("objectclass", "person");
    return ldapTemplate.search("o=person", filter.encode(), new AttributesMapper() {
    public Object mapFromAttributes(Attributes attrs) throws NamingException {
    return attrs.get("roles").get();
    }
    });
    }


    How do I do multivalue attributes search without doing NamingEnumeration?
    eg.
    NamingEnumeration rolesEnum = (NamingEnumeration) attrs.get("roles").get().getAll();
    while (rolesEnum.hasMore()) {
    System.out.println("Invidual roles : " + rolesEnum.next());
    }

    Thanks in advance.
    Regards.

  • #2
    Spring LDAP provides the DirContextAdapter class to help working with Attributes. It's described in the reference manual, but basically you'll supply a ContextMapper to your search in stead of an AttributesMapper:
    Code:
    List roles = ldapTemplate.search("o=person", filter.toString(),
        new ContextMapper(){
            public Object mapFromContext(Object ctx){
                DirContextAdapter adapter = (DirContextAdapter) ctx;
                return adapter.getStringAttributes("roles");
            }
        });
    The above will result in a List of String arrays representing the individual roles of each person returned from the search.

    Comment


    • #3
      Thanks so much Mattias. It did the trick.

      You guys are doing great job on the ldap forum.

      Thanks.

      Comment


      • #4
        Just to compare these two approaches, here are two mappers that will give the same result. First the AttributesMapper:

        Code:
        AttributesMapper mapper = new AttributesMapper() {
           public Object mapFromAttributes(Attributes attributes) throws NamingException {
              LinkedList list = new LinkedList();
              NamingEnumeration enumeration = attributes.get("roles").getAll();
              while (enumeration.hasMoreElements()) {
                 String value = (String) enumeration.nextElement();
                 list.add(value);
              }
              String[] roles = (String[]) list.toArray(new String[0]);
              return roles;
           }
        };
        Then the ContextMapper:

        Code:
        ContextMapper mapper = new ContextMapper() {
           public Object mapFromContext(Object ctx) {
              DirContextAdapter adapter = (DirContextAdapter) ctx;
              String[] roles = adapter.getStringAttributes("roles");
              return roles;
           }
        };
        The more work with Attributes, the more appealing is the ContextMapper approach.

        Comment

        Working...
        X