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 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      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             // Expose RequestContext instance for Spring macros.
105             // See org.springframework.web.servlet.view.AbstractTemplateView#setExposeSpringMacroHelpers
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             // 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             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 }