2 * Sone - StaticTemplatePage.java - Copyright © 2011 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.web.page;
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;
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;
34 * A template page is a single page that is created from a {@link Template} but
35 * does not necessarily return HTML.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class TemplatePage implements Page {
42 private static final Logger logger = Logging.getLogger(TemplatePage.class);
44 /** The path of this page. */
45 private final String path;
47 /** The content type of this page. */
48 private final String contentType;
50 /** The template context factory. */
51 private final TemplateContextFactory templateContextFactory;
53 /** The template to render. */
54 private final Template template;
57 * Creates a new template page.
60 * The path of the page
62 * The content type of the page
63 * @param templateContextFactory
64 * The template context factory
66 * The template to render
68 public TemplatePage(String path, String contentType, TemplateContextFactory templateContextFactory, Template template) {
70 this.contentType = contentType;
71 this.templateContextFactory = templateContextFactory;
72 this.template = template;
79 public String getPath() {
87 public Response handleRequest(Request request) {
88 ByteArrayOutputStream responseOutputStream = new ByteArrayOutputStream();
89 OutputStreamWriter responseWriter = null;
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);
98 Closer.close(responseWriter);
99 Closer.close(responseOutputStream);
101 ByteArrayInputStream responseInputStream = new ByteArrayInputStream(responseOutputStream.toByteArray());
102 /* no need to close a ByteArrayInputStream. */
103 return new Response(200, "OK", contentType, null, responseInputStream);