1 25 package org.riotfamily.website.template; 26 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import org.springframework.beans.factory.InitializingBean; 37 import org.springframework.web.servlet.DispatcherServlet; 38 import org.springframework.web.servlet.ModelAndView; 39 import org.springframework.web.servlet.mvc.AbstractController; 40 41 83 public class TemplateController extends AbstractController 84 implements InitializingBean { 85 86 90 protected static final String SLOTS_CONFIGURATION_ATTRIBUTE = 91 DispatcherServlet.class.getName() + "#" + 92 TemplateController.class.getName() + ".SLOTS_CONFIG"; 93 94 protected static final String SLOT_PATH_ATTRIBUTE = 95 DispatcherServlet.class.getName() + "#" + 96 TemplateController.class.getName() + ".SLOT_PATH"; 97 98 protected static final String SLOT_PARAMETER = 99 TemplateController.class.getName() + ".SLOT"; 100 101 private TemplateController parent; 102 103 private String viewName; 104 105 private Map configuration; 106 107 private Map mergedConfiguration; 108 109 private boolean session; 110 111 public TemplateController getParent() { 112 return parent; 113 } 114 115 public void setParent(TemplateController parent) { 116 this.parent = parent; 117 } 118 119 public String getViewName() { 120 return viewName; 121 } 122 123 public void setViewName(String view) { 124 this.viewName = view; 125 } 126 127 public Map getConfiguration() { 128 return configuration; 129 } 130 131 public void setConfiguration(Map configuration) { 132 this.configuration = configuration; 133 } 134 135 public void setSession(boolean session) { 136 this.session = session; 137 } 138 139 143 public final void afterPropertiesSet() throws Exception { 144 inheritView(); 145 inheritConfiguration(); 146 initController(); 147 } 148 149 154 protected void initController() { 155 } 156 157 160 private void inheritView() { 161 if (viewName == null && getParent() != null) { 162 viewName = getParent().getViewName(); 163 } 164 } 165 166 169 protected void inheritConfiguration() { 170 mergedConfiguration = new HashMap (); 171 if (parent != null) { 172 mergedConfiguration.putAll(parent.getMergedConfiguration()); 173 } 174 if (configuration != null) { 175 mergedConfiguration.putAll(configuration); 176 } 177 } 178 179 protected Map getMergedConfiguration() { 180 return this.mergedConfiguration; 181 } 182 183 188 private Map getEffectiveConfiguration(HttpServletRequest request) { 189 Map effectiveConfiguration = new HashMap (getMergedConfiguration()); 190 applyOverrides(effectiveConfiguration, request); 191 return effectiveConfiguration; 192 } 193 194 197 protected void applyOverrides(Map config, HttpServletRequest request) { 198 Map slotsConfiguration = (Map ) request.getAttribute( 199 SLOTS_CONFIGURATION_ATTRIBUTE); 200 201 if (slotsConfiguration != null) { 202 String slot = request.getParameter(SLOT_PARAMETER); 203 if (slot != null) { 204 String prefix = slot + '.'; 205 config.putAll(selectEntries(slotsConfiguration, prefix)); 206 } 207 else { 208 config.putAll(slotsConfiguration); 209 } 210 } 211 } 212 213 217 private static Map selectEntries(Map map, String prefix) { 218 Map result = new HashMap (); 219 int prefixLength = prefix.length(); 220 Iterator i = map.entrySet().iterator(); 221 while (i.hasNext()) { 222 Map.Entry entry = (Map.Entry ) i.next(); 223 String key = (String ) entry.getKey(); 224 if (key.startsWith(prefix)) { 225 result.put(key.substring(prefixLength), entry.getValue()); 226 } 227 } 228 return result; 229 } 230 231 234 protected Map buildUrlMap(Map config) { 235 Map model = new HashMap (); 236 Iterator i = config.entrySet().iterator(); 237 while (i.hasNext()) { 238 Map.Entry entry = (Map.Entry ) i.next(); 239 String slot = (String ) entry.getKey(); 240 if (slot.indexOf('.') == -1) { 241 if (entry.getValue() != null) { 242 model.put(slot, getUrlMapValue(entry.getValue(), slot)); 243 } 244 else { 245 model.remove(slot); 246 } 247 } 248 } 249 return model; 250 } 251 252 255 protected Object getUrlMapValue(Object value, String slot) { 256 if (value instanceof Collection ) { 257 ArrayList urls = new ArrayList (); 258 Iterator it = ((Collection ) value).iterator(); 259 while (it.hasNext()) { 260 String location = (String ) it.next(); 261 urls.add(getSlotUrl(location, slot)); 262 } 263 return urls; 264 } 265 else { 266 return getSlotUrl((String ) value, slot); 267 } 268 } 269 270 275 protected String getSlotUrl(String location, String slot) { 276 StringBuffer url = new StringBuffer (); 277 url.append(location); 278 url.append((url.indexOf("?") != -1) ? '&' : '?'); 279 url.append(SLOT_PARAMETER); 280 url.append('='); 281 url.append(slot); 282 return url.toString(); 283 } 284 285 protected ModelAndView handleRequestInternal(HttpServletRequest request, 286 HttpServletResponse response) throws Exception { 287 288 if (session) { 289 request.getSession(); 290 } 291 Map config = getEffectiveConfiguration(request); 292 request.setAttribute(SLOTS_CONFIGURATION_ATTRIBUTE, config); 293 request.setAttribute(SLOT_PATH_ATTRIBUTE, getSlotPath(request)); 294 return new ModelAndView(getViewName(), buildUrlMap(config)); 295 } 296 297 300 public static String getSlotPath(HttpServletRequest request) { 301 String slotPath = (String ) request.getAttribute(SLOT_PATH_ATTRIBUTE); 302 String slot = request.getParameter(SLOT_PARAMETER); 303 if (slot != null) { 304 if (slotPath != null) { 305 slotPath = slotPath + '.' + slot; 306 } 307 else { 308 slotPath = slot; 309 } 310 } 311 return slotPath; 312 } 313 314 } | Popular Tags |