1 16 17 package org.springframework.web.filter; 18 19 import java.io.IOException ; 20 21 import javax.servlet.Filter ; 22 import javax.servlet.FilterChain ; 23 import javax.servlet.ServletException ; 24 import javax.servlet.ServletRequest ; 25 import javax.servlet.ServletResponse ; 26 27 import org.springframework.web.context.WebApplicationContext; 28 import org.springframework.web.context.support.WebApplicationContextUtils; 29 30 65 public class DelegatingFilterProxy extends GenericFilterBean { 66 67 private String targetBeanName; 68 69 private boolean targetFilterLifecycle = false; 70 71 private Filter delegate; 72 73 74 80 public void setTargetBeanName(String targetBeanName) { 81 this.targetBeanName = targetBeanName; 82 } 83 84 87 protected String getTargetBeanName() { 88 return targetBeanName; 89 } 90 91 99 public void setTargetFilterLifecycle(boolean targetFilterLifecycle) { 100 this.targetFilterLifecycle = targetFilterLifecycle; 101 } 102 103 107 protected boolean isTargetFilterLifecycle() { 108 return targetFilterLifecycle; 109 } 110 111 112 protected void initFilterBean() throws ServletException { 113 if (this.targetBeanName == null) { 115 this.targetBeanName = getFilterName(); 116 } 117 WebApplicationContext wac = 121 WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 122 if (wac != null) { 123 this.delegate = initDelegate(wac); 124 } 125 } 126 127 public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) 128 throws ServletException , IOException { 129 130 if (this.delegate == null) { 132 WebApplicationContext wac = 133 WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); 134 this.delegate = initDelegate(wac); 135 } 136 137 this.delegate.doFilter(request, response, filterChain); 139 } 140 141 public void destroy() { 142 if (this.delegate != null) { 143 destroyDelegate(this.delegate); 144 } 145 } 146 147 148 162 protected Filter initDelegate(WebApplicationContext wac) throws ServletException { 163 Filter delegate = (Filter ) wac.getBean(getTargetBeanName(), Filter .class); 164 if (isTargetFilterLifecycle()) { 165 delegate.init(getFilterConfig()); 166 } 167 return delegate; 168 } 169 170 177 protected void destroyDelegate(Filter delegate) { 178 if (isTargetFilterLifecycle()) { 179 this.delegate.destroy(); 180 } 181 } 182 183 } 184 | Popular Tags |