View Javadoc
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 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   * Renders freemarker templates using the {@link SiteAwareFreemarkerRenderer renderer} provided by the site module. This
60   * enables use of the template prototype configured on the site.
61   *
62   * To construct this object use {@link SiteAwareFreemarkerTemplateViewRendererFactoryBean}.
63   *
64   * @see SiteAwareFreemarkerRenderer
65   * @see SiteAwareFreemarkerTemplateViewRendererFactoryBean
66   * @since 3.1.1
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             // Expose RequestContext instance for Spring macros.
121             // See org.springframework.web.servlet.view.AbstractTemplateView#setExposeSpringMacroHelpers
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             // Expose the model objects in the given map as request attributes.
132             // Necessary to let JSP tags access them, especially the spring form taglib.
133             // See org.springframework.web.servlet.view.AbstractView#exposeModelAsRequestAttributes
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 }