001/*
002 * Copyright 2002-2016 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.handler;
018
019import java.util.ArrayList;
020import java.util.List;
021
022import org.springframework.util.StringUtils;
023
024/**
025 * Implementation of the {@link org.springframework.web.servlet.HandlerMapping}
026 * interface that map from URLs to beans with names that start with a slash ("/"),
027 * similar to how Struts maps URLs to action names.
028 *
029 * <p>This is the default implementation used by the
030 * {@link org.springframework.web.servlet.DispatcherServlet}, along with
031 * {@link org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping}.
032 * Alternatively, {@link SimpleUrlHandlerMapping} allows for customizing a
033 * handler mapping declaratively.
034 *
035 * <p>The mapping is from URL to bean name. Thus an incoming URL "/foo" would map
036 * to a handler named "/foo", or to "/foo /foo2" in case of multiple mappings to
037 * a single handler. Note: In XML definitions, you'll need to use an alias
038 * name="/foo" in the bean definition, as the XML id may not contain slashes.
039 *
040 * <p>Supports direct matches (given "/test" -> registered "/test") and "*"
041 * matches (given "/test" -> registered "/t*"). Note that the default is
042 * to map within the current servlet mapping if applicable; see the
043 * {@link #setAlwaysUseFullPath "alwaysUseFullPath"} property for details.
044 * For details on the pattern options, see the
045 * {@link org.springframework.util.AntPathMatcher} javadoc.
046 *
047 * @author Rod Johnson
048 * @author Juergen Hoeller
049 * @see SimpleUrlHandlerMapping
050 */
051public class BeanNameUrlHandlerMapping extends AbstractDetectingUrlHandlerMapping {
052
053        /**
054         * Checks name and aliases of the given bean for URLs, starting with "/".
055         */
056        @Override
057        protected String[] determineUrlsForHandler(String beanName) {
058                List<String> urls = new ArrayList<String>();
059                if (beanName.startsWith("/")) {
060                        urls.add(beanName);
061                }
062                String[] aliases = getApplicationContext().getAliases(beanName);
063                for (String alias : aliases) {
064                        if (alias.startsWith("/")) {
065                                urls.add(alias);
066                        }
067                }
068                return StringUtils.toStringArray(urls);
069        }
070
071}