Provide support for filter registrations

The AbstractDispatcherServletInitializer now provides support for
the registration of filters to be mapped to the DispatcherServlet.
It also sets the asyncSupported flag by default on the
DispatcherServlet and all registered filters.

Issue: SPR-9696
This commit is contained in:
Rossen Stoyanchev 2012-08-28 22:29:23 -04:00
parent a49851d5eb
commit cb564b287f
7 changed files with 414 additions and 31 deletions

View File

@ -16,11 +16,19 @@
package org.springframework.web.servlet.support;
import java.util.EnumSet;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.core.Conventions;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.AbstractContextLoaderInitializer;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
@ -44,6 +52,7 @@ import org.springframework.web.servlet.DispatcherServlet;
*
* @author Arjen Poutsma
* @author Chris Beams
* @author Rossen Stoyanchev
* @since 3.2
*/
public abstract class AbstractDispatcherServletInitializer
@ -87,6 +96,14 @@ public abstract class AbstractDispatcherServletInitializer
servletContext.addServlet(servletName, dispatcherServlet);
registration.setLoadOnStartup(1);
registration.addMapping(getServletMappings());
registration.setAsyncSupported(isAsyncSupported());
Filter[] filters = getServletFilters();
if (!ObjectUtils.isEmpty(filters)) {
for (Filter filter : filters) {
registerServletFilter(servletContext, filter);
}
}
this.customizeRegistration(registration);
}
@ -111,12 +128,63 @@ public abstract class AbstractDispatcherServletInitializer
protected abstract WebApplicationContext createServletApplicationContext();
/**
* Specify the servlet mapping(s) for the {@code DispatcherServlet}, e.g. '/', '/app',
* etc.
* Specify the servlet mapping(s) for the {@code DispatcherServlet}, e.g. '/', '/app', etc.
* @see #registerDispatcherServlet(ServletContext)
*/
protected abstract String[] getServletMappings();
/**
* Specify filters to add and also map to the {@code DispatcherServlet}.
*
* @return an array of filters or {@code null}
* @see #registerServletFilters(ServletContext, String, Filter...)
*/
protected Filter[] getServletFilters() {
return null;
}
/**
* Add the given filter to the ServletContext and map it to the
* {@code DispatcherServlet} as follows:
* <ul>
* <li>a default filter name is chosen based on its concrete type
* <li>the {@code asyncSupported} flag is set depending on the
* return value of {@link #isAsyncSupported() asyncSupported}
* <li>a filter mapping is created with dispatcher types {@code REQUEST},
* {@code FORWARD}, {@code INCLUDE}, and conditionally {@code ASYNC} depending
* on the return value of {@link #isAsyncSupported() asyncSupported}
* </ul>
* <p>If the above defaults are not suitable or insufficient, register
* filters directly with the {@code ServletContext}.
*
* @param servletContext the servlet context to register filters with
* @param servletName the name of the servlet to map the filters to
* @param filters the filters to be registered
* @return the filter registration
*/
protected FilterRegistration.Dynamic registerServletFilter(ServletContext servletContext, Filter filter) {
String filterName = Conventions.getVariableName(filter);
Dynamic registration = servletContext.addFilter(filterName, filter);
registration.setAsyncSupported(isAsyncSupported());
registration.addMappingForServletNames(getDispatcherTypes(), false, getServletName());
return registration;
}
private EnumSet<DispatcherType> getDispatcherTypes() {
return isAsyncSupported() ?
EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ASYNC) :
EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE);
}
/**
* A single place to control the {@code asyncSupported} flag for the
* {@code DispatcherServlet} and all filters added via {@link #getServletFilters()}.
* <p>The default value is "true".
*/
protected boolean isAsyncSupported() {
return true;
}
/**
* Optionally perform further registration customization once
* {@link #registerDispatcherServlet(ServletContext)} has completed.

View File

@ -16,26 +16,33 @@
package org.springframework.web.servlet.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration.Dynamic;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.DispatcherServlet;
import static org.junit.Assert.*;
/**
* Test case for {@link AbstractAnnotationConfigDispatcherServletInitializer}.
*
@ -45,6 +52,8 @@ public class AnnotationConfigDispatcherServletInitializerTests {
private static final String SERVLET_NAME = "myservlet";
private static final String FILTER_NAME = "hiddenHttpMethodFilter";
private static final String ROLE_NAME = "role";
private static final String SERVLET_MAPPING = "/myservlet";
@ -55,14 +64,21 @@ public class AnnotationConfigDispatcherServletInitializerTests {
private Map<String, Servlet> servlets;
private Map<String, MockDynamic> registrations;
private Map<String, MockServletRegistration> servletRegistrations;
private Map<String, Filter> filters;
private Map<String, MockFilterRegistration> filterRegistrations;
@Before
public void setUp() throws Exception {
servletContext = new MyMockServletContext();
initializer = new MyAnnotationConfigDispatcherServletInitializer();
servlets = new LinkedHashMap<String, Servlet>(2);
registrations = new LinkedHashMap<String, MockDynamic>(2);
servlets = new LinkedHashMap<String, Servlet>(1);
servletRegistrations = new LinkedHashMap<String, MockServletRegistration>(1);
filters = new LinkedHashMap<String, Filter>(1);
filterRegistrations = new LinkedHashMap<String, MockFilterRegistration>();
}
@Test
@ -73,29 +89,82 @@ public class AnnotationConfigDispatcherServletInitializerTests {
assertNotNull(servlets.get(SERVLET_NAME));
DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
WebApplicationContext servletContext = servlet.getWebApplicationContext();
((AnnotationConfigWebApplicationContext) servletContext).refresh();
WebApplicationContext dispatcherServletContext = servlet.getWebApplicationContext();
((AnnotationConfigWebApplicationContext) dispatcherServletContext).refresh();
assertTrue(servletContext.containsBean("bean"));
assertTrue(servletContext.getBean("bean") instanceof MyBean);
assertTrue(dispatcherServletContext.containsBean("bean"));
assertTrue(dispatcherServletContext.getBean("bean") instanceof MyBean);
assertEquals(1, registrations.size());
assertNotNull(registrations.get(SERVLET_NAME));
assertEquals(1, servletRegistrations.size());
assertNotNull(servletRegistrations.get(SERVLET_NAME));
MockDynamic registration = registrations.get(SERVLET_NAME);
assertEquals(Collections.singleton(SERVLET_MAPPING), registration.getMappings());
assertEquals(1, registration.getLoadOnStartup());
assertEquals(ROLE_NAME, registration.getRunAsRole());
MockServletRegistration servletRegistration = servletRegistrations.get(SERVLET_NAME);
assertEquals(Collections.singleton(SERVLET_MAPPING), servletRegistration.getMappings());
assertEquals(1, servletRegistration.getLoadOnStartup());
assertEquals(ROLE_NAME, servletRegistration.getRunAsRole());
assertTrue(servletRegistration.isAsyncSupported());
assertEquals(1, filterRegistrations.size());
assertNotNull(filterRegistrations.get(FILTER_NAME));
MockFilterRegistration filterRegistration = filterRegistrations.get(FILTER_NAME);
assertTrue(filterRegistration.isAsyncSupported());
assertEquals(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ASYNC),
filterRegistration.getMappings().get(SERVLET_NAME));
}
@Test
public void asyncSupportedFalse() throws ServletException {
initializer = new MyAnnotationConfigDispatcherServletInitializer() {
@Override
protected boolean isAsyncSupported() {
return false;
}
};
initializer.onStartup(servletContext);
MockServletRegistration servletRegistration = servletRegistrations.get(SERVLET_NAME);
assertFalse(servletRegistration.isAsyncSupported());
MockFilterRegistration filterRegistration = filterRegistrations.get(FILTER_NAME);
assertFalse(filterRegistration.isAsyncSupported());
assertEquals(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE),
filterRegistration.getMappings().get(SERVLET_NAME));
}
@Test
public void noFilters() throws ServletException {
initializer = new MyAnnotationConfigDispatcherServletInitializer() {
@Override
protected Filter[] getServletFilters() {
return null;
}
};
initializer.onStartup(servletContext);
assertEquals(0, filterRegistrations.size());
}
private class MyMockServletContext extends MockServletContext {
@Override
public ServletRegistration.Dynamic addServlet(String servletName,
Servlet servlet) {
public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) {
servlets.put(servletName, servlet);
MockDynamic registration = new MockDynamic();
registrations.put(servletName, registration);
MockServletRegistration registration = new MockServletRegistration();
servletRegistrations.put(servletName, registration);
return registration;
}
@Override
public Dynamic addFilter(String filterName, Filter filter) {
filters.put(filterName, filter);
MockFilterRegistration registration = new MockFilterRegistration();
filterRegistrations.put(filterName, registration);
return registration;
}
}
@ -118,6 +187,11 @@ public class AnnotationConfigDispatcherServletInitializerTests {
return new String[]{"/myservlet"};
}
@Override
protected Filter[] getServletFilters() {
return new Filter[] { new HiddenHttpMethodFilter() };
}
@Override
protected void customizeRegistration(ServletRegistration.Dynamic registration) {
registration.setRunAsRole("role");

View File

@ -51,14 +51,14 @@ public class DispatcherServletInitializerTests {
private Map<String, Servlet> servlets;
private Map<String, MockDynamic> registrations;
private Map<String, MockServletRegistration> registrations;
@Before
public void setUp() throws Exception {
servletContext = new MyMockServletContext();
initializer = new MyDispatcherServletInitializer();
servlets = new LinkedHashMap<String, Servlet>(2);
registrations = new LinkedHashMap<String, MockDynamic>(2);
registrations = new LinkedHashMap<String, MockServletRegistration>(2);
}
@Test
@ -77,7 +77,7 @@ public class DispatcherServletInitializerTests {
assertEquals(1, registrations.size());
assertNotNull(registrations.get(SERVLET_NAME));
MockDynamic registration = registrations.get(SERVLET_NAME);
MockServletRegistration registration = registrations.get(SERVLET_NAME);
assertEquals(Collections.singleton(SERVLET_MAPPING), registration.getMappings());
assertEquals(1, registration.getLoadOnStartup());
assertEquals(ROLE_NAME, registration.getRunAsRole());
@ -89,7 +89,7 @@ public class DispatcherServletInitializerTests {
public ServletRegistration.Dynamic addServlet(String servletName,
Servlet servlet) {
servlets.put(servletName, servlet);
MockDynamic registration = new MockDynamic();
MockServletRegistration registration = new MockServletRegistration();
registrations.put(servletName, registration);
return registration;
}

View File

@ -0,0 +1,92 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.support;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration.Dynamic;
class MockFilterRegistration implements Dynamic {
private boolean asyncSupported = false;
private Map<String, EnumSet<DispatcherType>> mappings = new HashMap<String, EnumSet<DispatcherType>>();
public Map<String, EnumSet<DispatcherType>> getMappings() {
return this.mappings;
}
public boolean isAsyncSupported() {
return this.asyncSupported;
}
public void setAsyncSupported(boolean isAsyncSupported) {
this.asyncSupported = isAsyncSupported;
}
public void addMappingForServletNames(EnumSet<DispatcherType> dispatcherTypes,
boolean isMatchAfter, String... servletNames) {
for (String servletName : servletNames) {
this.mappings.put(servletName, dispatcherTypes);
}
}
// Not implemented
public String getName() {
return null;
}
public Collection<String> getServletNameMappings() {
return null;
}
public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes,
boolean isMatchAfter, String... urlPatterns) {
}
public Collection<String> getUrlPatternMappings() {
return null;
}
public String getClassName() {
return null;
}
public boolean setInitParameter(String name, String value) {
return false;
}
public String getInitParameter(String name) {
return null;
}
public Set<String> setInitParameters(Map<String, String> initParameters) {
return null;
}
public Map<String, String> getInitParameters() {
return null;
}
}

View File

@ -26,7 +26,7 @@ import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;
import javax.servlet.ServletSecurityElement;
class MockDynamic implements ServletRegistration.Dynamic {
class MockServletRegistration implements ServletRegistration.Dynamic {
private int loadOnStartup;
@ -34,6 +34,8 @@ class MockDynamic implements ServletRegistration.Dynamic {
private String roleName;
private boolean asyncSupported = false;
public int getLoadOnStartup() {
return loadOnStartup;
}
@ -59,10 +61,16 @@ class MockDynamic implements ServletRegistration.Dynamic {
return roleName;
}
// not implemented
public void setAsyncSupported(boolean isAsyncSupported) {
this.asyncSupported = isAsyncSupported;
}
public boolean isAsyncSupported() {
return this.asyncSupported;
}
// not implemented
public String getName() {
return null;
}

View File

@ -272,7 +272,33 @@
<para>In the preceding example, all requests starting with
<literal>/example</literal> will be handled by the
<classname>DispatcherServlet</classname> instance named
<literal>example</literal>. This is only the first step in setting up
<literal>example</literal>.
In a Servlet 3.0+ environment, you also have the
option of configuring the Servlet container programmatically. Below is the code
based equivalent of the above <filename>web.xml</filename> example:</para>
<programlisting language="java">public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet());
registration.setLoadOnStartup(1);
registration.addMapping("/example/*");
}
}</programlisting>
<para><interfacename>WebApplicationInitializer</interfacename> is an interface
provided by Spring MVC that ensures your code-based configuration is detected and
automatically used to initialize any Servlet 3 container. An abstract base class
implementation of this interace named
<classname>AbstractDispatcherServletInitializer</classname> makes it even easier
to register the <classname>DispatcherServlet</classname> by simply specifying
its servlet mapping. See
<link linkend="mvc-container-config">Code-based Servlet container initialization</link>
for more details.</para>
<para>The above is only the first step in setting up
Spring Web MVC. <!--The discussion below is a little vague about what you're doing, when you do it, and what you're accomplishing. --><!-- Is the next step shown in the next example screen?-->You
now need to configure the various beans used by the Spring Web MVC
framework (over and above the <classname>DispatcherServlet</classname>
@ -4394,6 +4420,108 @@ public class ErrorController {
&lt;/filter-mapping&gt;</programlisting>
</section>
<section id="mvc-container-config">
<title>Code-based Servlet container initialization</title>
<para>In a Servlet 3.0+ environment, you have the option of configuring the
Servlet container programmatically as an alternative or in combination with
a <filename>web.xml</filename> file.
Below is an example of registering a <classname>DispatcherServlet</classname>:</para>
<programlisting language="java">import org.springframework.web.WebApplicationInitializer;
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(appContext));
registration.setLoadOnStartup(1);
registration.addMapping("/");
}
}</programlisting>
<para><interfacename>WebApplicationInitializer</interfacename> is an interface
provided by Spring MVC that ensures your implementation is detected and
automatically used to initialize any Servlet 3 container. An abstract base
class implementation of <interfacename>WebApplicationInitializer</interfacename> named
<classname>AbstractDispatcherServletInitializer</classname> makes it even
easier to register the <classname>DispatcherServlet</classname> by simply overriding
methods to specify the servlet mapping and the location of the
<classname>DispatcherServlet</classname> configuration:</para>
<programlisting language="java">public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class&lt;?&gt;[] getRootConfigClasses() {
return null;
}
@Override
protected Class&lt;?&gt;[] getServletConfigClasses() {
return new Class[] { MyWebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}</programlisting>
<para>The above example is for an application that uses Java-based Spring
configuration. If using XML-based Spring configuration, extend directly from
<classname>AbstractDispatcherServletInitializer</classname>:</para>
<programlisting language="java">public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
@Override
protected WebApplicationContext createServletApplicationContext() {
XmlWebApplicationContext cxt = new XmlWebApplicationContext();
cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
return cxt;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}</programlisting>
<para><classname>AbstractDispatcherServletInitializer</classname> also provides
a convenient way to add <interfacename>Filter</interfacename> instances
and have them automatically mapped to the <classname>DispatcherServlet</classname>:</para>
<programlisting language="java">public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {
// ...
@Override
protected Filter[] getServletFilters() {
return new Filter[] { new HiddenHttpMethodFilter(), new CharacterEncodingFilter() };
}
}</programlisting>
<para>Each filter is added with a default name based on its concrete type and
automatically mapped to the <classname>DispatcherServlet</classname>.</para>
<para>The <code>isAsyncSupported</code> protected method of
<classname>AbstractDispatcherServletInitializer</classname> provides
a single place to enable async support on the <classname>DispatcherServlet</classname>
and all filters mapped to it. By default this flag is set to <code>true</code>.</para>
</section>
<section id="mvc-config">
<title>Configuring Spring MVC</title>

View File

@ -66,7 +66,7 @@
</section>
<section id="new-in-3.2-webmvc-controller-advice">
<title>New <interfacename>@ControllerAdvice</interfacename> annotation</title>
<title><interfacename>@ControllerAdvice</interfacename> annotation</title>
<para>Classes annotated with <interfacename>@ControllerAdvice</interfacename>
can contain <interfacename>@ExceptionHandler</interfacename>,
@ -88,8 +88,21 @@
For more details see <xref linkend="mvc-ann-matrix-variables"/>.</para>
</section>
<section id="new-in-3.2-dispatcher-servlet-initializer">
<title>Abstract base class for code-based Servlet 3+ container initialization</title>
<para>An abstract base class implementation of the
<interfacename>WebApplicationInitializer</interfacename> interface is provided to
simplify code-based registration of a DispatcherServlet and filters
mapped to it. The new class is named
<classname>AbstractDispatcherServletInitializer</classname> and its
sub-class <classname>AbstractAnnotationConfigDispatcherServletInitializer</classname>
can be used with Java-based Spring configuration.
For more details see <xref linkend="mvc-container-config"/>.</para>
</section>
<section id="new-in-3.2-webmvc-exception-handler-support">
<title>New <classname>ResponseEntityExceptionHandler</classname> class</title>
<title><classname>ResponseEntityExceptionHandler</classname> class</title>
<para>A convenient base class with an
<interfacename>@ExceptionHandler</interfacename>