001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/ogcwebservices/AbstractOGCWebServiceRequest.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;
038    
039    import java.io.Serializable;
040    import java.util.HashMap;
041    import java.util.Map;
042    
043    /**
044     * This is the abstract base class for all requests to OGC Web Services (OWS).
045     * <p>
046     * Contains utility methods to ease the extraction of values from KVP parameter maps.
047     *
048     * @author <a href="mailto:k.lupp@web.de">Katharina Lupp </a>
049     * @author last edited by: $Author: mschneider $
050     *
051     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
052     */
053    public abstract class AbstractOGCWebServiceRequest implements OGCWebServiceRequest, Serializable {
054    
055        private Map<String, String> vendorSpecificParameter;
056    
057        private String id;
058    
059        private String version;
060    
061        /**
062         * returns the ID of a request
063         */
064        public String getId() {
065            return id;
066        }
067    
068        /**
069         * returns the requested service version
070         */
071        public String getVersion() {
072            return version;
073        }
074    
075        /**
076         * Creates a new instance of <code>AbstractOGCWebServiceRequest</code>.
077         *
078         * @param version
079         * @param id
080         * @param vendorSpecificParameter
081         */
082        protected AbstractOGCWebServiceRequest( String version, String id, Map<String, String> vendorSpecificParameter ) {
083            this.id = id;
084            if ( vendorSpecificParameter != null ) {
085                this.vendorSpecificParameter = vendorSpecificParameter;
086            } else {
087                this.vendorSpecificParameter = new HashMap<String, String>();
088            }
089            this.version = version;
090        }
091    
092        /**
093         * Finally, the requests allow for optional vendor-specific parameters (VSPs) that will enhance
094         * the results of a request. Typically, these are used for private testing of non-standard
095         * functionality prior to possible standardization. A generic client is not required or expected
096         * to make use of these VSPs.
097         */
098        public Map<String, String> getVendorSpecificParameters() {
099            return vendorSpecificParameter;
100        }
101    
102        /**
103         * Finally, the requests allow for optional vendor-specific parameters (VSPs) that will enhance
104         * the results of a request. Typically, these are used for private testing of non-standard
105         * functionality prior to possible standardization. A generic client is not required or expected
106         * to make use of these VSPs.
107         */
108        public String getVendorSpecificParameter( String name ) {
109            return vendorSpecificParameter.get( name );
110        }
111    
112        /**
113         * returns the URI of a HTTP GET request. If the request doesn't support HTTP GET a
114         * <tt>WebServiceException</tt> will be thrown
115         *
116         */
117        public String getRequestParameter()
118                                throws OGCWebServiceException {
119            throw new OGCWebServiceException( "HTTP GET isn't supported" );
120        }
121    
122        /**
123         * Extracts a <code>String</code> parameter value from the given parameter map. If the given
124         * parameter does not exist, the also submitted default value is returned.
125         *
126         * @param name
127         *            name of the parameter to be looked up
128         * @param kvp
129         *            must contain Strings as keys and Strings as values
130         * @param defaultValue
131         *            default value to be used if parameter is missing
132         * @return parameter value
133         */
134        protected static String getParam( String name, Map<String, String> kvp, String defaultValue ) {
135            String value = kvp.remove( name );
136            if ( value == null ) {
137                value = defaultValue;
138            }
139            return value;
140        }
141    
142        /**
143         * Extracts a <code>String</code> list from the given parameter map. The single values are
144         * separated by commas. If the given parameter does not exist, the also submitted default value
145         * is returned.
146         *
147         * @param name
148         *            name of the parameter to be looked up
149         * @param kvp
150         *            must contain Strings as keys and Strings as values
151         * @param defaultValue
152         *            default value to be used if parameter is missing
153         * @return parameter value
154         */
155        protected static String[] getParamValues( String name, Map<String, String> kvp, String defaultValue ) {
156            String value = kvp.get( name );
157            if ( value == null ) {
158                value = defaultValue;
159            }
160            return value.split( "," );
161        }
162    
163        /**
164         * Extracts a <code>String</code> parameter value from the given parameter map. Generates
165         * exceptions with descriptive messages, if the parameter does not exist in the <code>Map</code>.
166         *
167         * @param name
168         *            name of the parameter to be looked up
169         * @param kvp
170         *            must contain Strings as keys and Strings as values
171         * @return parameter value
172         * @throws MissingParameterValueException
173         */
174        protected static String getRequiredParam( String name, Map<String, String> kvp )
175                                throws MissingParameterValueException {
176            String value = kvp.remove( name );
177            if ( value == null ) {
178                throw new MissingParameterValueException( "Cannot create OGC web service request. Required parameter '"
179                                                          + name + "' is missing.", name );
180            }
181            return value;
182        }
183    
184        /**
185         * Extracts an <code>int</code> parameter value from the given parameter map. If the given
186         * parameter does not exist, the also submitted default value is returned.
187         *
188         * @param name
189         *            name of the parameter to be looked up
190         * @param kvp
191         *            must contain Strings as keys and Strings as values
192         * @param defaultValue
193         *            default value to be used if parameter is missing
194         * @return parameter value
195         * @throws InvalidParameterValueException
196         */
197        protected static int getParamAsInt( String name, Map<String, String> kvp, int defaultValue )
198                                throws InvalidParameterValueException {
199            int value = defaultValue;
200            String paramValue = kvp.get( name );
201            if ( paramValue != null ) {
202                try {
203                    value = Integer.parseInt( paramValue );
204                } catch ( NumberFormatException e ) {
205                    throw new InvalidParameterValueException( "Value '" + paramValue + "' for parameter '" + name
206                                                              + "' is invalid. Must be of type integer." );
207                }
208            }
209            return value;
210        }
211    
212        @Override
213        public String toString() {
214            String ret = "vendorSpecificParameter = " + vendorSpecificParameter + "\n";
215            ret += ( "id = " + id + "\n" );
216            ret += ( "version = " + version );
217            return ret;
218        }
219    }