KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > authentication > context > AuthenticationContext


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.webapps.authentication.context;
17
18 import org.apache.avalon.framework.CascadingRuntimeException;
19 import org.apache.avalon.framework.context.Context;
20 import org.apache.avalon.framework.parameters.Parameters;
21
22 import org.apache.cocoon.ProcessingException;
23 import org.apache.cocoon.components.ContextHelper;
24 import org.apache.cocoon.components.source.SourceUtil;
25 import org.apache.cocoon.environment.Request;
26 import org.apache.cocoon.webapps.authentication.AuthenticationConstants;
27 import org.apache.cocoon.webapps.authentication.components.DefaultAuthenticationManager;
28 import org.apache.cocoon.webapps.authentication.configuration.ApplicationConfiguration;
29 import org.apache.cocoon.webapps.authentication.user.RequestState;
30 import org.apache.cocoon.webapps.authentication.user.UserHandler;
31 import org.apache.cocoon.webapps.session.context.SessionContext;
32 import org.apache.cocoon.webapps.session.context.SimpleSessionContext;
33 import org.apache.cocoon.xml.XMLUtils;
34 import org.apache.cocoon.xml.dom.DOMUtil;
35
36 import org.apache.excalibur.source.SourceParameters;
37 import org.apache.excalibur.source.SourceResolver;
38 import org.apache.excalibur.xml.xpath.XPathProcessor;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.DocumentFragment JavaDoc;
41 import org.w3c.dom.Node JavaDoc;
42 import org.w3c.dom.NodeList JavaDoc;
43 import org.xml.sax.ContentHandler JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45 import org.xml.sax.ext.LexicalHandler JavaDoc;
46
47 import java.io.IOException JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.HashMap JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.Map JavaDoc;
52 import java.util.StringTokenizer JavaDoc;
53
54 /**
55  * This is the implementation for the authentication context
56  *
57  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
58  * @version $Id: AuthenticationContext.java 164808 2005-04-26 16:07:03Z vgritsenko $
59 */

60 public class AuthenticationContext implements SessionContext {
61
62     protected String JavaDoc name;
63     protected UserHandler handler;
64     protected SessionContext authContext;
65     protected String JavaDoc handlerName;
66     protected boolean initialized;
67     protected Context context;
68     protected XPathProcessor xpathProcessor;
69     protected SourceResolver resolver;
70     /** A list of roles the user is in */
71     protected List JavaDoc roles;
72
73     /** Constructor */
74     public AuthenticationContext(Context context, XPathProcessor processor, SourceResolver resolver) {
75         this.context = context;
76         this.xpathProcessor = processor;
77         this.resolver = resolver;
78     }
79
80     /**
81      * Initialize the context. This method has to be called right after
82      * the constructor.
83      */

84     public void init(UserHandler handler) {
85         this.name = AuthenticationConstants.SESSION_CONTEXT_NAME;
86
87         this.handler = handler;
88         this.handlerName = this.handler.getHandlerName();
89         try {
90             this.authContext = new SimpleSessionContext(this.xpathProcessor, this.resolver);
91         } catch (ProcessingException pe) {
92             throw new CascadingRuntimeException("Unable to create simple context.", pe);
93         }
94     }
95
96     /**
97      * Return the current authentication state
98      */

99     protected RequestState getState() {
100         return DefaultAuthenticationManager.getRequestState( this.context );
101     }
102
103     /**
104      * Initialize this context
105      */

106     public void init(Document JavaDoc doc)
107     throws ProcessingException {
108         if ( initialized ) {
109             throw new ProcessingException("The context can only be initialized once.");
110         }
111         this.authContext.setNode("/", doc.getFirstChild());
112     }
113
114     /* (non-Javadoc)
115      * @see org.apache.cocoon.webapps.session.context.SessionContext#setup(java.lang.String, java.lang.String, java.lang.String)
116      */

117     public void setup(String JavaDoc value, String JavaDoc load, String JavaDoc save) {
118         // this is not used, everything is set in the constructor
119
}
120
121     /* (non-Javadoc)
122      * @see org.apache.cocoon.webapps.session.context.SessionContext#getName()
123      */

124     public String JavaDoc getName() {
125         return this.name;
126     }
127
128     /* (non-Javadoc)
129      * @see org.apache.cocoon.webapps.session.context.SessionContext#getXML(java.lang.String)
130      */

131     public DocumentFragment JavaDoc getXML(String JavaDoc path)
132     throws ProcessingException {
133         if (path == null) {
134             throw new ProcessingException("getXML: Path is required");
135         }
136         if (!path.startsWith("/")) path = '/' + path;
137
138         final String JavaDoc applicationName = this.getState().getApplicationName();
139
140         DocumentFragment JavaDoc frag = null;
141
142         if ( path.equals("/") ) {
143             // get all: first authentication then application
144
frag = this.authContext.getXML("/authentication");
145
146             if (frag != null) {
147                 // now add root node authentication
148
Node JavaDoc root = frag.getOwnerDocument().createElementNS(null, "authentication");
149                 Node JavaDoc child;
150                 while (frag.hasChildNodes()) {
151                     child = frag.getFirstChild();
152                     frag.removeChild(child);
153                     root.appendChild(child);
154                 }
155                 frag.appendChild(root);
156             }
157
158             if (applicationName != null) {
159                 // join
160
DocumentFragment JavaDoc appFrag = this.authContext.getXML("/applications/" + applicationName);
161                 if (appFrag != null) {
162                     // now add root node application
163
Node JavaDoc root = appFrag.getOwnerDocument().createElementNS(null, "application");
164                     Node JavaDoc child;
165                     while (appFrag.hasChildNodes() ) {
166                         child = appFrag.getFirstChild();
167                         appFrag.removeChild(child);
168                         root.appendChild(child);
169                     }
170                     appFrag.appendChild(root);
171
172                     if (frag == null) {
173                         frag = appFrag;
174                     } else {
175                         while (appFrag.hasChildNodes() ) {
176                             child = appFrag.getFirstChild();
177                             appFrag.removeChild(child);
178                             child = frag.getOwnerDocument().importNode(child, true);
179                             frag.appendChild(child);
180                         }
181                     }
182                 }
183             }
184
185         } else if (path.startsWith("/authentication") ) {
186             frag = this.authContext.getXML(path);
187
188         } else if (path.equals("/application") || path.startsWith("/application/") ) {
189             if (applicationName != null) {
190                 String JavaDoc appPath;
191                 if (path.equals("/application")) {
192                     appPath ="/";
193                 } else {
194                     appPath = path.substring("/application".length());
195                 }
196                 frag = this.authContext.getXML("/applications/" + applicationName + appPath);
197             }
198         } else {
199             frag = this.authContext.getXML(path);
200         }
201
202         return frag;
203     }
204
205     /* (non-Javadoc)
206      * @see org.apache.cocoon.webapps.session.context.SessionContext#setXML(java.lang.String, org.w3c.dom.DocumentFragment)
207      */

208     public void setXML(String JavaDoc path, DocumentFragment JavaDoc fragment)
209     throws ProcessingException {
210         if (path == null) {
211             throw new ProcessingException("setXML: Path is required");
212         }
213         if (!path.startsWith("/")) path = '/' + path;
214
215         final String JavaDoc applicationName = this.getState().getApplicationName();
216
217         if ( path.equals("/") ) {
218             // set all is not allowed with "/"
219
throw new ProcessingException("Path '/' is not allowed");
220
221         } else if ( path.startsWith("/authentication") ) {
222
223             this.cleanParametersCache();
224             this.authContext.setXML(path, fragment);
225
226         } else if (path.equals("/application")
227                    || path.startsWith("/application/") ) {
228
229             if (applicationName == null) {
230                 throw new ProcessingException("Application is required");
231             }
232             String JavaDoc appPath;
233             if (path.equals("/application")) {
234                 appPath = "/";
235             } else {
236                 appPath = path.substring("/application".length());
237             }
238             this.authContext.setXML("/applications/" + applicationName + appPath, fragment);
239
240         } else {
241             this.authContext.setXML(path, fragment);
242         }
243     }
244
245     /* (non-Javadoc)
246      * @see org.apache.cocoon.webapps.session.context.SessionContext#appendXML(java.lang.String, org.w3c.dom.DocumentFragment)
247      */

248     public void appendXML(String JavaDoc path, DocumentFragment JavaDoc fragment)
249     throws ProcessingException {
250         if (path == null) {
251             throw new ProcessingException("appendXML: Path is required");
252         }
253         if (!path.startsWith("/") ) path = '/' + path;
254
255         final String JavaDoc applicationName = this.getState().getApplicationName();
256
257         if ( path.equals("/") ) {
258             // set all is not allowed with "/"
259
throw new ProcessingException("Path '/' is not allowed");
260
261         } else if ( path.startsWith("/authentication") ) {
262
263             this.cleanParametersCache();
264             this.authContext.appendXML(path, fragment);
265
266         } else if (path.equals("/application")
267                    || path.startsWith("/application/") ) {
268
269             if (applicationName == null) {
270                 throw new ProcessingException("Application is required");
271             }
272             String JavaDoc appPath;
273             if (path.equals("/application") ) {
274                 appPath = "/";
275             } else {
276                 appPath = path.substring("/application".length());
277             }
278             this.authContext.appendXML("/applications/" + applicationName + appPath, fragment);
279
280         } else {
281             this.authContext.appendXML(path, fragment);
282         }
283     }
284
285     /* (non-Javadoc)
286      * @see org.apache.cocoon.webapps.session.context.SessionContext#removeXML(java.lang.String)
287      */

288     public void removeXML(String JavaDoc path)
289     throws ProcessingException {
290         if (path == null) {
291             throw new ProcessingException("removeXML: Path is required");
292         }
293         if (!path.startsWith("/") ) path = '/' + path;
294
295         final String JavaDoc applicationName = this.getState().getApplicationName();
296
297         if (path.equals("/") ) {
298             this.cleanParametersCache();
299             this.authContext.removeXML("/");
300
301         } else if (path.startsWith("/authentication") ) {
302
303             this.cleanParametersCache();
304             this.authContext.removeXML(path);
305
306         } else if (path.equals("/application")
307                    || path.startsWith("/application/") ) {
308             if (applicationName == null) {
309                 throw new ProcessingException("removeXML: Application is required for path " + path);
310             }
311             String JavaDoc appPath;
312             if (path.equals("/application") ) {
313                 appPath = "/";
314             } else {
315                 appPath = path.substring("/application".length());
316             }
317             this.authContext.removeXML("/applications/" + applicationName + appPath);
318         } else {
319             this.authContext.removeXML(path);
320         }
321     }
322
323     /* (non-Javadoc)
324      * @see org.apache.cocoon.webapps.session.context.SessionContext#setAttribute(java.lang.String, java.lang.Object)
325      */

326     public void setAttribute(String JavaDoc key, Object JavaDoc value)
327     throws ProcessingException {
328         this.authContext.setAttribute(key, value);
329     }
330
331     /* (non-Javadoc)
332      * @see org.apache.cocoon.webapps.session.context.SessionContext#getAttribute(java.lang.String)
333      */

334     public Object JavaDoc getAttribute(String JavaDoc key)
335     throws ProcessingException {
336         return this.authContext.getAttribute(key);
337     }
338
339     /* (non-Javadoc)
340      * @see org.apache.cocoon.webapps.session.context.SessionContext#getAttribute(java.lang.String, java.lang.Object)
341      */

342     public Object JavaDoc getAttribute(String JavaDoc key, Object JavaDoc defaultObject)
343     throws ProcessingException {
344         return this.authContext.getAttribute(key, defaultObject);
345     }
346
347     /**
348      * NOT IMPLEMENTED - throws an exception
349      */

350     public Node JavaDoc getSingleNode(String JavaDoc path)
351     throws ProcessingException {
352         throw new ProcessingException("This method is not supported by the authenticaton session context.");
353     }
354
355     /**
356      * NOT IMPLEMENTED - throws an exception
357      */

358     public NodeList JavaDoc getNodeList(String JavaDoc path)
359     throws ProcessingException {
360         throw new ProcessingException("This method is not supported by the authenticaton session context.");
361     }
362
363     /**
364      * NOT IMPLEMENTED - throws an exception
365      */

366     public void setNode(String JavaDoc path, Node JavaDoc node)
367     throws ProcessingException {
368         throw new ProcessingException("This method is not supported by the authenticaton session context.");
369     }
370
371     /**
372      * NOT IMPLEMENTED - throws an exception
373      */

374     public String JavaDoc getValueOfNode(String JavaDoc path)
375     throws ProcessingException {
376         throw new ProcessingException("This method is not supported by the authenticaton session context.");
377     }
378
379     /**
380      * NOT IMPLEMENTED - throws an exception
381      */

382     public void setValueOfNode(String JavaDoc path, String JavaDoc value)
383     throws ProcessingException {
384         throw new ProcessingException("This method is not supported by the authenticaton session context.");
385     }
386
387     /* (non-Javadoc)
388      * @see org.apache.cocoon.webapps.session.context.SessionContext#streamXML(java.lang.String, org.xml.sax.ContentHandler, org.xml.sax.ext.LexicalHandler)
389      */

390     public boolean streamXML(String JavaDoc path, ContentHandler JavaDoc contentHandler,
391                              LexicalHandler JavaDoc lexicalHandler)
392     throws SAXException JavaDoc, ProcessingException {
393         if (path == null) {
394             throw new ProcessingException("streamXML: Path is required");
395         }
396         if (!path.startsWith("/") ) path = '/' + path;
397
398         final String JavaDoc applicationName = this.getState().getApplicationName();
399
400         if (path.equals("/") ) {
401             // get all: first authentication then application
402
contentHandler.startElement("", "authentication", "authentication", XMLUtils.EMPTY_ATTRIBUTES);
403             this.authContext.streamXML("/authentication", contentHandler, lexicalHandler);
404             contentHandler.endElement("", "authentication", "authentication");
405
406             if (applicationName != null) {
407                 contentHandler.startElement("", "application", "application", XMLUtils.EMPTY_ATTRIBUTES);
408                 this.authContext.streamXML("/applications/" + applicationName, contentHandler, lexicalHandler);
409                 contentHandler.endElement("", "application", "application");
410             }
411             return true;
412
413         } else if (path.startsWith("/authentication") ) {
414             return this.authContext.streamXML(path, contentHandler, lexicalHandler);
415
416         } else if (path.equals("/application") || path.startsWith("/application/") ) {
417             if (applicationName != null) {
418                 String JavaDoc appPath;
419                 if (path.equals("/application") ) {
420                     appPath ="/";
421                 } else {
422                     appPath = path.substring("/application".length());
423                 }
424                 return this.authContext.streamXML("/applications/" + applicationName + appPath, contentHandler, lexicalHandler);
425             }
426         } else {
427             return this.authContext.streamXML(path, contentHandler, lexicalHandler);
428         }
429         return false;
430     }
431
432     /* (non-Javadoc)
433      * @see org.apache.cocoon.webapps.session.context.SessionContext#loadXML(java.lang.String, org.apache.excalibur.source.SourceParameters)
434      */

435     public void loadXML(String JavaDoc path,
436                         SourceParameters parameters)
437     throws SAXException JavaDoc, ProcessingException, IOException JavaDoc {
438         if (!path.startsWith("/") ) path = '/' + path;
439
440         final String JavaDoc applicationName = this.getState().getApplicationName();
441         if (path.equals("/") ) {
442             // load all: first authentication then application
443
this.loadAuthenticationXML("/authentication",
444                                        parameters,
445                                        resolver);
446             if (applicationName != null) {
447                 this.loadApplicationXML("/",
448                                         parameters,
449                                         resolver);
450             }
451
452         } else if (path.startsWith("/authentication") ) {
453             this.loadAuthenticationXML(path,
454                                        parameters,
455                                        resolver);
456
457         } else if (path.equals("/application") && applicationName != null) {
458             this.loadApplicationXML("/",
459                                     parameters,
460                                     resolver);
461         } else if (path.startsWith("/application/") && applicationName != null) {
462             this.loadApplicationXML(path.substring(12), // start path with '/'
463
parameters,
464                                     resolver);
465         } else {
466             throw new ProcessingException("loadXML: Path is not valid: " + path);
467         }
468     }
469
470     /* (non-Javadoc)
471      * @see org.apache.cocoon.webapps.session.context.SessionContext#saveXML(java.lang.String, org.apache.excalibur.source.SourceParameters)
472      */

473     public void saveXML(String JavaDoc path,
474                         SourceParameters parameters)
475     throws SAXException JavaDoc, ProcessingException, IOException JavaDoc {
476         if (!path.startsWith("/") ) path = '/' + path;
477
478         final String JavaDoc applicationName = this.getState().getApplicationName();
479
480         if (path.equals("/") ) {
481             // save all: first authentication then application
482
this.saveAuthenticationXML("/authentication",
483                                        parameters,
484                                        resolver);
485             if (applicationName != null) {
486                 this.saveApplicationXML("/",
487                                         parameters,
488                                         resolver);
489             }
490
491         } else if (path.startsWith("/authentication") ) {
492             this.saveAuthenticationXML(path,
493                                        parameters,
494                                        resolver);
495
496         } else if (path.equals("/application") && applicationName != null) {
497             this.saveApplicationXML("/",
498                                     parameters,
499                                     resolver);
500         } else if (path.startsWith("/application/") && applicationName != null) {
501             this.saveApplicationXML(path.substring(12), // start path with '/'
502
parameters,
503                                     resolver);
504         } else {
505             throw new ProcessingException("saveXML: Path is not valid: " + path);
506         }
507     }
508
509     /**
510      * Clean the parameters cache
511      */

512     private void cleanParametersCache()
513     throws ProcessingException {
514         this.authContext.setAttribute("cachedmap" , null);
515         this.authContext.setAttribute("cachedpar" , null);
516     }
517
518     /**
519      * Save Authentication
520      */

521     private void saveAuthenticationXML(String JavaDoc path,
522                                        SourceParameters parameters,
523                                        SourceResolver resolver)
524     throws ProcessingException {
525         String JavaDoc authSaveResource = this.handler.getHandlerConfiguration().getSaveResource();
526         SourceParameters authSaveResourceParameters = this.handler.getHandlerConfiguration().getSaveResourceParameters();
527
528         if (authSaveResource == null) {
529             throw new ProcessingException("The context " + this.name + " does not support saving.");
530         }
531
532         synchronized(this.authContext) {
533             DocumentFragment JavaDoc fragment = this.getXML(path);
534             if (fragment == null) {
535                 // create empty fake fragment
536
fragment = DOMUtil.createDocument().createDocumentFragment();
537             }
538             if (parameters != null) {
539                 parameters = (SourceParameters)parameters.clone();
540                 parameters.add(authSaveResourceParameters);
541             } else if (authSaveResourceParameters != null) {
542                 parameters = (SourceParameters)authSaveResourceParameters.clone();
543             }
544             parameters = this.createParameters(parameters,
545                                                path,
546                                                false);
547             SourceUtil.writeDOM(authSaveResource,
548                                 null,
549                                 parameters,
550                                 fragment,
551                                 resolver,
552                                 "xml");
553         } // end synchronized
554
}
555
556     /**
557      * Save Authentication
558      */

559     private void loadAuthenticationXML(String JavaDoc path,
560                                        SourceParameters parameters,
561                                        SourceResolver resolver)
562     throws ProcessingException {
563         String JavaDoc authLoadResource = this.handler.getHandlerConfiguration().getLoadResource();
564         SourceParameters authLoadResourceParameters = this.handler.getHandlerConfiguration().getLoadResourceParameters();
565
566         if (authLoadResource == null) {
567             throw new ProcessingException("The context " + this.name + " does not support loading.");
568         }
569
570         synchronized(this.authContext) {
571
572             if (parameters != null) {
573                 parameters = (SourceParameters)parameters.clone();
574                 parameters.add(authLoadResourceParameters);
575             } else if (authLoadResourceParameters != null) {
576                 parameters = (SourceParameters)authLoadResourceParameters.clone();
577             }
578             parameters = this.createParameters(parameters,
579                                                path,
580                                                false);
581             DocumentFragment JavaDoc frag;
582
583             frag = SourceUtil.readDOM(authLoadResource,
584                                       null,
585                                       parameters,
586                                       resolver);
587
588             this.setXML(path, frag);
589
590         } // end synchronized
591
}
592
593     /**
594      * Load XML of an application
595      */

596     private void loadApplicationXML(String JavaDoc path,
597                                     SourceParameters parameters,
598                                     SourceResolver resolver)
599     throws ProcessingException {
600         final String JavaDoc applicationName = this.getState().getApplicationName();
601
602         final ApplicationConfiguration conf = (ApplicationConfiguration)this.handler.getHandlerConfiguration().getApplications().get( applicationName );
603         String JavaDoc loadResource = conf.getLoadResource();
604         SourceParameters loadResourceParameters = conf.getLoadResourceParameters();
605         if (loadResource == null) {
606             throw new ProcessingException("The context " + this.name + " does not support loading.");
607         }
608         // synchronized
609
synchronized (this.authContext) {
610
611             if (parameters != null) {
612                 parameters = (SourceParameters)parameters.clone();
613                 parameters.add(loadResourceParameters);
614             } else if (loadResourceParameters != null) {
615                 parameters = (SourceParameters)loadResourceParameters.clone();
616             }
617             parameters = this.createParameters(parameters,
618                                                path,
619                                                true);
620             DocumentFragment JavaDoc fragment;
621             fragment = SourceUtil.readDOM(loadResource,
622                                           null,
623                                           parameters,
624                                           resolver);
625             this.authContext.setXML("/applications/" + applicationName + '/', fragment);
626
627         } // end synchronized
628

629     }
630
631     /**
632      * Save XML of an application
633      */

634     private void saveApplicationXML(String JavaDoc path,
635                                     SourceParameters parameters,
636                                     SourceResolver resolver)
637     throws ProcessingException {
638         final String JavaDoc applicationName = this.getState().getApplicationName();
639         final ApplicationConfiguration conf = (ApplicationConfiguration)this.handler.getHandlerConfiguration().getApplications().get( applicationName );
640         String JavaDoc saveResource = conf.getSaveResource();
641         SourceParameters saveResourceParameters = conf.getSaveResourceParameters();
642
643         if (saveResource == null) {
644             throw new ProcessingException("The context " + this.name + " does not support saving.");
645         }
646         // synchronized
647
synchronized (this.authContext) {
648
649             if (parameters != null) {
650                 parameters = (SourceParameters)parameters.clone();
651                 parameters.add(saveResourceParameters);
652             } else if (saveResourceParameters != null) {
653                 parameters = (SourceParameters)saveResourceParameters.clone();
654             }
655             parameters = this.createParameters(parameters,
656                                                path,
657                                                true);
658             DocumentFragment JavaDoc fragment = this.getXML("/application" + path);
659             if (fragment == null) {
660                 // create empty fake fragment
661
fragment = DOMUtil.createDocument().createDocumentFragment();
662             }
663
664             SourceUtil.writeDOM(saveResource,
665                                 null,
666                                 parameters,
667                                 fragment,
668                                 resolver,
669                                 "xml");
670
671         } // end synchronized
672

673     }
674
675     /**
676      * Build parameters for loading and saving of application data
677      */

678     private SourceParameters createParameters(SourceParameters parameters,
679                                                String JavaDoc path,
680                                                boolean appendAppInfo)
681     throws ProcessingException {
682         if (parameters == null) parameters = new SourceParameters();
683
684         final String JavaDoc applicationName = this.getState().getApplicationName();
685
686         // add all elements from inside the handler data
687
this.addParametersFromAuthenticationXML("/data",
688                                                 parameters);
689
690         // add all top level elements from authentication
691
this.addParametersFromAuthenticationXML("",
692                                                 parameters);
693
694         // add application and path
695
parameters.setSingleParameterValue("handler", this.handlerName);
696         if ( appendAppInfo ) {
697             if (applicationName != null) parameters.setSingleParameterValue("application", applicationName);
698         }
699         if (path != null) parameters.setSingleParameterValue("path", path);
700
701         return parameters;
702     }
703
704     /**
705      * Convert the authentication XML of a handler to parameters.
706      * The XML is flat and consists of elements which all have exactly one text node:
707      * <parone>value_one<parone>
708      * <partwo>value_two<partwo>
709      * A parameter can occur more than once with different values.
710      */

711     private void addParametersFromAuthenticationXML(String JavaDoc path,
712                                                      SourceParameters parameters)
713     throws ProcessingException {
714         final DocumentFragment JavaDoc fragment = this.authContext.getXML("/authentication" + path);
715         if (fragment != null) {
716             NodeList JavaDoc childs = fragment.getChildNodes();
717             if (childs != null) {
718                 Node JavaDoc current;
719                 for(int i = 0; i < childs.getLength(); i++) {
720                     current = childs.item(i);
721
722                     // only element nodes
723
if (current.getNodeType() == Node.ELEMENT_NODE) {
724                         current.normalize();
725                         NodeList JavaDoc valueChilds = current.getChildNodes();
726                         String JavaDoc key;
727                         StringBuffer JavaDoc valueBuffer;
728                         String JavaDoc value;
729
730                         key = current.getNodeName();
731                         valueBuffer = new StringBuffer JavaDoc();
732                         for(int m = 0; m < valueChilds.getLength(); m++) {
733                             current = valueChilds.item(m); // attention: current is reused here!
734
if (current.getNodeType() == Node.TEXT_NODE) { // only text nodes
735
if (valueBuffer.length() > 0) valueBuffer.append(' ');
736                                 valueBuffer.append(current.getNodeValue());
737                             }
738                         }
739                         value = valueBuffer.toString().trim();
740                         if (key != null && value != null && value.length() > 0) {
741                             parameters.setParameter(key, value);
742                         }
743                     }
744                 }
745             }
746         }
747     }
748
749     public Map JavaDoc getContextInfo()
750     throws ProcessingException {
751         Map JavaDoc map = (Map JavaDoc)this.authContext.getAttribute( "cachedmap" );
752         if (map == null) {
753             map = new HashMap JavaDoc(20);
754             Parameters pars = this.createParameters(null, null, false).getFirstParameters();
755             String JavaDoc[] names = pars.getNames();
756             if (names != null) {
757                 String JavaDoc key;
758                 String JavaDoc value;
759                 for(int i=0;i<names.length;i++) {
760                     key = names[i];
761                     value = pars.getParameter(key, null);
762                     if (value != null) map.put(key, value);
763                 }
764             }
765             this.authContext.setAttribute("cachedmap", map);
766         }
767         return map;
768     }
769
770     public SourceParameters getContextInfoAsParameters()
771     throws ProcessingException {
772         SourceParameters pars = (SourceParameters)this.authContext.getAttribute( "cachedpar" );
773         if (pars == null) {
774             pars = this.createParameters(null, null, false);
775             this.authContext.setAttribute("cachedpar", pars);
776         }
777         return pars;
778     }
779
780     /**
781      * Load XML of an application
782      */

783     public void loadApplicationXML(ApplicationConfiguration appConf,
784                                     SourceResolver resolver)
785     throws ProcessingException {
786         String JavaDoc loadResource = appConf.getLoadResource();
787         SourceParameters loadResourceParameters = appConf.getLoadResourceParameters();
788         if ( !this.handler.isApplicationLoaded(appConf) && loadResource != null ) {
789             synchronized (this.authContext) {
790
791                 SourceParameters parameters;
792                 if (loadResourceParameters != null) {
793                     parameters = (SourceParameters)loadResourceParameters.clone();
794                 } else {
795                     parameters = new SourceParameters();
796                 }
797                 parameters = this.createParameters(parameters,
798                                                    null,
799                                                    true);
800                 DocumentFragment JavaDoc fragment;
801                 fragment = SourceUtil.readDOM(loadResource,
802                                               null,
803                                               parameters,
804                                               resolver);
805                 this.authContext.setXML("/applications/" + appConf.getName() + '/', fragment);
806
807             } // end synchronized
808
}
809         this.handler.setApplicationIsLoaded(appConf);
810     }
811
812     /**
813      * Test if the user has a role
814      * @since 2.1.6
815      */

816     public boolean isUserInRole(String JavaDoc role) {
817         if ( this.roles == null ) {
818             this.roles = new ArrayList JavaDoc();
819             try {
820                 final String JavaDoc allRoles = (String JavaDoc)this.getContextInfo().get("roles");
821                 final StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( allRoles, ",");
822                 while ( st.hasMoreElements() ) {
823                     this.roles.add(st.nextElement());
824                 }
825             } catch (ProcessingException pe) {
826                 // we ignore this
827
}
828         }
829         if ( this.roles.contains( role ) ) {
830             return true;
831         }
832         final Request req = ContextHelper.getRequest(this.context);
833         return req.isUserInRole(role);
834     }
835 }
836
Popular Tags