1 18 19 package org.apache.struts.webapp.example; 20 21 import java.lang.reflect.InvocationTargetException ; 22 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 28 import org.apache.commons.beanutils.PropertyUtils; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.struts.action.Action; 32 import org.apache.struts.action.ActionForm; 33 import org.apache.struts.action.ActionForward; 34 import org.apache.struts.action.ActionMapping; 35 import org.apache.struts.action.ActionMessage; 36 import org.apache.struts.action.ActionMessages; 37 38 45 46 public final class SaveRegistrationAction extends Action { 47 48 50 53 private Log log = LogFactory.getLog("org.apache.struts.webapp.Example"); 54 55 57 public ActionForward execute( 59 ActionMapping mapping, 60 ActionForm form, 61 HttpServletRequest request, 62 HttpServletResponse response) 63 throws Exception { 64 65 HttpSession session = request.getSession(); 67 RegistrationForm regform = (RegistrationForm) form; 68 String action = regform.getAction(); 69 if (action == null) { 70 action = "Create"; 71 } 72 73 UserDatabase database = 74 (UserDatabase) servlet.getServletContext().getAttribute( 75 Constants.DATABASE_KEY); 76 77 if (log.isDebugEnabled()) { 78 log.debug("SaveRegistrationAction: Processing " + action + " action"); 79 } 80 81 User user = (User) session.getAttribute(Constants.USER_KEY); 83 if (!"Create".equals(action) && (user == null)) { 84 if (log.isTraceEnabled()) { 85 log.trace(" User is not logged on in session " + session.getId()); 86 } 87 return (mapping.findForward("logon")); 88 } 89 90 if (isCancelled(request)) { 92 if (log.isTraceEnabled()) { 93 log.trace(" Transaction '" + action + "' was cancelled"); 94 } 95 session.removeAttribute(Constants.SUBSCRIPTION_KEY); 96 return (mapping.findForward("success")); 97 } 98 99 ActionMessages errors = new ActionMessages(); 101 if (log.isTraceEnabled()) { 102 log.trace(" Checking transactional control token"); 103 } 104 105 if (!isTokenValid(request)) { 106 errors.add( 107 ActionMessages.GLOBAL_MESSAGE, 108 new ActionMessage("error.transaction.token")); 109 } 110 111 resetToken(request); 112 113 if (log.isTraceEnabled()) { 115 log.trace(" Performing extra validations"); 116 } 117 118 String value = null; 119 value = regform.getUsername(); 120 if (("Create".equals(action)) && (database.findUser(value) != null)) { 121 errors.add( 122 "username", 123 new ActionMessage("error.username.unique", regform.getUsername())); 124 } 125 126 if ("Create".equals(action)) { 127 value = regform.getPassword(); 128 if ((value == null) || (value.length() < 1)) { 129 errors.add("password", new ActionMessage("error.password.required")); 130 } 131 132 value = regform.getPassword2(); 133 134 if ((value == null) || (value.length() < 1)) { 135 errors.add( 136 "password2", 137 new ActionMessage("error.password2.required")); 138 } 139 } 140 141 if (!errors.isEmpty()) { 143 this.saveErrors(request, errors); 144 this.saveToken(request); 145 return (mapping.getInputForward()); 146 } 147 148 try { 150 if ("Create".equals(action)) { 151 user = database.createUser(regform.getUsername()); 152 } 153 154 String oldPassword = user.getPassword(); 155 PropertyUtils.copyProperties(user, regform); 156 if ((regform.getPassword() == null) 157 || (regform.getPassword().length() < 1)) { 158 159 user.setPassword(oldPassword); 160 } 161 162 } catch (InvocationTargetException e) { 163 Throwable t = e.getTargetException(); 164 if (t == null) { 165 t = e; 166 } 167 168 log.error("Registration.populate", t); 169 throw new ServletException ("Registration.populate", t); 170 171 } catch (Throwable t) { 172 log.error("Registration.populate", t); 173 throw new ServletException ("Subscription.populate", t); 174 } 175 176 try { 177 database.save(); 178 } catch (Exception e) { 179 log.error("Database save", e); 180 } 181 182 if ("Create".equals(action)) { 184 session.setAttribute(Constants.USER_KEY, user); 185 if (log.isTraceEnabled()) { 186 log.trace( 187 " User '" 188 + user.getUsername() 189 + "' logged on in session " 190 + session.getId()); 191 } 192 } 193 194 if (mapping.getAttribute() != null) { 196 if ("request".equals(mapping.getScope())) 197 request.removeAttribute(mapping.getAttribute()); 198 else 199 session.removeAttribute(mapping.getAttribute()); 200 } 201 202 if (log.isTraceEnabled()) { 204 log.trace(" Forwarding to success page"); 205 } 206 207 return (mapping.findForward("success")); 208 209 } 210 211 } 212 | Popular Tags |