1 /*
2  * Copyright 2002-2011 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.config.annotation;
18 
19 import java.util.HashMap;
20 import java.util.Map;
21 
22 import javax.servlet.ServletContext;
23 
24 import org.springframework.util.Assert;
25 import org.springframework.web.HttpRequestHandler;
26 import org.springframework.web.servlet.DispatcherServlet;
27 import org.springframework.web.servlet.handler.AbstractHandlerMapping;
28 import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
29 import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;
30 
31 /**
32  * Configures a request handler for serving static resources by forwarding the request to the Servlet container's
33  * "default" Servlet. This is indended to be used when the Spring MVC {@link DispatcherServlet} is mapped to "/"
34  * thus overriding the Servlet container's default handling of static resources. Since this handler is configured
35  * at the lowest precedence, effectively it allows all other handler mappings to handle the request, and if none
36  * of them do, this handler can forward it to the "default" Servlet.
37  *
38  * @author Rossen Stoyanchev
39  * @since 3.1
40  *
41  * @see DefaultServletHttpRequestHandler
42  */
43 public class DefaultServletHandlerConfigurer {
44 
45 	private final ServletContext servletContext;
46 
47 	private DefaultServletHttpRequestHandler handler;
48 
49 	/**
50 	 * Create a {@link DefaultServletHandlerConfigurer} instance.
51 	 * @param servletContext the ServletContext to use to configure the underlying DefaultServletHttpRequestHandler.
52 	 */
DefaultServletHandlerConfigurer(ServletContext servletContext)53 	public DefaultServletHandlerConfigurer(ServletContext servletContext) {
54 		Assert.notNull(servletContext, "A ServletContext is required to configure default servlet handling");
55 		this.servletContext = servletContext;
56 	}
57 
58 	/**
59 	 * Enable forwarding to the "default" Servlet. When this method is used the {@link DefaultServletHttpRequestHandler}
60 	 * will try to auto-detect the "default" Servlet name. Alternatively, you can specify the name of the default
61 	 * Servlet via {@link #enable(String)}.
62 	 * @see DefaultServletHttpRequestHandler
63 	 */
enable()64 	public void enable() {
65 		enable(null);
66 	}
67 
68 	/**
69 	 * Enable forwarding to the "default" Servlet identified by the given name.
70 	 * This is useful when the default Servlet cannot be auto-detected, for example when it has been manually configured.
71 	 * @see DefaultServletHttpRequestHandler
72 	 */
enable(String defaultServletName)73 	public void enable(String defaultServletName) {
74 		handler = new DefaultServletHttpRequestHandler();
75 		handler.setDefaultServletName(defaultServletName);
76 		handler.setServletContext(servletContext);
77 	}
78 
79 	/**
80 	 * Return a handler mapping instance ordered at {@link Integer#MAX_VALUE} containing the
81 	 * {@link DefaultServletHttpRequestHandler} instance mapped to {@code "/**"}; or {@code null} if
82 	 * default servlet handling was not been enabled.
83 	 */
getHandlerMapping()84 	protected AbstractHandlerMapping getHandlerMapping() {
85 		if (handler == null) {
86 			return null;
87 		}
88 
89 		Map<String, HttpRequestHandler> urlMap = new HashMap<String, HttpRequestHandler>();
90 		urlMap.put("/**", handler);
91 
92 		SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
93 		handlerMapping.setOrder(Integer.MAX_VALUE);
94 		handlerMapping.setUrlMap(urlMap);
95 		return handlerMapping;
96 	}
97 
98 }