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.Enumeration;
020import java.util.Properties;
021
022import org.springframework.beans.factory.InitializingBean;
023import org.springframework.util.AntPathMatcher;
024import org.springframework.util.Assert;
025import org.springframework.util.PathMatcher;
026
027/**
028 * The most flexible out-of-the-box implementation of the {@link MethodNameResolver}
029 * interface. Uses {@code java.util.Properties} to define the mapping
030 * between the URL of incoming requests and the corresponding method name.
031 * Such properties can be held in an XML document.
032 *
033 * <p>Properties format is
034 * {@code
035 * /welcome.html=displayGenresPage
036 * }
037 * Note that method overloading isn't allowed, so there's no need to
038 * specify arguments.
039 *
040 * <p>Supports direct matches, e.g. a registered "/test" matches "/test",
041 * and a various Ant-style pattern matches, e.g. a registered "/t*" matches
042 * both "/test" and "/team". For details, see the AntPathMatcher javadoc.
043 *
044 * @author Rod Johnson
045 * @author Juergen Hoeller
046 * @see java.util.Properties
047 * @see org.springframework.util.AntPathMatcher
048 * @deprecated as of 4.3, in favor of annotation-driven handler methods
049 */
050@Deprecated
051public class PropertiesMethodNameResolver extends AbstractUrlMethodNameResolver
052                implements InitializingBean {
053
054        private Properties mappings;
055
056        private PathMatcher pathMatcher = new AntPathMatcher();
057
058
059        /**
060         * Set explicit URL to method name mappings through a Properties object.
061         * @param mappings Properties with URL as key and method name as value
062         */
063        public void setMappings(Properties mappings) {
064                this.mappings = mappings;
065        }
066
067        /**
068         * Set the PathMatcher implementation to use for matching URL paths
069         * against registered URL patterns. Default is AntPathMatcher.
070         * @see org.springframework.util.AntPathMatcher
071         */
072        public void setPathMatcher(PathMatcher pathMatcher) {
073                Assert.notNull(pathMatcher, "PathMatcher must not be null");
074                this.pathMatcher = pathMatcher;
075        }
076
077        @Override
078        public void afterPropertiesSet() {
079                if (this.mappings == null || this.mappings.isEmpty()) {
080                        throw new IllegalArgumentException("'mappings' property is required");
081                }
082        }
083
084
085        @Override
086        protected String getHandlerMethodNameForUrlPath(String urlPath) {
087                String methodName = this.mappings.getProperty(urlPath);
088                if (methodName != null) {
089                        return methodName;
090                }
091                Enumeration<?> propNames = this.mappings.propertyNames();
092                while (propNames.hasMoreElements()) {
093                        String registeredPath = (String) propNames.nextElement();
094                        if (this.pathMatcher.match(registeredPath, urlPath)) {
095                                return (String) this.mappings.get(registeredPath);
096                        }
097                }
098                return null;
099        }
100
101}