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