FileDocCategorySizeDatePackage
LogonAction.javaAPI DocExample5387Mon Aug 09 16:26:02 BST 2004org.apache.struts.webapp.example

LogonAction

public final class LogonAction extends BaseAction

Validate a user logon.

version
$Revision: 1.24 $ $Date: 2004/03/12 23:49:29 $

Fields Summary
static String
USERNAME
Name of username field ["username"].
static String
PASSWORD
Name of password field ["password"].
Constructors Summary
Methods Summary
voidSaveUser(javax.servlet.http.HttpServletRequest request, User user)

Store User object in client session. If user object is null, any existing user object is removed.

param
request The request we are processing
param
user The user object returned from the database


        HttpSession session = request.getSession();
        session.setAttribute(Constants.USER_KEY, user);
        if (log.isDebugEnabled()) {
            log.debug(
                "LogonAction: User '"
                    + user.getUsername()
                    + "' logged on in session "
                    + session.getId());
        }

    
public org.apache.struts.action.ActionForwardexecute(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Use "username" and "password" fields from ActionForm to retrieve a User object from the database. If credentials are not valid, or database has disappeared, post error messages and forward to input.

param
mapping The ActionMapping used to select this instance
param
form The optional ActionForm bean for this request (if any)
param
request The HTTP request we are processing
param
response The HTTP response we are creating
exception
Exception if the application business logic throws an exception


        // Local variables
//        UserDatabase database = getUserDatabase(request);
        UserDatabase database = getUserDatabase();
        String username = (String) PropertyUtils.getSimpleProperty(form,
                USERNAME);
        String password = (String) PropertyUtils.getSimpleProperty(form,
                PASSWORD);
        ActionMessages errors = new ActionMessages();

        // Retrieve user
        User user = getUser(database,username,password,errors);

        // Save (or clear) user object
        SaveUser(request,user);

        // Report back any errors, and exit if any
        if (!errors.isEmpty()) {
            this.saveErrors(request, errors);
            return (mapping.getInputForward());
        }

        // Otherwise, return "success"
        return (findSuccess(mapping));

    
UsergetUser(UserDatabase database, java.lang.String username, java.lang.String password, org.apache.struts.action.ActionMessages errors)

Confirm user credentials. Post any errors and return User object (or null).

param
database Database in which to look up the user
param
username Username specified on the logon form
param
password Password specified on the logon form
param
errors ActionMessages queue to passback errors
return
Validated User object or null
throws
ExpiredPasswordException to be handled by Struts exception processor via the action-mapping


    // ------------------------------------------------------ Protected Methods

                                                                        
        
                                 

        User user = null;
        if (database == null){
            errors.add(
                ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("error.database.missing"));
        }
        else {
            user = database.findUser(username);
            if ((user != null) && !user.getPassword().equals(password)) {
                user = null;
            }
            if (user == null) {
                errors.add(
                    ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("error.password.mismatch"));
            }
        }

        return user;