001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: package org.apache.roller.webservices.adminapi;
019:
020: import java.io.IOException;
021: import java.io.Reader;
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.Date;
025: import javax.servlet.http.HttpServletRequest;
026: import org.jdom.Document;
027: import org.jdom.JDOMException;
028: import org.apache.roller.RollerException;
029: import org.apache.roller.business.UserManager;
030: import org.apache.roller.pojos.UserData;
031: import org.apache.roller.util.cache.CacheManager;
032: import org.apache.roller.webservices.adminapi.sdk.Entry;
033: import org.apache.roller.webservices.adminapi.sdk.EntrySet;
034: import org.apache.roller.webservices.adminapi.sdk.UnexpectedRootElementException;
035: import org.apache.roller.webservices.adminapi.sdk.UserEntry;
036: import org.apache.roller.webservices.adminapi.sdk.UserEntrySet;
037:
038: /**
039: * This class handles request concerning Roller users.
040: *
041: * @author jtb
042: */
043: class RollerUserHandler extends Handler {
044: public RollerUserHandler(HttpServletRequest request)
045: throws HandlerException {
046: super (request);
047: }
048:
049: protected EntrySet getEntrySet(Document d)
050: throws UnexpectedRootElementException {
051: return new UserEntrySet(d, getUrlPrefix());
052: }
053:
054: public EntrySet processGet() throws HandlerException {
055: if (getUri().isCollection()) {
056: return getCollection();
057: } else if (getUri().isEntry()) {
058: return getEntry();
059: } else {
060: throw new BadRequestException("ERROR: Unknown GET URI type");
061: }
062: }
063:
064: public EntrySet processPost(Reader r) throws HandlerException {
065: if (getUri().isCollection()) {
066: return postCollection(r);
067: } else {
068: throw new BadRequestException(
069: "ERROR: Unknown POST URI type");
070: }
071: }
072:
073: public EntrySet processPut(Reader r) throws HandlerException {
074: if (getUri().isCollection()) {
075: return putCollection(r);
076: } else if (getUri().isEntry()) {
077: return putEntry(r);
078: } else {
079: throw new BadRequestException("ERROR: Unknown PUT URI type");
080: }
081: }
082:
083: public EntrySet processDelete() throws HandlerException {
084: if (getUri().isEntry()) {
085: return deleteEntry();
086: } else {
087: throw new BadRequestException(
088: "ERROR: Unknown DELETE URI type");
089: }
090: }
091:
092: private EntrySet getCollection() throws HandlerException {
093: try {
094: List users = getRoller().getUserManager().getUsers(null,
095: null, null, 0, -1);
096: if (users == null) {
097: users = java.util.Collections.EMPTY_LIST;
098: }
099: EntrySet es = toUserEntrySet((UserData[]) users
100: .toArray(new UserData[0]));
101:
102: return es;
103: } catch (RollerException re) {
104: throw new InternalException(
105: "ERROR: Could not get user collection", re);
106: }
107: }
108:
109: private EntrySet getEntry() throws HandlerException {
110: UserData ud = getUserData(getUri().getEntryId());
111: UserData[] uds = new UserData[] { ud };
112:
113: EntrySet c = toUserEntrySet(uds);
114: return c;
115: }
116:
117: private EntrySet postCollection(Reader r) throws HandlerException {
118: EntrySet c = getEntrySet(r);
119: if (c.isEmpty()) {
120: throw new BadRequestException("ERROR: No entries");
121: }
122: c = createUsers((UserEntrySet) c);
123:
124: return c;
125: }
126:
127: private EntrySet putCollection(Reader r) throws HandlerException {
128: EntrySet c = getEntrySet(r);
129: if (c.isEmpty()) {
130: throw new BadRequestException("ERROR: No entries");
131: }
132: c = updateUsers((UserEntrySet) c);
133:
134: return c;
135: }
136:
137: private EntrySet putEntry(Reader r) throws HandlerException {
138: EntrySet c = getEntrySet(r);
139: if (c.isEmpty()) {
140: throw new BadRequestException("ERROR: No entries");
141: }
142: if (c.getEntries().length > 1) {
143: throw new BadRequestException(
144: "ERROR: Cannot put >1 entries per request");
145: }
146:
147: UserEntry entry = (UserEntry) c.getEntries()[0];
148: if (entry.getName() != null
149: && !entry.getName().equals(getUri().getEntryId())) {
150: throw new BadRequestException(
151: "ERROR: Content name does not match URI name");
152: }
153: entry.setName(getUri().getEntryId());
154: c = updateUsers((UserEntrySet) c);
155:
156: return c;
157: }
158:
159: private UserEntrySet createUsers(UserEntrySet c)
160: throws HandlerException {
161: try {
162: UserManager mgr = getRoller().getUserManager();
163:
164: List userDatas = new ArrayList();
165: for (int i = 0; i < c.getEntries().length; i++) {
166: UserEntry entry = (UserEntry) c.getEntries()[i];
167: if (entry.getDateCreated() == null) {
168: // if no creation date supplied, add it
169: entry.setDateCreated(new Date());
170: }
171: UserData ud = toUserData(entry);
172: mgr.addUser(ud);
173: getRoller().flush();
174: CacheManager.invalidate(ud);
175: userDatas.add(ud);
176: }
177: return toUserEntrySet((UserData[]) userDatas
178: .toArray(new UserData[0]));
179: } catch (RollerException re) {
180: throw new InternalException(
181: "ERROR: Could not create users: " + c, re);
182: }
183: }
184:
185: private UserEntrySet updateUsers(UserEntrySet c)
186: throws HandlerException {
187: List userDatas = new ArrayList();
188: for (int i = 0; i < c.getEntries().length; i++) {
189: UserEntry entry = (UserEntry) c.getEntries()[i];
190: UserData ud = getUserData(entry.getName());
191: updateUserData(ud, entry);
192: userDatas.add(ud);
193: }
194: return toUserEntrySet((UserData[]) userDatas
195: .toArray(new UserData[0]));
196: }
197:
198: private void updateUserData(UserData ud, UserEntry entry)
199: throws HandlerException {
200: // user name cannot be updated
201:
202: if (entry.getFullName() != null) {
203: ud.setFullName(entry.getFullName());
204: }
205: if (entry.getPassword() != null) {
206: ud.setPassword(entry.getPassword());
207: }
208: if (entry.getLocale() != null) {
209: ud.setLocale(entry.getLocale().toString());
210: }
211: if (entry.getTimezone() != null) {
212: ud.setTimeZone(entry.getTimezone().getID());
213: }
214: if (entry.getEmailAddress() != null) {
215: ud.setEmailAddress(entry.getEmailAddress());
216: }
217: if (entry.getEnabled() != null) {
218: ud.setEnabled(entry.getEnabled());
219: }
220:
221: try {
222: UserManager mgr = getRoller().getUserManager();
223: mgr.saveUser(ud);
224: getRoller().flush();
225: CacheManager.invalidate(ud);
226: } catch (RollerException re) {
227: throw new InternalException(
228: "ERROR: could not update user data", re);
229: }
230: }
231:
232: private EntrySet deleteEntry() throws HandlerException {
233: UserData ud = getUserData(getUri().getEntryId());
234:
235: // don't allow deletion of the currently authenticated user
236: if (ud.getUserName().equals(getUserName())) {
237: throw new NotAllowedException(
238: "ERROR: Can't delete authenticated user: "
239: + getUserName());
240: }
241:
242: UserData[] uds = new UserData[] { ud };
243:
244: try {
245: getRoller().getUserManager().removeUser(ud);
246: getRoller().flush();
247: CacheManager.invalidate(ud);
248: } catch (RollerException re) {
249: throw new InternalException(
250: "ERROR: could not delete user data", re);
251: }
252:
253: EntrySet es = toUserEntrySet(uds);
254: return es;
255: }
256:
257: private UserEntry toUserEntry(UserData ud) {
258: if (ud == null) {
259: throw new NullPointerException(
260: "ERROR: Null user data not allowed");
261: }
262:
263: // password field is not set
264: // we never return password field
265:
266: UserEntry ue = new UserEntry(ud.getUserName(), getUrlPrefix());
267: ue.setFullName(ud.getFullName());
268: ue.setLocale(ud.getLocale());
269: ue.setTimezone(ud.getTimeZone());
270: ue.setEmailAddress(ud.getEmailAddress());
271: ue.setDateCreated(ud.getDateCreated());
272: ue.setEnabled(ud.getEnabled());
273:
274: return ue;
275: }
276:
277: private UserEntrySet toUserEntrySet(UserData[] uds) {
278: if (uds == null) {
279: throw new NullPointerException(
280: "ERROR: Null user data not allowed");
281: }
282: UserEntrySet ues = new UserEntrySet(getUrlPrefix());
283:
284: List entries = new ArrayList();
285: for (int i = 0; i < uds.length; i++) {
286: UserData ud = uds[i];
287: Entry entry = toUserEntry(ud);
288: entries.add(entry);
289: }
290: ues.setEntries((Entry[]) entries.toArray(new Entry[0]));
291:
292: return ues;
293: }
294:
295: /** This object, as a Roller UserData object. */
296: public UserData toUserData(UserEntry ue) {
297: if (ue == null) {
298: throw new NullPointerException(
299: "ERROR: Null user entry not allowed");
300: }
301:
302: //
303: // if any of the entry fields are null, the set below amounts
304: // to a no-op.
305: //
306: UserData ud = new UserData();
307: ud.setUserName(ue.getName());
308:
309: if (ue.getFullName() != null) {
310: ud.setFullName(ue.getFullName());
311: }
312: if (ue.getPassword() != null) {
313: ud.setPassword(ue.getPassword());
314: }
315: if (ue.getEmailAddress() != null) {
316: ud.setEmailAddress(ue.getEmailAddress());
317: }
318: if (ue.getLocale() != null) {
319: ud.setLocale(ue.getLocale().toString());
320: }
321: if (ue.getTimezone() != null) {
322: ud.setTimeZone(ue.getTimezone().getID());
323: }
324: if (ue.getDateCreated() != null) {
325: ud.setDateCreated(ue.getDateCreated());
326: }
327: if (ue.getEnabled() != null) {
328: ud.setEnabled(ue.getEnabled());
329: }
330: return ud;
331: }
332: }
|