Source code: com/clra/web/LogonAction.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: LogonAction.java,v $
5 * $Date: 2003/02/26 03:38:46 $
6 * $Revision: 1.3 $
7 */
8
9 /*
10 * Copyright (c) 1999-2001 The Apache Software Foundation. All rights
11 * reserved.
12 *
13 * The Apache Software License, Version 1.1 (see licenses)
14 */
15 package com.clra.web;
16
17 import com.clra.member.Authentication;
18 import com.clra.web.MemberView;
19 import java.io.IOException;
20 import java.util.Hashtable;
21 import java.util.Locale;
22 import javax.servlet.RequestDispatcher;
23 import javax.servlet.ServletException;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpSession;
26 import javax.servlet.http.HttpServletResponse;
27 import org.apache.log4j.Category;
28 import org.apache.struts.action.Action;
29 import org.apache.struts.action.ActionError;
30 import org.apache.struts.action.ActionErrors;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34 import org.apache.struts.action.ActionServlet;
35 import org.apache.struts.util.MessageResources;
36
37 /**
38 * Implementation of <strong>Action</strong> that validates a user logon.
39 * From the struts documentation example.
40 *
41 * @author Craig R. McClanahan
42 * -- original author
43 * @author <a href="mailto:rphall@pluto.njcc.com">Rick Hall</a>
44 * -- adapted to CLRA
45 * @version $Revision: 1.3 $ $Date: 2003/02/26 03:38:46 $
46 */
47 public final class LogonAction extends Action {
48
49 private final static String base = LogonAction.class.getName();
50 private final static Category theLog = Category.getInstance( base );
51
52 /**
53 * Process the specified HTTP request, and create the corresponding HTTP
54 * response (or forward to another web component that will create it).
55 * Return an <code>ActionForward</code> instance describing where and how
56 * control should be forwarded, or <code>null</code> if the response has
57 * already been completed.
58 *
59 * @param mapping The ActionMapping used to select this instance
60 * @param actionForm The optional ActionForm bean for this request (if any)
61 * @param request The HTTP request we are processing
62 * @param response The HTTP response we are creating
63 *
64 * @exception IOException if an input/output error occurs
65 * @exception ServletException if a servlet exception occurs
66 */
67 public ActionForward perform(ActionMapping mapping, ActionForm form,
68 HttpServletRequest request, HttpServletResponse response)
69 throws IOException, ServletException {
70
71 System.out.println( "LogonAction.perform..." );
72
73 // Extract attributes we will need
74 Locale locale = getLocale(request);
75 MessageResources messages = getResources();
76
77 // Validate the request parameters specified by the user
78 HttpSession session = request.getSession();
79 ActionErrors errors = new ActionErrors();
80 String username = ((LogonForm) form).getUsername();
81 String password = ((LogonForm) form).getPassword();
82 Authentication authentication = null;
83 try {
84 authentication = new Authentication(username,password);
85 }
86 catch(Exception x) {
87 theLog.error(x.getMessage(),x);
88 errors.add(ActionErrors.GLOBAL_ERROR,
89 new ActionError("error.login.exception") );
90 }
91 try {
92
93 authentication.login();
94
95 // Save our logged-in user in the session
96 session.setAttribute(Constants.AUTHENTICATION_KEY, authentication);
97 String msg = "LogonAction: User '" + username
98 + "' logged on in session " + session.getId();
99 theLog.debug( msg );
100 if (servlet.getDebug() >= 1) {
101 servlet.log( msg );
102 }
103
104 }
105 catch(Exception x) {
106 theLog.error(x.toString()); // Somewhat expected, so no stack trace
107 errors.add(ActionErrors.GLOBAL_ERROR,
108 new ActionError("error.login.failed") );
109 }
110
111 // Report any errors we have discovered back to the original form
112 if (!errors.empty()) {
113 saveErrors(request, errors);
114 return (new ActionForward(mapping.getInput()));
115 }
116
117 // Remove the obsolete form bean
118 if (mapping.getAttribute() != null) {
119 if ("request".equals(mapping.getScope())) {
120 request.removeAttribute(mapping.getAttribute());
121 }
122 else {
123 session.removeAttribute(mapping.getAttribute());
124 }
125 }
126
127 // Forward control to the specified success URI
128 return (mapping.findForward("success"));
129
130 } // perform(..)
131
132 } // ActionForward
133
134 /*
135 * $Log: LogonAction.java,v $
136 * Revision 1.3 2003/02/26 03:38:46 rphall
137 * Added copyright and GPL license
138 *
139 * Revision 1.2 2002/02/18 18:06:13 rphall
140 * Ran dos2unix to remove ^M (carriage return) from end of lines
141 *
142 * Revision 1.1.1.1 2002/01/03 21:57:28 rphall
143 * Initial load, 5th try, Jan-03-2002 4:57 PM
144 *
145 * Revision 1.3 2001/12/11 23:38:43 rphall
146 * Moved MemberView, MemberSet from member to web package
147 *
148 * Revision 1.2 2001/11/30 11:38:00 rphall
149 * First working version, RowingSession entity bean
150 *
151 * Revision 1.1 2001/11/23 19:40:02 rphall
152 * Major revision
153 *
154 */
155