X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpage%2FTemplatePage.java;h=79dd20863d042a9c0a867c9f3617ceed4d3d98ec;hb=21c4819261de874652e7c8be37bde5923758cf4b;hp=7e478e7250ca7fea451e971a0fbef6a18008a195;hpb=b2bc9337f91c303e2700f001c785e8b14a9990ec;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 7e478e7..79dd208 100644 --- a/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java +++ b/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.Collections; 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; @@ -29,16 +30,16 @@ 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 { +public class TemplatePage implements Page, LinkEnabledCallback { /** The path of the page. */ private final String path; /** The template to render. */ - private final Template template; + protected final Template template; /** The L10n handler. */ private final BaseL10n l10n; @@ -48,7 +49,7 @@ public class TemplatePage implements Page { /** * Creates a new template page. - * + * * @param path * The path of the page * @param template @@ -89,8 +90,17 @@ public class TemplatePage implements Page { 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()); @@ -101,7 +111,7 @@ public class TemplatePage implements Page { /** * 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() { @@ -109,22 +119,33 @@ public class TemplatePage implements Page { } /** + * 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 @@ -133,4 +154,50 @@ public class TemplatePage implements Page { 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; + } + + } + }