001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/csw/capabilities/CatalogueGetCapabilities.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    package org.deegree.ogcwebservices.csw.capabilities;
037    
038    import java.util.Map;
039    
040    import org.deegree.framework.log.ILogger;
041    import org.deegree.framework.log.LoggerFactory;
042    import org.deegree.framework.util.StringTools;
043    import org.deegree.i18n.Messages;
044    import org.deegree.ogcbase.ExceptionCode;
045    import org.deegree.ogcwebservices.InvalidParameterValueException;
046    import org.deegree.ogcwebservices.OGCWebServiceException;
047    import org.deegree.ogcwebservices.csw.CSWPropertiesAccess;
048    import org.deegree.ogcwebservices.getcapabilities.GetCapabilities;
049    import org.w3c.dom.Element;
050    
051    /**
052     * Class representation of an <code>OGC-GetCapabilities</code> request in <code>CSW</code>
053     * flavour.
054     * <p>
055     * Special to the <code>CSW</code> version of the <code>GetCapabilities</code> request are these
056     * two additional parameters: <table border="1">
057     * <tr>
058     * <th>Name</th>
059     * <th>Occurences</th>
060     * <th>Function</th>
061     * </tr>
062     * <tr>
063     * <td>AcceptVersions</td>
064     * <td align="center">0|1</td>
065     * <td>Protocol versions supported by this service.</td>
066     * </tr>
067     * <tr>
068     * <td>AcceptFormats</td>
069     * <td align="center">0|1</td>
070     * <td>Formats accepted by this service.</td>
071     * </tr>
072     * </table>
073     *
074     * @since 2.0
075     * @author <a href="mailto:mschneider@lat-lon.de">Markus Schneider </a>
076     * @version $Revision: 18195 $
077     */
078    public class CatalogueGetCapabilities extends GetCapabilities {
079    
080        private static final long serialVersionUID = 7690283041658363481L;
081    
082        private static final ILogger LOG = LoggerFactory.getLogger( CatalogueGetCapabilities.class );
083    
084        /**
085         * Creates a new <code>CatalogueGetCapabilities</code> instance.
086         *
087         * @param id
088         *            request identifier
089         * @param updateSequence
090         * @param version
091         * @param acceptVersions
092         * @param acceptFormats
093         * @param sections
094         * @param vendoreSpec
095         */
096        CatalogueGetCapabilities( String id, String updateSequence, String version, String[] acceptVersions,
097                                  String[] acceptFormats, String[] sections, Map<String, String> vendoreSpec ) {
098            super( id, version, updateSequence, acceptVersions, sections, acceptFormats, vendoreSpec );
099        }
100    
101        /**
102         * Creates a <code>CatalogGetCapabilities</code> request from its KVP representation.
103         *
104         * @param kvp
105         *            Map containing the key-value pairs
106         * @return created <code>CatalogGetCapabilities</code> object
107         * @throws InvalidParameterValueException
108         */
109        public static CatalogueGetCapabilities create( Map<String, String> kvp )
110                                throws InvalidParameterValueException {
111    
112            String id = getParam( "ID", kvp, null );
113            // use 2.0.0 as default version
114            String version = getParam( "VERSION", kvp, CSWPropertiesAccess.getString( "DEFAULTVERSION" ) );
115            if ( !"2.0.0".equals( version ) && !"2.0.1".equals( version ) && !"2.0.2".equals( version ) ) {
116                throw new InvalidParameterValueException( Messages.get( "CSW_UNSUPPORTED_VERSION" ) );
117            }
118            String updateSequence = getParam( "UPDATESEQUENCE", kvp, null );
119            String[] acceptVersions = null;
120            if ( kvp.get( "ACCEPTVERSIONS" ) != null ) {
121                String tmp = getParam( "ACCEPTVERSIONS", kvp, null );
122    
123                acceptVersions = StringTools.toArray( tmp, ",", false );
124                version = CatalogueGetCapabilities.validateVersion( acceptVersions );
125                if ( version == null ) {
126                    throw new InvalidParameterValueException( Messages.get( "CSW_UNSUPPORTED_VERSION" ) );
127                }
128            }
129            LOG.logInfo( "process with version:", version );
130            String[] acceptFormats = null;
131            if ( kvp.get( "OUTPUTFORMAT" ) != null ) {
132                String tmp = getParam( "OUTPUTFORMAT", kvp, null );
133                acceptFormats = StringTools.toArray( tmp, ",", false );
134            }
135            String[] sections = null;
136            if ( kvp.get( "SECTIONS" ) != null ) {
137                String tmp = getParam( "SECTIONS", kvp, null );
138                sections = StringTools.toArray( tmp, ",", false );
139            }
140            return new CatalogueGetCapabilities( id, updateSequence, version, acceptVersions, acceptFormats, sections, kvp );
141    
142        }
143    
144        /**
145         * Creates a <code>CatalogGetCapabilities</code> request from its XML representation.
146         *
147         * @param id
148         *            unique ID of the request
149         * @param root
150         *            XML representation of the request
151         * @return created <code>CatalogGetCapabilities</code> object
152         * @throws OGCWebServiceException
153         *             thrown if something in the request is wrong
154         */
155        public static CatalogueGetCapabilities create( String id, Element root )
156                                throws OGCWebServiceException {
157    
158            CatalogueGetCapabilitiesDocument doc = new CatalogueGetCapabilitiesDocument();
159            doc.setRootElement( root );
160            CatalogueGetCapabilities request;
161            try {
162                request = doc.parse( id );
163            } catch ( Exception e ) {
164                LOG.logError( e.getMessage(), e );
165                ExceptionCode code = ExceptionCode.INVALID_FORMAT;
166                throw new OGCWebServiceException( "CatalogGetCapabilities", e.getMessage(), code );
167            }
168            return request;
169        }
170    
171        /**
172         * returns WCS as service name
173         */
174        public String getServiceName() {
175            return "CSW";
176        }
177    
178        /**
179         *
180         * @param acceptVersions
181         * @return the highst supported version or <code>null</code> if none of the passed versions is
182         *         supported
183         */
184        static String validateVersion( String[] acceptVersions ) {
185            String version = null;
186            for ( int i = 0; i < acceptVersions.length; i++ ) {
187                if ( acceptVersions[i].equals( "2.0.0" ) || acceptVersions[i].equals( "2.0.1" )
188                     || acceptVersions[i].equals( "2.0.2" ) ) {
189                    if ( version == null ) {
190                        version = acceptVersions[i];
191                    }
192                    if ( acceptVersions[i].compareTo( version ) > 0 ) {
193                        version = acceptVersions[i];
194                    }
195                }
196            }
197    
198            return version;
199        }
200    }