KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > LoginApple


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.frontend;
17
18 import org.outerj.daisy.frontend.util.AbstractDaisyApple;
19 import org.outerj.daisy.frontend.util.HttpMethodNotAllowedException;
20 import org.outerj.daisy.repository.Repository;
21 import org.outerj.daisy.repository.user.Role;
22 import org.apache.cocoon.components.flow.apples.AppleRequest;
23 import org.apache.cocoon.components.flow.apples.AppleResponse;
24 import org.apache.cocoon.environment.Request;
25 import org.apache.avalon.framework.service.ServiceManager;
26 import org.apache.avalon.framework.service.Serviceable;
27 import org.apache.avalon.framework.service.ServiceException;
28
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 public class LoginApple extends AbstractDaisyApple implements Serviceable {
33     private ServiceManager serviceManager;
34     private boolean init = false;
35     private String JavaDoc returnTo;
36     private Map JavaDoc viewData;
37     private Repository repository;
38
39     public void service(ServiceManager serviceManager) throws ServiceException {
40         this.serviceManager = serviceManager;
41     }
42
43     protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
44         Request request = appleRequest.getCocoonRequest();
45
46         if (!init) {
47             returnTo = request.getParameter("returnTo");
48
49             repository = WikiHelper.getRepository(request, serviceManager);
50
51             viewData = new HashMap JavaDoc();
52             viewData.put("pageContext", new PageContext(getMountPoint(), repository, getLayoutType(), getSkin(), SkinConfHelper.getGlobalSkinConf(serviceManager), getContext()));
53
54             init = true;
55         }
56
57         if (request.getMethod().equals("GET")) {
58             appleResponse.sendPage("LoginPipe", viewData);
59         } else if (request.getMethod().equals("POST")) {
60             String JavaDoc action = request.getParameter("action");
61             if (action == null) {
62                 throw new Exception JavaDoc("Missing action request parameter.");
63             } else if (action.equals("changeRole")) {
64                 String JavaDoc newRoleParam = request.getParameter("newrole");
65                 long[] newActiveRoles;
66                 if (newRoleParam.equals("all")) {
67                     // create a list of all roles the user has, without the admin role
68
long[] allRoles = repository.getAvailableRoles();
69                     boolean hasAdminRole = false;
70                     for (int i = 0; i < allRoles.length; i++) {
71                         if (allRoles[i] == Role.ADMINISTRATOR) {
72                             hasAdminRole = true;
73                             break;
74                         }
75                     }
76                     if (hasAdminRole) {
77                         newActiveRoles = new long[allRoles.length - 1];
78                         int p = 0;
79                         for (int i = 0; i < allRoles.length; i++) {
80                             if (allRoles[i] != Role.ADMINISTRATOR) {
81                                 newActiveRoles[p] = allRoles[i];
82                                 p++;
83                             }
84                         }
85                     } else {
86                         newActiveRoles = allRoles;
87                     }
88                 } else {
89                     long newRole = Long.parseLong(newRoleParam);
90                     newActiveRoles = new long[] {newRole};
91                 }
92                 repository.setActiveRoleIds(newActiveRoles);
93             } else if (action.equals("login")) {
94                 String JavaDoc login = request.getParameter("username");
95                 String JavaDoc password = request.getParameter("password");
96                 WikiHelper.login(login, password, request, serviceManager);
97             } else {
98                 throw new Exception JavaDoc("Unexpected action parameter value: " + action);
99             }
100
101             if (returnTo == null || returnTo.equals(""))
102                 appleResponse.redirectTo(getMountPoint() + "/");
103             else
104                 appleResponse.redirectTo(returnTo);
105         } else {
106             throw new HttpMethodNotAllowedException(request.getMethod());
107         }
108     }
109 }
110
Popular Tags