06fc9fcdc07d2af94f68035101ecdf3d3c0a4dfa
[Sone.git] / src / main / java / net / pterodactylus / sone / web / page / TemplatePage.java
1 /*
2  * Sone - StaticTemplatePage.java - Copyright © 2011 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.web.page;
19
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26
27 import net.pterodactylus.util.io.Closer;
28 import net.pterodactylus.util.logging.Logging;
29 import net.pterodactylus.util.template.Template;
30 import net.pterodactylus.util.template.TemplateContext;
31 import net.pterodactylus.util.template.TemplateContextFactory;
32
33 /**
34  * A template page is a single page that is created from a {@link Template} but
35  * does not necessarily return HTML.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class TemplatePage implements Page {
40
41         /** The logger. */
42         private static final Logger logger = Logging.getLogger(TemplatePage.class);
43
44         /** The path of this page. */
45         private final String path;
46
47         /** The content type of this page. */
48         private final String contentType;
49
50         /** The template context factory. */
51         private final TemplateContextFactory templateContextFactory;
52
53         /** The template to render. */
54         private final Template template;
55
56         /**
57          * Creates a new template page.
58          *
59          * @param path
60          *            The path of the page
61          * @param contentType
62          *            The content type of the page
63          * @param templateContextFactory
64          *            The template context factory
65          * @param template
66          *            The template to render
67          */
68         public TemplatePage(String path, String contentType, TemplateContextFactory templateContextFactory, Template template) {
69                 this.path = path;
70                 this.contentType = contentType;
71                 this.templateContextFactory = templateContextFactory;
72                 this.template = template;
73         }
74
75         /**
76          * {@inheritDoc}
77          */
78         @Override
79         public String getPath() {
80                 return path;
81         }
82
83         /**
84          * {@inheritDoc}
85          */
86         @Override
87         public Response handleRequest(Request request) {
88                 ByteArrayOutputStream responseOutputStream = new ByteArrayOutputStream();
89                 OutputStreamWriter responseWriter = null;
90                 try {
91                         responseWriter = new OutputStreamWriter(responseOutputStream, "UTF-8");
92                         TemplateContext templateContext = templateContextFactory.createTemplateContext();
93                         templateContext.set("request", request);
94                         template.render(templateContext, responseWriter);
95                 } catch (IOException ioe1) {
96                         logger.log(Level.WARNING, "Could not render template for path “" + path + "”!", ioe1);
97                 } finally {
98                         Closer.close(responseWriter);
99                         Closer.close(responseOutputStream);
100                 }
101                 ByteArrayInputStream responseInputStream = new ByteArrayInputStream(responseOutputStream.toByteArray());
102                 /* no need to close a ByteArrayInputStream. */
103                 return new Response(200, "OK", contentType, null, responseInputStream);
104         }
105
106 }