KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
11
12 import net.sf.hibernate.Session;
13 import netscape.ldap.LDAPAttribute;
14 import netscape.ldap.LDAPAttributeSet;
15 import netscape.ldap.LDAPConnection;
16 import netscape.ldap.LDAPEntry;
17 import netscape.ldap.LDAPException;
18 import netscape.ldap.LDAPModification;
19 import netscape.ldap.LDAPModificationSet;
20 import netscape.ldap.LDAPSearchResults;
21
22 import org.exoplatform.services.database.HibernateService;
23 import org.exoplatform.services.ldap.LDAPService;
24 import org.exoplatform.services.ldap.LDAPServiceContainer;
25 import org.exoplatform.services.organization.UserProfile;
26 import org.exoplatform.services.organization.UserProfileEventListener;
27
28 /**
29  * Created by the eXo platform team
30  * User: Daniel Summer
31  * Date: 28 august 2004
32  *
33  */

34 public class UserProfileLDAPHandler extends BaseLDAPHandler {
35     private final String JavaDoc BASEURL = OrganizationLDAPConfig.BASE_URL;
36     private final String JavaDoc PORTALURL = OrganizationLDAPConfig.PORTAL_URL;
37     private final String JavaDoc ROLESURL = OrganizationLDAPConfig.ROLES_URL;
38     private final String JavaDoc USERSURL = OrganizationLDAPConfig.USERS_URL;
39
40     private LDAPServiceContainer serviceContainer_;
41     private HibernateService service_;
42     private List JavaDoc listeners_;
43
44     public UserProfileLDAPHandler(
45         LDAPServiceContainer serviceContainer,
46         HibernateService service) {
47         serviceContainer_ = serviceContainer;
48         service_ = service;
49         listeners_ = new ArrayList JavaDoc(5);
50     }
51
52     public void addUserProfileEventListener(UserProfileEventListener listener) {
53         listeners_.add(listener);
54     }
55
56     void createUserProfileEntry(UserProfile up, Session session)
57         throws Exception JavaDoc {
58         UserProfileLDAPData upd = new UserProfileLDAPData();
59         LDAPService service = null;
60         service = serviceContainer_.createLDAPService();
61         String JavaDoc MY_FILTER = "uid=" + up.getUserName();
62         String JavaDoc MY_SEARCHBASE = USERSURL;
63         LDAPSearchResults res =
64             service.search(
65                 MY_SEARCHBASE,
66                 LDAPConnection.SCOPE_SUB,
67                 MY_FILTER,
68                 null,
69                 false);
70         List JavaDoc l = getUtil(res);
71         LDAPEntry findEntry = (LDAPEntry) l.get(0);
72         upd.setUserProfile(up);
73         LDAPModificationSet mods = new LDAPModificationSet();
74         mods.add(
75             LDAPModification.REPLACE,
76             new LDAPAttribute("profile", upd.getProfile()));
77         service.modify(findEntry.getDN(), mods);
78         serviceContainer_.closeLDAPService(service);
79     }
80
81     public void saveUserProfile(UserProfile profile) throws Exception JavaDoc {
82         LDAPService service = null;
83
84         UserProfileLDAPData upd = null;
85         try {
86             service = serviceContainer_.createLDAPService();
87             String JavaDoc MY_FILTER = "uid=" + profile.getUserName();
88             String JavaDoc MY_SEARCHBASE = USERSURL;
89             LDAPSearchResults res =
90                 service.search(
91                     MY_SEARCHBASE,
92                     LDAPConnection.SCOPE_SUB,
93                     MY_FILTER,
94                     null,
95                     false);
96             List JavaDoc l = getUtil(res);
97             LDAPEntry findEntry = (LDAPEntry) l.get(0);
98             upd = new UserProfileLDAPData();
99             upd.setUserProfile(profile);
100             LDAPModificationSet mods = new LDAPModificationSet();
101             mods.add(
102                 LDAPModification.REPLACE,
103                 new LDAPAttribute("profile", upd.getProfile()));
104             service.modify(findEntry.getDN(), mods);
105         } finally {
106             serviceContainer_.closeLDAPService(service);
107         }
108     }
109
110     public UserProfile removeUserProfile(String JavaDoc userName) throws Exception JavaDoc {
111         LDAPService service = null;
112         UserProfileLDAPData upd = null;
113         try {
114             service = serviceContainer_.createLDAPService();
115             String JavaDoc MY_FILTER = "uid=" + userName;
116             String JavaDoc MY_SEARCHBASE = USERSURL;
117             LDAPSearchResults res =
118                 service.search(
119                     MY_SEARCHBASE,
120                     LDAPConnection.SCOPE_SUB,
121                     MY_FILTER,
122                     null,
123                     false);
124             List JavaDoc l = getUtil(res);
125             LDAPEntry findEntry = (LDAPEntry) l.get(0);
126             LDAPModificationSet mods = new LDAPModificationSet();
127             LDAPAttribute attrRole = new LDAPAttribute("role");
128             mods.add(LDAPModification.DELETE, attrRole);
129             service.modify(findEntry.getDN(), mods);
130         } finally {
131             service = serviceContainer_.createLDAPService();
132         }
133         return upd.getUserProfile();
134     }
135
136     public UserProfile findUserProfileByName(String JavaDoc userName)
137         throws Exception JavaDoc {
138         LDAPService service = null;
139         try {
140             service = serviceContainer_.createLDAPService();
141             return findUserProfileByName(userName, service);
142         } finally {
143             serviceContainer_.closeLDAPService(service);
144         }
145     }
146
147     public UserProfile findUserProfileByName(
148         String JavaDoc userName,
149         LDAPService service)
150         throws Exception JavaDoc {
151         UserProfileLDAPData upd = new UserProfileLDAPData();
152         String JavaDoc MY_FILTER = "uid=" + userName;
153         String JavaDoc MY_SEARCHBASE = USERSURL;
154         LDAPSearchResults res =
155             service.search(
156                 MY_SEARCHBASE,
157                 LDAPConnection.SCOPE_SUB,
158                 MY_FILTER,
159                 null,
160                 false);
161         List JavaDoc l = getUtil(res);
162         LDAPEntry findEntry = (LDAPEntry) l.get(0);
163         LDAPAttributeSet attrs = findEntry.getAttributeSet();
164         upd.setProfile(getutil("profile", attrs));
165         upd.setUserName(userName);
166
167         if (upd != null) {
168             return upd.getUserProfile();
169         }
170         return null;
171     }
172
173     static void removeUserProfileEntry(
174         String JavaDoc userName,
175         Session session,
176         LDAPService service)
177         throws Exception JavaDoc {
178
179         UserProfileLDAPData upd = null;
180
181         String JavaDoc MY_FILTER = "uid=" + userName;
182         String JavaDoc MY_SEARCHBASE = OrganizationLDAPConfig.USERS_URL;;
183         LDAPSearchResults res =
184             service.search(
185                 MY_SEARCHBASE,
186                 LDAPConnection.SCOPE_SUB,
187                 MY_FILTER,
188                 null,
189                 false);
190         while (res.hasMoreElements()) {
191
192             LDAPEntry findEntry = null;
193             try {
194                 findEntry = res.next();
195                 LDAPModificationSet mods = new LDAPModificationSet();
196                 LDAPAttribute attrRole = new LDAPAttribute("profile");
197                 mods.add(LDAPModification.DELETE, attrRole);
198                 service.modify(findEntry.getDN(), mods);
199
200             } catch (LDAPException e) {
201                 continue;
202             }
203         }
204
205     }
206
207     public Collection JavaDoc findUserProfiles() throws Exception JavaDoc {
208         return null;
209     }
210 }
Popular Tags