Add page that renders a template but is not a Freenet page.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 2 Apr 2011 14:55:01 +0000 (16:55 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 2 Apr 2011 14:55:01 +0000 (16:55 +0200)
src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java [new file with mode: 0644]

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 (file)
index 0000000..06fc9fc
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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);
+       }
+
+}