1 24 package org.riotfamily.website.template; 25 26 import java.io.IOException ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import javax.servlet.ServletException ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.riotfamily.common.web.util.ServletUtils; 39 import org.riotfamily.website.interceptor.pushup.DeferredRenderingResponseWrapper; 40 import org.springframework.util.Assert; 41 import org.springframework.web.servlet.ModelAndView; 42 43 55 public class PushUpTemplateController extends TemplateController { 56 57 private static final Log log = LogFactory.getLog( 58 PushUpTemplateController.class); 59 60 private static final String RESPONSE_WRAPPERS_ATTRIBUTE = 61 PushUpTemplateController.class.getName() + ".responseWrappers"; 62 63 protected static final String SLOT_TO_RENDER_PARAMETER = "slotToRender"; 64 65 private List pushUpSlots; 66 67 public void setPushUpSlots(List pushUpSlots) { 68 this.pushUpSlots = pushUpSlots; 69 } 70 71 protected ModelAndView handleRequestInternal(HttpServletRequest request, 72 HttpServletResponse response) throws Exception { 73 74 String slotToRender = request.getParameter(SLOT_TO_RENDER_PARAMETER); 75 if (slotToRender != null) { 76 renderCapturedSlot(request, response, slotToRender); 77 return null; 78 } 79 handlePushUps(request, response); 80 return super.handleRequestInternal(request, response); 81 } 82 83 protected void handlePushUps(HttpServletRequest request, 84 HttpServletResponse response) 85 throws ServletException , IOException { 86 87 if (pushUpSlots != null && !pushUpSlots.isEmpty()) { 88 HashMap config = new HashMap (); 90 request.setAttribute(SLOTS_CONFIGURATION_ATTRIBUTE, config); 91 Iterator it = pushUpSlots.iterator(); 92 while (it.hasNext()) { 93 String slot = (String ) it.next(); 94 config.put(slot, getDeferredRenderingUrl(request, slot)); 95 handlePushUp(request, response, slot); 96 } 97 } 98 } 99 100 105 protected String getDeferredRenderingUrl(HttpServletRequest request, 106 String slot) { 107 108 StringBuffer url = new StringBuffer () 109 .append(ServletUtils.getPathWithinApplication(request)) 110 .append('?').append(SLOT_TO_RENDER_PARAMETER).append('=') 111 .append(slot); 112 113 return url.toString(); 114 } 115 116 protected final Map getResponseWrappers(HttpServletRequest request) { 117 Map wrappers = (Map ) request.getAttribute(RESPONSE_WRAPPERS_ATTRIBUTE); 118 if (wrappers == null) { 119 wrappers = new HashMap (); 120 request.setAttribute(RESPONSE_WRAPPERS_ATTRIBUTE, wrappers); 121 } 122 return wrappers; 123 } 124 125 protected void handlePushUp(HttpServletRequest request, 126 HttpServletResponse response, String slot) 127 throws ServletException , IOException { 128 129 DeferredRenderingResponseWrapper responseWrapper = 130 new DeferredRenderingResponseWrapper(response); 131 132 getResponseWrappers(request).put(slot, responseWrapper); 133 request.setAttribute(SLOT_PATH_ATTRIBUTE, slot); 134 135 String url = (String ) getMergedConfiguration().get(slot); 136 137 log.debug("Capturing pushed-up slot " + slot + " [" + url + "]"); 138 request.getRequestDispatcher(url).forward( 139 request, responseWrapper); 140 } 141 142 145 protected void renderCapturedSlot(HttpServletRequest request, 146 HttpServletResponse response, String slot) throws IOException { 147 148 log.debug("Rendering captured slot: " + slot); 149 DeferredRenderingResponseWrapper responseWrapper = 150 (DeferredRenderingResponseWrapper) 151 getResponseWrappers(request).get(slot); 152 153 Assert.notNull(responseWrapper, 154 "No wrapped response found for slot: " + slot); 155 156 responseWrapper.renderResponse(response); 157 } 158 } 159 | Popular Tags |