001    // $HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wcs/getcapabilities/WCSGetCapabilities.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.wcs.getcapabilities;
037    
038    import java.io.UnsupportedEncodingException;
039    import java.net.URLDecoder;
040    import java.util.Map;
041    
042    import org.deegree.framework.log.ILogger;
043    import org.deegree.framework.log.LoggerFactory;
044    import org.deegree.framework.util.CharsetUtils;
045    import org.deegree.framework.util.KVP2Map;
046    import org.deegree.framework.util.StringTools;
047    import org.deegree.framework.xml.XMLParsingException;
048    import org.deegree.framework.xml.XMLTools;
049    import org.deegree.ogcbase.CommonNamespaces;
050    import org.deegree.ogcbase.ExceptionCode;
051    import org.deegree.ogcwebservices.InvalidParameterValueException;
052    import org.deegree.ogcwebservices.MissingParameterValueException;
053    import org.deegree.ogcwebservices.OGCWebServiceException;
054    import org.deegree.ogcwebservices.getcapabilities.GetCapabilities;
055    import org.w3c.dom.Document;
056    import org.w3c.dom.Element;
057    
058    /**
059     * Each Web Coverage Server must describe its capabilities. This clause defines the structure
060     * intended to convey general information about the service itself, and summary information about
061     * the available data collections from which coverages may be requested.<p/> An instance of
062     * <tt>WCSGetCapabilities</tt> encapsulates a GetCapabilites request against a WCS and offeres two
063     * factory methods inherited from <tT>GetCapabilities</tt> for request creation using KVP and one
064     * own method for request creation from a DOM object.
065     *
066     * @version $Revision: 18195 $
067     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
068     * @author last edited by: $Author: mschneider $
069     *
070     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
071     *
072     * @since 2.0
073     */
074    public class WCSGetCapabilities extends GetCapabilities {
075    
076        private static final ILogger LOG = LoggerFactory.getLogger( WCSGetCapabilities.class );
077    
078        /**
079         * creates a GetCapabilities request from its KVP representation
080         *
081         * @param id
082         *            unique ID of the request
083         * @param kvp
084         *            request
085         * @return created <tt>DescribeCoverage</tt>
086         * @throws OGCWebServiceException
087         *             will be thrown if something general is wrong
088         * @throws InvalidParameterValueException
089         * @throws MissingParameterValueException
090         */
091        public static GetCapabilities create( String id, String kvp )
092                                throws OGCWebServiceException, InvalidParameterValueException,
093                                MissingParameterValueException {
094            Map<String, String> map = KVP2Map.toMap( kvp );
095            map.put( "ID", id );
096            return create( map );
097        }
098    
099        /**
100         * creates a GetCapabilities request from its KVP representation
101         *
102         * @param map
103         *            request
104         * @return created <tt>DescribeCoverage</tt>
105         * @throws OGCWebServiceException
106         *             will be thrown if something general is wrong
107         * @throws InvalidParameterValueException
108         * @throws MissingParameterValueException
109         */
110        public static GetCapabilities create( Map<String, String> map )
111                                throws OGCWebServiceException, InvalidParameterValueException,
112                                MissingParameterValueException {
113    
114            String version = getParam( "VERSION", map, "1.0.0" );
115            /*
116             * if ( version == null ) { version = "1.0.0"; } if ( version.compareTo("1.0.0") < 0 ) {
117             * ExceptionCode code = ExceptionCode.INVALIDPARAMETERVALUE; throw new
118             * InvalidParameterValueException( "WCSGetCapabilities", "version must be equal to " + "or
119             * greater than 1.0.0", code ); } else { version = "1.0.0"; }
120             */
121    
122            String service = getRequiredParam( "SERVICE", map );
123            if ( !service.equals( "WCS" ) ) {
124                ExceptionCode code = ExceptionCode.INVALIDPARAMETERVALUE;
125                throw new InvalidParameterValueException( "WCSGetCapabilities", "'service' must be 'WCS'", code );
126            }
127            String updateSeq = getParam( "UPDATESEQUENCE", map, null );
128            String tmp = getParam( "SECTION", map, null );
129            String[] sections = null;
130            if ( tmp != null ) {
131                sections = StringTools.toArray( tmp, ",", true );
132                if ( !validateSection( sections ) ) {
133                    ExceptionCode code = ExceptionCode.INVALIDPARAMETERVALUE;
134                    throw new InvalidParameterValueException( "WCSGetCapabilities", "invalid value for section parameter",
135                                                              code );
136                }
137            }
138            String id = getParam( "ID", map, "" + System.currentTimeMillis() );
139    
140            return new WCSGetCapabilities( id, version, updateSeq, sections, map );
141        }
142    
143        /**
144         * creates a GetCapabilities request from its XML representation
145         *
146         * @param id
147         *            unique ID of the request
148         * @param doc
149         *            XML representation of the request
150         * @return created <tt>DescribeCoverage</tt>
151         * @throws OGCWebServiceException
152         *             will be thrown if something general is wrong
153         * @throws InvalidParameterValueException
154         * @throws MissingParameterValueException
155         */
156        public static GetCapabilities create( String id, Document doc )
157                                throws OGCWebServiceException, InvalidParameterValueException,
158                                MissingParameterValueException {
159            String version = null;
160            String service = null;
161            String updateSeq = null;
162            String[] sections = null;
163            try {
164                Element root = XMLTools.getRequiredChildElement( "GetCapabilities", CommonNamespaces.WCSNS, doc );
165    
166                version = XMLTools.getAttrValue( root, null, "version", "1.0.0" );
167    
168                service = XMLTools.getAttrValue( root, null, "service", null );
169                if ( service == null ) {
170                    ExceptionCode code = ExceptionCode.MISSINGPARAMETERVALUE;
171                    throw new MissingParameterValueException( "WCSGetCapabilities", "'service' is missing", code );
172                } else if ( !service.equals( "WCS" ) ) {
173                    ExceptionCode code = ExceptionCode.INVALIDPARAMETERVALUE;
174                    throw new InvalidParameterValueException( "WCSGetCapabilities", "'service' must be 'WCS'", code );
175                }
176    
177                updateSeq = XMLTools.getAttrValue( root, null, "updateSequence", null );
178    
179                String tmp = XMLTools.getStringValue( "section", CommonNamespaces.WCSNS, root, "/" );
180                if ( tmp != null ) {
181                    sections = StringTools.toArray( tmp, ",", true );
182                    if ( !validateSection( sections ) ) {
183                        ExceptionCode code = ExceptionCode.INVALIDPARAMETERVALUE;
184                        throw new InvalidParameterValueException( "WCSGetCapabilities",
185                                                                  "invalid value for section parameter", code );
186                    }
187                }
188    
189            } catch ( XMLParsingException e ) {
190                LOG.logError( "WCSGetCapabilities", e );
191                ExceptionCode code = ExceptionCode.INVALID_FORMAT;
192                throw new OGCWebServiceException( "WCSGetCapabilities", e.getMessage(), code );
193            }
194    
195            return new WCSGetCapabilities( id, version, updateSeq, sections, null );
196        }
197    
198        /**
199         * valid values are:
200         * <ul>
201         * <li>null
202         * <li>/
203         * <li>/WCS_Capabilities/CapabilitiesService
204         * <li>/WCS_Capabilities/Capabilitiy
205         * <li>/WCS_Capabilities/ContentMetadata
206         * </ul>
207         *
208         * @param sections
209         */
210        private static boolean validateSection( String[] sections ) {
211            if ( sections == null )
212                return false;
213            for ( int i = 0; i < sections.length; i++ ) {
214                try {
215                    sections[i] = URLDecoder.decode( sections[i], CharsetUtils.getSystemCharset() );
216                } catch ( UnsupportedEncodingException e ) {
217                    e.printStackTrace();
218                }
219                if ( sections[i] != null && !"/".equals( sections[i] ) && !"/WCS_Capabilities/Service".equals( sections[i] )
220                     && !"/WCS_Capabilities/Capability".equals( sections[i] )
221                     && !"/WCS_Capabilities/ContentMetadata".equals( sections[i] ) ) {
222                    return false;
223                }
224            }
225            return true;
226        }
227    
228        /**
229         * @param id
230         * @param version
231         * @param updateSequence
232         * @param sections
233         * @param vendoreSpec
234         */
235        public WCSGetCapabilities( String id, String version, String updateSequence, String[] sections,
236                                   Map<String, String> vendoreSpec ) {
237            super( id, version, updateSequence, null, sections, null, vendoreSpec );
238        }
239    
240        /**
241         * returns WCS as service name
242         */
243        public String getServiceName() {
244            return "WCS";
245        }
246    }