1 24 package org.riotfamily.website.generic; 25 26 27 import java.util.Map ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.riotfamily.cachius.spring.AbstractCacheableController; 33 import org.riotfamily.website.generic.model.CacheableModelBuilder; 34 import org.riotfamily.website.generic.model.ModelBuilder; 35 import org.riotfamily.website.generic.model.ModelPostProcessor; 36 import org.springframework.web.servlet.ModelAndView; 37 38 43 public class GenericController extends AbstractCacheableController { 44 45 46 private ModelBuilder modelBuilder; 47 48 49 private CacheableModelBuilder cacheableModelBuilder; 50 51 52 private ModelPostProcessor[] postProcessors; 53 54 55 private String viewName; 56 57 private String contentType; 58 59 private boolean addUriToCacheKey; 60 61 62 private boolean bypassCache = false; 63 64 public GenericController() { 65 } 66 67 public GenericController(ModelBuilder modelBuilder) { 68 setModelBuilder(modelBuilder); 69 } 70 71 public void setModelBuilder(ModelBuilder modelBuilder) { 72 this.modelBuilder = modelBuilder; 73 if (modelBuilder instanceof CacheableModelBuilder) { 74 cacheableModelBuilder = (CacheableModelBuilder) modelBuilder; 75 } 76 } 77 78 public void setPostProcessors(ModelPostProcessor[] postProcessors) { 79 this.postProcessors = postProcessors; 80 } 81 82 public void setPostProcessor(ModelPostProcessor postProcessor) { 83 this.postProcessors = new ModelPostProcessor[] { postProcessor }; 84 } 85 86 public void setViewName(String viewName) { 87 this.viewName = viewName; 88 } 89 90 public void setContentType(String contentType) { 91 this.contentType = contentType; 92 } 93 94 public void setAddUriToCacheKey(boolean addUriToCacheKey) { 95 this.addUriToCacheKey = addUriToCacheKey; 96 } 97 98 public void setCache(boolean cache) { 99 this.bypassCache = !cache; 100 } 101 102 public ModelAndView handleRequest(HttpServletRequest request, 103 HttpServletResponse response) throws Exception { 104 105 Map model = modelBuilder.buildModel(request); 106 if (postProcessors != null) { 107 for (int i = 0; i < postProcessors.length; i++) { 108 postProcessors[i].postProcess(model, request); 109 } 110 } 111 if (contentType != null) { 112 response.setContentType(contentType); 113 } 114 return new ModelAndView(viewName, model); 115 } 116 117 protected boolean bypassCache(HttpServletRequest request) { 118 return bypassCache || cacheableModelBuilder == null; 119 } 120 121 public long getTimeToLive() { 122 return cacheableModelBuilder.getTimeToLive(); 123 } 124 125 public long getLastModified(HttpServletRequest request) { 126 return cacheableModelBuilder.getLastModified(request); 127 } 128 129 protected void appendCacheKey(StringBuffer key, 130 HttpServletRequest request) { 131 132 cacheableModelBuilder.appendCacheKey(key, request); 133 if (addUriToCacheKey) { 134 super.appendCacheKey(key, request); 135 } 136 } 137 138 } 139 | Popular Tags |