org.springframework.boot.context.embedded
Class FilterRegistrationBean

java.lang.Object
  extended by org.springframework.boot.context.embedded.RegistrationBean
      extended by org.springframework.boot.context.embedded.FilterRegistrationBean
All Implemented Interfaces:
ServletContextInitializer

public class FilterRegistrationBean
extends RegistrationBean

A ServletContextInitializer to register Filters in a Servlet 3.0+ container. Similar to the registration features provided by ServletContext but with a Spring Bean friendly design.

The Filter must be specified before calling onStartup(ServletContext). Registrations can be associated with URL patterns and/or servlets (either by name or via a ServletRegistrationBeans. When no URL pattern or servlets are specified the filter will be associated to '/*'. The filter name will be deduced if not specified.

Author:
Phillip Webb
See Also:
ServletContextInitializer, ServletContext.addFilter(String, Filter)

Constructor Summary
FilterRegistrationBean()
          Create a new FilterRegistrationBean instance.
FilterRegistrationBean(javax.servlet.Filter filter, ServletRegistrationBean... servletRegistrationBeans)
          Create a new FilterRegistrationBean instance to be registered with the specified ServletRegistrationBeans.
 
Method Summary
 void addServletNames(String... servletNames)
          Add servlet names for the filter.
 void addServletRegistrationBeans(ServletRegistrationBean... servletRegistrationBeans)
          Add ServletRegistrationBeans for the filter.
 void addUrlPatterns(String... urlPatterns)
          Add URL patterns that the filter will be registered against.
protected  void configure(javax.servlet.FilterRegistration.Dynamic registration)
          Configure registration settings.
protected  javax.servlet.Filter getFilter()
          Returns the filter being registered.
 Collection<String> getServletNames()
          Return a mutable collection of servlet names that the filter will be registered against.
 Collection<ServletRegistrationBean> getServletRegistrationBeans()
          Return a mutable collection of the ServletRegistrationBean that the filter will be registered against.
 Collection<String> getUrlPatterns()
          Return a mutable collection of URL patterns that the filter will be registered against.
 boolean isMatchAfter()
          Return if filter mappings should be matched after any declared Filter mappings of the ServletContext.
 void onStartup(javax.servlet.ServletContext servletContext)
          Configure the given ServletContext with any servlets, filters, listeners context-params and attributes necessary for initialization.
 void setFilter(javax.servlet.Filter filter)
          Set the filter to be registered.
 void setMatchAfter(boolean matchAfter)
          Set if the filter mappings should be matched after any declared filter mappings of the ServletContext.
 void setServletNames(Collection<String> servletNames)
          Set servlet names that the filter will be registered against.
 void setServletRegistrationBeans(Collection<? extends ServletRegistrationBean> servletRegistrationBeans)
          Set ServletRegistrationBeans that the filter will be registered against.
 void setUrlPatterns(Collection<String> urlPatterns)
          Set the URL patterns that the filter will be registered against.
 
Methods inherited from class org.springframework.boot.context.embedded.RegistrationBean
addInitParameter, configure, getInitParameters, getOrDeduceName, isAsyncSupported, setAsyncSupported, setInitParameters, setName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FilterRegistrationBean

public FilterRegistrationBean()
Create a new FilterRegistrationBean instance.


FilterRegistrationBean

public FilterRegistrationBean(javax.servlet.Filter filter,
                              ServletRegistrationBean... servletRegistrationBeans)
Create a new FilterRegistrationBean instance to be registered with the specified ServletRegistrationBeans.

Parameters:
filter - the filter to register
servletRegistrationBeans - associate ServletRegistrationBeans
Method Detail

getFilter

protected javax.servlet.Filter getFilter()
Returns the filter being registered.


setFilter

public void setFilter(javax.servlet.Filter filter)
Set the filter to be registered.


setServletRegistrationBeans

public void setServletRegistrationBeans(Collection<? extends ServletRegistrationBean> servletRegistrationBeans)
Set ServletRegistrationBeans that the filter will be registered against.

Parameters:
servletRegistrationBeans - the Servlet registration beans

getServletRegistrationBeans

public Collection<ServletRegistrationBean> getServletRegistrationBeans()
Return a mutable collection of the ServletRegistrationBean that the filter will be registered against. ServletRegistrationBeans.

Returns:
the Servlet registration beans
See Also:
setServletNames(java.util.Collection), setUrlPatterns(java.util.Collection)

addServletRegistrationBeans

public void addServletRegistrationBeans(ServletRegistrationBean... servletRegistrationBeans)
Add ServletRegistrationBeans for the filter.

Parameters:
servletRegistrationBeans - the servlet registration beans to add
See Also:
setServletRegistrationBeans(java.util.Collection)

setServletNames

public void setServletNames(Collection<String> servletNames)
Set servlet names that the filter will be registered against. This will replace any previously specified servlet names.

Parameters:
servletNames - the servlet names
See Also:
setServletRegistrationBeans(java.util.Collection), setUrlPatterns(java.util.Collection)

getServletNames

public Collection<String> getServletNames()
Return a mutable collection of servlet names that the filter will be registered against.

Returns:
the servlet names

addServletNames

public void addServletNames(String... servletNames)
Add servlet names for the filter.

Parameters:
servletNames - the servlet names to add

setUrlPatterns

public void setUrlPatterns(Collection<String> urlPatterns)
Set the URL patterns that the filter will be registered against. This will replace any previously specified URL patterns.

Parameters:
urlPatterns - the URL patterns
See Also:
setServletRegistrationBeans(java.util.Collection), setServletNames(java.util.Collection)

getUrlPatterns

public Collection<String> getUrlPatterns()
Return a mutable collection of URL patterns that the filter will be registered against.

Returns:
the URL patterns

addUrlPatterns

public void addUrlPatterns(String... urlPatterns)
Add URL patterns that the filter will be registered against.

Parameters:
urlPatterns - the URL patterns

setMatchAfter

public void setMatchAfter(boolean matchAfter)
Set if the filter mappings should be matched after any declared filter mappings of the ServletContext. Defaults to false indicating the filters are supposed to be matched before any declared filter mappings of the ServletContext.


isMatchAfter

public boolean isMatchAfter()
Return if filter mappings should be matched after any declared Filter mappings of the ServletContext.


onStartup

public void onStartup(javax.servlet.ServletContext servletContext)
               throws javax.servlet.ServletException
Description copied from interface: ServletContextInitializer
Configure the given ServletContext with any servlets, filters, listeners context-params and attributes necessary for initialization.

Parameters:
servletContext - the ServletContext to initialize
Throws:
javax.servlet.ServletException - if any call against the given ServletContext throws a ServletException

configure

protected void configure(javax.servlet.FilterRegistration.Dynamic registration)
Configure registration settings. Subclasses can override this method to perform additional configuration if required.



Copyright © 2013. All Rights Reserved.