1 16 17 package org.springframework.web.filter; 18 19 import java.io.IOException ; 20 21 import javax.servlet.FilterChain ; 22 import javax.servlet.ServletException ; 23 import javax.servlet.ServletRequest ; 24 import javax.servlet.ServletResponse ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 28 40 public abstract class OncePerRequestFilter extends GenericFilterBean { 41 42 47 public static final String ALREADY_FILTERED_SUFFIX = ".FILTERED"; 48 49 50 58 public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) 59 throws ServletException , IOException { 60 61 if (!(request instanceof HttpServletRequest ) || !(response instanceof HttpServletResponse )) { 62 throw new ServletException ("OncePerRequestFilter just supports HTTP requests"); 63 } 64 HttpServletRequest httpRequest = (HttpServletRequest ) request; 65 HttpServletResponse httpResponse = (HttpServletResponse ) response; 66 67 String alreadyFilteredAttributeName = getAlreadyFilteredAttributeName(); 68 if (request.getAttribute(alreadyFilteredAttributeName) != null || shouldNotFilter(httpRequest)) { 69 filterChain.doFilter(request, response); 71 } 72 else { 73 request.setAttribute(alreadyFilteredAttributeName, Boolean.TRUE); 75 doFilterInternal(httpRequest, httpResponse, filterChain); 76 } 77 } 78 79 88 protected String getAlreadyFilteredAttributeName() { 89 String name = getFilterName(); 90 if (name == null) { 91 name = getClass().getName(); 92 } 93 return name + ALREADY_FILTERED_SUFFIX; 94 } 95 96 104 protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException { 105 return false; 106 } 107 108 109 115 protected abstract void doFilterInternal( 116 HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 117 throws ServletException , IOException ; 118 119 } 120 | Popular Tags |