001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/csw/configuration/CatalogueConfigurationDocument.java $
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.ogcwebservices.csw.configuration;
038    
039    import java.io.IOException;
040    import java.net.URI;
041    import java.net.URL;
042    
043    import org.deegree.datatypes.xlink.SimpleLink;
044    import org.deegree.framework.log.ILogger;
045    import org.deegree.framework.log.LoggerFactory;
046    import org.deegree.framework.util.StringTools;
047    import org.deegree.framework.xml.ElementList;
048    import org.deegree.framework.xml.InvalidConfigurationException;
049    import org.deegree.framework.xml.NamespaceContext;
050    import org.deegree.framework.xml.XMLParsingException;
051    import org.deegree.framework.xml.XMLTools;
052    import org.deegree.io.JDBCConnection;
053    import org.deegree.model.filterencoding.capabilities.FilterCapabilities;
054    import org.deegree.model.filterencoding.capabilities.FilterCapabilities100Fragment;
055    import org.deegree.model.metadata.iso19115.OnlineResource;
056    import org.deegree.ogcbase.CommonNamespaces;
057    import org.deegree.ogcwebservices.csw.capabilities.CatalogueCapabilitiesDocument;
058    import org.deegree.owscommon.OWSDomainType;
059    import org.w3c.dom.Element;
060    import org.w3c.dom.Node;
061    import org.w3c.dom.NodeList;
062    import org.xml.sax.SAXException;
063    
064    /**
065     * Represents an XML configuration document for a deegree CSW 2.0 instance, i.e. it consists of all
066     * sections common to an OGC CSW 2.0 capabilities document plus a deegree specific section named
067     * <code>deegreeParams</code>.
068     *
069     * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </a>
070     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
071     * @author last edited by: $Author: mschneider $
072     *
073     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
074     */
075    public class CatalogueConfigurationDocument extends CatalogueCapabilitiesDocument {
076    
077        private static ILogger LOG = LoggerFactory.getLogger( CatalogueConfigurationDocument.class );
078    
079        private static final long serialVersionUID = -2923926335089417513L;
080    
081        private static final NamespaceContext nsc = CommonNamespaces.getNamespaceContext();
082    
083        protected static final URI DEEGREECSW = CommonNamespaces.DEEGREECSW;
084    
085        private static final String XML_TEMPLATE = "CatalogueConfigurationTemplate.xml";
086    
087        /**
088         * Creates a skeleton configuration document that contains the mandatory elements only.
089         *
090         * @throws IOException
091         * @throws SAXException
092         */
093        @Override
094        public void createEmptyDocument()
095                                throws IOException, SAXException {
096            URL url = CatalogueConfigurationDocument.class.getResource( XML_TEMPLATE );
097            if ( url == null ) {
098                throw new IOException( "The resource '" + XML_TEMPLATE + " could not be found." );
099            }
100            load( url );
101        }
102    
103        /**
104         * Creates a class representation of the whole document.
105         *
106         * @return class representation of the configuration document
107         * @throws InvalidConfigurationException
108         */
109        public CatalogueConfiguration getConfiguration()
110                                throws InvalidConfigurationException {
111            CatalogueConfiguration configuration = null;
112            try {
113                FilterCapabilities filterCapabilities = null;
114                Element filterCapabilitiesElement = (Element) XMLTools.getNode( getRootElement(),
115                                                                                "ogc:Filter_Capabilities", nsContext );
116                if ( filterCapabilitiesElement != null ) {
117                    filterCapabilities = new FilterCapabilities100Fragment( filterCapabilitiesElement, getSystemId() ).parseFilterCapabilities();
118                }
119                configuration = new CatalogueConfiguration( parseVersion(), parseUpdateSequence(),
120                                                            getServiceIdentification(), getServiceProvider(),
121                                                            getOperationsMetadata(), null, filterCapabilities,
122                                                            getDeegreeParams(), getSystemId(), parseEBRIMCapabilities() );
123            } catch ( XMLParsingException e ) {
124                LOG.logError( e.getMessage(), e );
125                throw new InvalidConfigurationException( "Class representation of the catalog configuration "
126                                                         + "document could not be generated: " + e.getMessage(), e );
127            }
128            return configuration;
129        }
130    
131        /**
132         * Creates a class representation of the <code>deegreeParams</code>- section.
133         *
134         * @return the DeegreeParams of the catalogue
135         * @throws InvalidConfigurationException
136         */
137        public CatalogueDeegreeParams getDeegreeParams()
138                                throws InvalidConfigurationException {
139    
140            CatalogueDeegreeParams deegreeParams = null;
141    
142            try {
143                Node root = this.getRootElement();
144                Element element = XMLTools.getRequiredChildElement( "deegreeParams", DEEGREECSW, root );
145    
146                // 'deegreecsw:DefaultOnlineResource'-element (mandatory)
147                OnlineResource defaultOnlineResource = parseOnLineResource( XMLTools.getRequiredChildElement(
148                                                                                                              "DefaultOnlineResource",
149                                                                                                              DEEGREECSW,
150                                                                                                              element ) );
151    
152                // 'deegreecsw:CacheSize'-element (optional, default: 100)
153                int cacheSize = XMLTools.getNodeAsInt( element, "./deegreecsw:CacheSize", nsContext, 100 );
154    
155                // 'deegreecsw:RequestTimeLimit'-element (optional, default: 2)
156                int requestTimeLimit = XMLTools.getNodeAsInt( element, "./deegreecsw:RequestTimeLimit", nsContext, 2 );
157    
158                // 'deegreecsw:Encoding'-element (optional, default: UTF-8)
159                String characterSet = XMLTools.getStringValue( "Encoding", DEEGREECSW, element, "UTF-8" );
160    
161                // default output schema used by a catalogue
162                String defaultOutputSchema = XMLTools.getStringValue( "DefaultOutputSchema", DEEGREECSW, element, "OGCCORE" );
163    
164                // 'deegreecsw:WFSResource'-element (mandatory)
165                SimpleLink wfsResource = parseSimpleLink( XMLTools.getRequiredChildElement( "WFSResource", DEEGREECSW,
166                                                                                            element ) );
167    
168                // 'deegreecsw:CatalogAddresses'-element (optional)
169                Element catalogAddressesElement = XMLTools.getChildElement( "CatalogAddresses", DEEGREECSW, element );
170                OnlineResource[] catalogAddresses = new OnlineResource[0];
171                if ( catalogAddressesElement != null ) {
172                    // 'deegreecsw:CatalogAddresses'-element (optional)
173                    ElementList el = XMLTools.getChildElements( "CatalogAddress", DEEGREECSW, catalogAddressesElement );
174                    catalogAddresses = new OnlineResource[el.getLength()];
175                    for ( int i = 0; i < catalogAddresses.length; i++ ) {
176                        catalogAddresses[i] = parseOnLineResource( el.item( i ) );
177                    }
178                }
179    
180                OnlineResource transInXslt = null;
181                Element elem = (Element) XMLTools.getNode( element, "deegreecsw:TransactionInputXSLT", nsc );
182                if ( elem != null ) {
183                    transInXslt = parseOnLineResource( elem );
184                }
185                OnlineResource transOutXslt = null;
186                elem = (Element) XMLTools.getNode( element, "deegreecsw:TransactionOutputXSLT", nsc );
187                if ( elem != null ) {
188                    transOutXslt = parseOnLineResource( elem );
189                }
190                if ( ( transInXslt != null && transOutXslt == null ) || ( transInXslt == null && transOutXslt != null ) ) {
191                    throw new InvalidConfigurationException(
192                                                             "if CSW-deegreeParam "
193                                                                                     + "'TransactionInputXSLT' is defined 'TransactionOutputXSLT' must "
194                                                                                     + "be defined too and vice versa!" );
195                }
196    
197                // 'deegreecsw:HarvestRepository'-element (optional)
198                Element harvestRepositoryElement = XMLTools.getChildElement( "HarvestRepository", DEEGREECSW, element );
199                JDBCConnection harvestRepository = null;
200                if ( harvestRepositoryElement != null ) {
201                    // 'deegreecsw:Connection'-element (optional)
202                    Element connectionElement = XMLTools.getChildElement( "Connection", DEEGREECSW,
203                                                                          harvestRepositoryElement );
204                    if ( connectionElement != null ) {
205                        harvestRepository = new JDBCConnection( XMLTools.getRequiredStringValue( "Driver", DEEGREECSW,
206                                                                                                 connectionElement ),
207                                                                XMLTools.getRequiredStringValue( "Logon", DEEGREECSW,
208                                                                                                 connectionElement ),
209                                                                XMLTools.getRequiredStringValue( "User", DEEGREECSW,
210                                                                                                 connectionElement ),
211                                                                XMLTools.getRequiredStringValue( "Password", DEEGREECSW,
212                                                                                                 connectionElement ), null,
213                                                                null, null );
214                    }
215                }
216                deegreeParams = new CatalogueDeegreeParams( defaultOnlineResource, cacheSize, requestTimeLimit,
217                                                            characterSet, wfsResource, catalogAddresses, harvestRepository,
218                                                            defaultOutputSchema, transInXslt, transOutXslt );
219            } catch ( XMLParsingException e ) {
220                LOG.logError( e.getMessage(), e );
221                throw new InvalidConfigurationException( "Error parsing the deegreeParams "
222                                                         + "section of the CSW configuration: \n" + e.getMessage()
223                                                         + StringTools.stackTraceToString( e ) );
224            }
225            return deegreeParams;
226        }
227    
228        /**
229         * Overwritten to cope with additional deegree CSW specific attributes (used in the
230         * "outputSchema" parameter element).
231         *
232         * @param operation
233         * @param parameterElement
234         * @return OWSDomainType
235         * @throws XMLParsingException
236         *
237         * @see "org.deegree.owscommon.OWSCommonCapabilitiesDocument#getParameter(org.w3c.dom.Element)"
238         */
239        @Override
240        protected OWSDomainType getOWSDomainType( String operation, Element parameterElement )
241                                throws XMLParsingException {
242            // "name"-attribute
243            String parameterName = XMLTools.getRequiredAttrValue( "name", null, parameterElement );
244    
245            if ( "GetRecords".equals( operation ) && "outputSchema".equals( parameterName ) ) {
246                // "ows:Value"-elements
247                NodeList valueNodes = parameterElement.getElementsByTagNameNS( OWSNS.toString(), "Value" );
248                CatalogueOutputSchemaValue[] values = new CatalogueOutputSchemaValue[valueNodes.getLength()];
249                for ( int i = 0; i < valueNodes.getLength(); i++ ) {
250                    String value = XMLTools.getStringValue( valueNodes.item( i ) );
251                    String input = XMLTools.getRequiredAttrValue( "input", DEEGREECSW, valueNodes.item( i ) );
252                    String output = XMLTools.getRequiredAttrValue( "output", DEEGREECSW, valueNodes.item( i ) );
253                    if ( value == null || value.equals( "" ) ) {
254                        throw new XMLParsingException( "Missing or empty node '" + value + "'." );
255                    }
256                    values[i] = new CatalogueOutputSchemaValue( value, input, output );
257                }
258                return new CatalogueOutputSchemaParameter( parameterName, values, null );
259            } else if ( "GetRecords".equals( operation ) && "typeName".equals( parameterName ) ) {
260                NodeList valueNodes = parameterElement.getElementsByTagNameNS( OWSNS.toString(), "Value" );
261                CatalogueTypeNameSchemaValue[] values = new CatalogueTypeNameSchemaValue[valueNodes.getLength()];
262                for ( int i = 0; i < valueNodes.getLength(); i++ ) {
263                    String value = XMLTools.getStringValue( valueNodes.item( i ) );
264                    String schema = XMLTools.getRequiredAttrValue( "schema", DEEGREECSW, valueNodes.item( i ) );
265                    values[i] = new CatalogueTypeNameSchemaValue( value, schema );
266                }
267                return new CatalogueTypeNameSchemaParameter( parameterName, values, null );
268            } else {
269                return super.getOWSDomainType( operation, parameterElement );
270            }
271        }
272    }