implement effective policy redirector.
authorFelix Dörre <felix@dogcraft.de>
Mon, 30 Jun 2014 02:28:12 +0000 (04:28 +0200)
committerFelix Dörre <felix@dogcraft.de>
Mon, 30 Jun 2014 02:28:12 +0000 (04:28 +0200)
src/org/cacert/gigi/Gigi.java
src/org/cacert/gigi/Launcher.java
src/org/cacert/gigi/PolicyRedirector.java [new file with mode: 0644]
src/org/cacert/gigi/pages/PolicyRedir.java [deleted file]

index 551c9be051175e25378eb0735788d7c42c534a72..445fe365a2bda70e8408836251823e05653aa201 100644 (file)
@@ -20,7 +20,6 @@ import org.cacert.gigi.email.EmailProvider;
 import org.cacert.gigi.pages.LoginPage;
 import org.cacert.gigi.pages.MainPage;
 import org.cacert.gigi.pages.Page;
-import org.cacert.gigi.pages.PolicyRedir;
 import org.cacert.gigi.pages.TestSecure;
 import org.cacert.gigi.pages.Verify;
 import org.cacert.gigi.pages.account.MailAdd;
@@ -52,7 +51,6 @@ public class Gigi extends HttpServlet {
                pages.put(MailCertificates.PATH, new MailCertificates());
                pages.put(MyDetails.PATH, new MyDetails());
                pages.put(RegisterPage.PATH, new RegisterPage());
-               pages.put(PolicyRedir.PATH, new PolicyRedir());
                pages.put(MailOverview.DEFAULT_PATH, new MailOverview(
                                "My email addresses"));
                pages.put(MailAdd.DEFAULT_PATH, new MailAdd("Add new email"));
index 4753025442d22b0895c2c24e103bc5e6e3c44d69..f8c7f783c23dc84c1690beba1876c79e58518b9d 100644 (file)
@@ -7,7 +7,6 @@ import java.util.Properties;
 import javax.net.ssl.SSLEngine;
 import javax.net.ssl.SSLParameters;
 import javax.net.ssl.TrustManagerFactory;
-
 import org.cacert.gigi.natives.SetUID;
 import org.cacert.gigi.util.CipherInfo;
 import org.eclipse.jetty.server.Connector;
@@ -21,6 +20,7 @@ import org.eclipse.jetty.server.SessionManager;
 import org.eclipse.jetty.server.SslConnectionFactory;
 import org.eclipse.jetty.server.handler.ContextHandler;
 import org.eclipse.jetty.server.handler.HandlerList;
+import org.eclipse.jetty.server.handler.HandlerWrapper;
 import org.eclipse.jetty.server.handler.ResourceHandler;
 import org.eclipse.jetty.servlet.ServletContextHandler;
 import org.eclipse.jetty.servlet.ServletHolder;
@@ -71,12 +71,16 @@ public class Launcher {
                return servlet;
        }
 
-       private static ContextHandler generateStaticContext() {
-               ResourceHandler rh = new ResourceHandler();
+       private static Handler generateStaticContext() {
+               final ResourceHandler rh = new ResourceHandler();
                rh.setResourceBase("static");
+               HandlerWrapper hw = new PolicyRedirector();
+               hw.setHandler(rh);
+
                ContextHandler ch = new ContextHandler();
-               ch.setHandler(rh);
                ch.setContextPath("/static");
+               ch.setHandler(hw);
+
                return ch;
        }
 
diff --git a/src/org/cacert/gigi/PolicyRedirector.java b/src/org/cacert/gigi/PolicyRedirector.java
new file mode 100644 (file)
index 0000000..8714d95
--- /dev/null
@@ -0,0 +1,25 @@
+package org.cacert.gigi;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.eclipse.jetty.server.Request;
+import org.eclipse.jetty.server.handler.HandlerWrapper;
+
+public class PolicyRedirector extends HandlerWrapper {
+       @Override
+       public void handle(String target, Request baseRequest,
+                       HttpServletRequest request, HttpServletResponse response)
+                       throws IOException, ServletException {
+               if (target.startsWith("/policy/") && target.endsWith(".php")) {
+                       target = target.replace(".php", ".html");
+                       response.sendRedirect("/static" + target);
+                       baseRequest.setHandled(true);
+                       return;
+               }
+               super.handle(target, baseRequest, request, response);
+       }
+}
diff --git a/src/org/cacert/gigi/pages/PolicyRedir.java b/src/org/cacert/gigi/pages/PolicyRedir.java
deleted file mode 100644 (file)
index e4e065c..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-package org.cacert.gigi.pages;
-
-import java.io.IOException;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-public class PolicyRedir extends Page {
-       public PolicyRedir() {
-               super("Policy");
-       }
-
-       public static final String PATH = "/policy/*";
-       @Override
-       public boolean beforeTemplate(HttpServletRequest req,
-                       HttpServletResponse resp) throws IOException {
-               String substring = req.getPathInfo().substring(PATH.length() - 1);
-               resp.sendRedirect("/static/policy/"
-                               + substring.replace(".php", ".html"));
-               return true;
-       }
-       @Override
-       public boolean needsLogin() {
-               return false;
-       }
-       @Override
-       public void doGet(HttpServletRequest req, HttpServletResponse resp)
-                       throws IOException {
-
-       }
-
-}