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      @Deprecated
65      public PlainTextTemplateRenderer() {
66      }
67  
68      @Inject
69      public PlainTextTemplateRenderer(RenderingEngine renderingEngine) {
70          super(renderingEngine);
71      }
72  
73      @Override
74      protected String resolveTemplateScript(Node content, RenderableDefinition definition, RenderingModel<?> model, String actionResult) {
75          return StringUtils.EMPTY;
76      }
77  
78      @Override
79      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 {
80          final HttpServletResponse response = MgnlContext.getWebContext().getResponse();
81  
82          final boolean isAdmin = ServerConfiguration.getInstance().isAdmin();
83          final boolean isPreview = MgnlContext.getAggregationState().isPreviewMode();
84          try {
85              // TODO: use components instead of rendering bars yourself
86              final String text = content.getProperty("text").getString();
87              final String contentType = content.getProperty("contentType").getString();
88  
89              AppendableWriter out = renderingCtx.getAppendable();
90              if (!isAdmin || isPreview) {
91                  response.setContentType(contentType);
92                  out.write(text);
93              } else {
94                  final String dialogName = (String) definition.getParameters().get("dialog");
95                  response.setContentType("text/html");
96                  // delegate to jsp ?
97                  out.write("<html>\n");
98                  out.write("<body>\n");
99  
100                 out.write("<h2 style=\"padding-top: 30px;\">");
101                 out.write(content.getPath());
102                 out.write(" : ");
103                 out.write(contentType);
104                 out.write("</h2>");
105                 out.write("<pre>\n");
106                 out.write(text);
107                 out.write("</pre>\n");
108                 out.write("</body>\n");
109                 out.write("</html>\n");
110             }
111         } catch (RepositoryException e) {
112             throw new info.magnolia.rendering.engine.RenderException(e);
113         } catch (IOException e) {
114             throw new info.magnolia.rendering.engine.RenderException(e);
115         }
116     }
117 
118     @Override
119     protected Map newContext() {
120         return new HashMap<String, Object>();
121     }
122 
123 }