1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.blossom.view;
35
36 import info.magnolia.context.MgnlContext;
37 import info.magnolia.freemarker.FreemarkerHelper;
38 import info.magnolia.module.blossom.render.RenderContext;
39 import info.magnolia.objectfactory.Components;
40 import info.magnolia.rendering.context.RenderingContext;
41 import info.magnolia.rendering.engine.RenderException;
42 import info.magnolia.rendering.engine.RenderingEngine;
43 import info.magnolia.rendering.model.RenderingModel;
44 import info.magnolia.rendering.renderer.ContextAttributeConfiguration;
45 import info.magnolia.rendering.renderer.FreemarkerRenderer;
46 import info.magnolia.rendering.template.RenderableDefinition;
47
48 import java.util.Map;
49
50 import javax.inject.Inject;
51 import javax.jcr.Node;
52 import javax.servlet.http.HttpServletRequest;
53
54 import org.springframework.web.servlet.support.RequestContext;
55 import org.springframework.web.servlet.view.AbstractTemplateView;
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public class FreemarkerTemplateViewRenderer extends FreemarkerRenderer {
70
71 private boolean exposeModelAsRequestAttributes = true;
72 private boolean exposeSpringMacroHelpers = true;
73
74 @Inject
75 public FreemarkerTemplateViewRenderer(FreemarkerHelper freemarkerHelper, RenderingEngine renderingEngine) {
76 super(freemarkerHelper, renderingEngine);
77 }
78
79
80
81
82 @Deprecated
83 public FreemarkerTemplateViewRenderer() {
84 this(Components.getComponent(FreemarkerHelper.class), Components.getComponent(RenderingEngine.class));
85 }
86
87 public boolean isExposeModelAsRequestAttributes() {
88 return exposeModelAsRequestAttributes;
89 }
90
91 public void setExposeModelAsRequestAttributes(boolean exposeModelAsRequestAttributes) {
92 this.exposeModelAsRequestAttributes = exposeModelAsRequestAttributes;
93 }
94
95 public boolean isExposeSpringMacroHelpers() {
96 return exposeSpringMacroHelpers;
97 }
98
99 public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) {
100 this.exposeSpringMacroHelpers = exposeSpringMacroHelpers;
101 }
102
103 public void addContextAttribute(String name, Class<?> componentClass) {
104 ContextAttributeConfiguration attributeConfiguration = new ContextAttributeConfiguration();
105 attributeConfiguration.setName(name);
106 attributeConfiguration.setComponentClass(componentClass);
107 addContextAttribute(name, attributeConfiguration);
108 }
109
110 @Override
111 protected void setupContext(Map<String, Object> ctx, Node content, RenderableDefinition definition, RenderingModel<?> model, Object actionResult) {
112 super.setupContext(ctx, content, definition, model, actionResult);
113 ctx.putAll(RenderContext.get().getModel());
114 }
115
116 @Override
117 protected String resolveTemplateScript(Node content, RenderableDefinition definition, RenderingModel<?> model, String actionResult) {
118 return RenderContext.get().getTemplateScript();
119 }
120
121 @Override
122 protected void onRender(Node content, RenderableDefinition definition, RenderingContext renderingCtx, Map<String, Object> ctx, String templateScript) throws RenderException {
123
124 if (MgnlContext.isWebContext()) {
125
126 @SuppressWarnings("unchecked")
127 Map<String, Object> model = ctx;
128
129
130
131 if (this.exposeSpringMacroHelpers) {
132 model.put(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE,
133 new RequestContext(
134 MgnlContext.getWebContext().getRequest(),
135 MgnlContext.getWebContext().getResponse(),
136 MgnlContext.getWebContext().getServletContext(),
137 model));
138 }
139
140
141
142
143 if (this.exposeModelAsRequestAttributes) {
144 HttpServletRequest request = MgnlContext.getWebContext().getRequest();
145 for (Map.Entry<String, Object> entry : model.entrySet()) {
146 String modelName = entry.getKey();
147 Object modelValue = entry.getValue();
148 if (modelValue != null) {
149 request.setAttribute(modelName, modelValue);
150 } else {
151 request.removeAttribute(modelName);
152 }
153 }
154 }
155 }
156
157 super.onRender(content, definition, renderingCtx, ctx, templateScript);
158 }
159 }