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