KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > example2 > LogonAction


1 /*
2  * Copyright 1999-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.struts.webapp.example2;
19
20
21 import java.util.Locale JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import javax.servlet.http.HttpSession JavaDoc;
25 import org.apache.commons.beanutils.PropertyUtils;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
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.util.ModuleException;
35
36
37 /**
38  * Implementation of <strong>Action</strong> that validates a user logon.
39  *
40  * @author Craig R. McClanahan
41  * @version $Rev: 155886 $ $Date: 2005-03-02 06:04:47 +0000 (Wed, 02 Mar 2005) $
42  */

43
44 public final class LogonAction extends Action {
45
46
47     // ----------------------------------------------------- Instance Variables
48

49
50     /**
51      * The <code>Log</code> instance for this application.
52      */

53     private Log log =
54         LogFactory.getLog("org.apache.struts.webapp.Example");
55
56
57     // --------------------------------------------------------- Public Methods
58

59
60     /**
61      * Process the specified HTTP request, and create the corresponding HTTP
62      * response (or forward to another web component that will create it).
63      * Return an <code>ActionForward</code> instance describing where and how
64      * control should be forwarded, or <code>null</code> if the response has
65      * already been completed.
66      *
67      * @param mapping The ActionMapping used to select this instance
68      * @param form The optional ActionForm bean for this request (if any)
69      * @param request The HTTP request we are processing
70      * @param response The HTTP response we are creating
71      *
72      * @exception Exception if business logic throws an exception
73      */

74     public ActionForward execute(ActionMapping mapping,
75                  ActionForm form,
76                  HttpServletRequest JavaDoc request,
77                  HttpServletResponse JavaDoc response)
78     throws Exception JavaDoc {
79
80     // Extract attributes we will need
81
Locale JavaDoc locale = getLocale(request);
82     User user = null;
83
84     // Validate the request parameters specified by the user
85
ActionErrors errors = new ActionErrors();
86     String JavaDoc username = (String JavaDoc)
87             PropertyUtils.getSimpleProperty(form, "username");
88         String JavaDoc password = (String JavaDoc)
89             PropertyUtils.getSimpleProperty(form, "password");
90     UserDatabase database = (UserDatabase)
91       servlet.getServletContext().getAttribute(Constants.DATABASE_KEY);
92     if (database == null)
93             errors.add(ActionErrors.GLOBAL_ERROR,
94                        new ActionError("error.database.missing"));
95     else {
96         user = getUser(database, username);
97         if ((user != null) && !user.getPassword().equals(password))
98         user = null;
99         if (user == null)
100                 errors.add(ActionErrors.GLOBAL_ERROR,
101                            new ActionError("error.password.mismatch"));
102     }
103
104     // Report any errors we have discovered back to the original form
105
if (!errors.isEmpty()) {
106         saveErrors(request, errors);
107             return (mapping.getInputForward());
108     }
109
110     // Save our logged-in user in the session
111
HttpSession JavaDoc session = request.getSession();
112     session.setAttribute(Constants.USER_KEY, user);
113         if (log.isDebugEnabled()) {
114             log.debug("LogonAction: User '" + user.getUsername() +
115                       "' logged on in session " + session.getId());
116         }
117
118         // Remove the obsolete form bean
119
if (mapping.getAttribute() != null) {
120             if ("request".equals(mapping.getScope()))
121                 request.removeAttribute(mapping.getAttribute());
122             else
123                 session.removeAttribute(mapping.getAttribute());
124         }
125
126     // Forward control to the specified success URI
127
return (mapping.findForward("success"));
128
129     }
130
131
132     // ------------------------------------------------------ Protected Methods
133

134
135     /**
136      * Look up the user, throwing an exception to simulate business logic
137      * rule exceptions.
138      *
139      * @param database Database in which to look up the user
140      * @param username Username specified on the logon form
141      *
142      * @exception AppException if a business logic rule is violated
143      */

144     public User getUser(UserDatabase database, String JavaDoc username)
145         throws ModuleException {
146
147         // Force an ArithmeticException which can be handled explicitly
148
if ("arithmetic".equals(username)) {
149             throw new ArithmeticException JavaDoc();
150         }
151
152         // Force an application-specific exception which can be handled
153
if ("expired".equals(username)) {
154             throw new ExpiredPasswordException(username);
155         }
156
157         // Look up and return the specified user
158
return (database.findUser(username));
159
160     }
161
162
163 }
164
Popular Tags