diff gwt-client/src/main/java/org/dive4elements/river/client/server/AuthenticationServlet.java @ 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
children 7b0db743f074
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);
+    }
+}

http://dive4elements.wald.intevation.org