1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
58
59 public class PlainTextTemplateRenderer extends AbstractRenderer {
60
61
62
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
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
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 }