1 24 package org.riotfamily.website.interceptor.pushup; 25 26 import java.io.IOException ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 31 import org.riotfamily.common.web.util.ServletUtils; 32 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 33 34 50 public class PushUpInterceptor extends HandlerInterceptorAdapter { 51 52 public static final String INCLUDE_URI_PARAM = "__includeUri"; 53 54 private static final String DISPATCHED_ATTRIBUTE = 55 PushUpInterceptor.class.getName() + ".dispatched"; 56 57 private static final String HANDLER_ATTRIBUTE = 58 PushUpInterceptor.class.getName() + ".handler"; 59 60 private static final String RESPONSE_WRAPPER_ATTRIBUTE = 61 PushUpInterceptor.class.getName() + ".responseWrapper"; 62 63 64 public boolean preHandle(HttpServletRequest request, 65 HttpServletResponse response, Object handler) throws Exception { 66 67 Object firstHandler = request.getAttribute(HANDLER_ATTRIBUTE); 68 if (firstHandler == null) { 69 boolean dispatched = request.getAttribute(DISPATCHED_ATTRIBUTE) != null; 70 if (dispatched) { 71 return handleFirstInclude(handler, request); 72 } 73 return handleUnknown(request, response); 74 } 75 return handleSubsequentInclude(handler, firstHandler, request, response); 76 } 77 78 protected boolean handleUnknown(HttpServletRequest request, 79 HttpServletResponse response) throws Exception { 80 81 String includeUri = request.getParameter(INCLUDE_URI_PARAM); 82 if (includeUri != null) { 83 request.setAttribute(DISPATCHED_ATTRIBUTE, Boolean.TRUE); 84 if (ServletUtils.isXmlHttpRequest(request)) { 85 request.getRequestDispatcher(includeUri).forward( 86 request, response); 87 88 return false; 89 } 90 else { 91 DeferredRenderingResponseWrapper responseWrapper = 92 new DeferredRenderingResponseWrapper(response); 93 94 request.setAttribute(RESPONSE_WRAPPER_ATTRIBUTE, responseWrapper); 95 request.getRequestDispatcher(includeUri).forward( 96 request, responseWrapper); 97 98 if (responseWrapper.isRedirectSent()) { 99 return false; 100 } 101 } 102 } 103 return true; 104 } 105 106 protected boolean handleFirstInclude(Object handler, HttpServletRequest request) { 107 request.setAttribute(HANDLER_ATTRIBUTE, handler); 108 return true; 109 } 110 111 protected boolean handleSubsequentInclude(Object handler, 112 Object firstHandler, HttpServletRequest request, 113 HttpServletResponse response) throws IOException { 114 115 if (handler.equals(firstHandler)) { 118 DeferredRenderingResponseWrapper responseWrapper = 120 (DeferredRenderingResponseWrapper) request.getAttribute( 121 RESPONSE_WRAPPER_ATTRIBUTE); 122 123 responseWrapper.renderResponse(response); 124 return false; 125 } 126 return true; 127 } 128 } 129 | Popular Tags |