teichmann@5957: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde bh@5953: * Software engineering by Intevation GmbH bh@5953: * teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3) bh@5953: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5993: * documentation coming with Dive4Elements River for details. bh@5953: */ bh@5953: bh@5953: package org.dive4elements.river.client.server; bh@5953: bh@5953: import java.io.IOException; bh@5953: bh@5953: import javax.servlet.ServletException; bh@5953: import javax.servlet.http.HttpServlet; bh@5953: import javax.servlet.http.HttpServletRequest; bh@5953: import javax.servlet.http.HttpServletResponse; bh@5953: import javax.servlet.http.HttpSession; bh@5953: bh@5953: import org.apache.log4j.Logger; bh@5953: bh@5953: import org.dive4elements.river.client.server.auth.User; bh@5953: import org.dive4elements.river.client.server.auth.UserClient; bh@5953: bh@5953: /** bh@5953: * Base class for servlets performing authentication and login. bh@5953: */ bh@5953: public class AuthenticationServlet extends HttpServlet { bh@5953: teichmann@8203: private static Logger log = Logger.getLogger(AuthenticationServlet.class); bh@5953: bh@5953: private static final String FLYS_PAGE = "FLYS.html"; bh@5953: private static final String LOGIN_PAGE = "login.jsp"; bh@5953: bh@5953: protected void redirectFailure(HttpServletResponse resp, String path) bh@5953: throws IOException { bh@5953: resp.sendRedirect(path + "/" + LOGIN_PAGE); bh@5953: } bh@5953: bh@5953: protected void redirectFailure(HttpServletResponse resp, String path, bh@5953: Exception e) throws IOException { bh@5953: this.redirectFailure(resp, path, e.getMessage()); bh@5953: } bh@5953: bh@5953: protected void redirectFailure(HttpServletResponse resp, String path, bh@5953: String message) throws IOException { bh@5953: resp.sendRedirect(path + "/" + LOGIN_PAGE + "?error=" + message); bh@5953: } bh@5953: bh@5953: protected void redirectSuccess(HttpServletResponse resp, String path, bh@5953: String uri) throws IOException { bh@5953: if (uri == null) { bh@5953: String redirecturl = getServletContext().getInitParameter("redirect-url"); bh@5953: if (redirecturl == null) { bh@5953: redirecturl = FLYS_PAGE; bh@5953: } bh@5953: uri = "/" + redirecturl; bh@5953: } bh@5953: resp.sendRedirect(uri); bh@5953: } bh@5953: bh@5953: @Override bh@5953: protected void doGet(HttpServletRequest req, HttpServletResponse resp) bh@5953: throws ServletException, IOException { teichmann@8203: log.debug("Processing get request"); bh@5953: this.redirectFailure(resp, req.getContextPath()); bh@5953: } bh@5953: bh@5953: protected void performLogin(HttpServletRequest req, bh@5953: HttpServletResponse resp, User user) bh@5953: throws ServletException, IOException { bh@5953: String url = getServletContext().getInitParameter("server-url"); bh@5953: UserClient client = new UserClient(url); bh@5953: if (!client.userExists(user)) { teichmann@8203: log.debug("Creating db user"); bh@5953: if (!client.createUser(user)) { bh@5953: this.redirectFailure(resp, req.getContextPath(), bh@5953: "Could not create new user"); bh@5953: return; bh@5953: } bh@5953: } bh@5953: bh@5953: HttpSession session = req.getSession(); bh@5953: session.setAttribute("user", user); bh@5953: bh@5953: String uri = (String)session.getAttribute("requesturi"); bh@5953: bh@5953: this.redirectSuccess(resp, req.getContextPath(), uri); bh@5953: } bh@5953: }