001: /*
002: * Copyright 2002-2007 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: * http://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:
017: package org.springframework.web.servlet.mvc.multiaction;
018:
019: import java.util.Map;
020:
021: import org.springframework.core.CollectionFactory;
022: import 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: */
039: public class InternalPathMethodNameResolver extends
040: AbstractUrlMethodNameResolver {
041:
042: private String prefix = "";
043:
044: private String suffix = "";
045:
046: /** Request URL path String --> method name String */
047: private final Map methodNameCache = CollectionFactory
048: .createConcurrentMapIfPossible(16);
049:
050: /**
051: * Specify a common prefix for handler method names.
052: * Will be prepended to the internal path found in the URL:
053: * e.g. internal path "baz", prefix "my" -> method name "mybaz".
054: */
055: public void setPrefix(String prefix) {
056: this .prefix = (prefix != null ? prefix : "");
057: }
058:
059: /**
060: * Return the common prefix for handler method names.
061: */
062: protected String getPrefix() {
063: return this .prefix;
064: }
065:
066: /**
067: * Specify a common suffix for handler method names.
068: * Will be appended to the internal path found in the URL:
069: * e.g. internal path "baz", suffix "Handler" -> method name "bazHandler".
070: */
071: public void setSuffix(String suffix) {
072: this .suffix = (suffix != null ? suffix : "");
073: }
074:
075: /**
076: * Return the common suffix for handler method names.
077: */
078: protected String getSuffix() {
079: return this .suffix;
080: }
081:
082: /**
083: * Extracts the method name indicated by the URL path.
084: * @see #extractHandlerMethodNameFromUrlPath
085: * @see #postProcessHandlerMethodName
086: */
087: protected String getHandlerMethodNameForUrlPath(String urlPath) {
088: String methodName = (String) this .methodNameCache.get(urlPath);
089: if (methodName == null) {
090: methodName = extractHandlerMethodNameFromUrlPath(urlPath);
091: methodName = postProcessHandlerMethodName(methodName);
092: this .methodNameCache.put(urlPath, methodName);
093: }
094: return methodName;
095: }
096:
097: /**
098: * Extract the handler method name from the given request URI.
099: * Delegates to <code>WebUtils.extractViewNameFromUrlPath(String)</code>.
100: * @param uri the request URI (e.g. "/index.html")
101: * @return the extracted URI filename (e.g. "index")
102: * @see org.springframework.web.util.WebUtils#extractFilenameFromUrlPath
103: */
104: protected String extractHandlerMethodNameFromUrlPath(String uri) {
105: return WebUtils.extractFilenameFromUrlPath(uri);
106: }
107:
108: /**
109: * Build the full handler method name based on the given method name
110: * as indicated by the URL path.
111: * <p>The default implementation simply applies prefix and suffix.
112: * This can be overridden, for example, to manipulate upper case
113: * / lower case, etc.
114: * @param methodName the original method name, as indicated by the URL path
115: * @return the full method name to use
116: * @see #getPrefix()
117: * @see #getSuffix()
118: */
119: protected String postProcessHandlerMethodName(String methodName) {
120: return getPrefix() + methodName + getSuffix();
121: }
122:
123: }
|