KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > ac > PublicationAccessControllerResolver


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: PublicationAccessControllerResolver.java 42660 2004-03-08 16:48:21Z gregor $ */
19
20 package org.apache.lenya.cms.ac;
21
22 import java.io.File JavaDoc;
23
24 import org.apache.avalon.framework.activity.Initializable;
25 import org.apache.avalon.framework.configuration.Configurable;
26 import org.apache.avalon.framework.configuration.Configuration;
27 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
28 import org.apache.excalibur.source.Source;
29 import org.apache.excalibur.source.SourceResolver;
30 import org.apache.excalibur.source.SourceUtil;
31 import org.apache.lenya.ac.AccessControlException;
32 import org.apache.lenya.ac.AccessController;
33 import org.apache.lenya.ac.impl.AbstractAccessControllerResolver;
34 import org.apache.lenya.cms.publication.Publication;
35 import org.apache.lenya.cms.publication.PublicationException;
36 import org.apache.lenya.cms.publication.PublicationFactory;
37 import org.apache.lenya.cms.publication.URLInformation;
38
39 /**
40  * Resolves the access controller according to the <code>ac.xconf</code> file of a publication.
41  */

42 public class PublicationAccessControllerResolver
43     extends AbstractAccessControllerResolver
44     implements Initializable {
45
46     protected static final String JavaDoc CONFIGURATION_FILE =
47         "config/ac/ac.xconf".replace('/', File.separatorChar);
48     protected static final String JavaDoc TYPE_ATTRIBUTE = "type";
49
50     /**
51      * This implementation uses the publication ID in combination with the context path
52      * as cache key.
53      * @see org.apache.lenya.ac.impl.AbstractAccessControllerResolver#generateCacheKey(java.lang.String, org.apache.excalibur.source.SourceResolver)
54      */

55     protected Object JavaDoc generateCacheKey(String JavaDoc webappUrl, SourceResolver resolver)
56         throws AccessControlException {
57             
58         URLInformation info = new URLInformation(webappUrl);
59
60         String JavaDoc publicationId = info.getPublicationId();
61         if (getLogger().isDebugEnabled()) {
62             getLogger().debug(
63                 "Using first URL step (might be publication ID) as cache key: [" + publicationId + "]");
64         }
65
66         return super.generateCacheKey(publicationId, resolver);
67     }
68
69     /**
70      * @see org.apache.lenya.ac.impl.AbstractAccessControllerResolver#doResolveAccessController(java.lang.String)
71      */

72     public AccessController doResolveAccessController(String JavaDoc webappUrl)
73         throws AccessControlException {
74         getLogger().debug("Resolving controller for URL [" + webappUrl + "]");
75
76         AccessController controller = null;
77         Publication publication = getPublication(webappUrl);
78
79         if (publication != null) {
80             String JavaDoc publicationUrl = webappUrl.substring(("/" + publication.getId()).length());
81             controller = resolveAccessController(publication, publicationUrl);
82         }
83         return controller;
84     }
85
86     /**
87      * Returns the publication for the webapp URL or null if the URL is not included
88      * in a publication.
89      * @param webappUrl The webapp URL.
90      * @return A publication.
91      * @throws AccessControlException when something went wrong.
92      */

93     protected Publication getPublication(String JavaDoc webappUrl) throws AccessControlException {
94         Publication publication = null;
95
96         assert webappUrl.startsWith("/");
97         // remove leading slash
98
String JavaDoc url = webappUrl.substring(1);
99
100         if (url.length() > 0) {
101
102             URLInformation info = new URLInformation(webappUrl);
103             String JavaDoc publicationId = info.getPublicationId();
104
105             File JavaDoc contextDir = getContext();
106             if (PublicationFactory
107                 .existsPublication(publicationId, contextDir.getAbsolutePath())) {
108
109                 getLogger().debug("Publication [" + publicationId + "] exists.");
110                 try {
111                     publication =
112                         PublicationFactory.getPublication(
113                             publicationId,
114                             contextDir.getAbsolutePath());
115                 } catch (PublicationException e) {
116                     throw new AccessControlException(e);
117                 }
118
119             } else {
120                 getLogger().debug("Publication [" + publicationId + "] does not exist.");
121             }
122         }
123         return publication;
124     }
125
126     /**
127      * Returns the servlet context.
128      * @return A file.
129      * @throws AccessControlException when something went wrong.
130      */

131     protected File JavaDoc getContext() throws AccessControlException {
132         return context;
133     }
134
135     private File JavaDoc context;
136
137     /**
138      * Resolves an access controller for a certain URL within a publication.
139      * @param publication The publication.
140      * @param url The url within the publication.
141      * @return An access controller.
142      * @throws AccessControlException when something went wrong.
143      */

144     public AccessController resolveAccessController(Publication publication, String JavaDoc url)
145         throws AccessControlException {
146
147         assert publication != null;
148
149         AccessController accessController = null;
150         File JavaDoc configurationFile = new File JavaDoc(publication.getDirectory(), CONFIGURATION_FILE);
151
152         if (configurationFile.isFile()) {
153             try {
154                 Configuration configuration =
155                     new DefaultConfigurationBuilder().buildFromFile(configurationFile);
156                 String JavaDoc type = configuration.getAttribute(TYPE_ATTRIBUTE);
157
158                 accessController =
159                     (AccessController) getManager().lookup(AccessController.ROLE + "/" + type);
160
161                 if (accessController instanceof Configurable) {
162                     ((Configurable) accessController).configure(configuration);
163                 }
164
165             } catch (Exception JavaDoc e) {
166                 throw new AccessControlException(e);
167             }
168         }
169
170         return accessController;
171     }
172
173     /**
174      * @see org.apache.avalon.framework.activity.Initializable#initialize()
175      */

176     public void initialize() throws Exception JavaDoc {
177         SourceResolver resolver = null;
178         Source contextSource = null;
179         File JavaDoc contextDir;
180         try {
181             resolver = (SourceResolver) getManager().lookup(SourceResolver.ROLE);
182             contextSource = resolver.resolveURI("context:///");
183             contextDir = SourceUtil.getFile(contextSource);
184             
185             if (contextDir == null || !contextDir.isDirectory()) {
186                 throw new AccessControlException("The servlet context is not a directory!");
187             }
188             
189         } finally {
190             if (resolver != null) {
191                 if (contextSource != null) {
192                     resolver.release(contextSource);
193                 }
194                 getManager().release(resolver);
195             }
196         }
197         this.context = contextDir;
198     }
199
200 }
201
Popular Tags