View Javadoc
1   /**
2    * This file Copyright (c) 2011-2018 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.templating.renderers;
35  
36  import info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.context.MgnlContext;
38  import info.magnolia.rendering.context.RenderingContext;
39  import info.magnolia.rendering.engine.RenderingEngine;
40  import info.magnolia.rendering.model.RenderingModel;
41  import info.magnolia.rendering.renderer.AbstractRenderer;
42  import info.magnolia.rendering.template.RenderableDefinition;
43  import info.magnolia.rendering.util.AppendableWriter;
44  
45  import java.io.IOException;
46  import java.util.HashMap;
47  import java.util.Map;
48  
49  import javax.inject.Inject;
50  import javax.jcr.Node;
51  import javax.jcr.RepositoryException;
52  import javax.servlet.http.HttpServletResponse;
53  
54  import org.apache.commons.lang3.StringUtils;
55  
56  /**
57   * Template renderer for plain text.
58   */
59  public class PlainTextTemplateRenderer extends AbstractRenderer {
60  
61      /**
62       * @deprecated since 5.0.2, use {@link #PlainTextTemplateRenderer(RenderingEngine)}
63       */
64      public PlainTextTemplateRenderer() {
65      }
66  
67      @Inject
68      public PlainTextTemplateRenderer(RenderingEngine renderingEngine) {
69          super(renderingEngine);
70      }
71  
72      @Override
73      protected String resolveTemplateScript(Node content, RenderableDefinition definition, RenderingModel<?> model, String actionResult) {
74          return StringUtils.EMPTY;
75      }
76  
77      @Override
78      protected void onRender(Node content, info.magnolia.rendering.template.RenderableDefinition definition, RenderingContext renderingCtx, Map<String, Object> ctx, String templateScript) throws info.magnolia.rendering.engine.RenderException {
79          final HttpServletResponse response = MgnlContext.getWebContext().getResponse();
80  
81          final boolean isAdmin = ServerConfiguration.getInstance().isAdmin();
82          final boolean isPreview = MgnlContext.getAggregationState().isPreviewMode();
83          try {
84              // TODO: use components instead of rendering bars yourself
85              final String text = content.getProperty("text").getString();
86              final String contentType = content.getProperty("contentType").getString();
87  
88              AppendableWriter out = renderingCtx.getAppendable();
89              if (!isAdmin || isPreview) {
90                  response.setContentType(contentType);
91                  out.write(text);
92              } else {
93                  final String dialogName = (String) definition.getParameters().get("dialog");
94                  response.setContentType("text/html");
95                  // delegate to jsp ?
96                  out.write("<html>\n");
97                  out.write("<body>\n");
98  
99                  out.write("<h2 style=\"padding-top: 30px;\">");
100                 out.write(content.getPath());
101                 out.write(" : ");
102                 out.write(contentType);
103                 out.write("</h2>");
104                 out.write("<pre>\n");
105                 out.write(text);
106                 out.write("</pre>\n");
107                 out.write("</body>\n");
108                 out.write("</html>\n");
109             }
110         } catch (RepositoryException e) {
111             throw new info.magnolia.rendering.engine.RenderException(e);
112         } catch (IOException e) {
113             throw new info.magnolia.rendering.engine.RenderException(e);
114         }
115     }
116 
117     @Override
118     protected Map newContext() {
119         return new HashMap<String, Object>();
120     }
121 
122 }