From c339c07590bd70cd03a0871d08455b673188fe7b 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:51:52 +0200 Subject: [PATCH] Rename TemplatePage to FreenetTemplatePage, which fits a lot better. --- .../pterodactylus/sone/web/SoneTemplatePage.java | 4 +- .../sone/web/page/FreenetTemplatePage.java | 255 +++++++++++++++++++++ .../pterodactylus/sone/web/page/TemplatePage.java | 254 -------------------- 3 files changed, 257 insertions(+), 256 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/web/page/FreenetTemplatePage.java delete mode 100644 src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java diff --git a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java index a0f0a79..f969208 100644 --- a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java +++ b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java @@ -25,7 +25,7 @@ import java.util.Collection; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.sone.web.page.Page; -import net.pterodactylus.sone.web.page.TemplatePage; +import net.pterodactylus.sone.web.page.FreenetTemplatePage; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import freenet.clients.http.SessionManager.Session; @@ -37,7 +37,7 @@ import freenet.support.api.HTTPRequest; * * @author David ‘Bombe’ Roden */ -public class SoneTemplatePage extends TemplatePage { +public class SoneTemplatePage extends FreenetTemplatePage { /** The Sone core. */ protected final WebInterface webInterface; diff --git a/src/main/java/net/pterodactylus/sone/web/page/FreenetTemplatePage.java b/src/main/java/net/pterodactylus/sone/web/page/FreenetTemplatePage.java new file mode 100644 index 0000000..97c8bf3 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/web/page/FreenetTemplatePage.java @@ -0,0 +1,255 @@ +/* + * shortener - TemplatePage.java - Copyright © 2010 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.StringWriter; +import java.util.Collection; +import java.util.Collections; +import java.util.logging.Level; +import java.util.logging.Logger; + +import net.pterodactylus.sone.web.page.Page.Request.Method; +import net.pterodactylus.util.logging.Logging; +import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; +import net.pterodactylus.util.template.TemplateContextFactory; +import freenet.clients.http.LinkEnabledCallback; +import freenet.clients.http.PageMaker; +import freenet.clients.http.PageNode; +import freenet.clients.http.ToadletContext; + +/** + * Base class for all {@link Page}s that are rendered with {@link Template}s and + * fit into Freenet’s web interface. + * + * @author David ‘Bombe’ Roden + */ +public class FreenetTemplatePage implements Page, LinkEnabledCallback { + + /** The logger. */ + private static final Logger logger = Logging.getLogger(FreenetTemplatePage.class); + + /** The path of the page. */ + private final String path; + + /** The template context factory. */ + private final TemplateContextFactory templateContextFactory; + + /** The template to render. */ + private final Template template; + + /** Where to redirect for invalid form passwords. */ + private final String invalidFormPasswordRedirectTarget; + + /** + * Creates a new template page. + * + * @param path + * The path of the page + * @param templateContextFactory + * The template context factory + * @param template + * The template to render + * @param invalidFormPasswordRedirectTarget + * The target to redirect to if a POST request does not contain + * the correct form password + */ + public FreenetTemplatePage(String path, TemplateContextFactory templateContextFactory, Template template, String invalidFormPasswordRedirectTarget) { + this.path = path; + this.templateContextFactory = templateContextFactory; + this.template = template; + this.invalidFormPasswordRedirectTarget = invalidFormPasswordRedirectTarget; + } + + /** + * {@inheritDoc} + */ + @Override + public String getPath() { + return path; + } + + /** + * Returns the title of the page. + * + * @param request + * The request to serve + * @return The title of the page + */ + protected String getPageTitle(Request request) { + return null; + } + + /** + * {@inheritDoc} + */ + @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(getPageTitle(request), toadletContext); + for (String styleSheet : getStyleSheets()) { + pageNode.addCustomStyleSheet(styleSheet); + } + String shortcutIcon = getShortcutIcon(); + if (shortcutIcon != null) { + pageNode.addForwardLink("icon", shortcutIcon); + } + + TemplateContext templateContext = templateContextFactory.createTemplateContext(); + templateContext.mergeContext(template.getInitialContext()); + try { + long start = System.nanoTime(); + processTemplate(request, templateContext); + 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()); + } + + StringWriter stringWriter = new StringWriter(); + template.render(templateContext, stringWriter); + pageNode.content.addChild("%", stringWriter.toString()); + + postProcess(request, templateContext); + + 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 templateContext + * The template context to set variables in + * @throws RedirectException + * if the processing page wants to redirect after processing + */ + protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException { + /* do nothing. */ + } + + /** + * This method will be called after + * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)} + * 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, TemplateContext)} + * throws a {@link RedirectException}! + * + * @param request + * The request being processed + * @param templateContext + * The template context that supplied the rendered data + */ + protected void postProcess(Request request, TemplateContext templateContext) { + /* 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 FreenetTemplatePage#processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)} + * method call. + * + * @author David ‘Bombe’ Roden + */ + public static 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; + } + + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java b/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java deleted file mode 100644 index a6636e2..0000000 --- a/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java +++ /dev/null @@ -1,254 +0,0 @@ -/* - * shortener - TemplatePage.java - Copyright © 2010 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.StringWriter; -import java.util.Collection; -import java.util.Collections; -import java.util.logging.Level; -import java.util.logging.Logger; - -import net.pterodactylus.sone.web.page.Page.Request.Method; -import net.pterodactylus.util.logging.Logging; -import net.pterodactylus.util.template.Template; -import net.pterodactylus.util.template.TemplateContext; -import net.pterodactylus.util.template.TemplateContextFactory; -import freenet.clients.http.LinkEnabledCallback; -import freenet.clients.http.PageMaker; -import freenet.clients.http.PageNode; -import freenet.clients.http.ToadletContext; - -/** - * Base class for all {@link Page}s that are rendered with {@link Template}s. - * - * @author David ‘Bombe’ Roden - */ -public class TemplatePage implements Page, LinkEnabledCallback { - - /** The logger. */ - private static final Logger logger = Logging.getLogger(TemplatePage.class); - - /** The path of the page. */ - private final String path; - - /** The template context factory. */ - private final TemplateContextFactory templateContextFactory; - - /** The template to render. */ - private final Template template; - - /** Where to redirect for invalid form passwords. */ - private final String invalidFormPasswordRedirectTarget; - - /** - * Creates a new template page. - * - * @param path - * The path of the page - * @param templateContextFactory - * The template context factory - * @param template - * The template to render - * @param invalidFormPasswordRedirectTarget - * The target to redirect to if a POST request does not contain - * the correct form password - */ - public TemplatePage(String path, TemplateContextFactory templateContextFactory, Template template, String invalidFormPasswordRedirectTarget) { - this.path = path; - this.templateContextFactory = templateContextFactory; - this.template = template; - this.invalidFormPasswordRedirectTarget = invalidFormPasswordRedirectTarget; - } - - /** - * {@inheritDoc} - */ - @Override - public String getPath() { - return path; - } - - /** - * Returns the title of the page. - * - * @param request - * The request to serve - * @return The title of the page - */ - protected String getPageTitle(Request request) { - return null; - } - - /** - * {@inheritDoc} - */ - @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(getPageTitle(request), toadletContext); - for (String styleSheet : getStyleSheets()) { - pageNode.addCustomStyleSheet(styleSheet); - } - String shortcutIcon = getShortcutIcon(); - if (shortcutIcon != null) { - pageNode.addForwardLink("icon", shortcutIcon); - } - - TemplateContext templateContext = templateContextFactory.createTemplateContext(); - templateContext.mergeContext(template.getInitialContext()); - try { - long start = System.nanoTime(); - processTemplate(request, templateContext); - 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()); - } - - StringWriter stringWriter = new StringWriter(); - template.render(templateContext, stringWriter); - pageNode.content.addChild("%", stringWriter.toString()); - - postProcess(request, templateContext); - - 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 templateContext - * The template context to set variables in - * @throws RedirectException - * if the processing page wants to redirect after processing - */ - protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException { - /* do nothing. */ - } - - /** - * This method will be called after - * {@link #processTemplate(net.pterodactylus.sone.web.page.Page.Request, TemplateContext)} - * 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, TemplateContext)} - * throws a {@link RedirectException}! - * - * @param request - * The request being processed - * @param templateContext - * The template context that supplied the rendered data - */ - protected void postProcess(Request request, TemplateContext templateContext) { - /* 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, TemplateContext)} - * method call. - * - * @author David ‘Bombe’ Roden - */ - public static 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; - } - - } - -} -- 2.7.4