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