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.lang.StringUtils;
55
56
57
58
59 public class PlainTextTemplateRenderer extends AbstractRenderer {
60
61
62
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
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
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 }