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