001    // $HeadURL:
002    // /cvsroot/deegree/src/org/deegree/ogcwebservices/csw/capabilities/CatalogCapabilitiesDocument.java,v
003    // 1.22 2004/08/05 15:40:08 ap Exp $
004    /*----------------------------------------------------------------------------
005     This file is part of deegree, http://deegree.org/
006     Copyright (C) 2001-2009 by:
007       Department of Geography, University of Bonn
008     and
009       lat/lon GmbH
010    
011     This library is free software; you can redistribute it and/or modify it under
012     the terms of the GNU Lesser General Public License as published by the Free
013     Software Foundation; either version 2.1 of the License, or (at your option)
014     any later version.
015     This library is distributed in the hope that it will be useful, but WITHOUT
016     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
017     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
018     details.
019     You should have received a copy of the GNU Lesser General Public License
020     along with this library; if not, write to the Free Software Foundation, Inc.,
021     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022    
023     Contact information:
024    
025     lat/lon GmbH
026     Aennchenstr. 19, 53177 Bonn
027     Germany
028     http://lat-lon.de/
029    
030     Department of Geography, University of Bonn
031     Prof. Dr. Klaus Greve
032     Postfach 1147, 53001 Bonn
033     Germany
034     http://www.geographie.uni-bonn.de/deegree/
035    
036     e-mail: info@deegree.org
037    ----------------------------------------------------------------------------*/
038    package org.deegree.ogcwebservices.wass.common;
039    
040    import java.io.IOException;
041    import java.net.MalformedURLException;
042    import java.net.URI;
043    import java.net.URISyntaxException;
044    import java.net.URL;
045    import java.util.ArrayList;
046    import java.util.List;
047    
048    import org.deegree.datatypes.Code;
049    import org.deegree.datatypes.xlink.SimpleLink;
050    import org.deegree.framework.xml.XMLParsingException;
051    import org.deegree.framework.xml.XMLTools;
052    import org.deegree.i18n.Messages;
053    import org.deegree.model.metadata.iso19115.Keywords;
054    import org.deegree.ogcbase.CommonNamespaces;
055    import org.deegree.ogcwebservices.getcapabilities.DCPType;
056    import org.deegree.ogcwebservices.getcapabilities.InvalidCapabilitiesException;
057    import org.deegree.ogcwebservices.getcapabilities.ServiceIdentification;
058    import org.deegree.ogcwebservices.getcapabilities.ServiceProvider;
059    import org.deegree.owscommon.OWSCommonCapabilitiesDocument;
060    import org.deegree.owscommon.OWSDomainType;
061    import org.deegree.owscommon.OWSMetadata;
062    import org.w3c.dom.Element;
063    import org.w3c.dom.Node;
064    import org.xml.sax.SAXException;
065    
066    /**
067     * The owscapabilities 1.0 specification parser. This class is able to parse the changes made in the
068     * ows capabilites specifications 1.0.
069     *
070     *
071     * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a>
072     * @author last edited by: $Author: mschneider $
073     *
074     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
075     */
076    public abstract class OWSCapabilitiesBaseDocument_1_0 extends OWSCommonCapabilitiesDocument {
077    
078        private static final long serialVersionUID = -3518136831402464466L;
079    
080        private static final String PRE = CommonNamespaces.OWS_PREFIX + ":";
081    
082        /**
083         * Creates an empty document loaded from the template given.
084         *
085         * @param template
086         *            the location/name of the template
087         * @throws IOException
088         * @throws SAXException
089         */
090        public void createEmptyDocument( String template )
091                                throws IOException, SAXException {
092            URL url = OWSCapabilitiesBaseDocument_1_0.class.getResource( template );
093            if ( url == null ) {
094                throw new IOException( Messages.getMessage( "WASS_ERROR_RESOURCE_NOT_FOUND", template ) );
095            }
096            load( url );
097        }
098    
099        /**
100         * Returns the class representation for the <code>ServiceIdentification</code> section of the
101         * document, according to the ows 1.0 spec.
102         *
103         * @return class representation for the <code>ServiceIdentification</code> section
104         * @throws XMLParsingException
105         */
106        public ServiceIdentification parseServiceIdentification()
107                                throws XMLParsingException {
108    
109            Element element = (Element) XMLTools.getRequiredNode( getRootElement(), PRE + "ServiceIdentification",
110                                                                  nsContext );
111    
112            // 'ServiceType' element (mandatory)
113            Element serviceTypeElement = (Element) XMLTools.getRequiredNode( element, PRE + "ServiceType", nsContext );
114            Code serviceType = null;
115            try {
116                String codeSpace = XMLTools.getAttrValue( serviceTypeElement, null, "codeSpace", null );
117                URI uri = codeSpace != null ? new URI( codeSpace ) : null;
118                serviceType = new Code( XMLTools.getStringValue( serviceTypeElement ), uri );
119            } catch ( URISyntaxException e ) {
120                String s = XMLTools.getAttrValue( serviceTypeElement, null, "codeSpace", null );
121                throw new XMLParsingException( Messages.getMessage( "WASS_ERROR_CODESPACE_NOT_URI",
122                                                                new Object[] { s, OWSNS.toString() } ) );
123            }
124    
125            // 'ServiceTypeVersion' elements (mandatory)
126            String[] serviceTypeVersions = XMLTools.getRequiredNodeAsStrings( element, PRE + "ServiceTypeVersion",
127                                                                              nsContext, ",;" );
128    
129            // 'Title' element (optional)
130            String title = XMLTools.getNodeAsString( element, PRE + "Title", nsContext,
131                                                     Messages.getMessage( "WASS_NO_TITLE" ) );
132    
133            // 'Abstract' element (optional)
134            String serviceAbstract = XMLTools.getNodeAsString(
135                                                               element,
136                                                               PRE + "Abstract",
137                                                               nsContext,
138                                                               Messages.getMessage( "WASS_NO_ABSTRACT_DOCUMENTATION" ) );
139    
140            // 'Keywords' elements (optional)
141            List<Node> keywordsList = XMLTools.getNodes( element, PRE + "Keywords", nsContext );
142            Keywords[] keywords = getKeywords( keywordsList );
143    
144            // 'Fees' element (optional)
145            String fees = XMLTools.getNodeAsString( element, PRE + "Fees", nsContext, null );
146    
147            // 'AccessConstraints' elements (optional)
148            String[] accessConstraints = XMLTools.getNodesAsStrings( element, PRE + "AccessConstraints", nsContext );
149    
150            ServiceIdentification serviceIdentification = new ServiceIdentification( serviceType, serviceTypeVersions,
151                                                                                     title, serviceAbstract, keywords,
152                                                                                     fees, accessConstraints );
153            return serviceIdentification;
154        }
155    
156        /**
157         * Wrapper to make it consistent.
158         *
159         * @return the ServiceProvider representation
160         * @throws XMLParsingException
161         */
162        public ServiceProvider parseServiceProvider()
163                                throws XMLParsingException {
164            return getServiceProvider();
165        }
166    
167        /**
168         * @return the OperationsMetada representation
169         * @throws XMLParsingException
170         * @throws InvalidCapabilitiesException
171         * @throws URISyntaxException
172         */
173        public OperationsMetadata_1_0 parseOperationsMetadata()
174                                throws XMLParsingException, InvalidCapabilitiesException, URISyntaxException {
175    
176            Element root = (Element) XMLTools.getRequiredNode( getRootElement(), PRE + "OperationsMetadata", nsContext );
177    
178            ArrayList<Operation_1_0> operations = new ArrayList<Operation_1_0>();
179    
180            List<Node> nl = XMLTools.getRequiredNodes( root, PRE + "Operation", nsContext );
181    
182            for ( int i = 0; i < nl.size(); ++i ) {
183                operations.add( parseOperation( (Element) nl.get( i ) ) );
184            }
185    
186            List<Node> parameterList = XMLTools.getNodes( root, PRE + "Parameter", nsContext );
187            OWSDomainType[] parameters = new OWSDomainType[parameterList.size()];
188            for ( int i = 0; i < parameters.length; i++ ) {
189                parameters[i] = parseOWSDomainType_1_0( (Element) parameterList.get( i ) );
190            }
191    
192            List<Node> constraintList = XMLTools.getNodes( root, PRE + "Constraint", nsContext );
193            OWSDomainType[] constraints = new OWSDomainType[constraintList.size()];
194            for ( int i = 0; i < constraints.length; i++ ) {
195                constraints[i] = parseOWSDomainType_1_0( (Element) constraintList.get( i ) );
196            }
197    
198            String extendedCapabilities = XMLTools.getNodeAsString( root, PRE + "ExtendedCapabilities", nsContext, null );
199    
200            // find the !not specified in the gdinrw! describeuser
201            Operation_1_0 describeUser = null;
202            // and find the GetCapabilities operation too.
203            Operation_1_0 getCapa = null;
204            for ( int i = 0; i < operations.size(); ++i ) {
205                if ( operations.get( i ).getName().equalsIgnoreCase( "describeUser" ) ) {
206                    describeUser = operations.get( i );
207                }
208                if ( operations.get( i ).getName().equalsIgnoreCase( "GetCapabilities" ) ) {
209                    getCapa = operations.get( i );
210                }
211            }
212    
213            if ( getCapa == null ) {
214                throw new InvalidCapabilitiesException(
215                                                        Messages.getMessage(
216                                                                         "WASS_ERROR_CAPABILITIES_MISSING_REQUIRED_OPERATION",
217                                                                         "GetCapabilities" ) );
218            }
219            if ( describeUser == null ) {
220                throw new InvalidCapabilitiesException(
221                                                        Messages.getMessage(
222                                                                         "WASS_ERROR_CAPABILITIES_MISSING_REQUIRED_OPERATION",
223                                                                         "DescribeUser" ) );
224            }
225    
226            OperationsMetadata_1_0 om = new OperationsMetadata_1_0(
227                                                                    operations.toArray( new Operation_1_0[operations.size()] ),
228                                                                    parameters, constraints, extendedCapabilities,
229                                                                    describeUser, getCapa );
230    
231    
232    
233            return om;
234        }
235    
236        /**
237         * @param root
238         *            the root node of the Operation xml fragment.
239         * @return The operation representation 1.0 conform
240         * @throws XMLParsingException
241         * @throws URISyntaxException
242         */
243        @SuppressWarnings("unchecked")
244        public Operation_1_0 parseOperation( Element root )
245                                throws XMLParsingException, URISyntaxException {
246    
247            DCPType[] dcps = getDCPs( XMLTools.getRequiredElements( root, PRE + "DCP", nsContext ) );
248    
249            String name = XMLTools.getAttrValue( root, null, "name", null );
250    
251            List parameterList = XMLTools.getNodes( root, PRE + "Parameter", nsContext );
252            OWSDomainType[] parameters = new OWSDomainType[parameterList.size()];
253            for ( int i = 0; i < parameters.length; i++ ) {
254                parameters[i] = parseOWSDomainType_1_0( (Element) parameterList.get( i ) );
255            }
256    
257            List constraintList = XMLTools.getNodes( root, PRE + "Constraint", nsContext );
258            OWSDomainType[] constraints = new OWSDomainType[constraintList.size()];
259            for ( int i = 0; i < constraints.length; i++ ) {
260                constraints[i] = parseOWSDomainType_1_0( (Element) constraintList.get( i ) );
261            }
262    
263            List metadata = XMLTools.getNodes( root, PRE + "Metadata", nsContext );
264    
265            ArrayList<OWSMetadata> metadatas = new ArrayList<OWSMetadata>();
266    
267            for ( int i = 0; i < metadata.size(); ++i ) {
268                metadatas.add( parseOWSMetadata( (Element) metadata.get( i ), name ) );
269            }
270    
271            Operation_1_0 operation = new Operation_1_0(
272                                                         name,
273                                                         dcps,
274                                                         parameters,
275                                                         constraints,
276                                                         (OWSMetadata[]) metadata.toArray( new OWSMetadata[metadata.size()] ) );
277    
278    
279            return operation;
280        }
281    
282        private OWSMetadata parseOWSMetadata( Element root, String name )
283                                throws XMLParsingException, URISyntaxException {
284    
285            SimpleLink link = parseSimpleLink( root );
286            URI about = new URI( XMLTools.getAttrValue( root, null, "about", null ) );
287    
288            // TODO: find out real name
289            OWSMetadata md = new OWSMetadata( about, link, name );
290    
291            return md;
292        }
293    
294        /**
295         * @param element
296         *            The element to get the Domaintype from.
297         * @return the OWSDomainType representation.
298         * @throws XMLParsingException
299         *
300         */
301        private OWSDomainType parseOWSDomainType_1_0( Element element )
302                                throws XMLParsingException {
303            return getOWSDomainType( null, element );
304        }
305    
306        /**
307         * @param nameSpace
308         *            The namespace of the supportedauthenticationlist should be wss or was
309         * @return ArrayList<SupportedAuthenticationMethod>
310         * @throws XMLParsingException
311         * @throws MalformedURLException
312         */
313        public ArrayList<SupportedAuthenticationMethod> parseSupportedAuthenticationMethods( String nameSpace )
314                                throws XMLParsingException, MalformedURLException {
315            Element capability = (Element) XMLTools.getRequiredNode( getRootElement(), nameSpace + ":Capability", nsContext );
316            Element methodList = (Element) XMLTools.getRequiredNode( capability, nameSpace
317                                                                                 + ":SupportedAuthenticationMethodList",
318                                                                     nsContext );
319            ArrayList<SupportedAuthenticationMethod> result = new AuthenticationDocument().parseSupportedAuthenticationMethodList( methodList );
320    
321            return result;
322        }
323    
324    }