KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > mvc > multiaction > InternalPathMethodNameResolver


1 /*
2  * Copyright 2002-2005 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
17 package org.springframework.web.servlet.mvc.multiaction;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.springframework.web.util.WebUtils;
24
25 /**
26  * Simple implementation of MethodNameResolver that maps URL to method
27  * name. Although this is the default implementation used by the
28  * MultiActionController class (because it requires no configuration),
29  * it's bit naive for most applications. In particular, we don't usually
30  * want to tie URL to implementation methods.
31  *
32  * <p>Maps the resource name after the last slash, ignoring an extension.
33  * E.g. "/foo/bar/baz.html" to "baz", assuming a "/foo/bar/baz.html"
34  * controller mapping to the corresponding MultiActionController handler.
35  * method. Doesn't support wildcards.
36  *
37  * @author Rod Johnson
38  * @author Juergen Hoeller
39 */

40 public class InternalPathMethodNameResolver extends AbstractUrlMethodNameResolver {
41
42     private String JavaDoc prefix = "";
43
44     private String JavaDoc suffix = "";
45
46     /** Request URL path String --> method name String */
47     private final Map JavaDoc methodNameCache = Collections.synchronizedMap(new HashMap JavaDoc());
48
49
50     /**
51      * Specify a common prefix for handler method names.
52      * Will be prepended to the internal path found in the URL:
53      * e.g. internal path "baz", prefix "my" -> method name "mybaz".
54      */

55     public void setPrefix(String JavaDoc prefix) {
56         this.prefix = (prefix != null ? prefix : "");
57     }
58
59     /**
60      * Return the common prefix for handler method names.
61      */

62     protected String JavaDoc getPrefix() {
63         return prefix;
64     }
65
66     /**
67      * Specify a common suffix for handler method names.
68      * Will be appended to the internal path found in the URL:
69      * e.g. internal path "baz", suffix "Handler" -> method name "bazHandler".
70      */

71     public void setSuffix(String JavaDoc suffix) {
72         this.suffix = (suffix != null ? suffix : "");
73     }
74
75     /**
76      * Return the common suffix for handler method names.
77      */

78     protected String JavaDoc getSuffix() {
79         return suffix;
80     }
81
82
83     /**
84      * Extracts the method name indicated by the URL path.
85      * @see #extractHandlerMethodNameFromUrlPath
86      * @see #postProcessHandlerMethodName
87      */

88     protected String JavaDoc getHandlerMethodNameForUrlPath(String JavaDoc urlPath) {
89         String JavaDoc methodName = (String JavaDoc) this.methodNameCache.get(urlPath);
90         if (methodName == null) {
91             methodName = extractHandlerMethodNameFromUrlPath(urlPath);
92             methodName = postProcessHandlerMethodName(methodName);
93             this.methodNameCache.put(urlPath, methodName);
94         }
95         return methodName;
96     }
97
98     /**
99      * Extract the handler method name from the given request URI.
100      * Delegates to <code>WebUtils.extractViewNameFromUrlPath(String)</code>.
101      * @param uri the request URI (e.g. "/index.html")
102      * @return the extracted URI filename (e.g. "index")
103      * @see org.springframework.web.util.WebUtils#extractFilenameFromUrlPath
104      */

105     protected String JavaDoc extractHandlerMethodNameFromUrlPath(String JavaDoc uri) {
106         return WebUtils.extractFilenameFromUrlPath(uri);
107     }
108
109     /**
110      * Build the full handler method name based on the given method name
111      * as indicated by the URL path.
112      * <p>The default implementation simply applies prefix and suffix.
113      * This can be overridden, for example, to manipulate upper case
114      * / lower case, etc.
115      * @param methodName the original method name, as indicated by the URL path
116      * @return the full method name to use
117      * @see #getPrefix()
118      * @see #getSuffix()
119      */

120     protected String JavaDoc postProcessHandlerMethodName(String JavaDoc methodName) {
121         return getPrefix() + methodName + getSuffix();
122     }
123
124 }
125
Popular Tags