changeset 5953:24dc13ac8e6c

Add AuthenticationServlet, a common base class for the login servlets LoginServlet and SamlServlet to reduce code duplication.
author Bernhard Herzog <bh@intevation.de>
date Wed, 08 May 2013 17:57:51 +0200
parents 42b8447fa7ef
children 259ed81d47e9
files gwt-client/src/main/java/org/dive4elements/river/client/server/AuthenticationServlet.java gwt-client/src/main/java/org/dive4elements/river/client/server/LoginServlet.java gwt-client/src/main/java/org/dive4elements/river/client/server/SamlServlet.java
diffstat 3 files changed, 93 insertions(+), 120 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/server/AuthenticationServlet.java	Wed May 08 17:57:51 2013 +0200
@@ -0,0 +1,89 @@
+/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
+ * Software engineering by Intevation GmbH
+ *
+ * This file is Free Software under the GNU AGPL (>=v3) 
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details. 
+ */
+
+package org.dive4elements.river.client.server;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.apache.log4j.Logger;
+
+import org.dive4elements.river.client.server.auth.User;
+import org.dive4elements.river.client.server.auth.UserClient;
+
+/**
+ * Base class for servlets performing authentication and login.
+ */
+public class AuthenticationServlet extends HttpServlet {
+
+    private static Logger logger = Logger.getLogger(AuthenticationServlet.class);
+
+    private static final String FLYS_PAGE = "FLYS.html";
+    private static final String LOGIN_PAGE = "login.jsp";
+
+    protected void redirectFailure(HttpServletResponse resp, String path)
+        throws IOException {
+        resp.sendRedirect(path + "/" + LOGIN_PAGE);
+    }
+
+    protected void redirectFailure(HttpServletResponse resp, String path,
+            Exception e) throws IOException {
+        this.redirectFailure(resp, path, e.getMessage());
+    }
+
+    protected void redirectFailure(HttpServletResponse resp, String path,
+            String message) throws IOException {
+        resp.sendRedirect(path + "/" + LOGIN_PAGE + "?error=" + message);
+    }
+
+    protected void redirectSuccess(HttpServletResponse resp, String path,
+            String uri) throws IOException {
+        if (uri == null) {
+            String redirecturl = getServletContext().getInitParameter("redirect-url");
+            if (redirecturl == null) {
+                redirecturl = FLYS_PAGE;
+            }
+            uri = "/" + redirecturl;
+        }
+        resp.sendRedirect(uri);
+    }
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+    throws ServletException, IOException {
+        logger.debug("Processing get request");
+        this.redirectFailure(resp, req.getContextPath());
+    }
+
+    protected void performLogin(HttpServletRequest req,
+                                HttpServletResponse resp, User user)
+                                    throws ServletException, IOException {
+        String url = getServletContext().getInitParameter("server-url");
+        UserClient client = new UserClient(url);
+        if (!client.userExists(user)) {
+            logger.debug("Creating db user");
+            if (!client.createUser(user)) {
+                this.redirectFailure(resp, req.getContextPath(),
+                                     "Could not create new user");
+                return;
+            }
+        }
+
+        HttpSession session = req.getSession();
+        session.setAttribute("user", user);
+
+        String uri = (String)session.getAttribute("requesturi");
+
+        this.redirectSuccess(resp, req.getContextPath(), uri);
+    }
+}
--- a/gwt-client/src/main/java/org/dive4elements/river/client/server/LoginServlet.java	Wed May 08 17:56:14 2013 +0200
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/server/LoginServlet.java	Wed May 08 17:57:51 2013 +0200
@@ -12,61 +12,20 @@
 
 import javax.servlet.ServletException;
 import javax.servlet.ServletContext;
-import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
 
 import org.apache.log4j.Logger;
 
 import org.dive4elements.river.client.server.auth.Authentication;
 import org.dive4elements.river.client.server.auth.AuthenticationException;
 import org.dive4elements.river.client.server.auth.AuthenticationFactory;
-import org.dive4elements.river.client.server.auth.User;
-import org.dive4elements.river.client.server.auth.UserClient;
 import org.dive4elements.river.client.server.features.Features;
 
-public class LoginServlet extends HttpServlet {
+public class LoginServlet extends AuthenticationServlet {
 
     private static Logger logger = Logger.getLogger(LoginServlet.class);
 
-    private static final String FLYS_PAGE = "FLYS.html";
-    private static final String LOGIN_PAGE = "login.jsp";
-
-    private void redirectFailure(HttpServletResponse resp, String path)
-        throws IOException {
-        resp.sendRedirect(path + "/" + LOGIN_PAGE);
-    }
-
-    private void redirectFailure(HttpServletResponse resp, String path,
-            Exception e) throws IOException {
-        this.redirectFailure(resp, path, e.getMessage());
-    }
-
-    private void redirectFailure(HttpServletResponse resp, String path,
-            String message) throws IOException {
-        resp.sendRedirect(path + "/" + LOGIN_PAGE + "?error=" + message);
-    }
-
-    private void redirectSuccess(HttpServletResponse resp, String path,
-            String uri) throws IOException {
-        if (uri == null) {
-            String redirecturl = getServletContext().getInitParameter("redirect-url");
-            if (redirecturl == null) {
-                redirecturl = FLYS_PAGE;
-            }
-            uri = "/" + redirecturl;
-        }
-        resp.sendRedirect(uri);
-    }
-
-    @Override
-    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
-    throws ServletException, IOException {
-        logger.debug("Processing get request");
-        this.redirectFailure(resp, req.getContextPath());
-    }
-
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException
@@ -90,25 +49,7 @@
                 this.redirectFailure(resp, req.getContextPath());
                 return;
             }
-            User user = aresp.getUser();
-
-            String url = getServletContext().getInitParameter("server-url");
-            UserClient client = new UserClient(url);
-            if (!client.userExists(user)) {
-                logger.debug("Creating db user");
-                if (!client.createUser(user)) {
-                    this.redirectFailure(resp, req.getContextPath(),
-                            "Could not create new user");
-                    return;
-                }
-            }
-
-            HttpSession session = req.getSession();
-            session.setAttribute("user", user);
-
-            String uri = (String)session.getAttribute("requesturi");
-
-            this.redirectSuccess(resp, req.getContextPath(), uri);
+            this.performLogin(req, resp, aresp.getUser());
         }
         catch(AuthenticationException e) {
             logger.error(e, e);
--- a/gwt-client/src/main/java/org/dive4elements/river/client/server/SamlServlet.java	Wed May 08 17:56:14 2013 +0200
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/server/SamlServlet.java	Wed May 08 17:57:51 2013 +0200
@@ -14,10 +14,8 @@
 
 import javax.servlet.ServletException;
 import javax.servlet.ServletContext;
-import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
 
 import org.apache.commons.codec.binary.Base64InputStream;
 
@@ -25,53 +23,15 @@
 
 import org.dive4elements.river.client.server.auth.AuthenticationException;
 import org.dive4elements.river.client.server.auth.User;
-import org.dive4elements.river.client.server.auth.UserClient;
 import org.dive4elements.river.client.server.auth.saml.TicketValidator;
 import org.dive4elements.river.client.server.auth.saml.Assertion;
 import org.dive4elements.river.client.server.features.Features;
 
 
-public class SamlServlet extends HttpServlet {
+public class SamlServlet extends AuthenticationServlet {
 
     private static Logger logger = Logger.getLogger(SamlServlet.class);
 
-    private static final String FLYS_PAGE = "FLYS.html";
-    private static final String LOGIN_PAGE = "login.jsp";
-
-    private void redirectFailure(HttpServletResponse resp, String path)
-        throws IOException {
-        resp.sendRedirect(path + "/" + LOGIN_PAGE);
-    }
-
-    private void redirectFailure(HttpServletResponse resp, String path,
-            Exception e) throws IOException {
-        this.redirectFailure(resp, path, e.getMessage());
-    }
-
-    private void redirectFailure(HttpServletResponse resp, String path,
-            String message) throws IOException {
-        resp.sendRedirect(path + "/" + LOGIN_PAGE + "?error=" + message);
-    }
-
-    private void redirectSuccess(HttpServletResponse resp, String path,
-            String uri) throws IOException {
-        if (uri == null) {
-            String redirecturl = getServletContext().getInitParameter("redirect-url");
-            if (redirecturl == null) {
-                redirecturl = FLYS_PAGE;
-            }
-            uri = "/" + redirecturl;
-        }
-        resp.sendRedirect(uri);
-    }
-
-    @Override
-    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
-    throws ServletException, IOException {
-        logger.debug("Processing get request");
-        this.redirectFailure(resp, req.getContextPath());
-    }
-
     @Override
     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException
@@ -94,24 +54,7 @@
                 this.redirectFailure(resp, req.getContextPath());
                 return;
             }
-
-            String url = getServletContext().getInitParameter("server-url");
-            UserClient client = new UserClient(url);
-            if (!client.userExists(user)) {
-                logger.debug("Creating db user");
-                if (!client.createUser(user)) {
-                    this.redirectFailure(resp, req.getContextPath(),
-                            "Could not create new user");
-                    return;
-                }
-            }
-
-            HttpSession session = req.getSession();
-            session.setAttribute("user", user);
-
-            String uri = (String)session.getAttribute("requesturi");
-
-            this.redirectSuccess(resp, req.getContextPath(), uri);
+            this.performLogin(req, resp, user);
         }
         catch(AuthenticationException e) {
             logger.error(e, e);

http://dive4elements.wald.intevation.org