1 16 17 package org.springframework.web.bind; 18 19 import javax.servlet.ServletException ; 20 import javax.servlet.http.HttpServletRequest ; 21 22 import org.springframework.web.HttpRequestMethodNotSupportedException; 23 24 37 public abstract class RequestUtils { 38 39 45 public static void rejectRequestMethod(HttpServletRequest request, String method) throws ServletException { 46 if (request.getMethod().equals(method)) { 47 throw new HttpRequestMethodNotSupportedException(method); 48 } 49 } 50 51 52 61 public static Integer getIntParameter(HttpServletRequest request, String name) 62 throws ServletRequestBindingException { 63 64 return ServletRequestUtils.getIntParameter(request, name); 65 } 66 67 74 public static int getIntParameter(HttpServletRequest request, String name, int defaultVal) { 75 return ServletRequestUtils.getIntParameter(request, name, defaultVal); 76 } 77 78 83 public static int[] getIntParameters(HttpServletRequest request, String name) { 84 return ServletRequestUtils.getIntParameters(request, name); 85 } 86 87 94 public static int getRequiredIntParameter(HttpServletRequest request, String name) 95 throws ServletRequestBindingException { 96 97 return ServletRequestUtils.getRequiredIntParameter(request, name); 98 } 99 100 107 public static int[] getRequiredIntParameters(HttpServletRequest request, String name) 108 throws ServletRequestBindingException { 109 110 return ServletRequestUtils.getRequiredIntParameters(request, name); 111 } 112 113 114 123 public static Long getLongParameter(HttpServletRequest request, String name) 124 throws ServletRequestBindingException { 125 126 return ServletRequestUtils.getLongParameter(request, name); 127 } 128 129 136 public static long getLongParameter(HttpServletRequest request, String name, long defaultVal) { 137 return ServletRequestUtils.getLongParameter(request, name, defaultVal); 138 } 139 140 145 public static long[] getLongParameters(HttpServletRequest request, String name) { 146 return ServletRequestUtils.getLongParameters(request, name); 147 } 148 149 156 public static long getRequiredLongParameter(HttpServletRequest request, String name) 157 throws ServletRequestBindingException { 158 159 return ServletRequestUtils.getRequiredLongParameter(request, name); 160 } 161 162 169 public static long[] getRequiredLongParameters(HttpServletRequest request, String name) 170 throws ServletRequestBindingException { 171 172 return ServletRequestUtils.getRequiredLongParameters(request, name); 173 } 174 175 176 185 public static Float getFloatParameter(HttpServletRequest request, String name) 186 throws ServletRequestBindingException { 187 188 return ServletRequestUtils.getFloatParameter(request, name); 189 } 190 191 198 public static float getFloatParameter(HttpServletRequest request, String name, float defaultVal) { 199 return ServletRequestUtils.getFloatParameter(request, name, defaultVal); 200 } 201 202 207 public static float[] getFloatParameters(HttpServletRequest request, String name) { 208 return ServletRequestUtils.getFloatParameters(request, name); 209 } 210 211 218 public static float getRequiredFloatParameter(HttpServletRequest request, String name) 219 throws ServletRequestBindingException { 220 221 return ServletRequestUtils.getRequiredFloatParameter(request, name); 222 } 223 224 231 public static float[] getRequiredFloatParameters(HttpServletRequest request, String name) 232 throws ServletRequestBindingException { 233 234 return ServletRequestUtils.getRequiredFloatParameters(request, name); 235 } 236 237 238 247 public static Double getDoubleParameter(HttpServletRequest request, String name) 248 throws ServletRequestBindingException { 249 250 return ServletRequestUtils.getDoubleParameter(request, name); 251 } 252 253 260 public static double getDoubleParameter(HttpServletRequest request, String name, double defaultVal) { 261 return ServletRequestUtils.getDoubleParameter(request, name, defaultVal); 262 } 263 264 269 public static double[] getDoubleParameters(HttpServletRequest request, String name) { 270 return ServletRequestUtils.getDoubleParameters(request, name); 271 } 272 273 280 public static double getRequiredDoubleParameter(HttpServletRequest request, String name) 281 throws ServletRequestBindingException { 282 283 return ServletRequestUtils.getRequiredDoubleParameter(request, name); 284 } 285 286 293 public static double[] getRequiredDoubleParameters(HttpServletRequest request, String name) 294 throws ServletRequestBindingException { 295 296 return ServletRequestUtils.getRequiredDoubleParameters(request, name); 297 } 298 299 300 311 public static Boolean getBooleanParameter(HttpServletRequest request, String 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 329 public static boolean getBooleanParameter(HttpServletRequest request, String 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 348 public static boolean[] getBooleanParameters(HttpServletRequest request, String name) { 349 try { 350 return getRequiredBooleanParameters(request, name); 351 } 352 catch (ServletRequestBindingException ex) { 353 return new boolean[0]; 354 } 355 } 356 357 367 public static boolean getRequiredBooleanParameter(HttpServletRequest request, String 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 388 public static boolean[] getRequiredBooleanParameters(HttpServletRequest request, String 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 411 public static String getStringParameter(HttpServletRequest request, String name) 412 throws ServletRequestBindingException { 413 414 if (request.getParameter(name) == null) { 415 return null; 416 } 417 return getRequiredStringParameter(request, name); 418 } 419 420 427 public static String getStringParameter(HttpServletRequest request, String name, String 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 444 public static String [] getStringParameters(HttpServletRequest request, String name) { 445 try { 446 return getRequiredStringParameters(request, name); 447 } 448 catch (ServletRequestBindingException ex) { 449 return new String [0]; 450 } 451 } 452 453 460 public static String getRequiredStringParameter(HttpServletRequest request, String name) 461 throws ServletRequestBindingException { 462 463 String 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 478 public static String [] getRequiredStringParameters(HttpServletRequest request, String name) 479 throws ServletRequestBindingException { 480 481 String [] 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 |