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.support;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.springframework.util.StringUtils;
023
024/**
025 * Implementation of {@link org.springframework.web.servlet.HandlerMapping} that
026 * follows a simple convention for generating URL path mappings from the <i>bean names</i>
027 * of registered {@link org.springframework.web.servlet.mvc.Controller} beans
028 * as well as {@code @Controller} annotated beans.
029 *
030 * <p>This is similar to {@link org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping}
031 * but doesn't expect bean names to follow the URL convention: It turns plain bean names
032 * into URLs by prepending a slash and optionally applying a specified prefix and/or suffix.
033 * However, it only does so for well-known {@link #isControllerType controller types},
034 * as listed above (analogous to {@link ControllerClassNameHandlerMapping}).
035 *
036 * @author Juergen Hoeller
037 * @since 2.5.3
038 * @see ControllerClassNameHandlerMapping
039 * @see org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
040 * @deprecated as of 4.3, in favor of annotation-driven handler methods
041 */
042@Deprecated
043public class ControllerBeanNameHandlerMapping extends AbstractControllerUrlHandlerMapping {
044
045        private String urlPrefix = "";
046
047        private String urlSuffix = "";
048
049
050        /**
051         * Set an optional prefix to prepend to generated URL mappings.
052         * <p>By default this is an empty String. If you want a prefix like
053         * "/myapp/", you can set it for all beans mapped by this mapping.
054         */
055        public void setUrlPrefix(String urlPrefix) {
056                this.urlPrefix = (urlPrefix != null ? urlPrefix : "");
057        }
058
059        /**
060         * Set an optional suffix to append to generated URL mappings.
061         * <p>By default this is an empty String. If you want a suffix like
062         * ".do", you can set it for all beans mapped by this mapping.
063         */
064        public void setUrlSuffix(String urlSuffix) {
065                this.urlSuffix = (urlSuffix != null ? urlSuffix : "");
066        }
067
068
069        @Override
070        protected String[] buildUrlsForHandler(String beanName, Class<?> beanClass) {
071                List<String> urls = new ArrayList<String>();
072                urls.add(generatePathMapping(beanName));
073                String[] aliases = getApplicationContext().getAliases(beanName);
074                for (String alias : aliases) {
075                        urls.add(generatePathMapping(alias));
076                }
077                return StringUtils.toStringArray(urls);
078        }
079
080        /**
081         * Prepends a '/' if required and appends the URL suffix to the name.
082         */
083        protected String generatePathMapping(String beanName) {
084                String name = (beanName.startsWith("/") ? beanName : "/" + beanName);
085                StringBuilder path = new StringBuilder();
086                if (!name.startsWith(this.urlPrefix)) {
087                        path.append(this.urlPrefix);
088                }
089                path.append(name);
090                if (!name.endsWith(this.urlSuffix)) {
091                        path.append(this.urlSuffix);
092                }
093                return path.toString();
094        }
095
096}