From edf7c9ba2868a35ef878ba9535ea0d74036c9a02 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 2 Apr 2011 16:55:01 +0200 Subject: [PATCH] Add page that renders a template but is not a Freenet page. --- .../pterodactylus/sone/web/page/TemplatePage.java | 106 +++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java diff --git a/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java b/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java new file mode 100644 index 0000000..06fc9fc --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java @@ -0,0 +1,106 @@ +/* + * Sone - StaticTemplatePage.java - Copyright © 2011 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.web.page; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.util.logging.Level; +import java.util.logging.Logger; + +import net.pterodactylus.util.io.Closer; +import net.pterodactylus.util.logging.Logging; +import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; +import net.pterodactylus.util.template.TemplateContextFactory; + +/** + * A template page is a single page that is created from a {@link Template} but + * does not necessarily return HTML. + * + * @author David ‘Bombe’ Roden + */ +public class TemplatePage implements Page { + + /** The logger. */ + private static final Logger logger = Logging.getLogger(TemplatePage.class); + + /** The path of this page. */ + private final String path; + + /** The content type of this page. */ + private final String contentType; + + /** The template context factory. */ + private final TemplateContextFactory templateContextFactory; + + /** The template to render. */ + private final Template template; + + /** + * Creates a new template page. + * + * @param path + * The path of the page + * @param contentType + * The content type of the page + * @param templateContextFactory + * The template context factory + * @param template + * The template to render + */ + public TemplatePage(String path, String contentType, TemplateContextFactory templateContextFactory, Template template) { + this.path = path; + this.contentType = contentType; + this.templateContextFactory = templateContextFactory; + this.template = template; + } + + /** + * {@inheritDoc} + */ + @Override + public String getPath() { + return path; + } + + /** + * {@inheritDoc} + */ + @Override + public Response handleRequest(Request request) { + ByteArrayOutputStream responseOutputStream = new ByteArrayOutputStream(); + OutputStreamWriter responseWriter = null; + try { + responseWriter = new OutputStreamWriter(responseOutputStream, "UTF-8"); + TemplateContext templateContext = templateContextFactory.createTemplateContext(); + templateContext.set("request", request); + template.render(templateContext, responseWriter); + } catch (IOException ioe1) { + logger.log(Level.WARNING, "Could not render template for path “" + path + "”!", ioe1); + } finally { + Closer.close(responseWriter); + Closer.close(responseOutputStream); + } + ByteArrayInputStream responseInputStream = new ByteArrayInputStream(responseOutputStream.toByteArray()); + /* no need to close a ByteArrayInputStream. */ + return new Response(200, "OK", contentType, null, responseInputStream); + } + +} -- 2.7.4