01: /*
02: * Copyright 2002-2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.Iterator;
20: import java.util.Properties;
21:
22: import org.springframework.beans.factory.InitializingBean;
23: import org.springframework.util.AntPathMatcher;
24: import org.springframework.util.Assert;
25: import org.springframework.util.PathMatcher;
26:
27: /**
28: * The most flexible out-of-the-box implementation of the MethodNameResolver
29: * interface. Uses <code>java.util.Properties</code> to define the mapping
30: * between the URL of incoming requests and the corresponding method name.
31: * Such properties can be held in an XML document.
32: *
33: * <p>Properties format is
34: * <code>
35: * /welcome.html=displayGenresPage
36: * </code>
37: * Note that method overloading isn't allowed, so there's no need to
38: * specify arguments.
39: *
40: * <p>Supports direct matches, e.g. a registered "/test" matches "/test",
41: * and a various Ant-style pattern matches, e.g. a registered "/t*" matches
42: * both "/test" and "/team". For details, see the AntPathMatcher javadoc.
43: *
44: * @author Rod Johnson
45: * @author Juergen Hoeller
46: * @see java.util.Properties
47: * @see org.springframework.util.AntPathMatcher
48: */
49: public class PropertiesMethodNameResolver extends
50: AbstractUrlMethodNameResolver implements InitializingBean {
51:
52: private Properties mappings;
53:
54: private PathMatcher pathMatcher = new AntPathMatcher();
55:
56: /**
57: * Set explicit URL to method name mappings through a Properties object.
58: * @param mappings Properties with URL as key and method name as value
59: */
60: public void setMappings(Properties mappings) {
61: this .mappings = mappings;
62: }
63:
64: /**
65: * Set the PathMatcher implementation to use for matching URL paths
66: * against registered URL patterns. Default is AntPathMatcher.
67: * @see org.springframework.util.AntPathMatcher
68: */
69: public void setPathMatcher(PathMatcher pathMatcher) {
70: Assert.notNull(pathMatcher, "PathMatcher must not be null");
71: this .pathMatcher = pathMatcher;
72: }
73:
74: public void afterPropertiesSet() {
75: if (this .mappings == null || this .mappings.isEmpty()) {
76: throw new IllegalArgumentException(
77: "'mappings' property is required");
78: }
79: }
80:
81: protected String getHandlerMethodNameForUrlPath(String urlPath) {
82: String methodName = this .mappings.getProperty(urlPath);
83: if (methodName != null) {
84: return methodName;
85: }
86: for (Iterator it = this .mappings.keySet().iterator(); it
87: .hasNext();) {
88: String registeredPath = (String) it.next();
89: if (this .pathMatcher.match(registeredPath, urlPath)) {
90: return (String) this.mappings.get(registeredPath);
91: }
92: }
93: return null;
94: }
95:
96: }
|