KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > webservices > adminapi > RollerUserHandler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18 package org.apache.roller.webservices.adminapi;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Date JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import org.jdom.Document;
27 import org.jdom.JDOMException;
28 import org.apache.roller.RollerException;
29 import org.apache.roller.model.UserManager;
30 import org.apache.roller.pojos.UserData;
31 import org.apache.roller.util.cache.CacheManager;
32 import org.apache.roller.webservices.adminapi.sdk.Entry;
33 import org.apache.roller.webservices.adminapi.sdk.EntrySet;
34 import org.apache.roller.webservices.adminapi.sdk.MissingElementException;
35 import org.apache.roller.webservices.adminapi.sdk.UnexpectedRootElementException;
36 import org.apache.roller.webservices.adminapi.sdk.UserEntry;
37 import org.apache.roller.webservices.adminapi.sdk.UserEntrySet;
38
39 /**
40  * This class handles request concerning Roller users.
41  *
42  * @author jtb
43  */

44 class RollerUserHandler extends Handler {
45     public RollerUserHandler(HttpServletRequest JavaDoc request) throws HandlerException {
46         super(request);
47     }
48     
49     protected EntrySet getEntrySet(Document d) throws MissingElementException, UnexpectedRootElementException {
50         return new UserEntrySet(d, getUrlPrefix());
51     }
52     
53     public EntrySet processGet() throws HandlerException {
54         if (getUri().isCollection()) {
55             return getCollection();
56         } else if (getUri().isEntry()) {
57             return getEntry();
58         } else {
59             throw new BadRequestException("ERROR: Unknown GET URI type");
60         }
61     }
62     
63     public EntrySet processPost(Reader JavaDoc r) throws HandlerException {
64         if (getUri().isCollection()) {
65             return postCollection(r);
66         } else {
67             throw new BadRequestException("ERROR: Unknown POST URI type");
68         }
69     }
70     
71     public EntrySet processPut(Reader JavaDoc r) throws HandlerException {
72         if (getUri().isCollection()) {
73             return putCollection(r);
74         } else if (getUri().isEntry()) {
75             return putEntry(r);
76         } else {
77             throw new BadRequestException("ERROR: Unknown PUT URI type");
78         }
79     }
80     
81     public EntrySet processDelete() throws HandlerException {
82         if (getUri().isEntry()) {
83             return deleteEntry();
84         } else {
85             throw new BadRequestException("ERROR: Unknown DELETE URI type");
86         }
87     }
88     
89     private EntrySet getCollection() throws HandlerException {
90         try {
91             List JavaDoc users = getRoller().getUserManager().getUsers(0, -1);
92             if (users == null) {
93                 users = java.util.Collections.EMPTY_LIST;
94             }
95             EntrySet es = toUserEntrySet((UserData[])users.toArray(new UserData[0]));
96             
97             return es;
98         } catch (RollerException re) {
99             throw new InternalException("ERROR: Could not get user collection", re);
100         }
101     }
102     
103     private EntrySet getEntry() throws HandlerException {
104         try {
105             UserData ud = getRoller().getUserManager().getUserByUserName(getUri().getEntryId());
106             if (ud == null) {
107                 throw new NotFoundException("ERROR: Unknown user: " + getUri().getEntryId());
108             }
109             UserData[] uds = new UserData[] { ud };
110             
111             EntrySet c = toUserEntrySet(uds);
112             return c;
113         } catch (RollerException re) {
114             throw new InternalException("ERROR: Could not get user collection", re);
115         }
116     }
117     
118     private EntrySet postCollection(Reader JavaDoc r) throws HandlerException {
119         EntrySet c = getEntrySet(r);
120         if (c.isEmpty()) {
121             throw new BadRequestException("ERROR: No entries");
122         }
123         c = createUsers((UserEntrySet)c);
124         
125         return c;
126     }
127     
128     private EntrySet putCollection(Reader JavaDoc r) throws HandlerException {
129         EntrySet c = getEntrySet(r);
130         if (c.isEmpty()) {
131             throw new BadRequestException("ERROR: No entries");
132         }
133         c = updateUsers((UserEntrySet)c);
134         
135         return c;
136     }
137     
138     private EntrySet putEntry(Reader JavaDoc r) throws HandlerException {
139         EntrySet c = getEntrySet(r);
140         if (c.isEmpty()) {
141             throw new BadRequestException("ERROR: No entries");
142         }
143         if (c.getEntries().length > 1) {
144             throw new BadRequestException("ERROR: Cannot put >1 entries per request");
145         }
146         
147         UserEntry entry = (UserEntry)c.getEntries()[0];
148         if (entry.getName() != null && !entry.getName().equals(getUri().getEntryId())) {
149             throw new BadRequestException("ERROR: Content name does not match URI name");
150         }
151         entry.setName(getUri().getEntryId());
152         c = updateUsers((UserEntrySet)c);
153         
154         return c;
155     }
156     
157     private UserEntrySet createUsers(UserEntrySet c) throws HandlerException {
158         try {
159             UserManager mgr = getRoller().getUserManager();
160             
161             List JavaDoc userDatas = new ArrayList JavaDoc();
162             for (int i = 0; i < c.getEntries().length; i++) {
163                 UserEntry entry = (UserEntry)c.getEntries()[i];
164                 if (entry.getDateCreated() == null) {
165                     // if no creation date supplied, add it
166
entry.setDateCreated(new Date JavaDoc());
167                 }
168                 UserData ud = toUserData(entry);
169                 mgr.addUser(ud);
170                 getRoller().flush();
171                 CacheManager.invalidate(ud);
172                 userDatas.add(ud);
173             }
174             return toUserEntrySet((UserData[])userDatas.toArray(new UserData[0]));
175         } catch (RollerException re) {
176             throw new InternalException("ERROR: Could not create users: " + c, re);
177         }
178     }
179     
180     private UserEntrySet updateUsers(UserEntrySet c) throws HandlerException {
181         try {
182             UserManager mgr = getRoller().getUserManager();
183             
184             List JavaDoc userDatas = new ArrayList JavaDoc();
185             for (int i = 0; i < c.getEntries().length; i++) {
186                 UserEntry entry = (UserEntry)c.getEntries()[i];
187                 UserData ud = mgr.getUserByUserName(entry.getName());
188                 if (ud == null) {
189                     throw new NotFoundException("ERROR: Unknown user: " + entry.getName());
190                 }
191                 updateUserData(ud, entry);
192                 userDatas.add(ud);
193             }
194             return toUserEntrySet((UserData[])userDatas.toArray(new UserData[0]));
195         } catch (RollerException re) {
196             throw new InternalException("ERROR: Could not update users: " + c, re);
197         }
198     }
199     
200     private void updateUserData(UserData ud, UserEntry entry) throws HandlerException {
201         // user name cannot be updated
202

203         if (entry.getFullName() != null) {
204             ud.setFullName(entry.getFullName());
205         }
206         if (entry.getPassword() != null) {
207             ud.setPassword(entry.getPassword());
208         }
209         if (entry.getLocale() != null) {
210             ud.setLocale(entry.getLocale().toString());
211         }
212         if (entry.getTimezone() != null) {
213             ud.setTimeZone(entry.getTimezone().getID());
214         }
215         if (entry.getEmailAddress() != null) {
216             ud.setEmailAddress(entry.getEmailAddress());
217         }
218         
219         try {
220             UserManager mgr = getRoller().getUserManager();
221             mgr.saveUser(ud);
222             getRoller().flush();
223             CacheManager.invalidate(ud);
224         } catch (RollerException re) {
225             throw new InternalException("ERROR: could not update user data", re);
226         }
227     }
228     
229     private EntrySet deleteEntry() throws HandlerException {
230         try {
231             UserManager mgr = getRoller().getUserManager();
232             UserData ud = mgr.getUserByUserName(getUri().getEntryId());
233             
234             if (ud == null) {
235                 throw new NotFoundException("ERROR: Unknown user: " + getUri().getEntryId());
236             }
237             // don't allow deletion of the currently authenticated user
238
if (ud.getUserName().equals(getUserName())) {
239                 throw new NotAllowedException("ERROR: Can't delete authenticated user: " + getUserName());
240             }
241             
242             UserData[] uds = new UserData[] { ud };
243             mgr.removeUser(ud);
244             getRoller().flush();
245             CacheManager.invalidate(ud);
246
247             EntrySet es = toUserEntrySet(uds);
248             return es;
249         } catch (RollerException re) {
250             throw new InternalException("ERROR: Could not delete entry: " + getUri().getEntryId(), re);
251         }
252     }
253     
254     private UserEntry toUserEntry(UserData ud) {
255         if (ud == null) {
256             throw new NullPointerException JavaDoc("ERROR: Null user data not allowed");
257         }
258         
259         // password field is not set
260
// we never return password field
261

262         UserEntry ue = new UserEntry(ud.getUserName(), getUrlPrefix());
263         ue.setFullName(ud.getFullName());
264         ue.setLocale(ud.getLocale());
265         ue.setTimezone(ud.getTimeZone());
266         ue.setEmailAddress(ud.getEmailAddress());
267         ue.setDateCreated(ud.getDateCreated());
268         
269         return ue;
270     }
271     
272     private UserEntrySet toUserEntrySet(UserData[] uds) {
273         if (uds == null) {
274             throw new NullPointerException JavaDoc("ERROR: Null user data not allowed");
275         }
276         UserEntrySet ues = new UserEntrySet(getUrlPrefix());
277         
278         List JavaDoc entries = new ArrayList JavaDoc();
279         for (int i = 0; i < uds.length; i++) {
280             UserData ud = uds[i];
281             Entry entry = toUserEntry(ud);
282             entries.add(entry);
283         }
284         ues.setEntries((Entry[])entries.toArray(new Entry[0]));
285         
286         return ues;
287     }
288     
289     /** This object, as a Roller UserData object. */
290     public UserData toUserData(UserEntry ue) {
291         if (ue == null) {
292             throw new NullPointerException JavaDoc("ERROR: Null user entry not allowed");
293         }
294         
295         //
296
// if any of the entry fields are null, the set below amounts
297
// to a no-op.
298
//
299
UserData ud = new UserData();
300         ud.setUserName(ue.getName());
301         ud.setFullName(ue.getFullName());
302         ud.setPassword(ue.getPassword());
303         ud.setEmailAddress(ue.getEmailAddress());
304         ud.setLocale(ue.getLocale().toString());
305         ud.setTimeZone(ue.getTimezone().getID());
306         ud.setDateCreated(ue.getDateCreated());
307         
308         return ud;
309     }
310 }
311
312
Popular Tags