1 package com.atlassian.user.impl.ldap;
2
3 import com.atlassian.user.EntityException;
4 import com.atlassian.user.impl.RepositoryException;
5 import com.atlassian.user.impl.ldap.properties.LdapMembershipProperties;
6 import com.atlassian.user.impl.ldap.properties.LdapSearchProperties;
7 import com.atlassian.util.profiling.UtilTimerStack;
8
9 import javax.naming.NamingEnumeration;
10 import javax.naming.NamingException;
11 import javax.naming.directory.Attribute;
12 import javax.naming.directory.Attributes;
13
14 public class DefaultLDAPGroupFactory implements LDAPGroupFactory
15 {
16 private final LdapSearchProperties searchProperties;
17 private final LdapMembershipProperties membershipProperties;
18
19 public DefaultLDAPGroupFactory(LdapSearchProperties searchProperties,
20 LdapMembershipProperties membershipProperties)
21 {
22 this.membershipProperties = membershipProperties;
23 this.searchProperties = searchProperties;
24 }
25
26 public DefaultLDAPGroup getGroup(Attributes attrs, String distinguishedName) throws EntityException
27 {
28 if (UtilTimerStack.isActive())
29 UtilTimerStack.push(this.getClass().getName() + "_delegating_getGroup(attrs, " + distinguishedName + ")");
30
31 DefaultLDAPGroup group;
32
33 try
34 {
35 Attribute groupNameAttribute = attrs.get(searchProperties.getGroupnameAttribute());
36 if (groupNameAttribute != null)
37 {
38 String groupName = (String) groupNameAttribute.get();
39 group = new DefaultLDAPGroup(groupName, distinguishedName);
40 }
41 else
42 {
43 Attribute membershipAttribute = attrs.get(membershipProperties.getMembershipAttribute());
44 group = getGroup(membershipAttribute);
45 }
46
47 }
48 catch (NamingException e)
49 {
50 throw new RepositoryException(e);
51 }
52
53 if (UtilTimerStack.isActive())
54 UtilTimerStack.pop(this.getClass().getName() + "_delegating_getGroup(attrs, " + distinguishedName + ")");
55
56 return group;
57 }
58
59 public DefaultLDAPGroup getGroup(String distinguishedName)
60 {
61 if (UtilTimerStack.isActive())
62 UtilTimerStack.push(this.getClass().getName() + "_delegating_getGroup(" + distinguishedName + ")");
63
64 String groupName = getGroupNameFromDN(distinguishedName);
65 DefaultLDAPGroup group = new DefaultLDAPGroup(groupName, distinguishedName);
66
67 if (UtilTimerStack.isActive())
68 UtilTimerStack.pop(this.getClass().getName() + "_delegating_getGroup(" + distinguishedName + ")");
69
70 return group;
71 }
72
73 public DefaultLDAPGroup getGroup(Attribute attribute) throws EntityException
74 {
75 if (UtilTimerStack.isActive())
76 UtilTimerStack.push(this.getClass().getName() + "_delegating_getGroup(attr)");
77
78 try
79 {
80 String groupDN = getGroupDNFromMembershipAttribute(attribute);
81 String groupName = getGroupNameFromDN(groupDN);
82
83 return new DefaultLDAPGroup(groupName, groupDN);
84 }
85 finally
86 {
87 if (UtilTimerStack.isActive())
88 UtilTimerStack.pop(this.getClass().getName() + "_delegating_getGroup(attr)");
89 }
90 }
91
92 private String getGroupDNFromMembershipAttribute(Attribute groupMembershipAtt) throws EntityException
93 {
94 if (UtilTimerStack.isActive())
95 UtilTimerStack.push(this.getClass().getName() + "_delegating_getGroupDNFromMembershipAttribute");
96
97 String groupDN = null;
98
99 try
100 {
101 NamingEnumeration groupAttrs = groupMembershipAtt.getAll();
102
103 while (groupAttrs.hasMoreElements())
104 {
105 groupDN = (String) groupAttrs.nextElement();
106 if (groupDN != null)
107 break;
108 }
109 }
110 catch (NamingException e)
111 {
112 throw new RepositoryException(e);
113 }
114
115 if (UtilTimerStack.isActive())
116 UtilTimerStack.pop(this.getClass().getName() + "_delegating_getGroupDNFromMembershipAttribute");
117
118 return groupDN;
119 }
120
121
122
123
124
125 private String getGroupNameFromDN(String groupDN)
126 {
127 if (groupDN.indexOf("=") == -1)
128 return groupDN;
129
130 String[] names = groupDN.split(",");
131
132 return names[0].split("=")[1];
133 }
134
135 public DefaultLDAPGroup getEntity(Attributes attrs, String distinguishedName) throws EntityException
136 {
137 return getGroup(attrs, distinguishedName);
138 }
139
140 @SuppressWarnings({"UnusedDeclaration"})
141 public DefaultLDAPGroup getEntity(Attribute attr, String distinguishedName) throws EntityException
142 {
143 return getGroup(attr);
144 }
145 }