001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.web.bind;
018:
019: import javax.servlet.ServletException;
020: import javax.servlet.http.HttpServletRequest;
021:
022: import org.springframework.web.HttpRequestMethodNotSupportedException;
023:
024: /**
025: * Parameter extraction methods, for an approach distinct from data binding,
026: * in which parameters of specific types are required.
027: *
028: * <p>This approach is very useful for simple submissions, where binding
029: * request parameters to a command object would be overkill.
030: *
031: * @author Rod Johnson
032: * @author Juergen Hoeller
033: * @author Keith Donald
034: * @deprecated since Spring 2.0: use ServletRequestUtils instead
035: * @see ServletRequestUtils
036: */
037: public abstract class RequestUtils {
038:
039: /**
040: * Throw a ServletException if the given HTTP request method should be rejected.
041: * @param request request to check
042: * @param method method (such as "GET") which should be rejected
043: * @throws ServletException if the given HTTP request is rejected
044: */
045: public static void rejectRequestMethod(HttpServletRequest request,
046: String method) throws ServletException {
047: if (request.getMethod().equals(method)) {
048: throw new HttpRequestMethodNotSupportedException(method);
049: }
050: }
051:
052: /**
053: * Get an Integer parameter, or <code>null</code> if not present.
054: * Throws an exception if it the parameter value isn't a number.
055: * @param request current HTTP request
056: * @param name the name of the parameter
057: * @return the Integer value, or <code>null</code> if not present
058: * @throws ServletRequestBindingException a subclass of ServletException,
059: * so it doesn't need to be caught
060: */
061: public static Integer getIntParameter(HttpServletRequest request,
062: String name) throws ServletRequestBindingException {
063:
064: return ServletRequestUtils.getIntParameter(request, name);
065: }
066:
067: /**
068: * Get an int parameter, with a fallback value. Never throws an exception.
069: * Can pass a distinguished value as default to enable checks of whether it was supplied.
070: * @param request current HTTP request
071: * @param name the name of the parameter
072: * @param defaultVal the default value to use as fallback
073: */
074: public static int getIntParameter(HttpServletRequest request,
075: String name, int defaultVal) {
076: return ServletRequestUtils.getIntParameter(request, name,
077: defaultVal);
078: }
079:
080: /**
081: * Get an array of int parameters, return an empty array if not found.
082: * @param request current HTTP request
083: * @param name the name of the parameter with multiple possible values
084: */
085: public static int[] getIntParameters(HttpServletRequest request,
086: String name) {
087: return ServletRequestUtils.getIntParameters(request, name);
088: }
089:
090: /**
091: * Get an int parameter, throwing an exception if it isn't found or isn't a number.
092: * @param request current HTTP request
093: * @param name the name of the parameter
094: * @throws ServletRequestBindingException a subclass of ServletException,
095: * so it doesn't need to be caught
096: */
097: public static int getRequiredIntParameter(
098: HttpServletRequest request, String name)
099: throws ServletRequestBindingException {
100:
101: return ServletRequestUtils.getRequiredIntParameter(request,
102: name);
103: }
104:
105: /**
106: * Get an array of int parameters, throwing an exception if not found or one is not a number..
107: * @param request current HTTP request
108: * @param name the name of the parameter with multiple possible values
109: * @throws ServletRequestBindingException a subclass of ServletException,
110: * so it doesn't need to be caught
111: */
112: public static int[] getRequiredIntParameters(
113: HttpServletRequest request, String name)
114: throws ServletRequestBindingException {
115:
116: return ServletRequestUtils.getRequiredIntParameters(request,
117: name);
118: }
119:
120: /**
121: * Get a Long parameter, or <code>null</code> if not present.
122: * Throws an exception if it the parameter value isn't a number.
123: * @param request current HTTP request
124: * @param name the name of the parameter
125: * @return the Long value, or <code>null</code> if not present
126: * @throws ServletRequestBindingException a subclass of ServletException,
127: * so it doesn't need to be caught
128: */
129: public static Long getLongParameter(HttpServletRequest request,
130: String name) throws ServletRequestBindingException {
131:
132: return ServletRequestUtils.getLongParameter(request, name);
133: }
134:
135: /**
136: * Get a long parameter, with a fallback value. Never throws an exception.
137: * Can pass a distinguished value as default to enable checks of whether it was supplied.
138: * @param request current HTTP request
139: * @param name the name of the parameter
140: * @param defaultVal the default value to use as fallback
141: */
142: public static long getLongParameter(HttpServletRequest request,
143: String name, long defaultVal) {
144: return ServletRequestUtils.getLongParameter(request, name,
145: defaultVal);
146: }
147:
148: /**
149: * Get an array of long parameters, return an empty array if not found.
150: * @param request current HTTP request
151: * @param name the name of the parameter with multiple possible values
152: */
153: public static long[] getLongParameters(HttpServletRequest request,
154: String name) {
155: return ServletRequestUtils.getLongParameters(request, name);
156: }
157:
158: /**
159: * Get a long parameter, throwing an exception if it isn't found or isn't a number.
160: * @param request current HTTP request
161: * @param name the name of the parameter
162: * @throws ServletRequestBindingException a subclass of ServletException,
163: * so it doesn't need to be caught
164: */
165: public static long getRequiredLongParameter(
166: HttpServletRequest request, String name)
167: throws ServletRequestBindingException {
168:
169: return ServletRequestUtils.getRequiredLongParameter(request,
170: name);
171: }
172:
173: /**
174: * Get an array of long parameters, throwing an exception if not found or one is not a number.
175: * @param request current HTTP request
176: * @param name the name of the parameter with multiple possible values
177: * @throws ServletRequestBindingException a subclass of ServletException,
178: * so it doesn't need to be caught
179: */
180: public static long[] getRequiredLongParameters(
181: HttpServletRequest request, String name)
182: throws ServletRequestBindingException {
183:
184: return ServletRequestUtils.getRequiredLongParameters(request,
185: name);
186: }
187:
188: /**
189: * Get a Float parameter, or <code>null</code> if not present.
190: * Throws an exception if it the parameter value isn't a number.
191: * @param request current HTTP request
192: * @param name the name of the parameter
193: * @return the Float value, or <code>null</code> if not present
194: * @throws ServletRequestBindingException a subclass of ServletException,
195: * so it doesn't need to be caught
196: */
197: public static Float getFloatParameter(HttpServletRequest request,
198: String name) throws ServletRequestBindingException {
199:
200: return ServletRequestUtils.getFloatParameter(request, name);
201: }
202:
203: /**
204: * Get a float parameter, with a fallback value. Never throws an exception.
205: * Can pass a distinguished value as default to enable checks of whether it was supplied.
206: * @param request current HTTP request
207: * @param name the name of the parameter
208: * @param defaultVal the default value to use as fallback
209: */
210: public static float getFloatParameter(HttpServletRequest request,
211: String name, float defaultVal) {
212: return ServletRequestUtils.getFloatParameter(request, name,
213: defaultVal);
214: }
215:
216: /**
217: * Get an array of float parameters, return an empty array if not found.
218: * @param request current HTTP request
219: * @param name the name of the parameter with multiple possible values
220: */
221: public static float[] getFloatParameters(
222: HttpServletRequest request, String name) {
223: return ServletRequestUtils.getFloatParameters(request, name);
224: }
225:
226: /**
227: * Get a float parameter, throwing an exception if it isn't found or isn't a number.
228: * @param request current HTTP request
229: * @param name the name of the parameter
230: * @throws ServletRequestBindingException a subclass of ServletException,
231: * so it doesn't need to be caught
232: */
233: public static float getRequiredFloatParameter(
234: HttpServletRequest request, String name)
235: throws ServletRequestBindingException {
236:
237: return ServletRequestUtils.getRequiredFloatParameter(request,
238: name);
239: }
240:
241: /**
242: * Get an array of float parameters, throwing an exception if not found or one is not a number.
243: * @param request current HTTP request
244: * @param name the name of the parameter with multiple possible values
245: * @throws ServletRequestBindingException a subclass of ServletException,
246: * so it doesn't need to be caught
247: */
248: public static float[] getRequiredFloatParameters(
249: HttpServletRequest request, String name)
250: throws ServletRequestBindingException {
251:
252: return ServletRequestUtils.getRequiredFloatParameters(request,
253: name);
254: }
255:
256: /**
257: * Get a Double parameter, or <code>null</code> if not present.
258: * Throws an exception if it the parameter value isn't a number.
259: * @param request current HTTP request
260: * @param name the name of the parameter
261: * @return the Double value, or <code>null</code> if not present
262: * @throws ServletRequestBindingException a subclass of ServletException,
263: * so it doesn't need to be caught
264: */
265: public static Double getDoubleParameter(HttpServletRequest request,
266: String name) throws ServletRequestBindingException {
267:
268: return ServletRequestUtils.getDoubleParameter(request, name);
269: }
270:
271: /**
272: * Get a double parameter, with a fallback value. Never throws an exception.
273: * Can pass a distinguished value as default to enable checks of whether it was supplied.
274: * @param request current HTTP request
275: * @param name the name of the parameter
276: * @param defaultVal the default value to use as fallback
277: */
278: public static double getDoubleParameter(HttpServletRequest request,
279: String name, double defaultVal) {
280: return ServletRequestUtils.getDoubleParameter(request, name,
281: defaultVal);
282: }
283:
284: /**
285: * Get an array of double parameters, return an empty array if not found.
286: * @param request current HTTP request
287: * @param name the name of the parameter with multiple possible values
288: */
289: public static double[] getDoubleParameters(
290: HttpServletRequest request, String name) {
291: return ServletRequestUtils.getDoubleParameters(request, name);
292: }
293:
294: /**
295: * Get a double parameter, throwing an exception if it isn't found or isn't a number.
296: * @param request current HTTP request
297: * @param name the name of the parameter
298: * @throws ServletRequestBindingException a subclass of ServletException,
299: * so it doesn't need to be caught
300: */
301: public static double getRequiredDoubleParameter(
302: HttpServletRequest request, String name)
303: throws ServletRequestBindingException {
304:
305: return ServletRequestUtils.getRequiredDoubleParameter(request,
306: name);
307: }
308:
309: /**
310: * Get an array of double parameters, throwing an exception if not found or one is not a number.
311: * @param request current HTTP request
312: * @param name the name of the parameter with multiple possible values
313: * @throws ServletRequestBindingException a subclass of ServletException,
314: * so it doesn't need to be caught
315: */
316: public static double[] getRequiredDoubleParameters(
317: HttpServletRequest request, String name)
318: throws ServletRequestBindingException {
319:
320: return ServletRequestUtils.getRequiredDoubleParameters(request,
321: name);
322: }
323:
324: /**
325: * Get a Boolean parameter, or <code>null</code> if not present.
326: * Throws an exception if it the parameter value isn't a boolean.
327: * <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
328: * treats every other non-empty value as false (i.e. parses leniently).
329: * @param request current HTTP request
330: * @param name the name of the parameter
331: * @return the Boolean value, or <code>null</code> if not present
332: * @throws ServletRequestBindingException a subclass of ServletException,
333: * so it doesn't need to be caught
334: */
335: public static Boolean getBooleanParameter(
336: HttpServletRequest request, String name)
337: throws ServletRequestBindingException {
338:
339: if (request.getParameter(name) == null) {
340: return null;
341: }
342: return (getRequiredBooleanParameter(request, name) ? Boolean.TRUE
343: : Boolean.FALSE);
344: }
345:
346: /**
347: * Get a boolean parameter, with a fallback value. Never throws an exception.
348: * Can pass a distinguished value as default to enable checks of whether it was supplied.
349: * <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
350: * treats every other non-empty value as false (i.e. parses leniently).
351: * @param request current HTTP request
352: * @param name the name of the parameter
353: * @param defaultVal the default value to use as fallback
354: */
355: public static boolean getBooleanParameter(
356: HttpServletRequest request, String name, boolean defaultVal) {
357: if (request.getParameter(name) == null) {
358: return defaultVal;
359: }
360: try {
361: return getRequiredBooleanParameter(request, name);
362: } catch (ServletRequestBindingException ex) {
363: return defaultVal;
364: }
365: }
366:
367: /**
368: * Get an array of boolean parameters, return an empty array if not found.
369: * <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
370: * treats every other non-empty value as false (i.e. parses leniently).
371: * @param request current HTTP request
372: * @param name the name of the parameter with multiple possible values
373: */
374: public static boolean[] getBooleanParameters(
375: HttpServletRequest request, String name) {
376: try {
377: return getRequiredBooleanParameters(request, name);
378: } catch (ServletRequestBindingException ex) {
379: return new boolean[0];
380: }
381: }
382:
383: /**
384: * Get a boolean parameter, throwing an exception if it isn't found
385: * or isn't a boolean.
386: * <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
387: * treats every other non-empty value as false (i.e. parses leniently).
388: * @param request current HTTP request
389: * @param name the name of the parameter
390: * @throws ServletRequestBindingException a subclass of ServletException,
391: * so it doesn't need to be caught
392: */
393: public static boolean getRequiredBooleanParameter(
394: HttpServletRequest request, String name)
395: throws ServletRequestBindingException {
396:
397: boolean value = ServletRequestUtils
398: .getRequiredBooleanParameter(request, name);
399: if (!value && "".equals(request.getParameter(name))) {
400: throw new ServletRequestBindingException(
401: "Required boolean parameter '" + name
402: + "' contains no value");
403: }
404: return value;
405: }
406:
407: /**
408: * Get an array of boolean parameters, throwing an exception if not found
409: * or one isn't a boolean.
410: * <p>Accepts "true", "on", "yes" (any case) and "1" as values for true;
411: * treats every other non-empty value as false (i.e. parses leniently).
412: * @param request current HTTP request
413: * @param name the name of the parameter
414: * @throws ServletRequestBindingException a subclass of ServletException,
415: * so it doesn't need to be caught
416: */
417: public static boolean[] getRequiredBooleanParameters(
418: HttpServletRequest request, String name)
419: throws ServletRequestBindingException {
420:
421: boolean[] values = ServletRequestUtils
422: .getRequiredBooleanParameters(request, name);
423: for (int i = 0; i < values.length; i++) {
424: if (!values[i]
425: && "".equals(request.getParameterValues(name)[i])) {
426: throw new ServletRequestBindingException(
427: "Required boolean parameter '" + name
428: + "' contains no value");
429: }
430: }
431: return values;
432: }
433:
434: /**
435: * Get a String parameter, or <code>null</code> if not present.
436: * Throws an exception if it the parameter value is empty.
437: * @param request current HTTP request
438: * @param name the name of the parameter
439: * @return the String value, or <code>null</code> if not present
440: * @throws ServletRequestBindingException a subclass of ServletException,
441: * so it doesn't need to be caught
442: */
443: public static String getStringParameter(HttpServletRequest request,
444: String name) throws ServletRequestBindingException {
445:
446: if (request.getParameter(name) == null) {
447: return null;
448: }
449: return getRequiredStringParameter(request, name);
450: }
451:
452: /**
453: * Get a String parameter, with a fallback value. Never throws an exception.
454: * Can pass a distinguished value to default to enable checks of whether it was supplied.
455: * @param request current HTTP request
456: * @param name the name of the parameter
457: * @param defaultVal the default value to use as fallback
458: */
459: public static String getStringParameter(HttpServletRequest request,
460: String name, String defaultVal) {
461: if (request.getParameter(name) == null) {
462: return defaultVal;
463: }
464: try {
465: return getRequiredStringParameter(request, name);
466: } catch (ServletRequestBindingException ex) {
467: return defaultVal;
468: }
469: }
470:
471: /**
472: * Get an array of String parameters, return an empty array if not found.
473: * @param request current HTTP request
474: * @param name the name of the parameter with multiple possible values
475: */
476: public static String[] getStringParameters(
477: HttpServletRequest request, String name) {
478: try {
479: return getRequiredStringParameters(request, name);
480: } catch (ServletRequestBindingException ex) {
481: return new String[0];
482: }
483: }
484:
485: /**
486: * Get a String parameter, throwing an exception if it isn't found or is empty.
487: * @param request current HTTP request
488: * @param name the name of the parameter
489: * @throws ServletRequestBindingException a subclass of ServletException,
490: * so it doesn't need to be caught
491: */
492: public static String getRequiredStringParameter(
493: HttpServletRequest request, String name)
494: throws ServletRequestBindingException {
495:
496: String value = ServletRequestUtils.getRequiredStringParameter(
497: request, name);
498: if ("".equals(value)) {
499: throw new ServletRequestBindingException(
500: "Required string parameter '" + name
501: + "' contains no value");
502: }
503: return value;
504: }
505:
506: /**
507: * Get an array of String parameters, throwing an exception if not found or one is empty.
508: * @param request current HTTP request
509: * @param name the name of the parameter
510: * @throws ServletRequestBindingException a subclass of ServletException,
511: * so it doesn't need to be caught
512: */
513: public static String[] getRequiredStringParameters(
514: HttpServletRequest request, String name)
515: throws ServletRequestBindingException {
516:
517: String[] values = ServletRequestUtils
518: .getRequiredStringParameters(request, name);
519: for (int i = 0; i < values.length; i++) {
520: if ("".equals(values[i])) {
521: throw new ServletRequestBindingException(
522: "Required string parameter '" + name
523: + "' contains no value");
524: }
525: }
526: return values;
527: }
528:
529: }
|