1. /*
  2. * Copyright 2002-2004 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. package org.springframework.web.bind;
  17. import javax.servlet.ServletException;
  18. import javax.servlet.http.HttpServletRequest;
  19. /**
  20. * Parameter extraction methods, for an approach distinct from data binding, in which
  21. * parameters of specific types are required. This is very useful for simple submissions.
  22. * @author Rod Johnson
  23. * @author Juergen Hoeller
  24. * @author Jean-Pierre Pawlak
  25. */
  26. public abstract class RequestUtils {
  27. /**
  28. * Throw a ServletException if the given HTTP request method should be rejected.
  29. * @param request request to check
  30. * @param method method (such as "GET") which should be rejected
  31. * @throws ServletException if the given HTTP request is rejected
  32. */
  33. public static void rejectRequestMethod(HttpServletRequest request, String method)
  34. throws ServletException {
  35. if (request.getMethod().equals(method)) {
  36. throw new ServletException("This resource does not support request method '" + method + "'");
  37. }
  38. }
  39. /**
  40. * Get an int parameter, with a fallback value. Never throws an exception.
  41. * Can pass a distinguished value as default to enable checks of whether it was supplied.
  42. */
  43. public static int getIntParameter(HttpServletRequest request, String name, int defaultVal) {
  44. try {
  45. return getRequiredIntParameter(request, name);
  46. }
  47. catch (ServletRequestBindingException ex) {
  48. return defaultVal;
  49. }
  50. }
  51. /**
  52. * Get an int parameter, throwing an exception if it isn't found or isn't a number.
  53. * @throws ServletRequestBindingException: subclass of ServletException,
  54. * so it doesn't need to be caught
  55. */
  56. public static int getRequiredIntParameter(HttpServletRequest request, String name)
  57. throws ServletRequestBindingException {
  58. String s = request.getParameter(name);
  59. if (s == null) {
  60. throw new ServletRequestBindingException("Required int parameter '" + name + "' was not supplied");
  61. }
  62. if ("".equals(s)) {
  63. throw new ServletRequestBindingException("Required int parameter '" + name + "' contained no value");
  64. }
  65. try {
  66. return Integer.parseInt(s);
  67. }
  68. catch (NumberFormatException ex) {
  69. throw new ServletRequestBindingException("Required int parameter '" + name + "' value of '" + s +
  70. "' was not a valid number");
  71. }
  72. }
  73. /**
  74. * Get an int parameter, with a fallback value. Never throws an exception.
  75. * Can pass a distinguished value as default to enable checks of whether it was supplied.
  76. */
  77. public static long getLongParameter(HttpServletRequest request, String name, long defaultVal) {
  78. try {
  79. return getRequiredLongParameter(request, name);
  80. }
  81. catch (ServletRequestBindingException ex) {
  82. return defaultVal;
  83. }
  84. }
  85. /**
  86. * Get a long parameter, throwing an exception if it isn't found or isn't a number.
  87. * @throws ServletRequestBindingException: subclass of ServletException,
  88. * so it doesn't need to be caught
  89. */
  90. public static long getRequiredLongParameter(HttpServletRequest request, String name)
  91. throws ServletRequestBindingException {
  92. String s = request.getParameter(name);
  93. if (s == null) {
  94. throw new ServletRequestBindingException("Required long parameter '" + name + "' was not supplied");
  95. }
  96. if ("".equals(s)) {
  97. throw new ServletRequestBindingException("Required long parameter '" + name + "' contained no value");
  98. }
  99. try {
  100. return Long.parseLong(s);
  101. }
  102. catch (NumberFormatException ex) {
  103. throw new ServletRequestBindingException("Required long parameter '" + name + "' value of '" + s +
  104. "' was not a valid number");
  105. }
  106. }
  107. /**
  108. * Get a double parameter, with a fallback value. Never throws an exception.
  109. * Can pass a distinguished value as default to enable checks of whether it was supplied.
  110. */
  111. public static float getFloatParameter(HttpServletRequest request, String name, float defaultVal) {
  112. try {
  113. return getRequiredFloatParameter(request, name);
  114. }
  115. catch (ServletRequestBindingException ex) {
  116. return defaultVal;
  117. }
  118. }
  119. /**
  120. * Get a double parameter, throwing an exception if it isn't found or isn't a number.
  121. * @throws ServletRequestBindingException: subclass of ServletException,
  122. * so it doesn't need to be caught
  123. */
  124. public static float getRequiredFloatParameter(HttpServletRequest request, String name)
  125. throws ServletRequestBindingException {
  126. String s = request.getParameter(name);
  127. if (s == null) {
  128. throw new ServletRequestBindingException("Required float parameter '" + name + "' was not supplied");
  129. }
  130. if ("".equals(s)) {
  131. throw new ServletRequestBindingException("Required float parameter '" + name + "' contained no value");
  132. }
  133. try {
  134. return Float.parseFloat(s);
  135. }
  136. catch (NumberFormatException ex) {
  137. throw new ServletRequestBindingException("Required float parameter '" + name + "' value of '" +
  138. s + "' was not a valid number");
  139. }
  140. }
  141. /**
  142. * Get a double parameter, with a fallback value. Never throws an exception.
  143. * Can pass a distinguished value as default to enable checks of whether it was supplied.
  144. */
  145. public static double getDoubleParameter(HttpServletRequest request, String name, double defaultVal) {
  146. try {
  147. return getRequiredDoubleParameter(request, name);
  148. }
  149. catch (ServletRequestBindingException ex) {
  150. return defaultVal;
  151. }
  152. }
  153. /**
  154. * Get a double parameter, throwing an exception if it isn't found or isn't a number.
  155. * @throws ServletRequestBindingException: subclass of ServletException,
  156. * so it doesn't need to be caught
  157. */
  158. public static double getRequiredDoubleParameter(HttpServletRequest request, String name)
  159. throws ServletRequestBindingException {
  160. String s = request.getParameter(name);
  161. if (s == null) {
  162. throw new ServletRequestBindingException("Required double parameter '" + name + "' was not supplied");
  163. }
  164. if ("".equals(s)) {
  165. throw new ServletRequestBindingException("Required double parameter '" + name + "' contained no value");
  166. }
  167. try {
  168. return Double.parseDouble(s);
  169. }
  170. catch (NumberFormatException ex) {
  171. throw new ServletRequestBindingException("Required double parameter '" + name + "' value of '" + s +
  172. "' was not a valid number");
  173. }
  174. }
  175. /**
  176. * Get a boolean parameter, with a fallback value. Never throws an exception.
  177. * Can pass a distinguished value as default to enable checks of whether it was supplied.
  178. */
  179. public static boolean getBooleanParameter(HttpServletRequest request, String name, boolean defaultVal) {
  180. try {
  181. return getRequiredBooleanParameter(request, name);
  182. }
  183. catch (ServletRequestBindingException ex) {
  184. return defaultVal;
  185. }
  186. }
  187. /**
  188. * Get a boolean parameter, throwing an exception if it isn't found or isn't a boolean.
  189. * True is "true" or "yes" or "on" (ignoring the case) or "1".
  190. * @throws ServletRequestBindingException: subclass of ServletException,
  191. * so it doesn't need to be caught
  192. */
  193. public static boolean getRequiredBooleanParameter(HttpServletRequest request, String name)
  194. throws ServletRequestBindingException {
  195. String s = request.getParameter(name);
  196. if (s == null) {
  197. throw new ServletRequestBindingException("Required boolean parameter '" + name + "' was not supplied");
  198. }
  199. if ("".equals(s)) {
  200. throw new ServletRequestBindingException("Required boolean parameter '" + name + "' contained no value");
  201. }
  202. return s.equalsIgnoreCase("true") || s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("on") || s.equals("1");
  203. }
  204. /**
  205. * Get a string parameter, with a fallback value. Never throws an exception.
  206. * Can pass a distinguished value to default to enable checks of whether it was supplied.
  207. */
  208. public static String getStringParameter(HttpServletRequest request, String name, String defaultVal) {
  209. try {
  210. return getRequiredStringParameter(request, name);
  211. }
  212. catch (ServletRequestBindingException ex) {
  213. return defaultVal;
  214. }
  215. }
  216. /**
  217. * Get a string parameter, throwing an exception if it isn't found or is empty.
  218. * @throws ServletRequestBindingException: subclass of ServletException,
  219. * so it doesn't need to be caught
  220. */
  221. public static String getRequiredStringParameter(HttpServletRequest request, String name)
  222. throws ServletRequestBindingException {
  223. String s = request.getParameter(name);
  224. if (s == null) {
  225. throw new ServletRequestBindingException("Required string parameter '" + name + "' was not supplied");
  226. }
  227. if ("".equals(s)) {
  228. throw new ServletRequestBindingException("Required string parameter '" + name + "' contained no value");
  229. }
  230. return s;
  231. }
  232. }