001    //$HeadURL$
002    /*----------------    FILE HEADER  ------------------------------------------
003     This file is part of deegree.
004     Copyright (C) 2001-2008 by:
005     Department of Geography, University of Bonn
006     http://www.giub.uni-bonn.de/deegree/
007     lat/lon GmbH
008     http://www.lat-lon.de
009    
010     This library is free software; you can redistribute it and/or
011     modify it under the terms of the GNU Lesser General Public
012     License as published by the Free Software Foundation; either
013     version 2.1 of the License, or (at your option) any later version.
014     This library is distributed in the hope that it will be useful,
015     but WITHOUT ANY WARRANTY; without even the implied warranty of
016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     Lesser General Public License for more details.
018     You should have received a copy of the GNU Lesser General Public
019     License along with this library; if not, write to the Free Software
020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021     Contact:
022    
023     Andreas Poth
024     lat/lon GmbH
025     Aennchenstr. 19
026     53177 Bonn
027     Germany
028     E-Mail: poth@lat-lon.de
029    
030     Prof. Dr. Klaus Greve
031     Department of Geography
032     University of Bonn
033     Meckenheimer Allee 166
034     53115 Bonn
035     Germany
036     E-Mail: greve@giub.uni-bonn.de
037     ---------------------------------------------------------------------------*/
038    
039    package org.deegree.security;
040    
041    import java.io.File;
042    import java.io.IOException;
043    import java.io.Reader;
044    import java.lang.reflect.Constructor;
045    import java.net.MalformedURLException;
046    import java.net.URL;
047    import java.util.ArrayList;
048    import java.util.HashMap;
049    import java.util.List;
050    import java.util.Map;
051    
052    import org.deegree.datatypes.QualifiedName;
053    import org.deegree.datatypes.parameter.InvalidParameterValueException;
054    import org.deegree.framework.xml.NamespaceContext;
055    import org.deegree.framework.xml.XMLFragment;
056    import org.deegree.framework.xml.XMLParsingException;
057    import org.deegree.framework.xml.XMLTools;
058    import org.deegree.ogcbase.CommonNamespaces;
059    import org.w3c.dom.Document;
060    import org.w3c.dom.Element;
061    import org.w3c.dom.Node;
062    import org.xml.sax.SAXException;
063    
064    /**
065     * 
066     * 
067     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
068     * @author last edited by: $Author: poth $
069     * 
070     * @version. $Revision: 6251 $, $Date: 2007-03-19 16:59:28 +0100 (Mo, 19 Mrz 2007) $
071     */
072    public class AuthenticationDocument extends XMLFragment {
073    
074        private static NamespaceContext nsc = CommonNamespaces.getNamespaceContext();
075    
076        /**
077         * 
078         * 
079         */
080        public AuthenticationDocument() {
081            super();
082        }
083    
084        /**
085         * 
086         * @param doc
087         * @param systemId
088         * @throws MalformedURLException
089         */
090        public AuthenticationDocument( Document doc, String systemId ) throws MalformedURLException {
091            super( doc, systemId );
092        }
093    
094        /**
095         * 
096         * @param element
097         */
098        public AuthenticationDocument( Element element ) {
099            super( element );
100        }
101    
102        /**
103         * 
104         * @param file
105         * @throws MalformedURLException
106         * @throws IOException
107         * @throws SAXException
108         */
109        public AuthenticationDocument( File file ) throws MalformedURLException, IOException, SAXException {
110            super( file );
111        }
112    
113        /**
114         * 
115         * @param elementName
116         */
117        public AuthenticationDocument( QualifiedName elementName ) {
118            super( elementName );
119        }
120    
121        /**
122         * 
123         * @param reader
124         * @param systemId
125         * @throws SAXException
126         * @throws IOException
127         */
128        public AuthenticationDocument( Reader reader, String systemId ) throws SAXException, IOException {
129            super( reader, systemId );
130        }
131    
132        /**
133         * 
134         * @param url
135         * @throws IOException
136         * @throws SAXException
137         */
138        public AuthenticationDocument( URL url ) throws IOException, SAXException {
139            super( url );
140        }
141    
142        /**
143         * parses the authentications document and returns the content as an instance of
144         * 
145         * @see Authentications
146         * @return
147         * @throws XMLParsingException
148         */
149        public Authentications createAuthentications()
150                                throws XMLParsingException {
151    
152            List<Node> methodNodes = XMLTools.getNodes( getRootElement(), "//Method", nsc );
153            List<AbstractAuthentication> authentications = new ArrayList<AbstractAuthentication>();
154            for ( Node node : methodNodes ) {
155                String name = XMLTools.getRequiredNodeAsString( node, "./@name", nsc );
156                String className = XMLTools.getRequiredNodeAsString( node, "./class/text()", nsc );
157    
158                // parameter type for map of init-params and authentication name
159                Class[] cl = new Class[2];
160                cl[0] = String.class;
161                cl[1] = Map.class;
162    
163                // set parameter to submitt to the constructor
164                Object[] o = new Object[2];
165                o[0] = name;
166                o[1] = createInitParams( node );
167    
168                try {
169                    Class clzz = Class.forName( className );
170                    Constructor con = clzz.getConstructor( cl );
171                    authentications.add( (AbstractAuthentication) con.newInstance( o ) );
172                } catch ( Exception e ) {
173                    throw new InvalidParameterValueException( e.getMessage(), "class", className );
174                }
175            }
176            return new Authentications( authentications );
177        }
178    
179        /**
180         * 
181         * @param methodNode
182         * @return
183         * @throws XMLParsingException
184         */
185        private Map<String, String> createInitParams( Node methodNode )
186                                throws XMLParsingException {
187            List<Node> nodes = XMLTools.getNodes( methodNode, "./init-param", nsc );
188            Map<String, String> map = new HashMap<String, String>();
189            for ( Node node : nodes ) {
190                String name = XMLTools.getRequiredNodeAsString( node, "./name/text()", nsc );
191                String value = XMLTools.getRequiredNodeAsString( node, "./value/text()", nsc );
192                map.put( name, value );
193            }
194            return map;
195        }
196    
197    }