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.servlet.mvc.multiaction;
  17. import javax.servlet.http.HttpServletRequest;
  18. import org.apache.commons.logging.Log;
  19. import org.apache.commons.logging.LogFactory;
  20. import org.springframework.web.util.UrlPathHelper;
  21. /**
  22. * Abstract base class for URL-based MethodNameResolver implementations.
  23. *
  24. * <p>Provides infrastructure for mapping handlers to URLs and configurable
  25. * URL lookup. For information on the latter, see alwaysUseFullPath property.
  26. *
  27. * @author Juergen Hoeller
  28. * @since 14.01.2004
  29. * @see #setAlwaysUseFullPath
  30. * @see #setUrlDecode
  31. */
  32. public abstract class AbstractUrlMethodNameResolver implements MethodNameResolver {
  33. protected final Log logger = LogFactory.getLog(getClass());
  34. private UrlPathHelper urlPathHelper = new UrlPathHelper();
  35. /**
  36. * Set if URL lookup should always use full path within current servlet
  37. * context. Else, the path within the current servlet mapping is used
  38. * if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml).
  39. * Default is false.
  40. * @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
  41. */
  42. public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
  43. this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
  44. }
  45. /**
  46. * Set if context path and request URI should be URL-decoded.
  47. * Both are returned <i>undecoded</i> by the Servlet API,
  48. * in contrast to the servlet path.
  49. * <p>Uses either the request encoding or the default encoding according
  50. * to the Servlet spec (ISO-8859-1).
  51. * <p>Note: Setting this to true requires J2SE 1.4, as J2SE 1.3's
  52. * URLDecoder class does not offer a way to specify the encoding.
  53. * @see org.springframework.web.util.UrlPathHelper#setUrlDecode
  54. */
  55. public void setUrlDecode(boolean urlDecode) {
  56. this.urlPathHelper.setUrlDecode(urlDecode);
  57. }
  58. /**
  59. * Set the UrlPathHelper to use for resolution of lookup paths.
  60. * <p>Use this to override the default UrlPathHelper with a custom subclass,
  61. * or to share common UrlPathHelper settings across multiple MethodNameResolvers
  62. * and HandlerMappings.
  63. * @see org.springframework.web.servlet.handler.AbstractUrlHandlerMapping#setUrlPathHelper
  64. */
  65. public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
  66. this.urlPathHelper = urlPathHelper;
  67. }
  68. /**
  69. * This implementation of getHandlerMethodName retrieves the URL path
  70. * to use for lookup and delegates to getHandlerMethodNameForUrlPath.
  71. * Converts null values to NoSuchRequestHandlingMethodExceptions.
  72. */
  73. public final String getHandlerMethodName(HttpServletRequest request) throws NoSuchRequestHandlingMethodException {
  74. String urlPath = this.urlPathHelper.getLookupPathForRequest(request);
  75. String name = getHandlerMethodNameForUrlPath(urlPath);
  76. if (name == null) {
  77. throw new NoSuchRequestHandlingMethodException(request);
  78. }
  79. logger.debug("Returning MultiActionController method name '" + name + "' for lookup path '" + urlPath + "'");
  80. return name;
  81. }
  82. /**
  83. * Return a method name that can handle this request, based on the
  84. * given lookup path. Called by this class' getHandlerMethodName.
  85. * @param urlPath the URL path to use for lookup,
  86. * according to the settings in this class
  87. * @return a method name that can handle this request.
  88. * Should return null if no matching method found.
  89. * @see #getHandlerMethodName
  90. * @see #setAlwaysUseFullPath
  91. * @see #setUrlDecode
  92. */
  93. protected abstract String getHandlerMethodNameForUrlPath(String urlPath);
  94. }