1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.view

File FreemarkerTemplateViewRenderer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
65% of files have more coverage

Code metrics

8
20
4
1
134
73
8
0.4
5
4
2
20% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
FreemarkerTemplateViewRenderer 57 20 20% 8 32
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2010-2016 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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    * Renders freemarker templates.
54    *
55    * @since 1.0
56    */
 
57    public class FreemarkerTemplateViewRenderer extends FreemarkerRenderer {
58   
59    private boolean exposeModelAsRequestAttributes = true;
60    private boolean exposeSpringMacroHelpers = true;
61   
 
62    toggle public boolean isExposeModelAsRequestAttributes() {
63    return exposeModelAsRequestAttributes;
64    }
65   
 
66    toggle public void setExposeModelAsRequestAttributes(boolean exposeModelAsRequestAttributes) {
67    this.exposeModelAsRequestAttributes = exposeModelAsRequestAttributes;
68    }
69   
 
70    toggle public boolean isExposeSpringMacroHelpers() {
71    return exposeSpringMacroHelpers;
72    }
73   
 
74    toggle public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) {
75    this.exposeSpringMacroHelpers = exposeSpringMacroHelpers;
76    }
77   
 
78  0 toggle public void addContextAttribute(String name, Class<?> componentClass) {
79  0 ContextAttributeConfiguration attributeConfiguration = new ContextAttributeConfiguration();
80  0 attributeConfiguration.setName(name);
81  0 attributeConfiguration.setComponentClass(componentClass);
82  0 addContextAttribute(name, attributeConfiguration);
83    }
84   
 
85  0 toggle @Override
86    protected void setupContext(Map<String, Object> ctx, Node content, RenderableDefinition definition, RenderingModel<?> model, Object actionResult) {
87  0 super.setupContext(ctx, content, definition, model, actionResult);
88  0 ctx.putAll(RenderContext.get().getModel());
89    }
90   
 
91  0 toggle @Override
92    protected String resolveTemplateScript(Node content, RenderableDefinition definition, RenderingModel<?> model, String actionResult) {
93  0 return RenderContext.get().getTemplateScript();
94    }
95   
 
96  0 toggle @Override
97    protected void onRender(Node content, RenderableDefinition definition, RenderingContext renderingCtx, Map<String, Object> ctx, String templateScript) throws RenderException {
98   
99  0 if (MgnlContext.isWebContext()) {
100   
101  0 @SuppressWarnings("unchecked")
102    Map<String, Object> model = ctx;
103   
104    // Expose RequestContext instance for Spring macros.
105    // See org.springframework.web.servlet.view.AbstractTemplateView#setExposeSpringMacroHelpers
106  0 if (this.exposeSpringMacroHelpers) {
107  0 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    // Expose the model objects in the given map as request attributes.
116    // Necessary to let JSP tags access them, especially the spring form taglib.
117    // See org.springframework.web.servlet.view.AbstractView#exposeModelAsRequestAttributes
118  0 if (this.exposeModelAsRequestAttributes) {
119  0 HttpServletRequest request = MgnlContext.getWebContext().getRequest();
120  0 for (Map.Entry<String, Object> entry : model.entrySet()) {
121  0 String modelName = entry.getKey();
122  0 Object modelValue = entry.getValue();
123  0 if (modelValue != null) {
124  0 request.setAttribute(modelName, modelValue);
125    } else {
126  0 request.removeAttribute(modelName);
127    }
128    }
129    }
130    }
131   
132  0 super.onRender(content, definition, renderingCtx, ctx, templateScript);
133    }
134    }