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 java.util.Map;
020import java.util.concurrent.ConcurrentHashMap;
021
022import org.springframework.web.util.WebUtils;
023
024/**
025 * Simple implementation of {@link MethodNameResolver} that maps URL to
026 * method name. Although this is the default implementation used by the
027 * {@link MultiActionController} class (because it requires no configuration),
028 * it's bit naive for most applications. In particular, we don't usually
029 * want to tie URL to implementation methods.
030 *
031 * <p>Maps the resource name after the last slash, ignoring an extension.
032 * E.g. "/foo/bar/baz.html" to "baz", assuming a "/foo/bar/baz.html"
033 * controller mapping to the corresponding MultiActionController handler.
034 * method. Doesn't support wildcards.
035 *
036 * @author Rod Johnson
037 * @author Juergen Hoeller
038 * @deprecated as of 4.3, in favor of annotation-driven handler methods
039 */
040@Deprecated
041public class InternalPathMethodNameResolver extends AbstractUrlMethodNameResolver {
042
043        private String prefix = "";
044
045        private String suffix = "";
046
047        /** Request URL path String --> method name String */
048        private final Map<String, String> methodNameCache = new ConcurrentHashMap<String, String>(16);
049
050
051        /**
052         * Specify a common prefix for handler method names.
053         * Will be prepended to the internal path found in the URL:
054         * e.g. internal path "baz", prefix "my" -> method name "mybaz".
055         */
056        public void setPrefix(String prefix) {
057                this.prefix = (prefix != null ? prefix : "");
058        }
059
060        /**
061         * Return the common prefix for handler method names.
062         */
063        protected String getPrefix() {
064                return this.prefix;
065        }
066
067        /**
068         * Specify a common suffix for handler method names.
069         * Will be appended to the internal path found in the URL:
070         * e.g. internal path "baz", suffix "Handler" -> method name "bazHandler".
071         */
072        public void setSuffix(String suffix) {
073                this.suffix = (suffix != null ? suffix : "");
074        }
075
076        /**
077         * Return the common suffix for handler method names.
078         */
079        protected String getSuffix() {
080                return this.suffix;
081        }
082
083
084        /**
085         * Extracts the method name indicated by the URL path.
086         * @see #extractHandlerMethodNameFromUrlPath
087         * @see #postProcessHandlerMethodName
088         */
089        @Override
090        protected String getHandlerMethodNameForUrlPath(String urlPath) {
091                String methodName = this.methodNameCache.get(urlPath);
092                if (methodName == null) {
093                        methodName = extractHandlerMethodNameFromUrlPath(urlPath);
094                        methodName = postProcessHandlerMethodName(methodName);
095                        this.methodNameCache.put(urlPath, methodName);
096                }
097                return methodName;
098        }
099
100        /**
101         * Extract the handler method name from the given request URI.
102         * Delegates to {@code WebUtils.extractFilenameFromUrlPath(String)}.
103         * @param uri the request URI (e.g. "/index.html")
104         * @return the extracted URI filename (e.g. "index")
105         * @see org.springframework.web.util.WebUtils#extractFilenameFromUrlPath
106         */
107        protected String extractHandlerMethodNameFromUrlPath(String uri) {
108                return WebUtils.extractFilenameFromUrlPath(uri);
109        }
110
111        /**
112         * Build the full handler method name based on the given method name
113         * as indicated by the URL path.
114         * <p>The default implementation simply applies prefix and suffix.
115         * This can be overridden, for example, to manipulate upper case
116         * / lower case, etc.
117         * @param methodName the original method name, as indicated by the URL path
118         * @return the full method name to use
119         * @see #getPrefix()
120         * @see #getSuffix()
121         */
122        protected String postProcessHandlerMethodName(String methodName) {
123                return getPrefix() + methodName + getSuffix();
124        }
125
126}