KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jresearch > gossip > actions > user > LogonAction


1 /*
2  * $$Id: LogonAction.java,v 1.3 2005/06/07 12:32:29 bel70 Exp $$
3  *
4  * ***** BEGIN LICENSE BLOCK *****
5  * The contents of this file are subject to the Mozilla Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License
8  * at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific language governing rights and
13  * limitations under the License.
14  *
15  * The Original Code is JGossip forum code.
16  *
17  * The Initial Developer of the Original Code is the JResearch, Org.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Dmitry Belov <bel@jresearch.org>
23  *
24  * ***** END LICENSE BLOCK ***** */

25 package org.jresearch.gossip.actions.user;
26
27 import java.sql.SQLException JavaDoc;
28
29 import javax.servlet.http.Cookie JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32 import javax.servlet.http.HttpSession JavaDoc;
33
34 import org.apache.struts.action.ActionForm;
35 import org.apache.struts.action.ActionForward;
36 import org.apache.struts.action.ActionMapping;
37 import org.apache.struts.action.ActionMessage;
38 import org.apache.struts.action.ActionMessages;
39 import org.apache.struts.util.MessageResources;
40 import org.jresearch.gossip.IConst;
41 import org.jresearch.gossip.actions.BaseAction;
42 import org.jresearch.gossip.beans.user.User;
43 import org.jresearch.gossip.configuration.Configurator;
44 import org.jresearch.gossip.dao.UserDAO;
45 import org.jresearch.gossip.exception.SystemException;
46 import org.jresearch.gossip.forms.LogonForm;
47 import org.jresearch.gossip.log.LogLevel;
48 import org.jresearch.gossip.log.avalon.JGossipLog;
49
50 /**
51  * Implementation of <strong>Action</strong> that validates a user logon.
52  *
53  * @version $Revision: 1.3 $ $Date: 2005/06/07 12:32:29 $
54  */

55 public final class LogonAction extends BaseAction {
56     /**
57      * DOCUMENT ME!
58      *
59      * @param mapping
60      * DOCUMENT ME!
61      * @param form
62      * DOCUMENT ME!
63      * @param request
64      * DOCUMENT ME!
65      * @param response
66      * DOCUMENT ME!
67      *
68      * @return DOCUMENT ME!
69      */

70     public ActionForward process(ActionMapping mapping, ActionForm form,
71             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
72             throws SystemException {
73         if (IConst.VALUES.FALSE.equals(Configurator.getInstance().get(
74                 IConst.CONFIG.ENABLE_FORUM_SIGN_ON))) {
75             return (mapping.findForward(IConst.TOKEN.DENIED));
76         }
77
78         // Extract attributes we will need
79
HttpSession JavaDoc session = request.getSession();
80         MessageResources messages = getResources(request);
81         LogonForm logonForm = (LogonForm) form;
82         User user = new User();
83
84         ActionMessages errors = new ActionMessages();
85
86         String JavaDoc username = logonForm.getUsername();
87         String JavaDoc password = logonForm.getPassword();
88
89         UserDAO userDAO = UserDAO.getInstance();
90
91         try {
92
93             user = userDAO.getUser(username, password);
94             user.setIp(request.getRemoteAddr());
95
96             if (user.getStatus() == 0) {
97                 errors.add(ActionMessages.GLOBAL_MESSAGE,
98                         new ActionMessage("forum.LOG_FAIL",
99                                 new Object JavaDoc[] { response
100                                         .encodeURL("showFogotPass.do?uid="
101                                                 + username) }));
102                 JGossipLog.audit(LogLevel.WARN, user, messages
103                         .getMessage("logs.LOG2")
104                         + " \"" + username + "\"", session);
105             } else {
106                 session.setAttribute(IConst.SESSION.USER_KEY, user);
107                 session.removeAttribute(IConst.SESSION.GROUPS_KEY);
108                 log(request, "logs.LOG1");
109             }
110         } catch (SQLException JavaDoc sqle) {
111             getServlet().log("Connection.process", sqle);
112             throw new SystemException(sqle);
113         }
114
115         // Report any errors we have discovered back to the original form
116
if (!errors.isEmpty()) {
117             saveErrors(request, errors);
118
119             return (mapping.getInputForward());
120         }
121
122         if (IConst.VALUES.TRUE.equals(Configurator.getInstance().get(
123                 IConst.CONFIG.ENABLE_AUTO_LOGIN))) {
124             // set autolog cookies if needed...
125
if (user.getSettings().isAutologin()) {
126                 Cookie JavaDoc userCookie = new Cookie JavaDoc(IConst.COOKIE.USER_COOKIE, user
127                         .getName()
128                         + "*" + user.getPassword());
129                 userCookie.setMaxAge(IConst.COOKIE.SECONDS_PER_YEAR);
130                 ((HttpServletResponse JavaDoc) response).addCookie(userCookie);
131             }
132         }
133
134         // Remove the obsolete form bean
135
if (mapping.getAttribute() != null) {
136             if ("request".equals(mapping.getScope())) {
137                 request.removeAttribute(mapping.getAttribute());
138             } else {
139                 session.removeAttribute(mapping.getAttribute());
140             }
141         }
142
143         // Forward control to the specified success URI
144
String JavaDoc redirectUri = logonForm.getRedirectUri();
145
146         if ((redirectUri != null) && !redirectUri.equals("")) {
147             return (new ActionForward(redirectUri, true));
148         }
149
150         return (mapping.findForward(IConst.TOKEN.WELCOME));
151     }
152 }
153
Popular Tags