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