1 package com.atlassian.user.impl.ldap.search.page;
2
3 import com.atlassian.user.EntityException;
4 import com.atlassian.user.impl.ldap.repository.LdapContextFactory;
5 import com.atlassian.user.impl.ldap.search.LDAPPagerInfo;
6 import com.atlassian.user.search.page.PagerUtils;
7 import org.apache.log4j.Category;
8
9 import javax.naming.NamingEnumeration;
10 import javax.naming.directory.Attribute;
11 import javax.naming.directory.Attributes;
12 import javax.naming.directory.SearchResult;
13 import java.util.List;
14
15 public class LDAPMembershipToUsernamePager extends LDAPSingleStringPager
16 {
17 public static final Category log = Category.getInstance(LDAPMembershipToUsernamePager.class);
18
19 public LDAPMembershipToUsernamePager(LdapContextFactory repository, LDAPPagerInfo info)
20 {
21 super(repository, info);
22 }
23
24 protected List<String> preloadSearchResult(SearchResult result, List<String> prefetched) throws EntityException
25 {
26 try
27 {
28 Attributes entityAttributes = result.getAttributes();
29 String attributeToFind = returningAttributes[0];
30 Attribute attr = entityAttributes.get(attributeToFind);
31
32 String val;
33
34 if (attr != null && attr.size() > 0)
35 {
36 if (attr.size() == 1)
37 {
38 val = (String) attr.get();
39 prefetched.add(PagerUtils.extractSearchResultName(val));
40 }
41 else
42 {
43 NamingEnumeration interiorList = attr.getAll();
44
45 while (interiorList.hasMoreElements())
46 {
47 val = (String) interiorList.nextElement();
48 String username = null;
49 try
50 {
51 username = PagerUtils.extractSearchResultName(val);
52 }
53 catch (Exception e)
54 {
55 log.error("Error extracting username from '" + val + "'", e);
56 }
57 prefetched.add(username);
58 }
59 }
60 }
61 }
62 catch (Throwable t)
63 {
64 log.error("Error converting search result: " + result + " into list of members as usernames.", t);
65 }
66
67 return prefetched;
68 }
69 }