001    //$HeadURL$
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    
037    package org.deegree.security;
038    
039    import java.io.File;
040    import java.io.IOException;
041    import java.io.Reader;
042    import java.lang.reflect.Constructor;
043    import java.net.MalformedURLException;
044    import java.net.URL;
045    import java.util.ArrayList;
046    import java.util.HashMap;
047    import java.util.List;
048    import java.util.Map;
049    
050    import org.deegree.datatypes.QualifiedName;
051    import org.deegree.datatypes.parameter.InvalidParameterValueException;
052    import org.deegree.framework.xml.NamespaceContext;
053    import org.deegree.framework.xml.XMLFragment;
054    import org.deegree.framework.xml.XMLParsingException;
055    import org.deegree.framework.xml.XMLTools;
056    import org.deegree.ogcbase.CommonNamespaces;
057    import org.w3c.dom.Document;
058    import org.w3c.dom.Element;
059    import org.w3c.dom.Node;
060    import org.xml.sax.SAXException;
061    
062    /**
063     *
064     *
065     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
066     * @author last edited by: $Author: poth $
067     *
068     * @version $Revision: 6251 $, $Date: 2007-03-19 16:59:28 +0100 (Mo, 19 Mrz 2007) $
069     */
070    public class AuthenticationDocument extends XMLFragment {
071    
072        private static final long serialVersionUID = 9122355584178027980L;
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         * {@link Authentications}
145         *
146         * @return new Authentications object
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 submit 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    }