1 16 17 package org.springframework.web.bind; 18 19 import javax.servlet.ServletRequest ; 20 21 32 public abstract class ServletRequestUtils { 33 34 private static final IntParser INT_PARSER = new IntParser(); 35 36 private static final LongParser LONG_PARSER = new LongParser(); 37 38 private static final FloatParser FLOAT_PARSER = new FloatParser(); 39 40 private static final DoubleParser DOUBLE_PARSER = new DoubleParser(); 41 42 private static final BooleanParser BOOLEAN_PARSER = new BooleanParser(); 43 44 private static final StringParser STRING_PARSER = new StringParser(); 45 46 47 56 public static Integer getIntParameter(ServletRequest request, String name) 57 throws ServletRequestBindingException { 58 59 if (request.getParameter(name) == null) { 60 return null; 61 } 62 return new Integer (getRequiredIntParameter(request, name)); 63 } 64 65 72 public static int getIntParameter(ServletRequest request, String name, int defaultVal) { 73 if (request.getParameter(name) == null) { 74 return defaultVal; 75 } 76 try { 77 return getRequiredIntParameter(request, name); 78 } 79 catch (ServletRequestBindingException ex) { 80 return defaultVal; 81 } 82 } 83 84 89 public static int[] getIntParameters(ServletRequest request, String name) { 90 try { 91 return getRequiredIntParameters(request, name); 92 } 93 catch (ServletRequestBindingException ex) { 94 return new int[0]; 95 } 96 } 97 98 105 public static int getRequiredIntParameter(ServletRequest request, String name) 106 throws ServletRequestBindingException { 107 108 return INT_PARSER.parseInt(name, request.getParameter(name)); 109 } 110 111 118 public static int[] getRequiredIntParameters(ServletRequest request, String name) 119 throws ServletRequestBindingException { 120 121 return INT_PARSER.parseInts(name, request.getParameterValues(name)); 122 } 123 124 125 134 public static Long getLongParameter(ServletRequest request, String name) 135 throws ServletRequestBindingException { 136 137 if (request.getParameter(name) == null) { 138 return null; 139 } 140 return new Long (getRequiredLongParameter(request, name)); 141 } 142 143 150 public static long getLongParameter(ServletRequest request, String name, long defaultVal) { 151 if (request.getParameter(name) == null) { 152 return defaultVal; 153 } 154 try { 155 return getRequiredLongParameter(request, name); 156 } 157 catch (ServletRequestBindingException ex) { 158 return defaultVal; 159 } 160 } 161 162 167 public static long[] getLongParameters(ServletRequest request, String name) { 168 try { 169 return getRequiredLongParameters(request, name); 170 } 171 catch (ServletRequestBindingException ex) { 172 return new long[0]; 173 } 174 } 175 176 183 public static long getRequiredLongParameter(ServletRequest request, String name) 184 throws ServletRequestBindingException { 185 186 return LONG_PARSER.parseLong(name, request.getParameter(name)); 187 } 188 189 196 public static long[] getRequiredLongParameters(ServletRequest request, String name) 197 throws ServletRequestBindingException { 198 199 return LONG_PARSER.parseLongs(name, request.getParameterValues(name)); 200 } 201 202 203 212 public static Float getFloatParameter(ServletRequest request, String name) 213 throws ServletRequestBindingException { 214 215 if (request.getParameter(name) == null) { 216 return null; 217 } 218 return new Float (getRequiredFloatParameter(request, name)); 219 } 220 221 228 public static float getFloatParameter(ServletRequest request, String name, float defaultVal) { 229 if (request.getParameter(name) == null) { 230 return defaultVal; 231 } 232 try { 233 return getRequiredFloatParameter(request, name); 234 } 235 catch (ServletRequestBindingException ex) { 236 return defaultVal; 237 } 238 } 239 240 245 public static float[] getFloatParameters(ServletRequest request, String name) { 246 try { 247 return getRequiredFloatParameters(request, name); 248 } 249 catch (ServletRequestBindingException ex) { 250 return new float[0]; 251 } 252 } 253 254 261 public static float getRequiredFloatParameter(ServletRequest request, String name) 262 throws ServletRequestBindingException { 263 264 return FLOAT_PARSER.parseFloat(name, request.getParameter(name)); 265 } 266 267 274 public static float[] getRequiredFloatParameters(ServletRequest request, String name) 275 throws ServletRequestBindingException { 276 277 return FLOAT_PARSER.parseFloats(name, request.getParameterValues(name)); 278 } 279 280 281 290 public static Double getDoubleParameter(ServletRequest request, String name) 291 throws ServletRequestBindingException { 292 293 if (request.getParameter(name) == null) { 294 return null; 295 } 296 return new Double (getRequiredDoubleParameter(request, name)); 297 } 298 299 306 public static double getDoubleParameter(ServletRequest request, String name, double defaultVal) { 307 if (request.getParameter(name) == null) { 308 return defaultVal; 309 } 310 try { 311 return getRequiredDoubleParameter(request, name); 312 } 313 catch (ServletRequestBindingException ex) { 314 return defaultVal; 315 } 316 } 317 318 323 public static double[] getDoubleParameters(ServletRequest request, String name) { 324 try { 325 return getRequiredDoubleParameters(request, name); 326 } 327 catch (ServletRequestBindingException ex) { 328 return new double[0]; 329 } 330 } 331 332 339 public static double getRequiredDoubleParameter(ServletRequest request, String name) 340 throws ServletRequestBindingException { 341 342 return DOUBLE_PARSER.parseDouble(name, request.getParameter(name)); 343 } 344 345 352 public static double[] getRequiredDoubleParameters(ServletRequest request, String name) 353 throws ServletRequestBindingException { 354 355 return DOUBLE_PARSER.parseDoubles(name, request.getParameterValues(name)); 356 } 357 358 359 370 public static Boolean getBooleanParameter(ServletRequest request, String name) 371 throws ServletRequestBindingException { 372 373 if (request.getParameter(name) == null) { 374 return null; 375 } 376 return (getRequiredBooleanParameter(request, name) ? Boolean.TRUE : Boolean.FALSE); 377 } 378 379 388 public static boolean getBooleanParameter(ServletRequest request, String name, boolean defaultVal) { 389 if (request.getParameter(name) == null) { 390 return defaultVal; 391 } 392 try { 393 return getRequiredBooleanParameter(request, name); 394 } 395 catch (ServletRequestBindingException ex) { 396 return defaultVal; 397 } 398 } 399 400 407 public static boolean[] getBooleanParameters(ServletRequest request, String name) { 408 try { 409 return getRequiredBooleanParameters(request, name); 410 } 411 catch (ServletRequestBindingException ex) { 412 return new boolean[0]; 413 } 414 } 415 416 426 public static boolean getRequiredBooleanParameter(ServletRequest request, String name) 427 throws ServletRequestBindingException { 428 429 return BOOLEAN_PARSER.parseBoolean(name, request.getParameter(name)); 430 } 431 432 442 public static boolean[] getRequiredBooleanParameters(ServletRequest request, String name) 443 throws ServletRequestBindingException { 444 445 return BOOLEAN_PARSER.parseBooleans(name, request.getParameterValues(name)); 446 } 447 448 449 457 public static String getStringParameter(ServletRequest request, String name) 458 throws ServletRequestBindingException { 459 460 if (request.getParameter(name) == null) { 461 return null; 462 } 463 return getRequiredStringParameter(request, name); 464 } 465 466 473 public static String getStringParameter(ServletRequest request, String name, String defaultVal) { 474 String val = request.getParameter(name); 475 return (val != null ? val : defaultVal); 476 } 477 478 483 public static String [] getStringParameters(ServletRequest request, String name) { 484 try { 485 return getRequiredStringParameters(request, name); 486 } 487 catch (ServletRequestBindingException ex) { 488 return new String [0]; 489 } 490 } 491 492 499 public static String getRequiredStringParameter(ServletRequest request, String name) 500 throws ServletRequestBindingException { 501 502 return STRING_PARSER.validateRequiredString(name, request.getParameter(name)); 503 } 504 505 512 public static String [] getRequiredStringParameters(ServletRequest request, String name) 513 throws ServletRequestBindingException { 514 515 return STRING_PARSER.validateRequiredStrings(name, request.getParameterValues(name)); 516 } 517 518 519 private abstract static class ParameterParser { 520 521 protected final Object parse(String name, String parameter) throws ServletRequestBindingException { 522 validateRequiredParameter(name, parameter); 523 try { 524 return doParse(parameter); 525 } 526 catch (NumberFormatException ex) { 527 throw new ServletRequestBindingException( 528 "Required " + getType() + " parameter '" + name + "' with value of '" + 529 parameter + "' is not a valid number", ex); 530 } 531 } 532 533 protected final void validateRequiredParameter(String name, Object parameter) 534 throws ServletRequestBindingException { 535 536 if (parameter == null) { 537 throw new MissingServletRequestParameterException(name, getType()); 538 } 539 } 540 541 protected abstract String getType(); 542 543 protected abstract Object doParse(String parameter) throws NumberFormatException ; 544 } 545 546 547 private static class IntParser extends ParameterParser { 548 549 protected String getType() { 550 return "int"; 551 } 552 553 protected Object doParse(String s) throws NumberFormatException { 554 return Integer.valueOf(s); 555 } 556 557 public int parseInt(String name, String parameter) throws ServletRequestBindingException { 558 return ((Number ) parse(name, parameter)).intValue(); 559 } 560 561 public int[] parseInts(String name, String [] values) throws ServletRequestBindingException { 562 validateRequiredParameter(name, values); 563 int[] parameters = new int[values.length]; 564 for (int i = 0; i < values.length; i++) { 565 parameters[i] = parseInt(name, values[i]); 566 } 567 return parameters; 568 } 569 } 570 571 572 private static class LongParser extends ParameterParser { 573 574 protected String getType() { 575 return "long"; 576 } 577 578 protected Object doParse(String parameter) throws NumberFormatException { 579 return Long.valueOf(parameter); 580 } 581 582 public long parseLong(String name, String parameter) throws ServletRequestBindingException { 583 return ((Number ) parse(name, parameter)).longValue(); 584 } 585 586 public long[] parseLongs(String name, String [] values) throws ServletRequestBindingException { 587 validateRequiredParameter(name, values); 588 long[] parameters = new long[values.length]; 589 for (int i = 0; i < values.length; i++) { 590 parameters[i] = parseLong(name, values[i]); 591 } 592 return parameters; 593 } 594 } 595 596 597 private static class FloatParser extends ParameterParser { 598 599 protected String getType() { 600 return "float"; 601 } 602 603 protected Object doParse(String parameter) throws NumberFormatException { 604 return Float.valueOf(parameter); 605 } 606 607 public float parseFloat(String name, String parameter) throws ServletRequestBindingException { 608 return ((Number ) parse(name, parameter)).floatValue(); 609 } 610 611 public float[] parseFloats(String name, String [] values) throws ServletRequestBindingException { 612 validateRequiredParameter(name, values); 613 float[] parameters = new float[values.length]; 614 for (int i = 0; i < values.length; i++) { 615 parameters[i] = parseFloat(name, values[i]); 616 } 617 return parameters; 618 } 619 } 620 621 622 private static class DoubleParser extends ParameterParser { 623 624 protected String getType() { 625 return "double"; 626 } 627 628 protected Object doParse(String parameter) throws NumberFormatException { 629 return Double.valueOf(parameter); 630 } 631 632 public double parseDouble(String name, String parameter) throws ServletRequestBindingException { 633 return ((Number ) parse(name, parameter)).doubleValue(); 634 } 635 636 public double[] parseDoubles(String name, String [] values) throws ServletRequestBindingException { 637 validateRequiredParameter(name, values); 638 double[] parameters = new double[values.length]; 639 for (int i = 0; i < values.length; i++) { 640 parameters[i] = parseDouble(name, values[i]); 641 } 642 return parameters; 643 } 644 } 645 646 647 private static class BooleanParser extends ParameterParser { 648 649 protected String getType() { 650 return "boolean"; 651 } 652 653 protected Object doParse(String parameter) throws NumberFormatException { 654 return (parameter.equalsIgnoreCase("true") || parameter.equalsIgnoreCase("on") || 655 parameter.equalsIgnoreCase("yes") || parameter.equals("1") ? Boolean.TRUE : Boolean.FALSE); 656 } 657 658 public boolean parseBoolean(String name, String parameter) throws ServletRequestBindingException { 659 return ((Boolean ) parse(name, parameter)).booleanValue(); 660 } 661 662 public boolean[] parseBooleans(String name, String [] values) throws ServletRequestBindingException { 663 validateRequiredParameter(name, values); 664 boolean[] parameters = new boolean[values.length]; 665 for (int i = 0; i < values.length; i++) { 666 parameters[i] = parseBoolean(name, values[i]); 667 } 668 return parameters; 669 } 670 } 671 672 673 private static class StringParser extends ParameterParser { 674 675 protected String getType() { 676 return "string"; 677 } 678 679 protected Object doParse(String parameter) throws NumberFormatException { 680 return parameter; 681 } 682 683 public String validateRequiredString(String name, String value) throws ServletRequestBindingException { 684 validateRequiredParameter(name, value); 685 return value; 686 } 687 688 public String [] validateRequiredStrings(String name, String [] values) throws ServletRequestBindingException { 689 validateRequiredParameter(name, values); 690 for (int i = 0; i < values.length; i++) { 691 validateRequiredParameter(name, values[i]); 692 } 693 return values; 694 } 695 } 696 697 } 698 | Popular Tags |