X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpage%2FTemplatePage.java;h=06fc9fcdc07d2af94f68035101ecdf3d3c0a4dfa;hb=be545f41a8fcaf98c63920fd38770ded27e875b3;hp=e823eb57aab49c46a21d20f394d88968b3491401;hpb=d9ce890a0ffc31808d86e735ab833d05727836b2;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java b/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java
index e823eb5..06fc9fc 100644
--- a/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java
+++ b/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java
@@ -1,5 +1,5 @@
/*
- * shortener - TemplatePage.java - Copyright © 2010 David Roden
+ * 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
@@ -17,67 +17,59 @@
package net.pterodactylus.sone.web.page;
-import java.io.StringWriter;
-import java.util.Collection;
-import java.util.Collections;
+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.sone.web.page.Page.Request.Method;
+import net.pterodactylus.util.io.Closer;
import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.template.Template;
-import freenet.clients.http.LinkEnabledCallback;
-import freenet.clients.http.PageMaker;
-import freenet.clients.http.PageNode;
-import freenet.clients.http.ToadletContext;
-import freenet.l10n.BaseL10n;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.template.TemplateContextFactory;
/**
- * Base class for all {@link Page}s that are rendered with {@link Template}s.
+ * 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, LinkEnabledCallback {
+public class TemplatePage implements Page {
/** The logger. */
private static final Logger logger = Logging.getLogger(TemplatePage.class);
- /** The path of the page. */
+ /** The path of this page. */
private final String path;
- /** The template to render. */
- protected final Template template;
-
- /** The L10n handler. */
- private final BaseL10n l10n;
+ /** The content type of this page. */
+ private final String contentType;
- /** The l10n key for the page title. */
- private final String pageTitleKey;
+ /** The template context factory. */
+ private final TemplateContextFactory templateContextFactory;
- /** Where to redirect for invalid form passwords. */
- private final String invalidFormPasswordRedirectTarget;
+ /** 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
- * @param l10n
- * The L10n handler
- * @param pageTitleKey
- * The l10n key of the title page
- * @param invalidFormPasswordRedirectTarget
- * The target to redirect to if a POST request does not contain
- * the correct form password
*/
- public TemplatePage(String path, Template template, BaseL10n l10n, String pageTitleKey, String invalidFormPasswordRedirectTarget) {
+ public TemplatePage(String path, String contentType, TemplateContextFactory templateContextFactory, Template template) {
this.path = path;
+ this.contentType = contentType;
+ this.templateContextFactory = templateContextFactory;
this.template = template;
- this.l10n = l10n;
- this.pageTitleKey = pageTitleKey;
- this.invalidFormPasswordRedirectTarget = invalidFormPasswordRedirectTarget;
}
/**
@@ -93,154 +85,22 @@ public class TemplatePage implements Page, LinkEnabledCallback {
*/
@Override
public Response handleRequest(Request request) {
- String redirectTarget = getRedirectTarget(request);
- if (redirectTarget != null) {
- return new RedirectResponse(redirectTarget);
- }
-
- ToadletContext toadletContext = request.getToadletContext();
- if (request.getMethod() == Method.POST) {
- /* require form password. */
- String formPassword = request.getHttpRequest().getPartAsStringFailsafe("formPassword", 32);
- if (!formPassword.equals(toadletContext.getContainer().getFormPassword())) {
- return new RedirectResponse(invalidFormPasswordRedirectTarget);
- }
- }
- PageMaker pageMaker = toadletContext.getPageMaker();
- PageNode pageNode = pageMaker.getPageNode(l10n.getString(pageTitleKey), toadletContext);
- for (String styleSheet : getStyleSheets()) {
- pageNode.addCustomStyleSheet(styleSheet);
- }
- String shortcutIcon = getShortcutIcon();
- if (shortcutIcon != null) {
- pageNode.addForwardLink("icon", shortcutIcon);
- }
-
+ ByteArrayOutputStream responseOutputStream = new ByteArrayOutputStream();
+ OutputStreamWriter responseWriter = null;
try {
- long start = System.nanoTime();
- processTemplate(request, template);
- long finish = System.nanoTime();
- logger.log(Level.FINEST, "Template was rendered in " + ((finish - start) / 1000) / 1000.0 + "ms.");
- } catch (RedirectException re1) {
- return new RedirectResponse(re1.getTarget());
+ 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);
}
-
- StringWriter stringWriter = new StringWriter();
- template.render(stringWriter);
- pageNode.content.addChild("%", stringWriter.toString());
-
- postProcess(request, template);
-
- return new Response(200, "OK", "text/html", pageNode.outer.generate());
- }
-
- /**
- * Can be overridden to return a custom set of style sheets that are to be
- * included in the pageâs header.
- *
- * @return Additional style sheets to load
- */
- protected Collection getStyleSheets() {
- return Collections.emptySet();
- }
-
- /**
- * Returns the name of the shortcut icon to include in the pageâs header.
- *
- * @return The URL of the shortcut icon, or {@code null} for no icon
- */
- protected String getShortcutIcon() {
- return null;
- }
-
- /**
- * Can be overridden when extending classes need to set variables in the
- * template before it is rendered.
- *
- * @param request
- * The request that is rendered
- * @param template
- * The template to set variables in
- * @throws RedirectException
- * if the processing page wants to redirect after processing
- */
- protected void processTemplate(Request request, Template template) throws RedirectException {
- /* do nothing. */
- }
-
- /**
- * This method will be called after
- * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, Template)}
- * has processed the template and the template was rendered. This method
- * will not be called if
- * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, Template)}
- * throws a {@link RedirectException}!
- *
- * @param request
- * The request being processed
- * @param template
- * The template that was rendered
- */
- protected void postProcess(Request request, Template template) {
- /* do nothing. */
- }
-
- /**
- * Can be overridden to redirect the user to a different page, in case a log
- * in is required, or something else is wrong.
- *
- * @param request
- * The request that is processed
- * @return The URL to redirect to, or {@code null} to not redirect
- */
- protected String getRedirectTarget(Page.Request request) {
- return null;
- }
-
- //
- // INTERFACE LinkEnabledCallback
- //
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean isEnabled(ToadletContext toadletContext) {
- return true;
- }
-
- /**
- * Exception that can be thrown to signal that a subclassed {@link Page}
- * wants to redirect the user during the
- * {@link TemplatePage#processTemplate(net.pterodactylus.sone.web.page.Page.Request, Template)}
- * method call.
- *
- * @author David âBombeâ Roden
- */
- public class RedirectException extends Exception {
-
- /** The target to redirect to. */
- private final String target;
-
- /**
- * Creates a new redirect exception.
- *
- * @param target
- * The target of the redirect
- */
- public RedirectException(String target) {
- this.target = target;
- }
-
- /**
- * Returns the target to redirect to.
- *
- * @return The target to redirect to
- */
- public String getTarget() {
- return target;
- }
-
+ ByteArrayInputStream responseInputStream = new ByteArrayInputStream(responseOutputStream.toByteArray());
+ /* no need to close a ByteArrayInputStream. */
+ return new Response(200, "OK", contentType, null, responseInputStream);
}
}