01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.
15: */
16: package org.outerj.daisy.httpconnector.handlers;
17:
18: import org.outerj.daisy.repository.Repository;
19: import org.outerj.daisy.repository.commonimpl.user.RolesUtil;
20: import org.outerj.daisy.repository.user.UserManager;
21: import org.outerj.daisy.repository.user.Users;
22: import org.outerj.daisy.repository.user.User;
23: import org.outerj.daisy.repository.user.Role;
24: import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
25: import org.outerj.daisy.util.HttpConstants;
26: import org.outerj.daisy.httpconnector.spi.RequestHandlerSupport;
27: import org.outerx.daisy.x10.UserDocument;
28: import org.outerx.daisy.x10.RoleDocument;
29: import org.outerx.daisy.x10.RolesDocument;
30: import org.apache.xmlbeans.XmlOptions;
31:
32: import javax.servlet.http.HttpServletRequest;
33: import javax.servlet.http.HttpServletResponse;
34: import java.util.Map;
35: import java.util.List;
36:
37: public class UsersHandler extends AbstractRepositoryRequestHandler {
38: public String getPathPattern() {
39: return "/user";
40: }
41:
42: public void handleRequest(Map matchMap, HttpServletRequest request,
43: HttpServletResponse response, Repository repository,
44: RequestHandlerSupport support) throws Exception {
45: UserManager userMan = repository.getUserManager();
46:
47: if (request.getMethod().equals(HttpConstants.GET)) {
48: Users users = userMan.getUsers();
49: users.getXml().save(response.getOutputStream());
50: } else if (request.getMethod().equals(HttpConstants.POST)) {
51: // on the other hand, POST creates a new User
52: // and returns the XML representation of the newly created User
53: XmlOptions xmlOptions = new XmlOptions()
54: .setLoadUseXMLReader(LocalSAXParserFactory
55: .newXmlReader());
56: UserDocument userDocument = UserDocument.Factory.parse(
57: request.getInputStream(), xmlOptions);
58: UserDocument.User userXml = userDocument.getUser();
59:
60: User user = userMan.createUser(userXml.getLogin());
61:
62: user.setEmail(userXml.getEmail());
63: user.setFirstName(userXml.getFirstName());
64: user.setLastName(userXml.getLastName());
65: user.setPassword(userXml.getPassword());
66: user.setUpdateableByUser(userXml.getUpdateableByUser());
67:
68: RolesDocument.Roles roles = userXml.getRoles();
69: List<RoleDocument.Role> rolesArr = roles.getRoleList();
70:
71: for (RoleDocument.Role role : rolesArr) {
72: Role r = userMan.getRole(role.getId(), false);
73: user.addToRole(r);
74: }
75:
76: RoleDocument.Role defaultRoleXml = userXml.getRole();
77: if (defaultRoleXml != null) {
78: Role defaultRole = RolesUtil.getRole(
79: user.getAllRoles(), defaultRoleXml.getName());
80: user.setDefaultRole(defaultRole);
81: } else {
82: user.setDefaultRole(null);
83: }
84: user.setConfirmed(userXml.getConfirmed());
85: user.setConfirmKey(userXml.getConfirmKey());
86: user.setAuthenticationScheme(userXml
87: .getAuthenticationScheme());
88:
89: user.save();
90:
91: user.getXml().save(response.getOutputStream());
92: } else {
93: response.sendError(HttpConstants._405_Method_Not_Allowed);
94: }
95: }
96: }
|