KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > organization > ldap > GroupLDAPHandler


1 /**
2  * Copyright 2001-2004 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.services.organization.ldap;
6
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Vector JavaDoc;
13
14 import net.sf.hibernate.Session;
15 import netscape.ldap.LDAPAttribute;
16 import netscape.ldap.LDAPAttributeSet;
17 import netscape.ldap.LDAPConnection;
18 import netscape.ldap.LDAPEntry;
19 import netscape.ldap.LDAPModification;
20 import netscape.ldap.LDAPModificationSet;
21 import netscape.ldap.LDAPSearchResults;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.exoplatform.services.database.HibernateService;
25 import org.exoplatform.services.database.XResources;
26 import org.exoplatform.services.ldap.LDAPService;
27 import org.exoplatform.services.ldap.LDAPServiceContainer;
28 import org.exoplatform.services.organization.Group;
29 import org.exoplatform.services.organization.GroupEventListener;
30 import org.exoplatform.services.organization.impl.GroupImpl;
31
32 /**
33  * Created by the eXo platform team
34  * User: Daniel Summer
35  * Date: 28 august 2004
36  *
37  */

38 public class GroupLDAPHandler extends BaseLDAPHandler {
39     public static final String JavaDoc BASEURL = OrganizationLDAPConfig.BASE_URL;
40     public static final String JavaDoc PORTALURL = OrganizationLDAPConfig.PORTAL_URL;
41     public static final String JavaDoc GROUPSURL = OrganizationLDAPConfig.GROUPS_URL;
42     public static final String JavaDoc USERSURL = OrganizationLDAPConfig.USERS_URL;
43
44     private LDAPServiceContainer serviceContainer_;
45     private HibernateService hService_;
46     private List JavaDoc listeners_;
47
48     public GroupLDAPHandler(
49         LDAPServiceContainer serviceContainer,
50         HibernateService hService) {
51         serviceContainer_ = serviceContainer;
52         hService_ = hService;
53         listeners_ = new ArrayList JavaDoc(5);
54     }
55
56     public void addGroupEventListener(GroupEventListener listener) {
57         listeners_.add(listener);
58     }
59
60     public void createGroup(Group group) throws Exception JavaDoc {
61         LDAPService service = null;
62         Session session = null;
63         try {
64             service = serviceContainer_.createLDAPService();
65             session = hService_.openSession();
66             String JavaDoc dn = "uid=" + group.getGroupName() + "," + GROUPSURL;
67             GroupImpl g = (GroupImpl) group;
68             String JavaDoc groupId = "/" + group.getGroupName();
69             g.setId(groupId);
70             LDAPAttributeSet attrs = new LDAPAttributeSet();
71
72             String JavaDoc objectclass_values[] = { "top", "exogroup" };
73             setutil("objectclass", objectclass_values, attrs, false);
74             setutil("id", g.getId(), attrs);
75             setutil("uid", g.getGroupName(), attrs);
76             setutil("groupname", g.getGroupName(), attrs);
77
78             LDAPEntry myEntry = new LDAPEntry(dn, attrs);
79             preSave(g, true, session);
80             service.add(myEntry);
81             postSave(g, true, session);
82             session.flush();
83
84         } finally {
85             serviceContainer_.closeLDAPService(service);
86             hService_.closeSession(session);
87         }
88     }
89
90     public void addChild(Group parent, Group child) throws Exception JavaDoc {
91         LDAPService service = null;
92         Session session = null;
93         try {
94             String JavaDoc MY_FILTER = "id=" + parent.getId();
95             String JavaDoc MY_SEARCHBASE = GROUPSURL;
96
97             service = serviceContainer_.createLDAPService();
98             session = hService_.openSession();
99             LDAPSearchResults res =
100                 service.search(
101                     MY_SEARCHBASE,
102                     LDAPConnection.SCOPE_SUB,
103                     MY_FILTER,
104                     null,
105                     false);
106
107             List JavaDoc l = getUtil(res);
108             LDAPEntry findEntry = (LDAPEntry) l.get(0);
109
110             GroupImpl g = (GroupImpl) child;
111             g.setParentId(getutil("id", findEntry.getAttributeSet()));
112
113             if (findEntry != null) {
114                 String JavaDoc dn =
115                     "uid="
116                         + g.getGroupName()
117                         + ","
118                         + "uid="
119                         + getutil("groupname", findEntry.getAttributeSet())
120                         + ","
121                         + GROUPSURL;
122                 LDAPAttributeSet attrs = new LDAPAttributeSet();
123
124                 g.setId(g.getParentId() + "/" + child.getGroupName());
125                 String JavaDoc objectclass_values[] = { "top", "exogroup" };
126
127                 setutil("objectclass", objectclass_values, attrs, false);
128                 setutil("id", g.getId(), attrs);
129                 setutil("uid", g.getGroupName(), attrs);
130                 setutil("groupname", g.getGroupName(), attrs);
131
132                 LDAPEntry myEntry = new LDAPEntry(dn, attrs);
133                 preSave(g, true, session);
134                 service.add(myEntry);
135                 postSave(g, true, session);
136                 session.flush();
137             } else {
138                 throw new Exception JavaDoc(
139                     "cannot find the parent group" + parent.getId());
140             }
141         } finally {
142             serviceContainer_.closeLDAPService(service);
143             hService_.closeSession(session);
144         }
145     }
146
147     public void saveGroup(Group group) throws Exception JavaDoc {
148         LDAPService service = null;
149         Session session = null;
150         GroupImpl groupImpl = (GroupImpl) group;
151         try {
152             service = serviceContainer_.createLDAPService();
153             session = hService_.openSession();
154             String JavaDoc olddn = groupImpl.getId();
155             LDAPModificationSet mods = new LDAPModificationSet();
156             mods.add(
157                 LDAPModification.REPLACE,
158                 new LDAPAttribute("groupname", groupImpl.getGroupName()));
159
160             preSave(group, false, session);
161             service.modify(olddn, mods);
162             service.rename(olddn, "uid=" + groupImpl.getGroupName(), true);
163             postSave(group, false, session);
164             session.flush();
165         } finally {
166             hService_.closeSession(session);
167             serviceContainer_.closeLDAPService(service);
168         }
169     }
170
171     public Group removeGroup(Group group) throws Exception JavaDoc {
172         LDAPService service = null;
173         Session session = null;
174         Group foundGroup = null;
175         try {
176             service = serviceContainer_.createLDAPService();
177             session = hService_.openSession();
178             String JavaDoc MY_FILTER = "id=" + group.getId();
179             String JavaDoc MY_SEARCHBASE = GROUPSURL;
180
181             LDAPSearchResults res =
182                 service.search(
183                     MY_SEARCHBASE,
184                     LDAPConnection.SCOPE_SUB,
185                     MY_FILTER,
186                     null,
187                     false);
188
189             List JavaDoc l = getUtil(res);
190             if (l.size() == 1) {
191                 LDAPEntry findEntry = (LDAPEntry) l.get(0);
192                 GroupImpl groupImpl = new GroupImpl();
193                 groupImpl.setId(getutil("id", findEntry.getAttributeSet()));
194                 groupImpl.setGroupName(
195                     getutil("groupname", findEntry.getAttributeSet()));
196                 groupImpl.setGroupName(
197                     getutil("description", findEntry.getAttributeSet()));
198                 preDelete(foundGroup, session);
199                 service.delete(findEntry.getDN());
200                 postDelete(foundGroup, session);
201                 session.flush();
202             } else {
203                 throw new Exception JavaDoc(
204                     "Cannot find the group name " + group.getId());
205             }
206
207             // delete cascade
208
MY_FILTER = "membership=*";
209             MY_SEARCHBASE = USERSURL;
210
211             res =
212                 service.search(
213                     MY_SEARCHBASE,
214                     LDAPConnection.SCOPE_SUB,
215                     MY_FILTER,
216                     null,
217                     false);
218             while (res.hasMoreElements()) {
219                 LDAPEntry entry = res.next();
220
221                 Vector JavaDoc memberships =
222                     getutil("membership", entry.getAttributeSet(), false);
223                 for (Iterator JavaDoc i = memberships.iterator(); i.hasNext();) {
224                     String JavaDoc s = (String JavaDoc) i.next();
225                     String JavaDoc[] membership = StringUtils.split(s, ",");
226                     if (membership[3].equals(group.getGroupName())) {
227                         LDAPModificationSet mods = new LDAPModificationSet();
228                         mods.add(
229                             LDAPModification.DELETE,
230                             new LDAPAttribute("membership", s));
231                         service.modify(entry.getDN(), mods);
232                     }
233                 }
234
235             }
236         } finally {
237             service = serviceContainer_.createLDAPService();
238         }
239         return foundGroup;
240     }
241
242     static void removeGroupEntry(
243         String JavaDoc groupName,
244         Session session,
245         LDAPService service)
246         throws Exception JavaDoc {
247         String JavaDoc MY_FILTER = "uid=" + groupName;
248         String JavaDoc MY_SEARCHBASE = GROUPSURL;
249         String JavaDoc ENTRYDN = MY_FILTER + MY_SEARCHBASE;
250         service.delete(ENTRYDN);
251     }
252
253     public Collection JavaDoc findGroupByMembership(
254         String JavaDoc userName,
255         String JavaDoc membershipType)
256         throws Exception JavaDoc {
257         LDAPService service = null;
258         List JavaDoc groups = new ArrayList JavaDoc();
259         List JavaDoc groupsImpl = new ArrayList JavaDoc();
260         Session session = null;
261         try {
262             service = serviceContainer_.createLDAPService();
263             String JavaDoc MY_SEARCHBASE = USERSURL;
264             String JavaDoc MY_FILTER = "uid=" + userName;
265             LDAPSearchResults res =
266                 service.search(
267                     MY_SEARCHBASE,
268                     LDAPConnection.SCOPE_SUB,
269                     MY_FILTER,
270                     null,
271                     false);
272
273             List JavaDoc l = getUtil(res);
274             LDAPEntry findEntry = (LDAPEntry) l.get(0);
275
276             LDAPAttributeSet attrs = findEntry.getAttributeSet();
277             Vector JavaDoc memberships = getutil("membership", attrs, false);
278
279             for (Iterator JavaDoc i = memberships.iterator(); i.hasNext();) {
280                 String JavaDoc s = (String JavaDoc) i.next();
281                 String JavaDoc[] membership = StringUtils.split(s, ",");
282                 if (membership[1].equals(membershipType)) {
283
284                     if (!groups.contains(membership[3])) {
285                         groups.add(membership[3]);
286                         GroupImpl groupImpl = new GroupImpl();
287                         //groupImpl.setId(findEntry.getDN());
288
groupImpl.setGroupName(membership[3]);
289                         groupsImpl.add(groupImpl);
290                     }
291                 }
292             }
293
294         } finally {
295             serviceContainer_.closeLDAPService(service);
296         }
297         return groupsImpl;
298     }
299
300     public Group findGroupByName(String JavaDoc groupName) throws Exception JavaDoc {
301         Group group = null;
302         LDAPService service = null;
303         try {
304             service = serviceContainer_.createLDAPService();
305             List JavaDoc l = null;
306             String JavaDoc MY_SEARCHBASE = GROUPSURL;
307             String JavaDoc MY_FILTER = "groupname=" + groupName;
308
309             LDAPSearchResults res =
310                 service.search(
311                     MY_SEARCHBASE,
312                     LDAPConnection.SCOPE_SUB,
313                     MY_FILTER,
314                     null,
315                     false);
316
317             l = getUtil(res);
318             LDAPEntry findEntry = (LDAPEntry) l.get(0);
319
320             LDAPAttributeSet attrs = findEntry.getAttributeSet();
321
322             String JavaDoc groupname = getutil("groupname", attrs);
323             GroupImpl groupImpl = new GroupImpl();
324             groupImpl.setId(findEntry.getDN());
325             groupImpl.setParentId(
326                 StringUtils.substringAfter(findEntry.getDN(), ","));
327             groupImpl.setGroupName(groupname);
328
329             return (Group) groupImpl;
330
331         } finally {
332             serviceContainer_.closeLDAPService(service);
333         }
334
335     }
336
337     public Group findGroupById(String JavaDoc groupId) throws Exception JavaDoc {
338         LDAPService service = null;
339         try {
340             service = serviceContainer_.createLDAPService();
341             LDAPEntry findEntry = service.read(groupId);
342             Group group = new GroupImpl();
343             group.setGroupName(
344                 getutil("groupname", findEntry.getAttributeSet()));
345             return group;
346         } finally {
347             serviceContainer_.closeLDAPService(service);
348         }
349
350     }
351
352     public Collection JavaDoc findGroups(Group parent) throws Exception JavaDoc {
353         Collection JavaDoc groups = null;
354         LDAPService service = null;
355         try {
356             service = serviceContainer_.createLDAPService();
357             String JavaDoc MY_SEARCHBASE = GROUPSURL;
358             if (parent == null) {
359
360                 LDAPSearchResults res =
361                     service.search(
362                         MY_SEARCHBASE,
363                         LDAPConnection.SCOPE_SUB,
364                         null,
365                         null,
366                         false);
367                 List JavaDoc l = getUtil(res);
368
369                 for (Iterator JavaDoc i = l.iterator(); i.hasNext();) {
370                     LDAPEntry entry = (LDAPEntry) i.next();
371                     GroupImpl g = new GroupImpl();
372                     g.setGroupName(
373                         getutil("groupname", entry.getAttributeSet()));
374                     groups.add(g);
375                 }
376
377             } else {
378                 String JavaDoc MY_SEARCHBASE2 = "groupname=" + parent + MY_SEARCHBASE;
379                 String JavaDoc MY_FILTER = "groupname=" + parent;
380                 LDAPSearchResults res =
381                     service.search(
382                         MY_SEARCHBASE2,
383                         LDAPConnection.SCOPE_SUB,
384                         null,
385                         null,
386                         false);
387                 List JavaDoc l = getUtil(res);
388
389                 if (l.size() == 1) {
390                     for (Iterator JavaDoc i = l.iterator(); i.hasNext();) {
391                         LDAPEntry entry = (LDAPEntry) i.next();
392                         GroupImpl g = new GroupImpl();
393                         g.setGroupName(
394                             getutil("groupname", entry.getAttributeSet()));
395                         groups.add(g);
396                     }
397                 } else {
398                     throw new Exception JavaDoc(
399                         "Cannot find the group parent " + parent);
400                 }
401             }
402         } finally {
403             serviceContainer_.closeLDAPService(service);
404         }
405         return groups;
406     }
407
408     public Collection JavaDoc findGroupsOfUser(String JavaDoc user) throws Exception JavaDoc {
409         List JavaDoc groups = new ArrayList JavaDoc();
410         LDAPService service = null;
411         try {
412             service = serviceContainer_.createLDAPService();
413             String JavaDoc MY_SEARCHBASE = USERSURL;
414             String JavaDoc MY_FILTER = "uid=" + user;
415             LDAPSearchResults res =
416                 service.search(
417                     MY_SEARCHBASE,
418                     LDAPConnection.SCOPE_SUB,
419                     MY_FILTER,
420                     null,
421                     false);
422
423             List JavaDoc l = getUtil(res);
424             LDAPEntry findEntry = (LDAPEntry) l.get(0);
425
426             LDAPAttributeSet attrs = findEntry.getAttributeSet();
427             Vector JavaDoc memberships = getutil("membership", attrs, false);
428
429             for (Iterator JavaDoc i = memberships.iterator(); i.hasNext();) {
430                 String JavaDoc s = (String JavaDoc) i.next();
431                 String JavaDoc[] membership = StringUtils.split(s, ",");
432                 if (!groups.contains(membership[3])) {
433                     groups.add(membership[3]);
434                 }
435             }
436
437         } finally {
438             serviceContainer_.closeLDAPService(service);
439         }
440         return groups;
441     }
442
443     private void preSave(Group group, boolean isNew, Session session)
444         throws Exception JavaDoc {
445         XResources xresources = new XResources();
446         xresources.addResource(Session.class, session);
447         for (int i = 0; i < listeners_.size(); i++) {
448             GroupEventListener listener =
449                 (GroupEventListener) listeners_.get(i);
450             listener.preSave(group, isNew, xresources);
451         }
452     }
453
454     private void postSave(Group group, boolean isNew, Session session)
455         throws Exception JavaDoc {
456         XResources xresources = new XResources();
457         xresources.addResource(Session.class, session);
458         for (int i = 0; i < listeners_.size(); i++) {
459             GroupEventListener listener =
460                 (GroupEventListener) listeners_.get(i);
461             listener.postSave(group, isNew, xresources);
462         }
463     }
464
465     private void preDelete(Group group, Session session) throws Exception JavaDoc {
466         XResources xresources = new XResources();
467         xresources.addResource(Session.class, session);
468         for (int i = 0; i < listeners_.size(); i++) {
469             GroupEventListener listener =
470                 (GroupEventListener) listeners_.get(i);
471             listener.preDelete(group, xresources);
472         }
473     }
474
475     private void postDelete(Group group, Session session) throws Exception JavaDoc {
476         XResources xresources = new XResources();
477         xresources.addResource(Session.class, session);
478         for (int i = 0; i < listeners_.size(); i++) {
479             GroupEventListener listener =
480                 (GroupEventListener) listeners_.get(i);
481             listener.postDelete(group, xresources);
482         }
483     }
484
485 }
Popular Tags