KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > tools > transformation > CheckAccessTransformer


1 /*
2  * Copyright 1999-2005 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 package org.apache.cocoon.portal.tools.transformation;
17
18 import org.apache.avalon.framework.activity.Disposable;
19 import org.apache.avalon.framework.service.ServiceException;
20 import org.apache.avalon.framework.service.ServiceManager;
21 import org.apache.cocoon.portal.tools.PortalToolManager;
22 import org.apache.cocoon.portal.tools.service.UserRightsService;
23 import org.apache.cocoon.transformation.AbstractSAXTransformer;
24 import org.apache.cocoon.xml.IncludeXMLConsumer;
25 import org.w3c.dom.DocumentFragment JavaDoc;
26 import org.xml.sax.Attributes JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28
29 /**
30  *
31  * @version $Id: CheckAccessTransformer.java 168366 2005-05-05 18:23:18Z vgritsenko $
32  */

33 public class CheckAccessTransformer extends AbstractSAXTransformer
34                                     implements Disposable {
35
36     public static final String JavaDoc RIGHTS_NAMESPACE_URI =
37         "http://apache.org/cocoon/portal/tools/rights/1.0";
38
39     public static final String JavaDoc ACCESS_TAG = "access";
40     public static final String JavaDoc RIGHT_ID = "id";
41
42     private UserRightsService urs;
43     private PortalToolManager ptm;
44
45     /* (non-Javadoc)
46      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#service(org.apache.avalon.framework.service.ServiceManager)
47      */

48     public void service(ServiceManager manager) throws ServiceException {
49         super.service(manager);
50         ptm = (PortalToolManager) this.manager.lookup(PortalToolManager.ROLE);
51         urs = ptm.getUserRightsService();
52     }
53
54     /* (non-Javadoc)
55      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
56      */

57     public void startElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw, Attributes JavaDoc attr)
58     throws SAXException JavaDoc {
59         if (RIGHTS_NAMESPACE_URI.equals(uri) && ACCESS_TAG.equals(name)) {
60             String JavaDoc id = attr.getValue(RIGHT_ID);
61             if(!urs.userIsAllowed(id, this.ptm.getPortalObjects().getProfileManager().getUser())) {
62                 this.stack.push(new Boolean JavaDoc(false));
63             }
64             this.startRecording();
65         } else {
66             super.startElement(uri, name, raw, attr);
67         }
68     }
69
70     /* (non-Javadoc)
71      * @see org.apache.cocoon.transformation.AbstractSAXTransformer#endElement(java.lang.String, java.lang.String, java.lang.String)
72      */

73     public void endElement(String JavaDoc uri, String JavaDoc name, String JavaDoc raw)
74             throws SAXException JavaDoc {
75         if(RIGHTS_NAMESPACE_URI.equals(uri) && ACCESS_TAG.equals(name)) {
76             DocumentFragment JavaDoc frag = this.endRecording();
77             Boolean JavaDoc ignore = (Boolean JavaDoc) this.stack.pop();
78             if(!ignore.booleanValue())
79                 IncludeXMLConsumer.includeNode(frag, this.contentHandler, this.lexicalHandler);
80         } else {
81             super.endElement(uri, name, raw);
82         }
83     }
84
85     /* (non-Javadoc)
86      * @see org.apache.avalon.framework.activity.Disposable#dispose()
87      */

88     public void dispose() {
89         if (this.manager != null) {
90             this.manager.release(ptm);
91             ptm = null;
92             this.manager = null;
93         }
94         super.dispose();
95     }
96 }
97
Popular Tags