001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet.mvc.multiaction;
018
019import javax.servlet.http.HttpServletRequest;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023
024import org.springframework.util.Assert;
025import org.springframework.web.util.UrlPathHelper;
026
027/**
028 * Abstract base class for URL-based {@link MethodNameResolver} implementations.
029 *
030 * <p>Provides infrastructure for mapping handlers to URLs and configurable
031 * URL lookup. For information on the latter, see the
032 * {@link #setAlwaysUseFullPath} "alwaysUseFullPath"}
033 * and {@link #setUrlDecode "urlDecode"} properties.
034 *
035 * @author Juergen Hoeller
036 * @since 14.01.2004
037 * @deprecated as of 4.3, in favor of annotation-driven handler methods
038 */
039@Deprecated
040public abstract class AbstractUrlMethodNameResolver implements MethodNameResolver {
041
042        /** Logger available to subclasses */
043        protected final Log logger = LogFactory.getLog(getClass());
044
045        private UrlPathHelper urlPathHelper = new UrlPathHelper();
046
047
048        /**
049         * Set if URL lookup should always use full path within current servlet
050         * context. Else, the path within the current servlet mapping is used
051         * if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml).
052         * Default is "false".
053         * @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
054         */
055        public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
056                this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
057        }
058
059        /**
060         * Set if context path and request URI should be URL-decoded.
061         * Both are returned <i>undecoded</i> by the Servlet API,
062         * in contrast to the servlet path.
063         * <p>Uses either the request encoding or the default encoding according
064         * to the Servlet spec (ISO-8859-1).
065         * @see org.springframework.web.util.UrlPathHelper#setUrlDecode
066         */
067        public void setUrlDecode(boolean urlDecode) {
068                this.urlPathHelper.setUrlDecode(urlDecode);
069        }
070
071        /**
072         * Set the UrlPathHelper to use for resolution of lookup paths.
073         * <p>Use this to override the default UrlPathHelper with a custom subclass,
074         * or to share common UrlPathHelper settings across multiple MethodNameResolvers
075         * and HandlerMappings.
076         * @see org.springframework.web.servlet.handler.AbstractUrlHandlerMapping#setUrlPathHelper
077         */
078        public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
079                Assert.notNull(urlPathHelper, "UrlPathHelper must not be null");
080                this.urlPathHelper = urlPathHelper;
081        }
082
083
084        /**
085         * Retrieves the URL path to use for lookup and delegates to
086         * {@code getHandlerMethodNameForUrlPath}.
087         * Converts {@code null} values to NoSuchRequestHandlingMethodExceptions.
088         * @see #getHandlerMethodNameForUrlPath
089         */
090        @Override
091        public final String getHandlerMethodName(HttpServletRequest request)
092                        throws NoSuchRequestHandlingMethodException {
093
094                String urlPath = this.urlPathHelper.getLookupPathForRequest(request);
095                String name = getHandlerMethodNameForUrlPath(urlPath);
096                if (name == null) {
097                        throw new NoSuchRequestHandlingMethodException(urlPath, request.getMethod(), request.getParameterMap());
098                }
099                if (logger.isDebugEnabled()) {
100                        logger.debug("Returning handler method name '" + name + "' for lookup path: " + urlPath);
101                }
102                return name;
103        }
104
105        /**
106         * Return a method name that can handle this request, based on the
107         * given lookup path. Called by {@code getHandlerMethodName}.
108         * @param urlPath the URL path to use for lookup,
109         * according to the settings in this class
110         * @return a method name that can handle this request.
111         * Should return null if no matching method found.
112         * @see #getHandlerMethodName
113         * @see #setAlwaysUseFullPath
114         * @see #setUrlDecode
115         */
116        protected abstract String getHandlerMethodNameForUrlPath(String urlPath);
117
118}