X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpage%2FTemplatePage.java;h=d1febb2d1c7c2c8e13d88244b99c22e74c4eefb4;hb=a37bbba98b4f281a305aef2f03db8b244eae9dbb;hp=73a3057fb734df2708dca28a75ae7f52865c7322;hpb=f3b816d5e2f61115b7be28dd6e686aaf4173c117;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..d1febb2 100644 --- a/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java +++ b/src/main/java/net/pterodactylus/sone/web/page/TemplatePage.java @@ -91,7 +91,12 @@ public class TemplatePage implements Page, LinkEnabledCallback { pageNode.addCustomStyleSheet(styleSheet); } - processTemplate(request, template); + try { + processTemplate(request, template); + } catch (RedirectException re1) { + return new RedirectResponse(re1.getTarget()); + } + StringWriter stringWriter = new StringWriter(); template.render(stringWriter); pageNode.content.addChild("%", stringWriter.toString()); @@ -117,8 +122,10 @@ public class TemplatePage implements Page, LinkEnabledCallback { * 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. */ } @@ -146,4 +153,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; + } + + } + }