KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > web > view > freemarker > RiotFreeMarkerView


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.common.web.view.freemarker;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.riotfamily.common.web.view.MacroHelperFactory;
35 import org.springframework.web.servlet.support.RequestContextUtils;
36 import org.springframework.web.servlet.view.freemarker.FreeMarkerView;
37
38 /**
39  * Differences to Spring's FreeMarkerView:
40  * <ul>
41  * <li>Model attributes may override attributes from the request or the session</li>
42  * <li>The HttpServletRequest is exposed under the key "request"</li>
43  * <li>The model is only exposed to the request if freeMarkerServletMode is enabled</li>
44  * <li>Provides the possibility to expose objects created by a {@link MacroHelperFactory}</li>
45  * </ul>
46  */

47 public class RiotFreeMarkerView extends FreeMarkerView {
48     
49     public static final String JavaDoc REQUEST_ATTRIBUTE = "request";
50     
51     public static final String JavaDoc MODEL_ATTRIBUTE =
52             RiotFreeMarkerView.class.getName() + ".model";
53     
54     private boolean allowModelOverride = true;
55     
56     private boolean freeMarkerServletMode = false;
57     
58     private Map JavaDoc macroHelperFactories;
59     
60     public void setAllowModelOverride(boolean allowModelOverride) {
61         this.allowModelOverride = allowModelOverride;
62     }
63     
64     public void setFreeMarkerServletMode(boolean freeMarkerServletMode) {
65         this.freeMarkerServletMode = freeMarkerServletMode;
66     }
67     
68     public void setMacroHelperFactories(Map JavaDoc macroHelperFactories) {
69         this.macroHelperFactories = macroHelperFactories;
70     }
71
72     public void render(Map JavaDoc model, HttpServletRequest JavaDoc request,
73             HttpServletResponse JavaDoc response) throws Exception JavaDoc {
74     
75         if (allowModelOverride) {
76             Map JavaDoc emptyModel = new HashMap JavaDoc();
77             emptyModel.put(MODEL_ATTRIBUTE, model);
78             model = emptyModel;
79         }
80         super.render(model, request, response);
81     }
82     
83     private void unwrapModel(Map JavaDoc model) {
84         Map JavaDoc originalModel = (Map JavaDoc) model.remove(MODEL_ATTRIBUTE);
85         if (originalModel != null) {
86             model.putAll(originalModel);
87         }
88     }
89     
90     protected void renderMergedTemplateModel(final Map JavaDoc model,
91             final HttpServletRequest JavaDoc request,
92             final HttpServletResponse JavaDoc response)
93             throws Exception JavaDoc {
94
95         unwrapModel(model);
96         model.put(REQUEST_ATTRIBUTE, request);
97         if (macroHelperFactories != null) {
98             Iterator JavaDoc it = macroHelperFactories.entrySet().iterator();
99             while (it.hasNext()) {
100                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
101                 MacroHelperFactory factory = (MacroHelperFactory) entry.getValue();
102                 model.put(entry.getKey(), factory.createMacroHelper(request, response));
103             }
104         }
105         if (freeMarkerServletMode) {
106             super.doRender(model, request, response);
107         }
108         else {
109             if (logger.isDebugEnabled()) {
110                 logger.debug("Rendering FreeMarker template [" + getUrl()
111                         + "] in FreeMarkerView '" + getBeanName() + "'");
112             }
113             Locale JavaDoc locale = RequestContextUtils.getLocale(request);
114             processTemplate(getTemplate(locale), model, response);
115         }
116     }
117
118 }
119
Popular Tags