KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > RoleFilterTransformer


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 package org.apache.cocoon.transformation;
17
18 import org.apache.avalon.framework.parameters.Parameters;
19 import org.apache.cocoon.ProcessingException;
20 import org.apache.cocoon.environment.ObjectModelHelper;
21 import org.apache.cocoon.environment.Request;
22 import org.apache.cocoon.environment.SourceResolver;
23 import org.xml.sax.Attributes JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
25 import org.xml.sax.helpers.AttributesImpl JavaDoc;
26
27 import java.io.IOException JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 /**
32  * @cocoon.sitemap.component.documentation
33  * Filter XML fragments based on a user's role. This will help in
34  * the development of smart forms that must only show information to
35  * people who are logged in and have the correct role. The Role is
36  * specified by the Request semantics. You can specify multiple roles
37  * by using comma delimiting.
38  *
39  * @cocoon.sitemap.component.name role-filter
40  * @cocoon.sitemap.component.logger sitemap.transformer.role-filter
41  *
42  * Filter XML fragments based on a user's role. This will help in
43  * the development of smart forms that must only show information to
44  * people who are logged in and have the correct role. The Role is
45  * specified by the Request semantics. You can specify multiple roles
46  * by using comma delimiting.
47  *
48  * <pre>
49  * &lt;root xmlns:roles="http://apache.org/cocoon/role-filter/1.0"&gt;
50  * &lt;textbox name="identifier" roles:restricted="admin,boss"/&gt;
51  * &lt;textbox name="name" roles:read-only="admin,boss"/&gt;
52  * &lt;/root&gt;
53  * </pre>
54  *
55  * The previous example will only show the "identifier" textbox for the
56  * roles "admin" and "boss". It will pass role:read-only="" if the
57  * roles "admin" or "boss" are accessing the page. That way you can
58  * specify any special processing by testing for the read-only attribute.
59  * This filter does not care about the prefix, only the namespace URI.
60  * That means you can reassign the namespace to another prefix and all
61  * will work as expected.
62  *
63  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
64  * @version CVS $Id: RoleFilterTransformer.java 36466 2004-08-16 10:08:39Z cziegeler $
65  */

66 public class RoleFilterTransformer extends FilterTransformer {
67     private final static String JavaDoc URI = "http://apache.org/cocoon/role-filter/1.0";
68     private final static String JavaDoc RESTRICT = "restricted";
69     private final static String JavaDoc VIEW = "read-only";
70     Request request = null;
71
72     public RoleFilterTransformer() {
73     }
74
75     public final void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters params)
76     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
77         super.setup(resolver, objectModel, src, params);
78         this.request = ObjectModelHelper.getRequest(objectModel);
79     }
80
81     /**
82      * Disable caching
83      */

84     public java.io.Serializable JavaDoc getKey() {
85         return null;
86     }
87
88     public final void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a)
89     throws SAXException JavaDoc {
90         int roleIndex = a.getIndex(RoleFilterTransformer.URI, RoleFilterTransformer.RESTRICT);
91         int viewIndex = a.getIndex(RoleFilterTransformer.URI, RoleFilterTransformer.VIEW);
92         boolean propogate = true;
93         boolean readOnly = false;
94
95         if (roleIndex >= 0) {
96             String JavaDoc roleRestriction = a.getValue(roleIndex);
97             StringTokenizer JavaDoc roles = new StringTokenizer JavaDoc(roleRestriction, ",", false);
98             propogate = false;
99
100             while ((! propogate) && roles.hasMoreTokens()) {
101                 if (request.isUserInRole(roles.nextToken())) {
102                     propogate = true;
103                 }
104             }
105         }
106
107         if (! propogate) {
108             super.elementName = loc;
109         } else {
110             if (viewIndex >= 0) {
111                 String JavaDoc viewRestriction = a.getValue(viewIndex);
112                 StringTokenizer JavaDoc roles = new StringTokenizer JavaDoc(viewRestriction, ",", false);
113
114                 while ((! readOnly) && roles.hasMoreTokens()) {
115                     if (request.isUserInRole(roles.nextToken())) {
116                         readOnly = true;
117                     }
118                 }
119             }
120         }
121
122         super.startElement(uri, loc, raw,
123                 this.copyAttributes(a, roleIndex, viewIndex, readOnly));
124     }
125
126     public final void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw)
127     throws SAXException JavaDoc {
128         super.endElement(uri, loc, raw);
129
130         if (! super.skip) {
131             super.elementName = "";
132         }
133     }
134
135     private final Attributes JavaDoc copyAttributes(final Attributes JavaDoc a, final int role,
136                                             final int view, boolean readOnly) {
137         if (role < 0 && view < 0) {
138             return a;
139         }
140
141         AttributesImpl JavaDoc attr = new AttributesImpl JavaDoc();
142         attr.setAttributes(a);
143         if (role >= 0) {
144             attr.removeAttribute(role);
145         }
146
147         if (view >= 0) {
148             if (readOnly) {
149                 attr.setValue(view, "");
150             } else {
151                 attr.removeAttribute(view);
152             }
153         }
154
155         return attr;
156     }
157
158     public void recycle() {
159         this.request = null;
160         super.recycle();
161     }
162 }
163
Popular Tags