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