Add possibility to redirect during processing.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / page / TemplatePage.java
index 7e478e7..d1febb2 100644 (file)
@@ -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;
@@ -32,7 +33,7 @@ import freenet.l10n.BaseL10n;
  * 
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class TemplatePage implements Page {
+public class TemplatePage implements Page, LinkEnabledCallback {
 
        /** The path of the page. */
        private final String path;
@@ -90,7 +91,12 @@ public class TemplatePage implements Page {
                        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());
@@ -116,8 +122,10 @@ public class TemplatePage implements Page {
         *            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. */
        }
 
@@ -133,4 +141,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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       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;
+               }
+
+       }
+
 }