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