KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > bind > RequestUtils


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.bind;
18
19 import javax.servlet.ServletException JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21
22 import org.springframework.web.HttpRequestMethodNotSupportedException;
23
24 /**
25  * Parameter extraction methods, for an approach distinct from data binding,
26  * in which parameters of specific types are required.
27  *
28  * <p>This approach is very useful for simple submissions, where binding
29  * request parameters to a command object would be overkill.
30  *
31  * @author Rod Johnson
32  * @author Juergen Hoeller
33  * @author Keith Donald
34  * @deprecated since Spring 2.0: use ServletRequestUtils instead
35  * @see ServletRequestUtils
36  */

37 public abstract class RequestUtils {
38
39     /**
40      * Throw a ServletException if the given HTTP request method should be rejected.
41      * @param request request to check
42      * @param method method (such as "GET") which should be rejected
43      * @throws ServletException if the given HTTP request is rejected
44      */

45     public static void rejectRequestMethod(HttpServletRequest JavaDoc request, String JavaDoc method) throws ServletException JavaDoc {
46         if (request.getMethod().equals(method)) {
47             throw new HttpRequestMethodNotSupportedException(method);
48         }
49     }
50
51
52     /**
53      * Get an Integer parameter, or <code>null</code> if not present.
54      * Throws an exception if it the parameter value isn't a number.
55      * @param request current HTTP request
56      * @param name the name of the parameter
57      * @return the Integer value, or <code>null</code> if not present
58      * @throws ServletRequestBindingException a subclass of ServletException,
59      * so it doesn't need to be caught
60      */

61     public static Integer JavaDoc getIntParameter(HttpServletRequest JavaDoc request, String JavaDoc name)
62             throws ServletRequestBindingException {
63
64         return ServletRequestUtils.getIntParameter(request, name);
65     }
66
67     /**
68      * Get an int parameter, with a fallback value. Never throws an exception.
69      * Can pass a distinguished value as default to enable checks of whether it was supplied.
70      * @param request current HTTP request
71      * @param name the name of the parameter
72      * @param defaultVal the default value to use as fallback
73      */

74     public static int getIntParameter(HttpServletRequest JavaDoc request, String JavaDoc name, int defaultVal) {
75         return ServletRequestUtils.getIntParameter(request, name, defaultVal);
76     }
77
78     /**
79      * Get an array of int parameters, return an empty array if not found.
80      * @param request current HTTP request
81      * @param name the name of the parameter with multiple possible values
82      */

83     public static int[] getIntParameters(HttpServletRequest JavaDoc request, String JavaDoc name) {
84         return ServletRequestUtils.getIntParameters(request, name);
85     }
86
87     /**
88      * Get an int parameter, throwing an exception if it isn't found or isn't a number.
89      * @param request current HTTP request
90      * @param name the name of the parameter
91      * @throws ServletRequestBindingException a subclass of ServletException,
92      * so it doesn't need to be caught
93      */

94     public static int getRequiredIntParameter(HttpServletRequest JavaDoc request, String JavaDoc name)
95             throws ServletRequestBindingException {
96
97         return ServletRequestUtils.getRequiredIntParameter(request, name);
98     }
99
100     /**
101      * Get an array of int parameters, throwing an exception if not found or one is not a number..
102      * @param request current HTTP request
103      * @param name the name of the parameter with multiple possible values
104      * @throws ServletRequestBindingException a subclass of ServletException,
105      * so it doesn't need to be caught
106      */

107     public static int[] getRequiredIntParameters(HttpServletRequest JavaDoc request, String JavaDoc name)
108             throws ServletRequestBindingException {
109
110         return ServletRequestUtils.getRequiredIntParameters(request, name);
111     }
112
113
114     /**
115      * Get a Long parameter, or <code>null</code> if not present.
116      * Throws an exception if it the parameter value isn't a number.
117      * @param request current HTTP request
118      * @param name the name of the parameter
119      * @return the Long value, or <code>null</code> if not present
120      * @throws ServletRequestBindingException a subclass of ServletException,
121      * so it doesn't need to be caught
122      */

123     public static Long JavaDoc getLongParameter(HttpServletRequest JavaDoc request, String JavaDoc name)
124             throws ServletRequestBindingException {
125
126         return ServletRequestUtils.getLongParameter(request, name);
127     }
128
129     /**
130      * Get a long parameter, with a fallback value. Never throws an exception.
131      * Can pass a distinguished value as default to enable checks of whether it was supplied.
132      * @param request current HTTP request
133      * @param name the name of the parameter
134      * @param defaultVal the default value to use as fallback
135      */

136     public static long getLongParameter(HttpServletRequest JavaDoc request, String JavaDoc name, long defaultVal) {
137         return ServletRequestUtils.getLongParameter(request, name, defaultVal);
138     }
139
140     /**
141      * Get an array of long parameters, return an empty array if not found.
142      * @param request current HTTP request
143      * @param name the name of the parameter with multiple possible values
144      */

145     public static long[] getLongParameters(HttpServletRequest JavaDoc request, String JavaDoc name) {
146         return ServletRequestUtils.getLongParameters(request, name);
147     }
148
149     /**
150      * Get a long parameter, throwing an exception if it isn't found or isn't a number.
151      * @param request current HTTP request
152      * @param name the name of the parameter
153      * @throws ServletRequestBindingException a subclass of ServletException,
154      * so it doesn't need to be caught
155      */

156     public static long getRequiredLongParameter(HttpServletRequest JavaDoc request, String JavaDoc name)
157             throws ServletRequestBindingException {
158
159         return ServletRequestUtils.getRequiredLongParameter(request, name);
160     }
161
162     /**
163      * Get an array of long parameters, throwing an exception if not found or one is not a number.
164      * @param request current HTTP request
165      * @param name the name of the parameter with multiple possible values
166      * @throws ServletRequestBindingException a subclass of ServletException,
167      * so it doesn't need to be caught
168      */

169     public static long[] getRequiredLongParameters(HttpServletRequest JavaDoc request, String JavaDoc name)
170             throws ServletRequestBindingException {
171
172         return ServletRequestUtils.getRequiredLongParameters(request, name);
173     }
174
175
176     /**
177      * Get a Float parameter, or <code>null</code> if not present.
178      * Throws an exception if it the parameter value isn't a number.
179      * @param request current HTTP request
180      * @param name the name of the parameter
181      * @return the Float value, or <code>null</code> if not present
182      * @throws ServletRequestBindingException a subclass of ServletException,
183      * so it doesn't need to be caught
184      */

185     public static Float JavaDoc getFloatParameter(HttpServletRequest JavaDoc request, String JavaDoc name)
186             throws ServletRequestBindingException {
187
188         return ServletRequestUtils.getFloatParameter(request, name);
189     }
190
191     /**
192      * Get a float parameter, with a fallback value. Never throws an exception.
193      * Can pass a distinguished value as default to enable checks of whether it was supplied.
194      * @param request current HTTP request
195      * @param name the name of the parameter
196      * @param defaultVal the default value to use as fallback
197      */

198     public static float getFloatParameter(HttpServletRequest JavaDoc request, String JavaDoc name, float defaultVal) {
199         return ServletRequestUtils.getFloatParameter(request, name, defaultVal);
200     }
201
202     /**
203      * Get an array of float parameters, return an empty array if not found.
204      * @param request current HTTP request
205      * @param name the name of the parameter with multiple possible values
206      */

207     public static float[] getFloatParameters(HttpServletRequest JavaDoc request, String JavaDoc name) {
208         return ServletRequestUtils.getFloatParameters(request, name);
209     }
210
211     /**
212      * Get a float parameter, throwing an exception if it isn't found or isn't a number.
213      * @param request current HTTP request
214      * @param name the name of the parameter
215      * @throws ServletRequestBindingException a subclass of ServletException,
216      * so it doesn't need to be caught
217      */

218     public static float getRequiredFloatParameter(HttpServletRequest JavaDoc request, String JavaDoc name)
219             throws ServletRequestBindingException {
220
221         return ServletRequestUtils.getRequiredFloatParameter(request, name);
222     }
223
224     /**
225      * Get an array of float parameters, throwing an exception if not found or one is not a number.
226      * @param request current HTTP request
227      * @param name the name of the parameter with multiple possible values
228      * @throws ServletRequestBindingException a subclass of ServletException,
229      * so it doesn't need to be caught
230      */

231     public static float[] getRequiredFloatParameters(HttpServletRequest JavaDoc request, String JavaDoc name)
232             throws ServletRequestBindingException {
233
234         return ServletRequestUtils.getRequiredFloatParameters(request, name);
235     }
236
237
238     /**
239      * Get a Double parameter, or <code>null</code> if not present.
240      * Throws an exception if it the parameter value isn't a number.
241      * @param request current HTTP request
242      * @param name the name of the parameter
243      * @return the Double value, or <code>null</code> if not present
244      * @throws ServletRequestBindingException a subclass of ServletException,
245      * so it doesn't need to be caught
246      */

247     public static Double JavaDoc getDoubleParameter(HttpServletRequest JavaDoc request, String JavaDoc name)
248             throws ServletRequestBindingException {
249
250         return ServletRequestUtils.getDoubleParameter(request, name);
251     }
252
253     /**
254      * Get a double parameter, with a fallback value. Never throws an exception.
255      * Can pass a distinguished value as default to enable checks of whether it was supplied.
256      * @param request current HTTP request
257      * @param name the name of the parameter
258      * @param defaultVal the default value to use as fallback
259      */

260     public static double getDoubleParameter(HttpServletRequest JavaDoc request, String JavaDoc name, double defaultVal) {
261         return ServletRequestUtils.getDoubleParameter(request, name, defaultVal);
262     }
263
264     /**
265      * Get an array of double parameters, return an empty array if not found.
266      * @param request current HTTP request
267      * @param name the name of the parameter with multiple possible values
268      */

269     public static double[] getDoubleParameters(HttpServletRequest JavaDoc request, String JavaDoc name) {
270         return ServletRequestUtils.getDoubleParameters(request, name);
271     }
272
273     /**
274      * Get a double parameter, throwing an exception if it isn't found or isn't a number.
275      * @param request current HTTP request
276      * @param name the name of the parameter
277      * @throws ServletRequestBindingException a subclass of ServletException,
278      * so it doesn't need to be caught
279      */

280     public static double getRequiredDoubleParameter(HttpServletRequest JavaDoc request, String JavaDoc name)
281             throws ServletRequestBindingException {
282
283         return ServletRequestUtils.getRequiredDoubleParameter(request, name);
284     }
285
286     /**
287      * Get an array of double parameters, throwing an exception if not found or one is not a number.
288      * @param request current HTTP request
289      * @param name the name of the parameter with multiple possible values
290      * @throws ServletRequestBindingException a subclass of ServletException,
291      * so it doesn't need to be caught
292      */

293     public static double[] getRequiredDoubleParameters(HttpServletRequest JavaDoc request, String JavaDoc name)
294             throws ServletRequestBindingException {
295
296         return ServletRequestUtils.getRequiredDoubleParameters(request, name);
297     }
298
299
300     /**
301      * Get a Boolean parameter, or <code>null</code> if not present.
302      * Throws an exception if it the parameter value isn't a boolean.
303      * <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
304      * treats every other non-empty value as false (i.e. parses leniently).
305      * @param request current HTTP request
306      * @param name the name of the parameter
307      * @return the Boolean value, or <code>null</code> if not present
308      * @throws ServletRequestBindingException a subclass of ServletException,
309      * so it doesn't need to be caught
310      */

311     public static Boolean JavaDoc getBooleanParameter(HttpServletRequest JavaDoc request, String JavaDoc name)
312             throws ServletRequestBindingException {
313
314         if (request.getParameter(name) == null) {
315             return null;
316         }
317         return (getRequiredBooleanParameter(request, name) ? Boolean.TRUE : Boolean.FALSE);
318     }
319
320     /**
321      * Get a boolean parameter, with a fallback value. Never throws an exception.
322      * Can pass a distinguished value as default to enable checks of whether it was supplied.
323      * <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
324      * treats every other non-empty value as false (i.e. parses leniently).
325      * @param request current HTTP request
326      * @param name the name of the parameter
327      * @param defaultVal the default value to use as fallback
328      */

329     public static boolean getBooleanParameter(HttpServletRequest JavaDoc request, String JavaDoc name, boolean defaultVal) {
330         if (request.getParameter(name) == null) {
331             return defaultVal;
332         }
333         try {
334             return getRequiredBooleanParameter(request, name);
335         }
336         catch (ServletRequestBindingException ex) {
337             return defaultVal;
338         }
339     }
340
341     /**
342      * Get an array of boolean parameters, return an empty array if not found.
343      * <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
344      * treats every other non-empty value as false (i.e. parses leniently).
345      * @param request current HTTP request
346      * @param name the name of the parameter with multiple possible values
347      */

348     public static boolean[] getBooleanParameters(HttpServletRequest JavaDoc request, String JavaDoc name) {
349         try {
350             return getRequiredBooleanParameters(request, name);
351         }
352         catch (ServletRequestBindingException ex) {
353             return new boolean[0];
354         }
355     }
356
357     /**
358      * Get a boolean parameter, throwing an exception if it isn't found
359      * or isn't a boolean.
360      * <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
361      * treats every other non-empty value as false (i.e. parses leniently).
362      * @param request current HTTP request
363      * @param name the name of the parameter
364      * @throws ServletRequestBindingException a subclass of ServletException,
365      * so it doesn't need to be caught
366      */

367     public static boolean getRequiredBooleanParameter(HttpServletRequest JavaDoc request, String JavaDoc name)
368             throws ServletRequestBindingException {
369
370         boolean value = ServletRequestUtils.getRequiredBooleanParameter(request, name);
371         if (!value && "".equals(request.getParameter(name))) {
372             throw new ServletRequestBindingException(
373                     "Required boolean parameter '" + name + "' contains no value");
374         }
375         return value;
376     }
377
378     /**
379      * Get an array of boolean parameters, throwing an exception if not found
380      * or one isn't a boolean.
381      * <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
382      * treats every other non-empty value as false (i.e. parses leniently).
383      * @param request current HTTP request
384      * @param name the name of the parameter
385      * @throws ServletRequestBindingException a subclass of ServletException,
386      * so it doesn't need to be caught
387      */

388     public static boolean[] getRequiredBooleanParameters(HttpServletRequest JavaDoc request, String JavaDoc name)
389             throws ServletRequestBindingException {
390
391         boolean[] values = ServletRequestUtils.getRequiredBooleanParameters(request, name);
392         for (int i = 0; i < values.length; i++) {
393             if (!values[i] && "".equals(request.getParameterValues(name)[i])) {
394                 throw new ServletRequestBindingException(
395                         "Required boolean parameter '" + name + "' contains no value");
396             }
397         }
398         return values;
399     }
400
401
402     /**
403      * Get a String parameter, or <code>null</code> if not present.
404      * Throws an exception if it the parameter value is empty.
405      * @param request current HTTP request
406      * @param name the name of the parameter
407      * @return the String value, or <code>null</code> if not present
408      * @throws ServletRequestBindingException a subclass of ServletException,
409      * so it doesn't need to be caught
410      */

411     public static String JavaDoc getStringParameter(HttpServletRequest JavaDoc request, String JavaDoc name)
412             throws ServletRequestBindingException {
413
414         if (request.getParameter(name) == null) {
415             return null;
416         }
417         return getRequiredStringParameter(request, name);
418     }
419
420     /**
421      * Get a String parameter, with a fallback value. Never throws an exception.
422      * Can pass a distinguished value to default to enable checks of whether it was supplied.
423      * @param request current HTTP request
424      * @param name the name of the parameter
425      * @param defaultVal the default value to use as fallback
426      */

427     public static String JavaDoc getStringParameter(HttpServletRequest JavaDoc request, String JavaDoc name, String JavaDoc defaultVal) {
428         if (request.getParameter(name) == null) {
429             return defaultVal;
430         }
431         try {
432             return getRequiredStringParameter(request, name);
433         }
434         catch (ServletRequestBindingException ex) {
435             return defaultVal;
436         }
437     }
438
439     /**
440      * Get an array of String parameters, return an empty array if not found.
441      * @param request current HTTP request
442      * @param name the name of the parameter with multiple possible values
443      */

444     public static String JavaDoc[] getStringParameters(HttpServletRequest JavaDoc request, String JavaDoc name) {
445         try {
446             return getRequiredStringParameters(request, name);
447         }
448         catch (ServletRequestBindingException ex) {
449             return new String JavaDoc[0];
450         }
451     }
452
453     /**
454      * Get a String parameter, throwing an exception if it isn't found or is empty.
455      * @param request current HTTP request
456      * @param name the name of the parameter
457      * @throws ServletRequestBindingException a subclass of ServletException,
458      * so it doesn't need to be caught
459      */

460     public static String JavaDoc getRequiredStringParameter(HttpServletRequest JavaDoc request, String JavaDoc name)
461             throws ServletRequestBindingException {
462
463         String JavaDoc value = ServletRequestUtils.getRequiredStringParameter(request, name);
464         if ("".equals(value)) {
465             throw new ServletRequestBindingException(
466                     "Required string parameter '" + name + "' contains no value");
467         }
468         return value;
469     }
470
471     /**
472      * Get an array of String parameters, throwing an exception if not found or one is empty.
473      * @param request current HTTP request
474      * @param name the name of the parameter
475      * @throws ServletRequestBindingException a subclass of ServletException,
476      * so it doesn't need to be caught
477      */

478     public static String JavaDoc[] getRequiredStringParameters(HttpServletRequest JavaDoc request, String JavaDoc name)
479             throws ServletRequestBindingException {
480
481         String JavaDoc[] values = ServletRequestUtils.getRequiredStringParameters(request, name);
482         for (int i = 0; i < values.length; i++) {
483             if ("".equals(values[i])) {
484                 throw new ServletRequestBindingException(
485                         "Required string parameter '" + name + "' contains no value");
486             }
487         }
488         return values;
489     }
490
491 }
492
Popular Tags