KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > httpconnector > handlers > UsersHandler


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */

16 package org.outerj.daisy.httpconnector.handlers;
17
18 import org.mortbay.http.HttpRequest;
19 import org.mortbay.http.HttpResponse;
20 import org.outerj.daisy.repository.Repository;
21 import org.outerj.daisy.repository.commonimpl.user.RolesUtil;
22 import org.outerj.daisy.repository.user.UserManager;
23 import org.outerj.daisy.repository.user.Users;
24 import org.outerj.daisy.repository.user.User;
25 import org.outerj.daisy.repository.user.Role;
26 import org.outerj.daisy.httpconnector.RequestHandler;
27 import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
28 import org.outerx.daisy.x10.UserDocument;
29 import org.outerx.daisy.x10.RoleDocument;
30 import org.outerx.daisy.x10.RolesDocument;
31 import org.apache.xmlbeans.XmlOptions;
32
33 import java.util.Map JavaDoc;
34
35 public class UsersHandler implements RequestHandler {
36     public String JavaDoc getPathPattern() {
37         return "/user";
38     }
39
40     public void handleRequest(Map JavaDoc matchMap, HttpRequest request, HttpResponse response, Repository repository) throws Exception JavaDoc {
41         UserManager userMan = repository.getUserManager();
42
43         if (request.getMethod().equals(HttpRequest.__GET)) {
44             Users users = userMan.getUsers();
45             users.getXml().save(response.getOutputStream());
46             response.commit();
47         } else if (request.getMethod().equals(HttpRequest.__POST)) {
48             // on the other hand, POST creates a new User
49
// and returns the XML representation of the newly created User
50
XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader());
51             UserDocument userDocument = UserDocument.Factory.parse(request.getInputStream(), xmlOptions);
52             UserDocument.User userXml = userDocument.getUser();
53
54             User user = userMan.createUser(userXml.getLogin());
55
56             user.setEmail(userXml.getEmail());
57             user.setFirstName(userXml.getFirstName());
58             user.setLastName(userXml.getLastName());
59             user.setPassword(userXml.getPassword());
60             user.setUpdateableByUser(userXml.getUpdateableByUser());
61
62             RolesDocument.Roles roles = userXml.getRoles();
63             RoleDocument.Role[] rolesArr = roles.getRoleArray();
64
65             for (int i = 0; i < rolesArr.length; i++) {
66                 RoleDocument.Role role = rolesArr[i];
67                 Role r = userMan.getRole(role.getId(), false);
68                 user.addToRole(r);
69             }
70
71             RoleDocument.Role defaultRoleXml = userXml.getRole();
72             if (defaultRoleXml != null) {
73                 Role defaultRole = RolesUtil.getRole(user.getAllRoles(), defaultRoleXml.getName());
74                 user.setDefaultRole(defaultRole);
75             } else {
76                 user.setDefaultRole(null);
77             }
78             user.setConfirmed(userXml.getConfirmed());
79             user.setConfirmKey(userXml.getConfirmKey());
80             user.setAuthenticationScheme(userXml.getAuthenticationScheme());
81
82             user.save();
83
84             user.getXml().save(response.getOutputStream());
85             response.commit();
86             return;
87         } else {
88             response.sendError(HttpResponse.__405_Method_Not_Allowed);
89             return;
90         }
91     }
92 }
93
Popular Tags