KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 package org.apache.struts.webapp.example;
19
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22 import javax.servlet.http.HttpSession JavaDoc;
23
24 import org.apache.commons.beanutils.PropertyUtils;
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.action.ActionForward;
27 import org.apache.struts.action.ActionMapping;
28 import org.apache.struts.action.ActionMessage;
29 import org.apache.struts.action.ActionMessages;
30
31 /**
32  * <p>Validate a user logon.</p>
33  *
34  * @version $Rev: 164858 $ $Date: 2005-04-26 20:03:35 +0100 (Tue, 26 Apr 2005) $
35  */

36 public final class LogonAction extends BaseAction {
37
38     /**
39      * Name of username field ["username"].
40      */

41     static String JavaDoc USERNAME = "username";
42
43     /**
44      * Name of password field ["password"].
45      */

46     static String JavaDoc PASSWORD = "password";
47
48     // ------------------------------------------------------ Protected Methods
49

50     /**
51      * <p>Confirm user credentials. Post any errors and return User object
52      * (or null).</p>
53      *
54      * @param database Database in which to look up the user
55      * @param username Username specified on the logon form
56      * @param password Password specified on the logon form
57      * @param errors ActionMessages queue to passback errors
58      *
59      * @return Validated User object or null
60      * @throws ExpiredPasswordException to be handled by Struts exception
61      * processor via the action-mapping
62      */

63     User getUser(UserDatabase database, String JavaDoc username,
64                            String JavaDoc password, ActionMessages errors) throws ExpiredPasswordException {
65
66         User user = null;
67         if (database == null){
68             errors.add(
69                 ActionMessages.GLOBAL_MESSAGE,
70                 new ActionMessage("error.database.missing"));
71         }
72         else {
73             user = database.findUser(username);
74             if ((user != null) && !user.getPassword().equals(password)) {
75                 user = null;
76             }
77             if (user == null) {
78                 errors.add(
79                     ActionMessages.GLOBAL_MESSAGE,
80                     new ActionMessage("error.password.mismatch"));
81             }
82         }
83
84         return user;
85
86     }
87
88
89     /**
90      * <p>Store User object in client session.
91      * If user object is null, any existing user object is removed.</p>
92      *
93      * @param request The request we are processing
94      * @param user The user object returned from the database
95      */

96     void SaveUser(HttpServletRequest JavaDoc request, User user) {
97
98         HttpSession JavaDoc session = request.getSession();
99         session.setAttribute(Constants.USER_KEY, user);
100         if (log.isDebugEnabled()) {
101             log.debug(
102                 "LogonAction: User '"
103                     + user.getUsername()
104                     + "' logged on in session "
105                     + session.getId());
106         }
107
108     }
109
110     // --------------------------------------------------------- Public Methods
111

112     /**
113      * Use "username" and "password" fields from ActionForm to retrieve a User
114      * object from the database. If credentials are not valid, or database
115      * has disappeared, post error messages and forward to input.
116      *
117      * @param mapping The ActionMapping used to select this instance
118      * @param form The optional ActionForm bean for this request (if any)
119      * @param request The HTTP request we are processing
120      * @param response The HTTP response we are creating
121      *
122      * @exception Exception if the application business logic throws
123      * an exception
124      */

125     public ActionForward execute(
126         ActionMapping mapping,
127         ActionForm form,
128         HttpServletRequest JavaDoc request,
129         HttpServletResponse JavaDoc response)
130         throws Exception JavaDoc {
131
132         // Local variables
133
UserDatabase database = getUserDatabase(request);
134         String JavaDoc username = (String JavaDoc) PropertyUtils.getSimpleProperty(form,
135                 USERNAME);
136         String JavaDoc password = (String JavaDoc) PropertyUtils.getSimpleProperty(form,
137                 PASSWORD);
138         ActionMessages errors = new ActionMessages();
139
140         // Retrieve user
141
User user = getUser(database,username,password,errors);
142
143         // Report back any errors, and exit if any
144
if (!errors.isEmpty()) {
145             this.saveErrors(request, errors);
146             return (mapping.getInputForward());
147         }
148
149         // Save user object
150
SaveUser(request,user);
151
152
153         // Otherwise, return "success"
154
return (findSuccess(mapping));
155
156     }
157
158 }
159
Popular Tags