01: package vqwiki.servlets;
02:
03: import org.apache.log4j.Logger;
04: import vqwiki.Environment;
05: import vqwiki.utils.JSPUtils;
06:
07: import javax.servlet.ServletException;
08: import javax.servlet.http.HttpServletRequest;
09: import javax.servlet.http.HttpServletResponse;
10: import java.io.IOException;
11:
12: /**
13: * Servlet responsible for managing login and logout.
14: *
15: * @author garethc
16: * Date: 5/03/2003
17: */
18: public class LoginServlet extends VQWikiServlet {
19:
20: /** Logger */
21: private static final Logger logger = Logger
22: .getLogger(LoginServlet.class);
23:
24: /**
25: * Respond to get request. This will be a logout request from the link that appears at the bottom of pages if
26: * there is a user principal in the session.
27: * @param httpServletRequest
28: * @param httpServletResponse
29: * @throws ServletException
30: * @throws IOException
31: */
32: protected void doGet(HttpServletRequest httpServletRequest,
33: HttpServletResponse httpServletResponse)
34: throws ServletException, IOException {
35: String logoutParameter = httpServletRequest
36: .getParameter("logout");
37: if (logoutParameter != null) {
38: Boolean logout = new Boolean(logoutParameter);
39: if (logout.booleanValue()) {
40: httpServletRequest.getSession().invalidate();
41: String redirect = JSPUtils.createRedirectURL(
42: httpServletRequest, httpServletRequest
43: .getParameter("redirect"));
44: redirect(redirect, httpServletResponse);
45: }
46: }
47: }
48:
49: /**
50: * Respond to post request. This will be called when the login form is filled out in login.jsp and is used for
51: * admin authentication for AdminOnlyTopics and the admin console.
52: * @param request
53: * @param httpServletResponse
54: * @throws ServletException
55: * @throws IOException
56: */
57: protected void doPost(HttpServletRequest request,
58: HttpServletResponse response) throws ServletException,
59: IOException {
60: String password = request.getParameter("password");
61: String username = request.getParameter("username");
62: Environment environment = Environment.getInstance();
63: String redirect = request.getParameter("redirect");
64: if ("admin".equals(username)
65: && environment.getAdminPassword().equals(password)) {
66: request.getSession().setAttribute("admin", "true");
67: } else {
68: // should this return a specific message instead?
69: request.setAttribute("loginFailure", "true");
70: request.setAttribute("redirect", redirect);
71: dispatch("/jsp/login.jsp", request, response);
72: return;
73: }
74: redirect(redirect, response);
75: }
76: }
|