comparison 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
comparison
equal deleted inserted replaced
5952:42b8447fa7ef 5953:24dc13ac8e6c
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
2 * Software engineering by Intevation GmbH
3 *
4 * This file is Free Software under the GNU AGPL (>=v3)
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
6 * documentation coming with Dive4Elements River for details.
7 */
8
9 package org.dive4elements.river.client.server;
10
11 import java.io.IOException;
12
13 import javax.servlet.ServletException;
14 import javax.servlet.http.HttpServlet;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17 import javax.servlet.http.HttpSession;
18
19 import org.apache.log4j.Logger;
20
21 import org.dive4elements.river.client.server.auth.User;
22 import org.dive4elements.river.client.server.auth.UserClient;
23
24 /**
25 * Base class for servlets performing authentication and login.
26 */
27 public class AuthenticationServlet extends HttpServlet {
28
29 private static Logger logger = Logger.getLogger(AuthenticationServlet.class);
30
31 private static final String FLYS_PAGE = "FLYS.html";
32 private static final String LOGIN_PAGE = "login.jsp";
33
34 protected void redirectFailure(HttpServletResponse resp, String path)
35 throws IOException {
36 resp.sendRedirect(path + "/" + LOGIN_PAGE);
37 }
38
39 protected void redirectFailure(HttpServletResponse resp, String path,
40 Exception e) throws IOException {
41 this.redirectFailure(resp, path, e.getMessage());
42 }
43
44 protected void redirectFailure(HttpServletResponse resp, String path,
45 String message) throws IOException {
46 resp.sendRedirect(path + "/" + LOGIN_PAGE + "?error=" + message);
47 }
48
49 protected void redirectSuccess(HttpServletResponse resp, String path,
50 String uri) throws IOException {
51 if (uri == null) {
52 String redirecturl = getServletContext().getInitParameter("redirect-url");
53 if (redirecturl == null) {
54 redirecturl = FLYS_PAGE;
55 }
56 uri = "/" + redirecturl;
57 }
58 resp.sendRedirect(uri);
59 }
60
61 @Override
62 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
63 throws ServletException, IOException {
64 logger.debug("Processing get request");
65 this.redirectFailure(resp, req.getContextPath());
66 }
67
68 protected void performLogin(HttpServletRequest req,
69 HttpServletResponse resp, User user)
70 throws ServletException, IOException {
71 String url = getServletContext().getInitParameter("server-url");
72 UserClient client = new UserClient(url);
73 if (!client.userExists(user)) {
74 logger.debug("Creating db user");
75 if (!client.createUser(user)) {
76 this.redirectFailure(resp, req.getContextPath(),
77 "Could not create new user");
78 return;
79 }
80 }
81
82 HttpSession session = req.getSession();
83 session.setAttribute("user", user);
84
85 String uri = (String)session.getAttribute("requesturi");
86
87 this.redirectSuccess(resp, req.getContextPath(), uri);
88 }
89 }

http://dive4elements.wald.intevation.org