KickJava   Java API By Example, From Geeks To Geeks.

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


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.RepositoryException;
22 import org.outerj.daisy.repository.acl.AccessManager;
23 import org.outerj.daisy.repository.acl.Acl;
24 import org.outerj.daisy.repository.acl.AclResultInfo;
25 import org.outerj.daisy.httpconnector.RequestHandler;
26 import org.outerj.daisy.httpconnector.HttpUtil;
27 import org.outerj.daisy.httpconnector.BadRequestException;
28 import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
29 import org.outerx.daisy.x10.AclDocument;
30 import org.apache.xmlbeans.XmlOptions;
31
32 import java.util.Map JavaDoc;
33 import java.util.List JavaDoc;
34
35 public class AclHandler implements RequestHandler {
36     public String JavaDoc getPathPattern() {
37         return "/acl/*";
38     }
39
40     public void handleRequest(Map JavaDoc matchMap, HttpRequest request, HttpResponse response, Repository repository) throws Exception JavaDoc {
41         String JavaDoc aclName = (String JavaDoc)matchMap.get("1");
42
43         if (!aclName.equals("live") && !aclName.equals("staging")) {
44             response.sendError(HttpResponse.__404_Not_Found);
45             return;
46         }
47
48         AccessManager accessManager = repository.getAccessManager();
49
50         String JavaDoc action = request.getParameter("action");
51
52         if (action == null) {
53             if (request.getMethod().equals(HttpRequest.__GET)) {
54                 Acl acl = aclName.equals("live") ? accessManager.getLiveAcl() : accessManager.getStagingAcl();
55                 acl.getXml().save(response.getOutputStream());
56                 response.commit();
57                 return;
58             } else if (request.getMethod().equals(HttpRequest.__POST)) {
59                 // Note that the live ACL is not updateable, but that will give an exception anyway so we don't
60
// handle that case here explicitely
61
Acl acl = aclName.equals("live") ? accessManager.getLiveAcl() : accessManager.getStagingAcl();
62                 XmlOptions xmlOptions = new XmlOptions().setLoadUseXMLReader(LocalSAXParserFactory.newXmlReader());
63                 AclDocument aclDocument = AclDocument.Factory.parse(request.getInputStream(), xmlOptions);
64
65                 if (acl.getUpdateCount() != aclDocument.getAcl().getUpdateCount())
66                     throw new RepositoryException("The ACL was updated by someone else concurrently.");
67
68                 acl.setFromXml(aclDocument.getAcl());
69                 acl.save();
70
71                 // Possible optimalisation here: only send XML containing lastModified/lastModifier, since that's
72
// the only new information.
73
acl.getXml().save(response.getOutputStream());
74                 response.commit();
75                 return;
76             } else {
77                 response.sendError(HttpResponse.__405_Method_Not_Allowed);
78                 return;
79             }
80         } else if (action.equals("putLive") && aclName.equals("staging")) {
81             accessManager.copyStagingToLive();
82             response.commit();
83             return;
84         } else if (action.equals("revertChanges") && aclName.equals("staging")) {
85             accessManager.copyLiveToStaging();
86             response.commit();
87             return;
88         } else if (action.equals("evaluate")) {
89             long documentId = HttpUtil.getLongParam(request, "documentId");
90             long userId = HttpUtil.getLongParam(request, "user");
91             long[] roleIds = getLongParams(request, "role");
92             long branchId = HttpUtil.getBranchId(request, repository);
93             long languageId = HttpUtil.getLanguageId(request, repository);
94
95             AclResultInfo result;
96             if (aclName.equals("live"))
97                 result = accessManager.getAclInfoOnLive(userId, roleIds, documentId, branchId, languageId);
98             else
99                 result = accessManager.getAclInfoOnStaging(userId, roleIds, documentId, branchId, languageId);
100
101             result.getXml().save(response.getOutputStream());
102             response.commit();
103             return;
104         } else {
105             throw new BadRequestException("Unsupported value for action parameter: " + action);
106         }
107     }
108
109     private static long[] getLongParams(HttpRequest request, String JavaDoc name) throws Exception JavaDoc {
110         List JavaDoc values = request.getParameterValues(name);
111         if (values == null || values.size() == 0)
112             return new long[0];
113
114         long[] longValues = new long[values.size()];
115         for (int i = 0; i < values.size(); i++) {
116             try {
117                 longValues[i] = Long.parseLong((String JavaDoc)values.get(i));
118             } catch (NumberFormatException JavaDoc e) {
119                 throw new BadRequestException("The value of the request parameter \"" + name + "\" should be an integer value, got: " + values.get(i));
120             }
121         }
122
123         return longValues;
124     }
125
126 }
127
Popular Tags