View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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.support;
18  
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  import org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping;
25  
26  /**
27   * Base class for {@link org.springframework.web.servlet.HandlerMapping} implementations
28   * that derive URL paths according to conventions for specific controller types.
29   *
30   * @author Juergen Hoeller
31   * @since 2.5.3
32   * @see ControllerClassNameHandlerMapping
33   * @see ControllerBeanNameHandlerMapping
34   */
35  public abstract class AbstractControllerUrlHandlerMapping extends AbstractDetectingUrlHandlerMapping  {
36  
37  	private ControllerTypePredicate predicate = new AnnotationControllerTypePredicate();
38  
39  	private Set<String> excludedPackages = Collections.singleton("org.springframework.web.servlet.mvc");
40  
41  	private Set<Class<?>> excludedClasses = Collections.emptySet();
42  
43  
44  	/**
45  	 * Set whether to activate or deactivate detection of annotated controllers.
46  	 */
47  	public void setIncludeAnnotatedControllers(boolean includeAnnotatedControllers) {
48  		this.predicate = (includeAnnotatedControllers ?
49  				new AnnotationControllerTypePredicate() : new ControllerTypePredicate());
50  	}
51  
52  	/**
53  	 * Specify Java packages that should be excluded from this mapping.
54  	 * Any classes in such a package (or any of its subpackages) will be
55  	 * ignored by this HandlerMapping.
56  	 * <p>Default is to exclude the entire "org.springframework.web.servlet.mvc"
57  	 * package, including its subpackages, since none of Spring's out-of-the-box
58  	 * Controller implementations is a reasonable candidate for this mapping strategy.
59  	 * Such controllers are typically handled by a separate HandlerMapping,
60  	 * e.g. a {@link org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping},
61  	 * alongside this ControllerClassNameHandlerMapping for application controllers.
62  	 */
63  	public void setExcludedPackages(String... excludedPackages) {
64  		this.excludedPackages = (excludedPackages != null) ?
65  				new HashSet<String>(Arrays.asList(excludedPackages)) : new HashSet<String>();
66  	}
67  
68  	/**
69  	 * Specify controller classes that should be excluded from this mapping.
70  	 * Any such classes will simply be ignored by this HandlerMapping.
71  	 */
72  	public void setExcludedClasses(Class<?>... excludedClasses) {
73  		this.excludedClasses = (excludedClasses != null) ?
74  				new HashSet<Class<?>>(Arrays.asList(excludedClasses)) : new HashSet<Class<?>>();
75  	}
76  
77  
78  	/**
79  	 * This implementation delegates to {@link #buildUrlsForHandler},
80  	 * provided that {@link #isEligibleForMapping} returns {@code true}.
81  	 */
82  	@Override
83  	protected String[] determineUrlsForHandler(String beanName) {
84  		Class<?> beanClass = getApplicationContext().getType(beanName);
85  		if (isEligibleForMapping(beanName, beanClass)) {
86  			return buildUrlsForHandler(beanName, beanClass);
87  		}
88  		else {
89  			return null;
90  		}
91  	}
92  
93  	/**
94  	 * Determine whether the specified controller is excluded from this mapping.
95  	 * @param beanName the name of the controller bean
96  	 * @param beanClass the concrete class of the controller bean
97  	 * @return whether the specified class is excluded
98  	 * @see #setExcludedPackages
99  	 * @see #setExcludedClasses
100 	 */
101 	protected boolean isEligibleForMapping(String beanName, Class<?> beanClass) {
102 		if (beanClass == null) {
103 			if (logger.isDebugEnabled()) {
104 				logger.debug("Excluding controller bean '" + beanName + "' from class name mapping " +
105 						"because its bean type could not be determined");
106 			}
107 			return false;
108 		}
109 		if (this.excludedClasses.contains(beanClass)) {
110 			if (logger.isDebugEnabled()) {
111 				logger.debug("Excluding controller bean '" + beanName + "' from class name mapping " +
112 						"because its bean class is explicitly excluded: " + beanClass.getName());
113 			}
114 			return false;
115 		}
116 		String beanClassName = beanClass.getName();
117 		for (String packageName : this.excludedPackages) {
118 			if (beanClassName.startsWith(packageName)) {
119 				if (logger.isDebugEnabled()) {
120 					logger.debug("Excluding controller bean '" + beanName + "' from class name mapping " +
121 							"because its bean class is defined in an excluded package: " + beanClass.getName());
122 				}
123 				return false;
124 			}
125 		}
126 		return isControllerType(beanClass);
127 	}
128 
129 	/**
130 	 * Determine whether the given bean class indicates a controller type
131 	 * that is supported by this mapping strategy.
132 	 * @param beanClass the class to introspect
133 	 */
134 	protected boolean isControllerType(Class<?> beanClass) {
135 		return this.predicate.isControllerType(beanClass);
136 	}
137 
138 	/**
139 	 * Determine whether the given bean class indicates a controller type
140 	 * that dispatches to multiple action methods.
141 	 * @param beanClass the class to introspect
142 	 */
143 	protected boolean isMultiActionControllerType(Class<?> beanClass) {
144 		return this.predicate.isMultiActionControllerType(beanClass);
145 	}
146 
147 
148 	/**
149 	 * Abstract template method to be implemented by subclasses.
150 	 * @param beanName the name of the bean
151 	 * @param beanClass the type of the bean
152 	 * @return the URLs determined for the bean
153 	 */
154 	protected abstract String[] buildUrlsForHandler(String beanName, Class<?> beanClass);
155 
156 }