/gator/tags/gator-1.0/src/main/java/edu/vt/middleware/gator/web/RequestParamExtractor.java

http://vt-middleware.googlecode.com/ · Java · 104 lines · 41 code · 11 blank · 52 comment · 2 complexity · a4f3bf635dcb09770b0bda705febb2ec MD5 · raw file

  1. /*
  2. $Id: RequestParamExtractor.java 24 2009-02-12 20:52:03Z marvin.addison $
  3. Copyright (C) 2008 Virginia Tech, Marvin S. Addison.
  4. All rights reserved.
  5. SEE LICENSE FOR MORE INFORMATION
  6. Author: Marvin S. Addison
  7. Email: serac@vt.edu
  8. Version: $Revision: 24 $
  9. Updated: $Date: 2009-02-12 21:52:03 +0100 (Thu, 12 Feb 2009) $
  10. */
  11. package edu.vt.middleware.gator.web;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14. import javax.servlet.http.HttpServletRequest;
  15. /**
  16. * Helper class that pulls parameters out of pretty URLs.
  17. *
  18. * @author Marvin S. Addison
  19. * @version $Revision: 24 $
  20. *
  21. */
  22. public class RequestParamExtractor
  23. {
  24. protected static final Pattern PROJECT_NAME_PATTERN =
  25. Pattern.compile("/project/([^/]+)/");
  26. protected static final Pattern APPENDER_ID_PATTERN =
  27. Pattern.compile("/appender/(\\d+)/");
  28. protected static final Pattern CATEGORY_ID_PATTERN =
  29. Pattern.compile("/category/(\\d+)/");
  30. protected static final Pattern CLIENT_ID_PATTERN =
  31. Pattern.compile("/client/(\\d+)/");
  32. /**
  33. * Extracts the project name from a request URI of the form
  34. * /project/NAME/file.html.
  35. * @param request Request to extract project name from.
  36. * @return Project name in given request or null if no project name is
  37. * found in the URI.
  38. */
  39. public static String getProjectName(final HttpServletRequest request)
  40. {
  41. final Matcher m = PROJECT_NAME_PATTERN.matcher(request.getRequestURI());
  42. return m.find() ? m.group(1) : null;
  43. }
  44. /**
  45. * Extracts the appender ID from a request URI of the form
  46. * /project/ID/appender/APPID/file.html.
  47. * @param request Request to extract appender ID from.
  48. * @return Appender ID in given request.
  49. */
  50. public static int getAppenderId(final HttpServletRequest request)
  51. {
  52. return getParam(APPENDER_ID_PATTERN, request.getRequestURI());
  53. }
  54. /**
  55. * Extracts the category ID from a request URI of the form
  56. * /project/ID/category/CATID/file.html.
  57. * @param request Request to extract category ID from.
  58. * @return Category ID in given request.
  59. */
  60. public static int getCategoryId(final HttpServletRequest request)
  61. {
  62. return getParam(CATEGORY_ID_PATTERN, request.getRequestURI());
  63. }
  64. /**
  65. * Extracts the client ID from a request URI of the form
  66. * /project/ID/client/CLIENTID/file.html.
  67. * @param request Request to extract client ID from.
  68. * @return Client ID in given request.
  69. */
  70. public static int getClientId(final HttpServletRequest request)
  71. {
  72. return getParam(CLIENT_ID_PATTERN, request.getRequestURI());
  73. }
  74. /**
  75. * Extracts a parameter from a URL string using the given pattern.
  76. * @param pattern Pattern that contains a capture expression to extract
  77. * the parameter.
  78. * @param url URL path string.
  79. * @return Parameter value or -1 if no matching parameter is found.
  80. */
  81. protected static int getParam(final Pattern pattern, final String url)
  82. {
  83. final Matcher m = pattern.matcher(url);
  84. if (m.find()) {
  85. return Integer.parseInt(m.group(1));
  86. } else {
  87. return -1;
  88. }
  89. }
  90. }