X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpage%2FTemplatePage.java;h=761e89b6b3b4162ffe99bf49e16ab085d2ffa68e;hb=01e8cfe1d7cf5ff68428964441d6c77731adb6bd;hp=73a3057fb734df2708dca28a75ae7f52865c7322;hpb=e3cd6bfb5d34bbd5f5f54672793ddf04732f279d;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 73a3057..761e89b 100644 --- a/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java +++ b/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java @@ -21,6 +21,7 @@ import java.io.StringWriter; import java.util.Collection; import java.util.Collections; +import net.pterodactylus.sone.web.page.Page.Request.Method; import net.pterodactylus.util.template.Template; import freenet.clients.http.LinkEnabledCallback; import freenet.clients.http.PageMaker; @@ -30,7 +31,7 @@ import freenet.l10n.BaseL10n; /** * Base class for all {@link Page}s that are rendered with {@link Template}s. - * + * * @author David ‘Bombe’ Roden */ public class TemplatePage implements Page, LinkEnabledCallback { @@ -39,7 +40,7 @@ public class TemplatePage implements Page, LinkEnabledCallback { private final String path; /** The template to render. */ - private final Template template; + protected final Template template; /** The L10n handler. */ private final BaseL10n l10n; @@ -47,9 +48,12 @@ public class TemplatePage implements Page, LinkEnabledCallback { /** The l10n key for the page title. */ private final String pageTitleKey; + /** Where to redirect for invalid form passwords. */ + private final String invalidFormPasswordRedirectTarget; + /** * Creates a new template page. - * + * * @param path * The path of the page * @param template @@ -58,12 +62,16 @@ public class TemplatePage implements Page, LinkEnabledCallback { * 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) { + public TemplatePage(String path, Template template, BaseL10n l10n, String pageTitleKey, String invalidFormPasswordRedirectTarget) { this.path = path; this.template = template; this.l10n = l10n; this.pageTitleKey = pageTitleKey; + this.invalidFormPasswordRedirectTarget = invalidFormPasswordRedirectTarget; } /** @@ -85,13 +93,29 @@ public class TemplatePage implements Page, LinkEnabledCallback { } 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); + } + + try { + processTemplate(request, template); + } catch (RedirectException re1) { + return new RedirectResponse(re1.getTarget()); + } - processTemplate(request, template); StringWriter stringWriter = new StringWriter(); template.render(stringWriter); pageNode.content.addChild("%", stringWriter.toString()); @@ -102,7 +126,7 @@ public class TemplatePage implements Page, LinkEnabledCallback { /** * 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() { @@ -110,22 +134,33 @@ public class TemplatePage implements Page, LinkEnabledCallback { } /** + * 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) { + protected void processTemplate(Request request, Template template) throws RedirectException { /* 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 @@ -146,4 +181,38 @@ public class TemplatePage implements Page, LinkEnabledCallback { 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; + } + + } + }