001/*
002 * Copyright 2002-2019 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 maps 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.method.annotation.RequestMappingHandlerMapping}.
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.
038 *
039 * <p>Supports direct matches (given "/test" -&gt; registered "/test") and "*"
040 * matches (given "/test" -&gt; registered "/t*"). Note that the default is
041 * to map within the current servlet mapping if applicable; see the
042 * {@link #setAlwaysUseFullPath "alwaysUseFullPath"} property for details.
043 * For details on the pattern options, see the
044 * {@link org.springframework.util.AntPathMatcher} javadoc.
045 *
046 * @author Rod Johnson
047 * @author Juergen Hoeller
048 * @see SimpleUrlHandlerMapping
049 */
050public class BeanNameUrlHandlerMapping extends AbstractDetectingUrlHandlerMapping {
051
052        /**
053         * Checks name and aliases of the given bean for URLs, starting with "/".
054         */
055        @Override
056        protected String[] determineUrlsForHandler(String beanName) {
057                List<String> urls = new ArrayList<>();
058                if (beanName.startsWith("/")) {
059                        urls.add(beanName);
060                }
061                String[] aliases = obtainApplicationContext().getAliases(beanName);
062                for (String alias : aliases) {
063                        if (alias.startsWith("/")) {
064                                urls.add(alias);
065                        }
066                }
067                return StringUtils.toStringArray(urls);
068        }
069
070}