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.rendering.renderer;
35
36 import info.magnolia.cms.core.AggregationState;
37 import info.magnolia.context.MgnlContext;
38 import info.magnolia.jcr.decoration.ContentDecoratorUtil;
39 import info.magnolia.jcr.util.ContentMap;
40 import info.magnolia.jcr.util.NodeUtil;
41 import info.magnolia.jcr.wrapper.ChannelVisibilityContentDecorator;
42 import info.magnolia.jcr.wrapper.HTMLEscapingNodeWrapper;
43 import info.magnolia.jcr.wrapper.I18nNodeWrapper;
44 import info.magnolia.objectfactory.Components;
45 import info.magnolia.objectfactory.MgnlInstantiationException;
46 import info.magnolia.objectfactory.ParameterInfo;
47 import info.magnolia.objectfactory.ParameterResolver;
48 import info.magnolia.rendering.context.RenderingContext;
49 import info.magnolia.rendering.engine.RenderException;
50 import info.magnolia.rendering.engine.RenderingEngine;
51 import info.magnolia.rendering.model.EarlyExecutionAware;
52 import info.magnolia.rendering.model.ModelExecutionFilter;
53 import info.magnolia.rendering.model.RenderingModel;
54 import info.magnolia.rendering.model.RenderingModelImpl;
55 import info.magnolia.rendering.template.RenderableDefinition;
56
57 import java.lang.reflect.InvocationTargetException;
58 import java.util.HashMap;
59 import java.util.Map;
60 import java.util.Map.Entry;
61
62 import javax.inject.Inject;
63 import javax.jcr.Node;
64 import javax.jcr.RepositoryException;
65
66 import org.apache.commons.beanutils.BeanUtils;
67 import org.apache.commons.lang3.StringUtils;
68 import org.apache.commons.lang3.exception.ExceptionUtils;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71
72
73
74
75
76
77
78
79
80
81
82
83 public abstract class AbstractRenderer implements Renderer, RenderingModelBasedRenderer {
84
85 private static final Logger log = LoggerFactory.getLogger(AbstractRenderer.class);
86
87 protected static final String MODEL_ATTRIBUTE = RenderingModel.class.getName();
88
89 private Map<String, ContextAttributeConfiguration> contextAttributes = new HashMap<String, ContextAttributeConfiguration>();
90
91 private RenderingEngine renderingEngine = null;
92
93
94
95
96 public AbstractRenderer() {
97 this.renderingEngine = Components.getComponent(RenderingEngine.class);
98 }
99
100 @Inject
101 public AbstractRenderer(RenderingEngine renderingEngine) {
102 this.renderingEngine = renderingEngine;
103 }
104
105 @Override
106 public void render(RenderingContext renderingCtx, Map<String, Object> contextObjects) throws RenderException {
107
108 final RenderingModel<?> parentModel = MgnlContext.getAttribute(MODEL_ATTRIBUTE);
109 Node content = renderingCtx.getCurrentContent();
110 RenderableDefinition definition = renderingCtx.getRenderableDefinition();
111
112 RenderingModel<?> model = null;
113 String actionResult = null;
114
115 if (content != null) {
116 String uuid;
117 try {
118 uuid = content.getIdentifier();
119 } catch (RepositoryException e) {
120 throw new RenderException(e);
121 }
122
123 model = MgnlContext.getAttribute(ModelExecutionFilter.MODEL_ATTRIBUTE_PREFIX + uuid);
124 if (model != null) {
125 actionResult = MgnlContext.getAttribute(ModelExecutionFilter.ACTION_RESULT_ATTRIBUTE_PREFIX + uuid);
126 if (model instanceof EarlyExecutionAware) {
127 ((EarlyExecutionAware) model).setParent(parentModel);
128 }
129 }
130 }
131
132 if (model == null) {
133 model = newModel(content, definition, parentModel);
134 if (model != null) {
135 actionResult = model.execute();
136 if (RenderingModel.SKIP_RENDERING.equals(actionResult)) {
137 return;
138 }
139 }
140 }
141
142 String templatePath = resolveTemplateScript(content, definition, model, actionResult);
143 if (templatePath == null) {
144 throw new RenderException("No template script defined for the template definition [" + definition + "]");
145 }
146
147 final Map<String, Object> ctx = newContext();
148 final Map<String, Object> savedContextState = saveContextState(ctx);
149 setupContext(ctx, content, definition, model, actionResult);
150 ctx.putAll(contextObjects);
151 MgnlContext.setAttribute(MODEL_ATTRIBUTE, model);
152 content = wrapNodeForModel(content);
153 renderingCtx.push(content, definition);
154 try {
155 onRender(content, definition, renderingCtx, ctx, templatePath);
156 } finally {
157 renderingCtx.pop();
158 }
159 MgnlContext.setAttribute(MODEL_ATTRIBUTE, parentModel);
160
161 restoreContext(ctx, savedContextState);
162 }
163
164
165
166
167
168
169
170 protected String resolveTemplateScript(Node content, RenderableDefinition definition, RenderingModel<?> model, final String actionResult) {
171 return definition.getTemplateScript();
172 }
173
174
175
176
177
178 @Override
179 public RenderingModel<?> newModel(final Node content, final RenderableDefinition definition, final RenderingModel<?> parentModel) throws RenderException {
180
181 Class clazz = definition.getModelClass();
182
183
184 if (clazz == null) {
185 clazz = RenderingModelImpl.class;
186 }
187
188 final Node wrappedContent = wrapNodeForModel(content);
189
190 return newModel(clazz, wrappedContent, definition, parentModel);
191 }
192
193 protected <T extends RenderingModel<?>> T newModel(Class<T> modelClass, final Node content, final RenderableDefinition definition, final RenderingModel<?> parentModel) throws RenderException {
194
195 try {
196
197 T model = Components.getComponentProvider().newInstanceWithParameterResolvers(modelClass,
198 new ParameterResolver() {
199 @Override
200 public Object resolveParameter(ParameterInfo parameter) {
201 if (parameter.getParameterType().equals(Node.class)) {
202 return content;
203 }
204 if (parameter.getParameterType().isAssignableFrom(definition.getClass())) {
205 return definition;
206 }
207 if (parameter.getParameterType().equals(RenderingModel.class)) {
208 return parentModel;
209 }
210 return UNRESOLVED;
211 }
212 }
213 );
214
215
216 final boolean autoPopulateFromRequest = definition.getAutoPopulateFromRequest() != null ? definition.getAutoPopulateFromRequest() : renderingEngine.getAutoPopulateFromRequest();
217 if (!autoPopulateFromRequest) {
218 return model;
219 }
220
221 Map<String, String[]> params = MgnlContext.getWebContext().getRequest().getParameterMap();
222 if (params == null || params.isEmpty()) {
223 return model;
224 }
225
226
227
228 Map<String, Object> filtered = new HashMap<String, Object>();
229 for (Entry<String, String[]> entry : params.entrySet()) {
230 String key = entry.getKey();
231 String[] value = entry.getValue();
232 if (StringUtils.contains(key, "[")) {
233 key = StringUtils.substringBefore(key, "[");
234 }
235 filtered.put(key, value);
236 }
237
238 BeanUtils.populate(model, filtered);
239
240 return model;
241
242 } catch (MgnlInstantiationException e) {
243 throw new RenderException("Can't instantiate model: " + modelClass, e);
244 } catch (InvocationTargetException e) {
245 throw new RenderException("Can't populate rendering model: " + ExceptionUtils.getRootCauseMessage(e), e);
246 } catch (IllegalAccessException e) {
247 throw new RenderException("Can't populate rendering model: " + ExceptionUtils.getRootCauseMessage(e), e);
248 }
249 }
250
251 protected Map<String, Object> saveContextState(final Map<String, Object> ctx) {
252 Map<String, Object> state = new HashMap<String, Object>();
253
254 saveAttribute(ctx, state, "content");
255 saveAttribute(ctx, state, "def");
256 saveAttribute(ctx, state, "state");
257 saveAttribute(ctx, state, "model");
258 saveAttribute(ctx, state, "actionResult");
259
260 return state;
261 }
262
263 protected void saveAttribute(final Map<String, Object> ctx, Map<String, Object> state, String name) {
264 state.put(name, ctx.get(name));
265 }
266
267 protected void restoreContext(final Map<String, Object> ctx, Map<String, Object> state) {
268 for (Entry<String, Object> entry : state.entrySet()) {
269 setContextAttribute(ctx, entry.getKey(), entry.getValue());
270 }
271 }
272
273 protected void setupContext(final Map<String, Object> ctx, Node content, RenderableDefinition definition, RenderingModel<?> model, Object actionResult) {
274 setContextAttribute(ctx, "content", content != null ? new ContentMap(wrapNodeForTemplate(content)) : null);
275 setContextAttribute(ctx, "def", definition);
276 setContextAttribute(ctx, "state", getAggregationStateSafely());
277 setContextAttribute(ctx, "model", model);
278 setContextAttribute(ctx, "actionResult", actionResult);
279
280 for (Entry<String, ContextAttributeConfiguration> entry : contextAttributes.entrySet()) {
281 setContextAttribute(ctx, entry.getValue());
282 }
283 }
284
285
286
287
288 protected Node getMainContentSafely(Node content) {
289 AggregationState state = getAggregationStateSafely();
290 return state == null ? content : state.getMainContentNode();
291 }
292
293
294
295
296 protected AggregationState getAggregationStateSafely() {
297 if (MgnlContext.isWebContext()) {
298 return MgnlContext.getAggregationState();
299 }
300 return null;
301 }
302
303
304
305
306
307
308
309 protected Node wrapNodeForModel(Node content) {
310 NodeUtil.deepUnwrap(content, HTMLEscapingNodeWrapper.class);
311 content = wrapWithChannelVisibilityWrapper(content);
312 content = wrapWithI18NWrapper(content);
313 return content;
314 }
315
316
317
318
319
320
321
322 protected Node wrapNodeForTemplate(Node content) {
323 content = wrapWithChannelVisibilityWrapper(content);
324 content = wrapWithI18NWrapper(content);
325 content = wrapWithHTMLEscapingWrapper(content);
326 return content;
327 }
328
329 private Node wrapWithHTMLEscapingWrapper(Node content) {
330 if (!NodeUtil.isWrappedWith(content, HTMLEscapingNodeWrapper.class)) {
331 content = new HTMLEscapingNodeWrapper(content, true);
332 }
333 return content;
334 }
335
336 private Node wrapWithI18NWrapper(Node content) {
337 if (!NodeUtil.isWrappedWith(content, I18nNodeWrapper.class)) {
338 content = new I18nNodeWrapper(content);
339 }
340 return content;
341 }
342
343 private Node wrapWithChannelVisibilityWrapper(Node content) {
344
345 if (ContentDecoratorUtil.isDecoratedWith(content, ChannelVisibilityContentDecorator.class)) {
346 return content;
347 }
348 AggregationState aggregationState = getAggregationStateSafely();
349 if (aggregationState == null) {
350 return content;
351 }
352 String channel = aggregationState.getChannel().getName();
353 if (StringUtils.isEmpty(channel) || channel.equalsIgnoreCase("all")) {
354 return content;
355 }
356 return new ChannelVisibilityContentDecorator(channel).wrapNode(content);
357 }
358
359
360
361
362 private void setContextAttribute(Map<String, Object> ctx, ContextAttributeConfiguration contextAttributeConfiguration) {
363 final String ctxAttributeName = contextAttributeConfiguration.getName();
364 final Class ctxAttributeComponentClass = contextAttributeConfiguration.getComponentClass();
365 if (ctxAttributeName != null && ctxAttributeComponentClass != null) {
366 setContextAttribute(ctx, ctxAttributeName, Components.getComponent(ctxAttributeComponentClass));
367 } else {
368 log.error("Could not add contextAttribute with name '{}' and componentClass '{}'. Please make sure that name and componentClass are not null.", ctxAttributeName, ctxAttributeComponentClass);
369 }
370 }
371
372 protected Object setContextAttribute(final Map<String, Object> ctx, final String name, Object value) {
373 return ctx.put(name, value);
374 }
375
376 public Map<String, ContextAttributeConfiguration> getContextAttributes() {
377 return contextAttributes;
378 }
379
380 public void setContextAttributes(Map<String, ContextAttributeConfiguration> contextAttributes) {
381 if (this.contextAttributes != null) {
382 this.contextAttributes.putAll(contextAttributes);
383 } else {
384 this.contextAttributes = contextAttributes;
385 }
386 }
387
388 public void addContextAttribute(String name, ContextAttributeConfiguration contextAttributeConfiguration) {
389 this.contextAttributes.put(name, contextAttributeConfiguration);
390 }
391
392
393
394
395 protected abstract Map<String, Object> newContext();
396
397
398
399
400 protected abstract void onRender(Node content, RenderableDefinition definition, RenderingContext renderingCtx, Map<String, Object> ctx, String templateScript) throws RenderException;
401
402 }