org.springframework.web.filter
public class: DelegatingFilterProxy [javadoc |
source]
java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.DelegatingFilterProxy
All Implemented Interfaces:
Filter, DisposableBean, BeanNameAware, InitializingBean, ServletContextAware
Proxy for a standard Servlet 2.3 Filter, delegating to a Spring-managed
bean that implements the Filter interface. Supports a "targetBeanName"
filter init-param in
web.xml, specifying the name of the
target bean in the Spring application context.
web.xml will usually contain a DelegatingFilterProxy definition,
with the specified filter-name corresponding to a bean name in
Spring's root application context. All calls to the filter proxy will then
be delegated to that bean in the Spring context, which is required to implement
the standard Servlet 2.3 Filter interface.
This approach is particularly useful for Filter implementation with complex
setup needs, allowing to apply the full Spring bean definition machinery to
Filter instances. Alternatively, consider standard Filter setup in combination
with looking up service beans from the Spring root application context.
NOTE: The lifecycle methods defined by the Servlet Filter interface
will by default not be delegated to the target bean, relying on the
Spring application context to manage the lifecycle of that bean. Specifying
the "targetFilterLifecycle" filter init-param as "true" will enforce invocation
of the Filter.init and Filter.destroy lifecycle methods
on the target bean, letting the servlet container manage the filter lifecycle.
This class is inspired by Acegi Security's FilterToBeanProxy class,
written by Ben Alex.
| Method from org.springframework.web.filter.DelegatingFilterProxy Summary: |
|---|
|
destroy, destroyDelegate, doFilter, findWebApplicationContext, getContextAttribute, getTargetBeanName, initDelegate, initFilterBean, invokeDelegate, isTargetFilterLifecycle, setContextAttribute, setTargetBeanName, setTargetFilterLifecycle |
| Methods from org.springframework.web.filter.GenericFilterBean: |
|---|
|
addRequiredProperty, afterPropertiesSet, destroy, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setServletContext |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from org.springframework.web.filter.DelegatingFilterProxy Detail: |
public void destroy() {
Filter delegateToUse = null;
synchronized (this.delegateMonitor) {
delegateToUse = this.delegate;
}
if (delegateToUse != null) {
destroyDelegate(delegateToUse);
}
}
|
protected void destroyDelegate(Filter delegate) {
if (isTargetFilterLifecycle()) {
delegate.destroy();
}
}
Destroy the Filter delegate.
Default implementation simply calls Filter.destroy on it. |
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
// Lazily initialize the delegate if necessary.
Filter delegateToUse = null;
synchronized (this.delegateMonitor) {
if (this.delegate == null) {
WebApplicationContext wac = findWebApplicationContext();
if (wac == null) {
throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
}
this.delegate = initDelegate(wac);
}
delegateToUse = this.delegate;
}
// Let the delegate perform the actual doFilter operation.
invokeDelegate(delegateToUse, request, response, filterChain);
}
|
protected WebApplicationContext findWebApplicationContext() {
String attrName = getContextAttribute();
if (attrName != null) {
return WebApplicationContextUtils.getWebApplicationContext(getServletContext(), attrName);
}
else {
return WebApplicationContextUtils.getWebApplicationContext(getServletContext());
}
}
Retrieve a WebApplicationContext from the ServletContext
attribute with the configured name . The
WebApplicationContext must have already been loaded and stored in the
ServletContext before this filter gets initialized (or invoked).
Subclasses may override this method to provide a different
WebApplicationContext retrieval strategy. |
public String getContextAttribute() {
return this.contextAttribute;
}
Return the name of the ServletContext attribute which should be used to retrieve the
WebApplicationContext from which to load the delegate Filter bean. |
protected String getTargetBeanName() {
return this.targetBeanName;
}
Return the name of the target bean in the Spring application context. |
protected Filter initDelegate(WebApplicationContext wac) throws ServletException {
Filter delegate = (Filter) wac.getBean(getTargetBeanName(), Filter.class);
if (isTargetFilterLifecycle()) {
delegate.init(getFilterConfig());
}
return delegate;
}
Initialize the Filter delegate, defined as bean the given Spring
application context.
Default implementation fetches the bean from the application context
and calls the standard Filter.init method on it, passing
in the FilterConfig of this Filter proxy. |
protected void initFilterBean() throws ServletException {
// If no target bean name specified, use filter name.
if (this.targetBeanName == null) {
this.targetBeanName = getFilterName();
}
// Fetch Spring root application context and initialize the delegate early,
// if possible. If the root application context will be started after this
// filter proxy, we'll have to resort to lazy initialization.
synchronized (this.delegateMonitor) {
WebApplicationContext wac = findWebApplicationContext();
if (wac != null) {
this.delegate = initDelegate(wac);
}
}
}
|
protected void invokeDelegate(Filter delegate,
ServletRequest request,
ServletResponse response,
FilterChain filterChain) throws ServletException, IOException {
delegate.doFilter(request, response, filterChain);
}
Actually invoke the delegate Filter with the given request and response. |
protected boolean isTargetFilterLifecycle() {
return this.targetFilterLifecycle;
}
Return whether to invoke the Filter.init and
Filter.destroy lifecycle methods on the target bean. |
public void setContextAttribute(String contextAttribute) {
this.contextAttribute = contextAttribute;
}
Set the name of the ServletContext attribute which should be used to retrieve the
WebApplicationContext from which to load the delegate Filter bean. |
public void setTargetBeanName(String targetBeanName) {
this.targetBeanName = targetBeanName;
}
Set the name of the target bean in the Spring application context.
The target bean must implement the standard Servlet 2.3 Filter interface.
By default, the filter-name as specified for the
DelegatingFilterProxy in web.xml will be used. |
public void setTargetFilterLifecycle(boolean targetFilterLifecycle) {
this.targetFilterLifecycle = targetFilterLifecycle;
}
Set whether to invoke the Filter.init and
Filter.destroy lifecycle methods on the target bean.
Default is "false"; target beans usually rely on the Spring application
context for managing their lifecycle. Setting this flag to "true" means
that the servlet container will control the lifecycle of the target
Filter, with this proxy delegating the corresponding calls. |