KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > admin > ManageAclApple


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.admin;
17
18 import org.apache.avalon.framework.service.Serviceable;
19 import org.apache.avalon.framework.service.ServiceManager;
20 import org.apache.avalon.framework.service.ServiceException;
21 import org.apache.cocoon.environment.Request;
22 import org.apache.cocoon.components.flow.apples.AppleRequest;
23 import org.apache.cocoon.components.flow.apples.AppleResponse;
24 import org.apache.cocoon.components.flow.apples.StatelessAppleController;
25 import org.apache.xmlbeans.XmlCursor;
26 import org.outerj.daisy.repository.Repository;
27 import org.outerj.daisy.repository.user.UserManager;
28 import org.outerj.daisy.repository.acl.AccessManager;
29 import org.outerj.daisy.frontend.util.XmlObjectXMLizable;
30 import org.outerj.daisy.frontend.util.AbstractDaisyApple;
31 import org.outerj.daisy.frontend.PageContext;
32 import org.outerj.daisy.frontend.SkinConfHelper;
33 import org.outerj.daisy.frontend.WikiHelper;
34 import org.outerx.daisy.x10.AclDocument;
35 import org.outerx.daisy.x10.AclObjectDocument;
36 import org.outerx.daisy.x10.AclEntryDocument;
37
38 import java.util.Map JavaDoc;
39 import java.util.HashMap JavaDoc;
40
41 public class ManageAclApple extends AbstractDaisyApple implements StatelessAppleController, Serviceable {
42     private ServiceManager serviceManager;
43
44     public void service(ServiceManager serviceManager) throws ServiceException {
45         this.serviceManager = serviceManager;
46     }
47
48     protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
49         String JavaDoc aclName = appleRequest.getSitemapParameter("name");
50
51         if (!"live".equals(aclName) && !"staging".equals(aclName))
52             throw new org.apache.cocoon.ResourceNotFoundException("ACL does not exist: " + aclName);
53
54         Request request = appleRequest.getCocoonRequest();
55         Repository repository = WikiHelper.getRepository(request, serviceManager);
56         AccessManager accessManager = repository.getAccessManager();
57
58         String JavaDoc action = request.getParameter("action");
59         if (action == null) {
60             // show the ACL
61
AclDocument aclXml;
62             if (aclName.equals("live"))
63                 aclXml = accessManager.getLiveAcl().getXml();
64             else
65                 aclXml = accessManager.getStagingAcl().getXml();
66
67             annotateAcl(aclXml, repository);
68
69             Map JavaDoc viewData = new HashMap JavaDoc();
70             viewData.put("pageXml", new XmlObjectXMLizable(aclXml));
71             viewData.put("pageContext", new PageContext(getMountPoint(), repository, getLayoutType(), getSkin(), SkinConfHelper.getGlobalSkinConf(serviceManager), getContext()));
72
73             appleResponse.sendPage("ShowAclPipe", viewData);
74         } else if (action.equals("putLive") && aclName.equals("staging")) {
75             if (isConfirmed(request)) {
76                 accessManager.copyStagingToLive();
77
78                 Map JavaDoc viewData = new HashMap JavaDoc();
79                 viewData.put("title", "Done");
80                 viewData.put("message", "The staging ACL has been put live.");
81                 viewData.put("linkTitle", "Administration Home");
82                 viewData.put("link", getMountPoint() + "/admin");
83                 viewData.put("pageContext", new PageContext(getMountPoint(), repository, getLayoutType(), getSkin(), SkinConfHelper.getGlobalSkinConf(serviceManager), getContext()));
84
85                 appleResponse.sendPage("MessagePagePipe", viewData);
86             } else {
87                 Map JavaDoc viewData = new HashMap JavaDoc();
88                 viewData.put("title", "Are you sure?");
89                 viewData.put("message", "Are you sure you want to put the current staging ACL live?");
90                 viewData.put("confirmURL", getMountPoint() + "/admin/acl/staging?action=putLive&confirmed=true");
91                 viewData.put("cancelURL", getMountPoint() + "/admin");
92                 viewData.put("pageContext", new PageContext(getMountPoint(), repository, getLayoutType(), getSkin(), SkinConfHelper.getGlobalSkinConf(serviceManager), getContext()));
93                 appleResponse.sendPage("ConfirmationPagePipe", viewData);
94             }
95         } else if (action.equals("revertChanges") && aclName.equals("staging")) {
96             if (isConfirmed(request)) {
97                 accessManager.copyLiveToStaging();
98
99                 Map JavaDoc viewData = new HashMap JavaDoc();
100                 viewData.put("title", "Done");
101                 viewData.put("message", "The staging ACL has been overwritten with the contents of the live ACL.");
102                 viewData.put("linkTitle", "Administration Home");
103                 viewData.put("link", getMountPoint() + "/admin");
104                 viewData.put("pageContext", new PageContext(getMountPoint(), repository, getLayoutType(), getSkin(), SkinConfHelper.getGlobalSkinConf(serviceManager), getContext()));
105
106                 appleResponse.sendPage("MessagePagePipe", viewData);
107             } else {
108                 Map JavaDoc viewData = new HashMap JavaDoc();
109                 viewData.put("title", "Are you sure?");
110                 viewData.put("message", "Are you sure you want to overwrite the staging ACL with the live ACL?");
111                 viewData.put("confirmURL", getMountPoint() + "/admin/acl/staging?action=revertChanges&confirmed=true");
112                 viewData.put("cancelURL", getMountPoint() + "/admin");
113                 viewData.put("pageContext", new PageContext(getMountPoint(), repository, getLayoutType(), getSkin(), SkinConfHelper.getGlobalSkinConf(serviceManager), getContext()));
114                 appleResponse.sendPage("ConfirmationPagePipe", viewData);
115             }
116         } else {
117             throw new java.lang.Exception JavaDoc("Illegal request with action parameter = " + action);
118         }
119     }
120
121     private boolean isConfirmed(Request request) {
122         String JavaDoc confirmed = request.getParameter("confirmed");
123         return confirmed != null && confirmed.equals("true");
124     }
125
126     private void annotateAcl(AclDocument aclDocument, Repository repository) {
127         UserManager userManager = repository.getUserManager();
128         AclObjectDocument.AclObject[] aclObjects = aclDocument.getAcl().getAclObjectArray();
129         for (int i = 0; i < aclObjects.length; i++) {
130             AclEntryDocument.AclEntry[] entries = aclObjects[i].getAclEntryArray();
131             for (int k = 0; k < entries.length; k++) {
132                 AclEntryDocument.AclEntry entry = entries[k];
133                 String JavaDoc subjectType = entry.getSubjectType().toString();
134                 long subjectValue = entry.getSubjectValue();
135                 String JavaDoc label = "";
136                 if (subjectType.equals("role")) {
137                     try {
138                         label = userManager.getRoleDisplayName(subjectValue);
139                     } catch (Exception JavaDoc e) {
140                         label = "(error)";
141                     }
142                 } else if (subjectType.equals("user")) {
143                     try {
144                         label = userManager.getUserDisplayName(subjectValue);
145                     } catch (Exception JavaDoc e) {
146                         label = "(error)";
147                     }
148                 }
149
150                 XmlCursor cursor = entry.newCursor();
151                 cursor.toNextToken();
152                 cursor.insertAttributeWithValue("subjectValueLabel", label);
153                 cursor.dispose();
154             }
155         }
156     }
157 }
158
Popular Tags