1 18 package org.apache.struts.webapp.example; 19 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpServletResponse ; 22 import javax.servlet.http.HttpSession ; 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 36 public final class LogonAction extends BaseAction { 37 38 41 static String USERNAME = "username"; 42 43 46 static String PASSWORD = "password"; 47 48 50 63 User getUser(UserDatabase database, String username, 64 String 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 96 void SaveUser(HttpServletRequest request, User user) { 97 98 HttpSession 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 112 125 public ActionForward execute( 126 ActionMapping mapping, 127 ActionForm form, 128 HttpServletRequest request, 129 HttpServletResponse response) 130 throws Exception { 131 132 UserDatabase database = getUserDatabase(request); 134 String username = (String ) PropertyUtils.getSimpleProperty(form, 135 USERNAME); 136 String password = (String ) PropertyUtils.getSimpleProperty(form, 137 PASSWORD); 138 ActionMessages errors = new ActionMessages(); 139 140 User user = getUser(database,username,password,errors); 142 143 if (!errors.isEmpty()) { 145 this.saveErrors(request, errors); 146 return (mapping.getInputForward()); 147 } 148 149 SaveUser(request,user); 151 152 153 return (findSuccess(mapping)); 155 156 } 157 158 } 159 | Popular Tags |