001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/ogcwebservices/wass/common/AuthenticationDocument.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003     
004     This file is part of deegree.
005     Copyright (C) 2001-2004 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/exse/
008     lat/lon GmbH
009     http://www.lat-lon.de
010     
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015     
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020     
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024     
025     Contact:
026     
027     Andreas Poth
028     lat/lon GmbH
029     Meckenheimer Allee 176
030     53115 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033     
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041     
042     ---------------------------------------------------------------------------*/
043    
044    package org.deegree.ogcwebservices.wass.common;
045    
046    import java.net.MalformedURLException;
047    import java.net.URL;
048    import java.util.ArrayList;
049    import java.util.List;
050    
051    import org.deegree.framework.xml.XMLFragment;
052    import org.deegree.framework.xml.XMLParsingException;
053    import org.deegree.framework.xml.XMLTools;
054    import org.deegree.ogcbase.CommonNamespaces;
055    import org.w3c.dom.Element;
056    import org.w3c.dom.Node;
057    
058    /**
059     * Parser class that can parse all elements within the namespace.
060     * 
061     * Namespace: http://www.gdi-nrw.de/authentication
062     * 
063     * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
064     * @author last edited by: $Author: aschmitz $
065     * 
066     * @version 2.0, $Revision: 6895 $, $Date: 2007-05-08 14:49:27 +0200 (Di, 08 Mai 2007) $
067     * 
068     * @since 2.0
069     */
070    public class AuthenticationDocument extends XMLFragment {
071    
072        private static final long serialVersionUID = -6467874541905139362L;
073    
074        private final static String PRE = CommonNamespaces.GDINRW_AUTH_PREFIX + ":";
075    
076        /**
077         * Parses a SupportedAuthenticationMethodList element.
078         * 
079         * @param listRoot
080         *            the list element
081         * @return an ArrayList with the parsed methods
082         * @throws MalformedURLException
083         * @throws XMLParsingException
084         */
085        public ArrayList<SupportedAuthenticationMethod> parseSupportedAuthenticationMethodList(
086                                                                                               Element listRoot )
087                                throws MalformedURLException, XMLParsingException {
088            
089            List<Node> methods = XMLTools.getRequiredNodes( listRoot, PRE + "SupportedAuthenticationMethod",
090                                                      nsContext );
091            ArrayList<SupportedAuthenticationMethod> values = new ArrayList<SupportedAuthenticationMethod>();
092            for ( Object element : methods ) {
093                values.add( parseSupportedAuthenticationMethod( (Element) element ) );
094            }
095            
096            return values;
097        }
098    
099        /**
100         * Parses a SupportedAuthenticationMethod element.
101         * 
102         * @param elem
103         *            the element
104         * @return the parsed data
105         * @throws XMLParsingException
106         * @throws MalformedURLException
107         */
108        public SupportedAuthenticationMethod parseSupportedAuthenticationMethod( Node elem )
109                                throws XMLParsingException, MalformedURLException {
110            
111            Node method = XMLTools.getNode( elem, PRE + "AuthenticationMethod", nsContext );
112            URN methodURN = parseAuthenticationMethod( method );
113            Node metadata = XMLTools.getNode( elem, PRE + "WASAuthenticationMethodMD", nsContext );
114            if ( metadata != null ) {
115                WASAuthenticationMethodMD wasamd = parseWASAuthenticationMethodMD( metadata );
116                return new SupportedAuthenticationMethod( methodURN, wasamd );
117            }
118            metadata = XMLTools.getNode( elem, PRE + "UnknownMethodMetadata", nsContext );
119            String ummd = null;
120            if ( metadata != null )
121                ummd = parseUnknownMethodMetadata( metadata );
122    
123            SupportedAuthenticationMethod result = new SupportedAuthenticationMethod( methodURN, ummd );
124            
125            return result;
126        }
127    
128        /**
129         * Parses an AuthenticationMethod.
130         * 
131         * @param elem
132         *            the AuthenticationMethod element
133         * @return an URN with the method
134         * @throws XMLParsingException
135         */
136        public URN parseAuthenticationMethod( Node elem )
137                                throws XMLParsingException {
138            return new URN( XMLTools.getNodeAsString( elem, "@id", nsContext, null ) );
139        }
140    
141        /**
142         * Parses an UnknownMethodMetadata element.
143         * 
144         * @param elem
145         *            the element
146         * @return a String with the data
147         * @throws XMLParsingException
148         */
149        public String parseUnknownMethodMetadata( Node elem )
150                                throws XMLParsingException {
151            return XMLTools.getNodeAsString( elem, ".", nsContext, null );
152        }
153    
154        /**
155         * Parses a WASAuthenticationMethodMD element.
156         * 
157         * @param elem
158         *            the element
159         * @return an object with the parsed data
160         * @throws XMLParsingException
161         * @throws MalformedURLException
162         */
163        public WASAuthenticationMethodMD parseWASAuthenticationMethodMD( Node elem )
164                                throws XMLParsingException, MalformedURLException {
165            
166            String mdName = XMLTools.getRequiredNodeAsString( elem, PRE + "Name", nsContext );
167            URL mdURL = XMLTools.getRequiredNodeAsURI( elem, PRE + "URL", nsContext ).toURL();
168            ArrayList<URN> mdAuthenticationMethods = new ArrayList<URN>();
169            String[] urns = XMLTools.getNodesAsStrings( elem, PRE + "AuthenticationMethod", nsContext );
170            for ( int i = 0; i < urns.length; ++i )
171                mdAuthenticationMethods.add( new URN( urns[i] ) );
172    
173            WASAuthenticationMethodMD result = new WASAuthenticationMethodMD( mdName, mdURL,
174                                                                              mdAuthenticationMethods );
175            
176            return result;
177        }
178    
179        /**
180         * Parses an AuthenticationData element
181         * 
182         * @param elem
183         *            the element
184         * @return an object with the parsed data
185         * @throws XMLParsingException
186         */
187        public AuthenticationData parseAuthenticationData( Node elem )
188                                throws XMLParsingException {
189            
190            Node method = XMLTools.getRequiredNode( elem, PRE + "AuthenticationMethod", nsContext );
191            URN authenticationMethod = parseAuthenticationMethod( method );
192            Node cred = XMLTools.getRequiredNode( elem, PRE + "Credentials", nsContext );
193            String credentials = parseCredentials( cred );
194    
195            AuthenticationData result = new AuthenticationData( authenticationMethod, credentials );
196            
197            return result;
198        }
199    
200        /**
201         * Parses a Credentials element.
202         * 
203         * @param elem
204         *            the element
205         * @return a String containing the data
206         * @throws XMLParsingException
207         */
208        public String parseCredentials( Node elem )
209                                throws XMLParsingException {
210            return XMLTools.getRequiredNodeAsString( elem, ".", nsContext );
211        }
212    
213    }