KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > website > generic > GenericController


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.website.generic;
25
26
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
39  * Generic controller implementation that utilizes a ModelBuilder to create
40  * a model which is passed on to a view with a configurable name. Additionally
41  * ModelPostProcessors can be registered to tweak the model.
42  */

43 public class GenericController extends AbstractCacheableController {
44
45     /** The ModelBuilder */
46     private ModelBuilder modelBuilder;
47
48     /** Set if modelBuilder is cacheable to reduce number of casts */
49     private CacheableModelBuilder cacheableModelBuilder;
50
51     /** ModelPostProcessors */
52     private ModelPostProcessor[] postProcessors;
53
54     /** View name to use */
55     private String JavaDoc viewName;
56
57     private String JavaDoc contentType;
58
59     private boolean addUriToCacheKey;
60
61     /** Controls whether caching is being used or not */
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 JavaDoc viewName) {
87         this.viewName = viewName;
88     }
89
90     public void setContentType(String JavaDoc 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 JavaDoc request,
103             HttpServletResponse JavaDoc response) throws Exception JavaDoc {
104
105         Map JavaDoc 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 JavaDoc request) {
118         return bypassCache || cacheableModelBuilder == null;
119     }
120
121     public long getTimeToLive() {
122         return cacheableModelBuilder.getTimeToLive();
123     }
124
125     public long getLastModified(HttpServletRequest JavaDoc request) {
126         return cacheableModelBuilder.getLastModified(request);
127     }
128
129     protected void appendCacheKey(StringBuffer JavaDoc key,
130             HttpServletRequest JavaDoc request) {
131
132         cacheableModelBuilder.appendCacheKey(key, request);
133         if (addUriToCacheKey) {
134             super.appendCacheKey(key, request);
135         }
136     }
137
138 }
139
Popular Tags