KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > transformation > UsecaseMenuTransformer


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: UsecaseMenuTransformer.java 109488 2004-12-02 10:12:22Z andreas $ */
19
20 package org.apache.lenya.cms.cocoon.transformation;
21
22 import java.io.IOException JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.avalon.framework.activity.Disposable;
26 import org.apache.avalon.framework.parameters.Parameters;
27 import org.apache.avalon.framework.service.ServiceSelector;
28 import org.apache.cocoon.ProcessingException;
29 import org.apache.cocoon.environment.ObjectModelHelper;
30 import org.apache.cocoon.environment.Request;
31 import org.apache.cocoon.environment.SourceResolver;
32 import org.apache.cocoon.transformation.AbstractSAXTransformer;
33 import org.apache.lenya.ac.AccessControlException;
34 import org.apache.lenya.ac.AccessController;
35 import org.apache.lenya.ac.AccessControllerResolver;
36 import org.apache.lenya.ac.Authorizer;
37 import org.apache.lenya.ac.Role;
38 import org.apache.lenya.ac.impl.DefaultAccessController;
39 import org.apache.lenya.ac.impl.PolicyAuthorizer;
40 import org.apache.lenya.cms.ac.usecase.UsecaseAuthorizer;
41 import org.apache.lenya.cms.publication.Publication;
42 import org.apache.lenya.cms.publication.PublicationFactory;
43 import org.apache.lenya.util.ServletHelper;
44 import org.xml.sax.Attributes JavaDoc;
45 import org.xml.sax.SAXException JavaDoc;
46 import org.xml.sax.helpers.AttributesImpl JavaDoc;
47
48 /**
49  * This transformer disables menu items (by removing the href attribute)
50  * which are not allowed with respect to the usecase policies.
51  */

52 public class UsecaseMenuTransformer extends AbstractSAXTransformer implements Disposable {
53
54     public static final String JavaDoc MENU_ELEMENT = "menu";
55     public static final String JavaDoc ITEM_ELEMENT = "item";
56     public static final String JavaDoc USECASE_ATTRIBUTE = "usecase";
57     public static final String JavaDoc NAMESPACE = "http://apache.org/cocoon/lenya/usecase/1.0";
58
59     /** (non-Javadoc)
60      * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
61      */

62     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc raw, Attributes JavaDoc attr)
63         throws SAXException JavaDoc {
64
65         Attributes JavaDoc attributes = attr;
66
67         if (authorizer != null && localName.equals(ITEM_ELEMENT)) {
68             String JavaDoc usecase = attr.getValue(NAMESPACE, USECASE_ATTRIBUTE);
69
70             // filter item if usecase not allowed
71
if (usecase != null) {
72                 getLogger().debug("Found usecase [" + usecase + "]");
73
74                 try {
75                     if (!authorizer.authorizeUsecase(usecase, roles, publication)) {
76                         getLogger().debug("Usecase not authorized");
77                         int hrefIndex = attributes.getIndex("href");
78                         if (hrefIndex > -1) {
79                             attributes = new AttributesImpl JavaDoc(attr);
80                             ((AttributesImpl JavaDoc) attributes).removeAttribute(hrefIndex);
81                         }
82                     }
83                 } catch (AccessControlException e) {
84                     throw new SAXException JavaDoc(e);
85                 }
86             }
87         }
88
89         super.startElement(uri, localName, raw, attributes);
90
91     }
92
93     private UsecaseAuthorizer authorizer;
94     private ServiceSelector serviceSelector = null;
95     private Role[] roles;
96     private Publication publication;
97     private AccessControllerResolver acResolver;
98
99     /**
100      * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
101      */

102     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters parameters)
103         throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
104         
105         super.setup(resolver, objectModel, src, parameters);
106
107         getLogger().debug("Setting up transformer");
108
109         serviceSelector = null;
110         acResolver = null;
111         authorizer = null;
112
113         Request request = ObjectModelHelper.getRequest(objectModel);
114
115         try {
116             roles = PolicyAuthorizer.getRoles(request);
117
118             publication = PublicationFactory.getPublication(objectModel);
119
120             serviceSelector =
121                 (ServiceSelector) manager.lookup(AccessControllerResolver.ROLE + "Selector");
122             acResolver =
123                 (AccessControllerResolver) serviceSelector.select(
124                     AccessControllerResolver.DEFAULT_RESOLVER);
125             getLogger().debug("Resolved AC resolver [" + acResolver + "]");
126
127             String JavaDoc webappUrl = ServletHelper.getWebappURI(request);
128             AccessController accessController = acResolver.resolveAccessController(webappUrl);
129
130             if (accessController instanceof DefaultAccessController) {
131                 DefaultAccessController defaultAccessController =
132                     (DefaultAccessController) accessController;
133                 Authorizer[] authorizers = defaultAccessController.getAuthorizers();
134                 for (int i = 0; i < authorizers.length; i++) {
135                     if (authorizers[i] instanceof UsecaseAuthorizer) {
136                         authorizer = (UsecaseAuthorizer) authorizers[i];
137                     }
138                 }
139             }
140
141             getLogger().debug("Using authorizer [" + authorizer + "]");
142         } catch (Exception JavaDoc e) {
143             throw new ProcessingException(e);
144         }
145     }
146
147     /**
148      * @see org.apache.avalon.framework.activity.Disposable#dispose()
149      */

150     public void dispose() {
151         getLogger().debug("Disposing transformer");
152         if (serviceSelector != null) {
153             if (acResolver != null) {
154                 serviceSelector.release(acResolver);
155             }
156             manager.release(serviceSelector);
157         }
158     }
159
160 }
161
Popular Tags