KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > ac > impl > URLPolicy


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
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  */

17
18 /* $Id: URLPolicy.java 160351 2005-04-07 00:28:28Z gregor $ */
19
20 package org.apache.lenya.ac.impl;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.apache.lenya.ac.AccessControlException;
27 import org.apache.lenya.ac.AccreditableManager;
28 import org.apache.lenya.ac.Identity;
29 import org.apache.lenya.ac.Policy;
30 import org.apache.lenya.ac.Role;
31
32 /**
33  * A policy at a certain URL. The final policy is computed by merging the subtree
34  * policies of all ancestor-or-self directories with the URL policy of the actual URL.
35  */

36 public class URLPolicy implements Policy {
37
38     /**
39      * Returns the resulting policy for a certain URL.
40      * @param controller The acccess controller.
41      * @param url The URL.
42      * @param manager The policy manager.
43      */

44     public URLPolicy(AccreditableManager controller, String JavaDoc url, InheritingPolicyManager manager) {
45         assert url != null;
46         this.url = url;
47
48         assert manager != null;
49         policyManager = manager;
50
51         assert controller != null;
52         this.accreditableManager = controller;
53     }
54
55     private String JavaDoc url;
56     private InheritingPolicyManager policyManager;
57     private AccreditableManager accreditableManager;
58     private Policy[] policies = null;
59
60     /**
61      * Obtains the policies from the policy manager.
62      * This method is expensive and therefore only called when needed.
63      * @throws AccessControlException when something went wrong.
64      */

65     protected void obtainPolicies() throws AccessControlException {
66         if (policies == null) {
67             policies = getPolicyManager().getPolicies(getAccreditableManager(), getUrl());
68         }
69     }
70
71     static final String JavaDoc[] VISITOR_ROLES = { "visitor", "visit" };
72     static final String JavaDoc[] ADMINISTRATOR_ROLES = { "administrator", "admin", "organize" };
73     static final String JavaDoc[] AUTHOR_ROLES = { "author", "edit" };
74
75     /**
76      * @see org.apache.lenya.ac.Policy#getRoles(org.apache.lenya.ac.Identity)
77      */

78     public Role[] getRoles(Identity identity) throws AccessControlException {
79         obtainPolicies();
80         Set JavaDoc roles = new HashSet JavaDoc();
81
82         // no policies defined: return "visit" or "visitor" role
83
if (isEmpty()) {
84             Role visitorRole = getVisitorRole(getAccreditableManager());
85             if (visitorRole != null) {
86                 roles.add(visitorRole);
87             }
88         } else {
89             for (int i = 0; i < policies.length; i++) {
90                 addRoles(policies[i], identity, roles);
91             }
92         }
93         return (Role[]) roles.toArray(new Role[roles.size()]);
94     }
95
96     /**
97      * Returns the visitor role.
98      * @param manager The accreditable manager.
99      * @return A role.
100      * @throws AccessControlException when something went wrong.
101      */

102     public static Role getVisitorRole(AccreditableManager manager) throws AccessControlException {
103         Role visitorRole = null;
104         for (int i = 0; i < VISITOR_ROLES.length; i++) {
105             Role role = manager.getRoleManager().getRole(VISITOR_ROLES[i]);
106             if (role != null) {
107                 visitorRole = role;
108             }
109         }
110         return visitorRole;
111     }
112
113     /**
114      * Returns the administrator role.
115      * @param manager The accreditable manager.
116      * @return A role.
117      * @throws AccessControlException when something went wrong.
118      */

119     public static Role getAdministratorRole(AccreditableManager manager) throws AccessControlException {
120         Role administratorRole = null;
121         for (int i = 0; i < ADMINISTRATOR_ROLES.length; i++) {
122             Role role = manager.getRoleManager().getRole(ADMINISTRATOR_ROLES[i]);
123             if (role != null) {
124                 administratorRole = role;
125             }
126         }
127         return administratorRole;
128     }
129
130     /**
131      * Returns the author role.
132      * @param manager The accreditable manager.
133      * @return A role.
134      * @throws AccessControlException when something went wrong.
135      */

136     public static Role getAuthorRole(AccreditableManager manager) throws AccessControlException {
137         Role administratorRole = null;
138         for (int i = 0; i < AUTHOR_ROLES.length; i++) {
139             Role role = manager.getRoleManager().getRole(AUTHOR_ROLES[i]);
140             if (role != null) {
141                 administratorRole = role;
142             }
143         }
144         return administratorRole;
145     }
146
147     /**
148      * Adds the roles of an identity of a policy to a role set.
149      * @param policy The policy.
150      * @param identity The identity.
151      * @param roles The role set.
152      * @throws AccessControlException when something went wrong.
153      */

154     protected void addRoles(Policy policy, Identity identity, Set JavaDoc roles)
155         throws AccessControlException {
156         roles.addAll(Arrays.asList(policy.getRoles(identity)));
157     }
158
159     /**
160      * Returns the URL of this policy.
161      * @return The URL of this policy.
162      */

163     public String JavaDoc getUrl() {
164         return url;
165     }
166
167     /**
168      * Returns the policy builder.
169      * @return A policy builder.
170      */

171     public InheritingPolicyManager getPolicyManager() {
172         return policyManager;
173     }
174
175     /**
176      * Returns the access controller.
177      * @return An access controller.
178      */

179     public AccreditableManager getAccreditableManager() {
180         return accreditableManager;
181     }
182
183     /**
184      * The URL policy requires SSL protection if one of its
185      * member policies requires SSL protection.
186      * @see org.apache.lenya.ac.Policy#isSSLProtected()
187      */

188     public boolean isSSLProtected() throws AccessControlException {
189         obtainPolicies();
190
191         boolean ssl = false;
192
193         int i = 0;
194         while (!ssl && i < policies.length) {
195             ssl = ssl || policies[i].isSSLProtected();
196             i++;
197         }
198
199         return ssl;
200     }
201
202     /**
203      * @see org.apache.lenya.ac.Policy#isEmpty()
204      */

205     public boolean isEmpty() throws AccessControlException {
206         boolean empty = true;
207
208         int i = 0;
209         while (empty && i < policies.length) {
210             empty = empty && policies[i].isEmpty();
211             i++;
212         }
213
214         return empty;
215     }
216
217 }
218
Popular Tags