1 24 package org.riotfamily.common.web.util; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 public abstract class OncePerRequestInterceptor extends PathMatchingInterceptor { 30 31 private String counterAttribute = getClass().getName() + ".interceptions"; 32 33 protected final boolean doPreHandle(HttpServletRequest request, 34 HttpServletResponse response, Object handler) throws Exception { 35 36 int interceptions = 0; 37 Integer counter = (Integer ) request.getAttribute(counterAttribute); 38 if (counter != null) { 39 interceptions = counter.intValue(); 40 } 41 interceptions++; 42 request.setAttribute(counterAttribute, new Integer (interceptions)); 43 44 if (interceptions == 1) { 45 return preHandleOnce(request, response, handler); 46 } 47 48 return true; 49 } 50 51 protected boolean preHandleOnce(HttpServletRequest request, 52 HttpServletResponse response, Object handler) throws Exception { 53 54 return true; 55 } 56 57 public final void afterCompletion(HttpServletRequest request, 58 HttpServletResponse response, Object handler, Exception exception) 59 throws Exception { 60 61 Integer counter = (Integer ) request.getAttribute(counterAttribute); 62 if (counter != null) { 63 int interceptions = counter.intValue() - 1; 64 request.setAttribute(counterAttribute, new Integer (interceptions)); 65 if (interceptions == 0) { 66 afterLastCompletion(request, response, handler, exception); 67 } 68 } 69 } 70 71 protected void afterLastCompletion(HttpServletRequest request, 72 HttpServletResponse response, Object handler, Exception exception) 73 throws Exception { 74 } 75 76 } 77 | Popular Tags |