View Javadoc

1   /*
2    * $Id: XmlAuthorizationsHandler.java 125 2004-11-01 19:10:53Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.security.memory;
24  
25  import java.io.InputStream;
26  import java.util.Collections;
27  import java.util.HashMap;
28  import java.util.HashSet;
29  import java.util.Map;
30  import java.util.Set;
31  import java.util.Stack;
32  import java.util.StringTokenizer;
33  
34  import org.talika.tarsis.security.RoleImpl;
35  import org.xml.sax.Attributes;
36  import org.xml.sax.InputSource;
37  import org.xml.sax.SAXException;
38  import org.xml.sax.SAXParseException;
39  import org.xml.sax.helpers.AttributesImpl;
40  import org.xml.sax.helpers.DefaultHandler;
41  
42  /**
43   * Implementation of SAX handlers for Tarsis MVC Authorizations XML files.
44   *
45   * @author  Jose M. Palomar
46   * @version $Revision: 125 $
47   */
48  public final class XmlAuthorizationsHandler extends DefaultHandler {
49  
50      // Constants
51      /**
52       * <tarsis-authorizations> tag.
53       */
54      public static final String AUTHORIZATIONS_TAG = "tarsis-authorizations";
55  
56      /**
57       * <command-auth> tag.
58       */
59      public static final String COMMAND_AUTH_TAG = "command-auth";
60  
61      /**
62       * <package-auth> tag.
63       */
64      public static final String PACKAGE_AUTH_TAG = "package-auth";
65  
66      /**
67       * Name attribute.
68       */
69      public static final String NAME_ATTR = "name";
70  
71      /**
72       * Roles attribute.
73       */
74      public static final String ROLES_ATTR = "roles";
75  
76      /**
77       * Roles separator.
78       */
79      public static final String ROLES_SEPARATOR = ",";
80  
81      /**
82       * DTD public id.
83       */
84      public static final String DTD_PUBLICID = "-//Talika Open Source Group//Authorizations DTD 1.0//ES";
85  
86      /**
87       * DTD class path.
88       */
89      public static final String DTD_CLASSPATH = "/org/talika/dtds/tarsis-authorizations_1_0.dtd";
90  
91      // Fields
92      /**
93       * Command authorizations map.
94       */
95      private Map commands = new HashMap();
96  
97      /**
98       * Package authorizations map.
99       */
100     private Map packages = new HashMap();
101 
102     /**
103      * Tag attributes stack.
104      */
105     private Stack stAtts = new Stack();
106 
107     /**
108      * Map of parameters.
109      */
110     private Map auxParams = new HashMap();
111 
112     // Constructors
113     /**
114      * Creates a new <code>XmlAuthorizationsHandler</code>.
115      */
116     public XmlAuthorizationsHandler() {
117     }
118 
119     // Methods
120     // XML Handler
121     /**
122      * Receive notification of the beginning of a document.
123      *
124      * @throws SAXException Any SAX exception, possibly wrapping another exception.
125      * @see org.xml.sax.ContentHandler#startDocument()
126      */
127     public void startDocument() throws SAXException {
128         // Nothing to do
129     }
130 
131     /**
132      * Receive notification of the end of a document.
133      *
134      * @throws SAXException Any SAX exception, possibly wrapping another exception.
135      * @see org.xml.sax.ContentHandler#endDocument()
136      */
137     public void endDocument() throws SAXException {
138         // Nothing to do
139     }
140 
141     /**
142      * Receive notification of the beginning of an element.
143      *
144      * @param namespaceURI String The Namespace URI, or the empty string if the
145      * element has no Namespace URI or if Namespace processing is not being performed.
146      * @param localName String The local name (without prefix), or the empty string
147      * if Namespace processing is not being performed.
148      * @param name String The qualified name (with prefix), or the empty string if
149      * qualified names are not available.
150      * @param atts Attributes The attributes attached to the element. If there are no
151      * attributes, it shall be an empty Attributes object.
152      * @throws SAXException Any SAX exception, possibly wrapping another exception.
153      * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
154      */
155     public void startElement(String namespaceURI, String localName, String name, Attributes atts) throws SAXException {
156         stAtts.push(new AttributesImpl(atts));
157     }
158 
159     /**
160      * Receive notification of the end of an element.
161      *
162      * @param namespaceURI String The Namespace URI, or the empty string if the
163      * element has no Namespace URI or if Namespace processing is not being performed.
164      * @param localName String The local name (without prefix), or the empty string
165      * if Namespace processing is not being performed.
166      * @param name String The qualified name (with prefix), or the empty string if
167      * qualified names are not available.
168      * @throws SAXException Any SAX exception, possibly wrapping another exception.
169      * @see org.xml.sax.ContentHandler#endElement(String, String, String)
170      */
171     public void endElement(String namespaceURI, String localName, String name) throws SAXException {
172 
173         Attributes atts = (Attributes) stAtts.pop();
174 
175         if (name.equals(AUTHORIZATIONS_TAG)) {
176             // Nothing to do
177         }
178         else if (name.equals(COMMAND_AUTH_TAG)) {
179             processCommandTag(atts);
180         }
181         else if (name.equals(PACKAGE_AUTH_TAG)) {
182             processPackageTag(atts);
183         }
184 
185     }
186 
187     // Entity Resolver
188     /**
189      * Allow the application to resolve external entities.
190      *
191      * @param publicId String The public identifier of the external entity being
192      * referenced, or <code>null</code> if none was supplied.
193      * @param systemId String The system identifier of the external entity being
194      * referenced.
195      * @return InputSource A Java-specific IO exception, possibly the result of
196      * creating a new <code>InputStream</code> or <code>Reader</code> for the
197      * <code>InputSource</code>.
198      * @see org.xml.sax.EntityResolver#resolveEntity(String, String)
199      */
200     public InputSource resolveEntity (String publicId, String systemId) {
201 
202         if (publicId.equals(DTD_PUBLICID)) {
203 
204             InputStream is = this.getClass().getResourceAsStream(DTD_CLASSPATH);
205             if (is != null) {
206                 return new InputSource(is);
207             }
208 
209         }
210 
211         return null;
212 
213     }
214 
215     // Error Handler
216     /**
217      * Receive notification of a warning.
218      *
219      * @param spe SAXParseException The warning information encapsulated in a SAX
220      * parse exception.
221      * @throws SAXException Any SAX exception, possibly wrapping another exception.
222      * @see org.xml.sax.ErrorHandler#warning(SAXParseException)
223      */
224     public void warning(SAXParseException spe) throws SAXException {
225     }
226 
227     /**
228      * Receive notification of a recoverable error.
229      *
230      * @param spe SAXParseException The error information encapsulated in a SAX parse
231      * exception.
232      * @throws SAXException Any SAX exception, possibly wrapping another exception.
233      * @see org.xml.sax.ErrorHandler#error(SAXParseException)
234      */
235     public void error(SAXParseException spe) throws SAXException {
236         throw new SAXException("** Authorizations parse error\n" +
237                             "  at [" + spe.getLineNumber() +
238                             ":" + spe.getColumnNumber() +
239                             "] in " + spe.getSystemId(), spe);
240     }
241 
242     /**
243      * Receive notification of a non-recoverable error.
244      *
245      * @param spe SAXParseException The error information encapsulated in a SAX parse
246      * exception.
247      * @throws SAXException Any SAX exception, possibly wrapping another exception.
248      * @see org.xml.sax.ErrorHandler#fatalError(SAXParseException)
249      */
250     public void fatalError(SAXParseException spe) throws SAXException {
251         throw new SAXException("** Authorizations parse error\n" +
252                             "  at [" + spe.getLineNumber() +
253                             ":" + spe.getColumnNumber() +
254                             "] in " + spe.getSystemId(), spe);
255     }
256 
257     // Accesors
258     /**
259      * Returns parsed command authorizations map.
260      *
261      * @return Map parsed command authorizations map.
262      */
263     public Map getCommands() {
264         return Collections.unmodifiableMap(commands);
265     }
266 
267     /**
268      * Returns parsed package authorizations map.
269      *
270      * @return Map parsed package authorizations map.
271      */
272     public Map getPackages() {
273         return Collections.unmodifiableMap(packages);
274     }
275 
276     // Helper
277     /**
278      * Process &lt;command-auth&gt; tag.
279      *
280      * @param atts Attributes tag attributes.
281      * @throws SAXException if there is an error while processing tag.
282      */
283     protected void processCommandTag(Attributes atts) throws SAXException {
284 
285         String name = atts.getValue(NAME_ATTR);
286         String rolesStr = atts.getValue(ROLES_ATTR);
287 
288         Set roles = parseRoles(rolesStr);
289 
290         this.commands.put(name, roles);
291 
292     }
293 
294     /**
295      * Process &lt;package-auth&gt; tag.
296      *
297      * @param atts Attributes tag attributes.
298      * @throws SAXException if there is an error while processing tag.
299      */
300     protected void processPackageTag(Attributes atts) throws SAXException {
301 
302         String name = atts.getValue(NAME_ATTR);
303         String rolesStr = atts.getValue(ROLES_ATTR);
304 
305         Set roles = parseRoles(rolesStr);
306 
307         this.packages.put(name, roles);
308 
309     }
310 
311     /**
312      * Returns a <code>Set</code> of roles parsed from string.
313      *
314      * @param rolesStr String string to be parsed.
315      * @return Set a <code>Set</code> of roles parsed from string.
316      */
317     protected Set parseRoles(String rolesStr) {
318 
319         StringTokenizer st = new StringTokenizer(rolesStr, ROLES_SEPARATOR);
320 
321         HashSet roles = new HashSet();
322         while (st.hasMoreTokens()) {
323             String roleName = st.nextToken().trim();
324             roles.add(new RoleImpl(roleName));
325         }
326 
327         return roles;
328     }
329 
330 }