001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.lenya.cms.ac;
020:
021: import java.io.IOException;
022: import java.net.MalformedURLException;
023: import java.util.HashSet;
024: import java.util.Set;
025:
026: import javax.xml.parsers.ParserConfigurationException;
027:
028: import org.apache.avalon.framework.logger.AbstractLogEnabled;
029: import org.apache.avalon.framework.service.ServiceException;
030: import org.apache.avalon.framework.service.ServiceManager;
031: import org.apache.avalon.framework.service.Serviceable;
032: import org.apache.excalibur.source.Source;
033: import org.apache.excalibur.source.SourceNotFoundException;
034: import org.apache.excalibur.source.SourceResolver;
035: import org.apache.lenya.ac.AccessControlException;
036: import org.apache.lenya.ac.Accreditable;
037: import org.apache.lenya.ac.AccreditableManager;
038: import org.apache.lenya.ac.Credential;
039: import org.apache.lenya.ac.Identity;
040: import org.apache.lenya.ac.Policy;
041: import org.apache.lenya.ac.PolicyManager;
042: import org.apache.lenya.ac.Role;
043: import org.apache.lenya.ac.impl.PolicyBuilder;
044: import org.apache.lenya.xml.DocumentHelper;
045: import org.w3c.dom.Document;
046: import org.xml.sax.SAXException;
047:
048: /**
049: * Policy manager based on Cocoon sitemaps.
050: * @version $Id: SitemapPolicyManager.java 473861 2006-11-12 03:51:14Z gregor $
051: */
052: public class SitemapPolicyManager extends AbstractLogEnabled implements
053: PolicyManager, Serviceable {
054:
055: private Credential[] credentials;
056:
057: /**
058: * @see org.apache.lenya.ac.PolicyManager#getPolicy(org.apache.lenya.ac.AccreditableManager,
059: * java.lang.String)
060: */
061: public Policy getPolicy(AccreditableManager accreditableManager,
062: String url) throws AccessControlException {
063:
064: url = url.substring(1);
065:
066: int slashIndex = url.indexOf("/");
067: if (slashIndex == -1) {
068: slashIndex = url.length();
069: }
070:
071: String publicationId = url.substring(0, slashIndex);
072: url = url.substring(publicationId.length());
073:
074: SourceResolver resolver = null;
075: Policy policy = null;
076: Source source = null;
077: try {
078: resolver = (SourceResolver) getManager().lookup(
079: SourceResolver.ROLE);
080:
081: String policyUrl = publicationId + "/policies" + url
082: + ".acml";
083: getLogger().debug("Policy URL: " + policyUrl);
084: source = resolver.resolveURI("cocoon://" + policyUrl);
085: Document document = DocumentHelper.readDocument(source
086: .getInputStream());
087: policy = new PolicyBuilder(accreditableManager)
088: .buildPolicy(document);
089: this .credentials = policy.getCredentials();
090: } catch (SourceNotFoundException e) {
091: throw new AccessControlException(e);
092: } catch (ServiceException e) {
093: throw new AccessControlException(e);
094: } catch (MalformedURLException e) {
095: throw new AccessControlException(e);
096: } catch (IOException e) {
097: throw new AccessControlException(e);
098: } catch (ParserConfigurationException e) {
099: throw new AccessControlException(e);
100: } catch (SAXException e) {
101: throw new AccessControlException(e);
102: } catch (AccessControlException e) {
103: throw new AccessControlException(e);
104: } finally {
105: if (resolver != null) {
106: if (source != null) {
107: resolver.release(source);
108: }
109: getManager().release(resolver);
110: }
111: }
112:
113: return policy;
114: }
115:
116: private ServiceManager manager;
117:
118: /**
119: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
120: */
121: public void service(ServiceManager _manager)
122: throws ServiceException {
123: this .manager = _manager;
124: }
125:
126: /**
127: * Returns the service manager.
128: * @return A service manager.
129: */
130: public ServiceManager getManager() {
131: return this .manager;
132: }
133:
134: /**
135: * @see org.apache.lenya.ac.PolicyManager#accreditableRemoved(org.apache.lenya.ac.AccreditableManager,
136: * org.apache.lenya.ac.Accreditable)
137: */
138: public void accreditableRemoved(AccreditableManager _manager,
139: Accreditable accreditable) throws AccessControlException {
140: // do nothing
141: }
142:
143: /**
144: * @see org.apache.lenya.ac.PolicyManager#accreditableAdded(org.apache.lenya.ac.AccreditableManager,
145: * org.apache.lenya.ac.Accreditable)
146: */
147: public void accreditableAdded(AccreditableManager _manager,
148: Accreditable accreditable) throws AccessControlException {
149: // do nothing
150: }
151:
152: public Credential[] getCredentials(AccreditableManager controller,
153: String url) throws AccessControlException {
154: Credential[] copy = new Credential[credentials.length];
155: for (int i = 0; i < credentials.length; i++) {
156: copy[i] = credentials[i];
157: }
158: return copy;
159: }
160:
161: public Role[] getGrantedRoles(
162: AccreditableManager accreditableManager, Identity identity,
163: String url) throws AccessControlException {
164: Role[] roles = accreditableManager.getRoleManager().getRoles();
165: Set grantedRoles = new HashSet();
166: Policy policy = getPolicy(accreditableManager, url);
167: for (int i = 0; i < roles.length; i++) {
168: if (policy.check(identity, roles[i]) == Policy.RESULT_GRANTED) {
169: grantedRoles.add(roles[i]);
170: }
171: }
172: return (Role[]) grantedRoles.toArray(new Role[grantedRoles
173: .size()]);
174: }
175:
176: }
|