View Javadoc

1   package com.atlassian.user.impl.ldap;
2   
3   import com.atlassian.user.impl.RepositoryException;
4   import com.atlassian.user.impl.ldap.properties.LdapSearchProperties;
5   import com.atlassian.user.User;
6   import com.atlassian.util.profiling.UtilTimerStack;
7   
8   import javax.naming.NamingException;
9   import javax.naming.directory.Attribute;
10  import javax.naming.directory.Attributes;
11  import javax.naming.directory.SearchResult;
12  import java.util.ArrayList;
13  import java.util.Collection;
14  import java.util.Enumeration;
15  
16  public class DefaultLDAPUserFactory implements LDAPUserFactory
17  {
18      private final LdapSearchProperties searchProperties;
19  
20      public DefaultLDAPUserFactory(LdapSearchProperties searchProperties)
21      {
22          this.searchProperties = searchProperties;
23      }
24  
25      public DefaultLDAPUser getUser(Attributes attrs, String distinguishedName) throws RepositoryException
26      {
27          DefaultLDAPUser user;
28  
29          if (distinguishedName.indexOf(searchProperties.getBaseUserNamespace()) == -1)
30              distinguishedName = distinguishedName + "," + searchProperties.getBaseUserNamespace();
31  
32          try
33          {
34              Attribute uidAttr = attrs.get(searchProperties.getUsernameAttribute());
35              if (uidAttr == null) return null;
36  
37              user = new DefaultLDAPUser((String) uidAttr.get(), distinguishedName);
38  
39              if (UtilTimerStack.isActive())
40              {
41                  String stackKey = this.getClass().getName() + "_getUser(" + user.getName() + ")";
42                  UtilTimerStack.push(stackKey);
43              }
44  
45              Attribute givenNameAttr = attrs.get(searchProperties.getFirstnameAttribute());
46              Attribute surNameAttr = attrs.get(searchProperties.getSurnameAttribute());
47  
48              if (givenNameAttr != null)
49              {
50                  String givenName = (String) givenNameAttr.get();
51  
52                  String surName = "";
53  
54                  if (surNameAttr != null)
55                      surName = (String) surNameAttr.get();
56  
57                  givenName = givenName.concat(" ");
58                  user.setFullName(givenName.concat(surName));
59              }
60  
61              Attribute emailAttr = attrs.get(searchProperties.getEmailAttribute());
62              if (emailAttr != null)
63                  user.setEmail((String) emailAttr.get());
64  
65          }
66          catch (NamingException e)
67          {
68              throw new RepositoryException(e);
69          }
70  
71          if (UtilTimerStack.isActive())
72          {
73              String stackKey = this.getClass().getName() + "_getUser(" + user.getName() + ")";
74              UtilTimerStack.pop(stackKey);
75          }
76  
77          return user;
78      }
79  
80      public Collection<User> getUsers(Enumeration userNamingEnumeration) throws RepositoryException
81      {
82          if (UtilTimerStack.isActive())
83              UtilTimerStack.push(this.getClass().getName() + "_getUserCollection");
84  
85          ArrayList<User> users = new ArrayList<User>();
86  
87          while (userNamingEnumeration.hasMoreElements())
88          {
89              SearchResult result = (SearchResult) userNamingEnumeration.nextElement();
90              Attributes attrs = result.getAttributes();
91              users.add(getUser(attrs, result.getName()));
92          }
93  
94          if (UtilTimerStack.isActive())
95              UtilTimerStack.pop(this.getClass().getName() + "_getUserCollection");
96  
97          return users;
98      }
99  
100     public DefaultLDAPUser getEntity(Attributes attrs, String distinguishedName) throws RepositoryException
101     {
102         if (distinguishedName.startsWith("\"") && distinguishedName.endsWith("\""))
103         {
104             distinguishedName = distinguishedName.substring(1,distinguishedName.length() - 1);
105         }
106         return getUser(attrs, distinguishedName);
107     }
108 }