KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.lenya.ac.impl;
19
20 import java.io.InputStream JavaDoc;
21
22 import javax.xml.parsers.ParserConfigurationException JavaDoc;
23
24 import org.apache.lenya.ac.AccessControlException;
25 import org.apache.lenya.ac.AccessController;
26 import org.apache.lenya.ac.Accreditable;
27 import org.apache.lenya.ac.AccreditableManager;
28 import org.apache.lenya.ac.Role;
29 import org.apache.lenya.ac.User;
30 import org.apache.lenya.ac.World;
31 import org.apache.lenya.ac.cache.BuildException;
32 import org.apache.lenya.ac.cache.InputStreamBuilder;
33 import org.apache.lenya.xml.DocumentHelper;
34 import org.apache.lenya.xml.NamespaceHelper;
35 import org.w3c.dom.Document JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37
38 /**
39  * Builds policies from input streams.
40  * @version $Id: PolicyBuilder.java 43241 2004-08-16 16:36:57Z andreas $
41  */

42 public class PolicyBuilder implements InputStreamBuilder {
43
44     /**
45      * Ctor.
46      * @param accreditableManager An accreditable manager.
47      */

48     public PolicyBuilder(AccreditableManager accreditableManager) {
49         assert accreditableManager != null;
50         this.accreditableManager = accreditableManager;
51     }
52     
53     /**
54      * Returns the accreditable manager.
55      * @return An accreditable manager.
56      */

57     public AccreditableManager getAccreditableManager() {
58         return accreditableManager;
59     }
60
61     private AccreditableManager accreditableManager;
62
63     protected static final String JavaDoc POLICY_ELEMENT = "policy";
64     protected static final String JavaDoc GROUP_ELEMENT = "group";
65     protected static final String JavaDoc USER_ELEMENT = "user";
66     protected static final String JavaDoc ROLE_ELEMENT = "role";
67     protected static final String JavaDoc WORLD_ELEMENT = "world";
68     protected static final String JavaDoc IP_RANGE_ELEMENT = "ip-range";
69     protected static final String JavaDoc ID_ATTRIBUTE = "id";
70     protected static final String JavaDoc SSL_ATTRIBUTE = "ssl";
71     
72     /**
73      * Builds a policy from an input stream.
74      * @param stream The input stream to read the policy from.
75      * @return A policy.
76      * @throws AccessControlException when something went wrong.
77      */

78     public DefaultPolicy buildPolicy(InputStream JavaDoc stream)
79         throws AccessControlException {
80
81         Document JavaDoc document;
82
83         try {
84             document = DocumentHelper.readDocument(stream);
85         } catch (Exception JavaDoc e) {
86             throw new AccessControlException(e);
87         }
88
89         return buildPolicy(document);
90     }
91
92     /**
93      * Builds a policy from an XML document.
94      * @param document The XML document.
95      * @return A policy.
96      * @throws AccessControlException when something went wrong.
97      */

98     public DefaultPolicy buildPolicy(Document JavaDoc document)
99         throws AccessControlException {
100
101         DefaultPolicy policy = new DefaultPolicy();
102         Element JavaDoc policyElement = document.getDocumentElement();
103         assert policyElement.getLocalName().equals(POLICY_ELEMENT);
104
105         NamespaceHelper helper =
106             new NamespaceHelper(
107                 AccessController.NAMESPACE,
108                 AccessController.DEFAULT_PREFIX,
109                 document);
110
111         Element JavaDoc[] credentialElements = helper.getChildren(policyElement);
112
113         for (int i = 0; i < credentialElements.length; i++) {
114             Accreditable accreditable = null;
115
116             String JavaDoc id = credentialElements[i].getAttribute(ID_ATTRIBUTE);
117             accreditable = getAccreditable(credentialElements[i].getLocalName(), id);
118
119             Credential credential = new Credential(accreditable);
120
121             Element JavaDoc[] roleElements = helper.getChildren(credentialElements[i], ROLE_ELEMENT);
122
123             for (int j = 0; j < roleElements.length; j++) {
124                 String JavaDoc roleId = roleElements[j].getAttribute(ID_ATTRIBUTE);
125                 Role role = getAccreditableManager().getRoleManager().getRole(roleId);
126                 credential.addRole(role);
127             }
128
129             policy.addCredential(credential);
130         }
131         
132         boolean ssl = false;
133         String JavaDoc sslString = policyElement.getAttribute(SSL_ATTRIBUTE);
134         if (sslString != null) {
135             ssl = Boolean.valueOf(sslString).booleanValue();
136         }
137         policy.setSSL(ssl);
138
139         return policy;
140     }
141
142     /**
143      * Creates an accredtiable for an element.
144      * @param elementName The elment name.
145      * @param id The ID of the accreditable.
146      * @return An accreditable.
147      * @throws AccessControlException when something went wrong.
148      */

149     protected Accreditable getAccreditable(
150         String JavaDoc elementName,
151         String JavaDoc id)
152         throws AccessControlException {
153         Accreditable accreditable = null;
154
155         if (elementName.equals(USER_ELEMENT)) {
156             accreditable = getAccreditableManager().getUserManager().getUser(id);
157         } else if (elementName.equals(GROUP_ELEMENT)) {
158             accreditable = getAccreditableManager().getGroupManager().getGroup(id);
159         } else if (elementName.equals(WORLD_ELEMENT)) {
160             accreditable = World.getInstance();
161         } else if (elementName.equals(IP_RANGE_ELEMENT)) {
162             accreditable = getAccreditableManager().getIPRangeManager().getIPRange(id);
163         }
164
165         if (accreditable == null) {
166             throw new AccessControlException(
167                 "Unknown accreditable [" + elementName + "] with ID [" + id + "]");
168         }
169
170         return accreditable;
171     }
172
173     /**
174      * Saves a policy to an XML document.
175      * @param policy The policy to save.
176      * @return A DOM document.
177      * @throws AccessControlException when something went wrong.
178      */

179     public static Document JavaDoc savePolicy(DefaultPolicy policy) throws AccessControlException {
180         NamespaceHelper helper;
181
182         try {
183             helper =
184                 new NamespaceHelper(
185                     AccessController.NAMESPACE,
186                     AccessController.DEFAULT_PREFIX,
187                     POLICY_ELEMENT);
188         } catch (ParserConfigurationException JavaDoc e) {
189             throw new AccessControlException(e);
190         }
191
192         Credential[] credentials = policy.getCredentials();
193         Element JavaDoc policyElement = helper.getDocument().getDocumentElement();
194
195         for (int i = 0; i < credentials.length; i++) {
196             Accreditable accreditable = credentials[i].getAccreditable();
197             Element JavaDoc accreditableElement = save(accreditable, helper);
198             
199             Role[] roles = credentials[i].getRoles();
200             for (int j = 0; j < roles.length; j++) {
201                 Element JavaDoc roleElement = helper.createElement(ROLE_ELEMENT);
202                 roleElement.setAttribute(ID_ATTRIBUTE, roles[j].getId());
203                 accreditableElement.appendChild(roleElement);
204             }
205             
206             policyElement.appendChild(accreditableElement);
207         }
208         
209         policyElement.setAttribute(SSL_ATTRIBUTE, Boolean.toString(policy.isSSLProtected()));
210
211         return helper.getDocument();
212     }
213
214     /**
215      * Saves an accreditable to an XML element.
216      * @param accreditable The accreditable.
217      * @param helper The namespace helper to be used.
218      * @return An XML element.
219      * @throws AccessControlException when something went wrong.
220      */

221     protected static Element JavaDoc save(Accreditable accreditable, NamespaceHelper helper)
222         throws AccessControlException {
223         String JavaDoc localName = null;
224         String JavaDoc id = null;
225
226         if (accreditable instanceof User) {
227             localName = USER_ELEMENT;
228             id = ((User) accreditable).getId();
229         } else if (accreditable instanceof AbstractGroup) {
230             localName = GROUP_ELEMENT;
231             id = ((AbstractGroup) accreditable).getId();
232         } else if (accreditable instanceof World) {
233             localName = WORLD_ELEMENT;
234         } else if (accreditable instanceof AbstractIPRange) {
235             localName = IP_RANGE_ELEMENT;
236             id = ((AbstractIPRange) accreditable).getId();
237         }
238
239         if (localName == null) {
240             throw new AccessControlException("Could not save accreditable [" + accreditable + "]");
241         }
242
243         Element JavaDoc element = helper.createElement(localName);
244
245         if (id != null) {
246             element.setAttribute(ID_ATTRIBUTE, id);
247         }
248
249         return element;
250     }
251
252     /**
253      * @see org.apache.lenya.ac.cache.InputStreamBuilder#build(java.io.InputStream)
254      */

255     public Object JavaDoc build(InputStream JavaDoc stream) throws BuildException {
256         Object JavaDoc value = null;
257         try {
258             value = buildPolicy(stream);
259         } catch (AccessControlException e) {
260             throw new BuildException(e);
261         }
262         return value;
263     }
264
265 }
266
Popular Tags