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 java.util.Map;
37 import javax.jcr.Node;
38 import javax.servlet.http.HttpServletRequest;
39
40 import info.magnolia.context.MgnlContext;
41 import info.magnolia.module.blossom.render.RenderContext;
42 import info.magnolia.rendering.context.RenderingContext;
43 import info.magnolia.rendering.engine.RenderException;
44 import info.magnolia.rendering.model.RenderingModel;
45 import info.magnolia.rendering.renderer.ContextAttributeConfiguration;
46 import info.magnolia.rendering.renderer.FreemarkerRenderer;
47 import info.magnolia.rendering.template.RenderableDefinition;
48
49 import org.springframework.web.servlet.support.RequestContext;
50 import org.springframework.web.servlet.view.AbstractTemplateView;
51
52
53
54
55
56
57 public class FreemarkerTemplateViewRenderer extends FreemarkerRenderer {
58
59 private boolean exposeModelAsRequestAttributes = true;
60 private boolean exposeSpringMacroHelpers = true;
61
62 public boolean isExposeModelAsRequestAttributes() {
63 return exposeModelAsRequestAttributes;
64 }
65
66 public void setExposeModelAsRequestAttributes(boolean exposeModelAsRequestAttributes) {
67 this.exposeModelAsRequestAttributes = exposeModelAsRequestAttributes;
68 }
69
70 public boolean isExposeSpringMacroHelpers() {
71 return exposeSpringMacroHelpers;
72 }
73
74 public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) {
75 this.exposeSpringMacroHelpers = exposeSpringMacroHelpers;
76 }
77
78 public void addContextAttribute(String name, Class<?> componentClass) {
79 ContextAttributeConfiguration attributeConfiguration = new ContextAttributeConfiguration();
80 attributeConfiguration.setName(name);
81 attributeConfiguration.setComponentClass(componentClass);
82 addContextAttribute(name, attributeConfiguration);
83 }
84
85 @Override
86 protected void setupContext(Map<String, Object> ctx, Node content, RenderableDefinition definition, RenderingModel<?> model, Object actionResult) {
87 super.setupContext(ctx, content, definition, model, actionResult);
88 ctx.putAll(RenderContext.get().getModel());
89 }
90
91 @Override
92 protected String resolveTemplateScript(Node content, RenderableDefinition definition, RenderingModel<?> model, String actionResult) {
93 return RenderContext.get().getTemplateScript();
94 }
95
96 @Override
97 protected void onRender(Node content, RenderableDefinition definition, RenderingContext renderingCtx, Map<String, Object> ctx, String templateScript) throws RenderException {
98
99 if (MgnlContext.isWebContext()) {
100
101 @SuppressWarnings("unchecked")
102 Map<String, Object> model = ctx;
103
104
105
106 if (this.exposeSpringMacroHelpers) {
107 model.put(AbstractTemplateView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE,
108 new RequestContext(
109 MgnlContext.getWebContext().getRequest(),
110 MgnlContext.getWebContext().getResponse(),
111 MgnlContext.getWebContext().getServletContext(),
112 model));
113 }
114
115
116
117
118 if (this.exposeModelAsRequestAttributes) {
119 HttpServletRequest request = MgnlContext.getWebContext().getRequest();
120 for (Map.Entry<String, Object> entry : model.entrySet()) {
121 String modelName = entry.getKey();
122 Object modelValue = entry.getValue();
123 if (modelValue != null) {
124 request.setAttribute(modelName, modelValue);
125 } else {
126 request.removeAttribute(modelName);
127 }
128 }
129 }
130 }
131
132 super.onRender(content, definition, renderingCtx, ctx, templateScript);
133 }
134 }