import java.lang.reflect.InvocationTargetException; import org.apache.commons.beanutils.PropertyUtils; import org.apache.struts.action.*; import org.apache.struts.webapp.example.*; form = struts.form; String act = form.getAction(); if (act == null) { act = "Create"; } database = application.getAttribute(Constants.DATABASE_KEY); if (log.isDebugEnabled()) { log.debug("SaveRegistrationAction: Processing " + act + " act"); } // Is there a currently logged on user (unless creating)? user = session.getAttribute(Constants.USER_KEY); if (!"Create".equals(act) && (user == null)) { if (log.isTraceEnabled()) { log.trace(" User is not logged on in session " + session.getId()); } struts.forwardName="logon"; return; } // Was this transact cancelled? if (struts.action.isCancelled(request)) { if (log.isTraceEnabled()) { log.trace(" Transact '" + act + "' was cancelled"); } session.removeAttribute(Constants.SUBSCRIPTION_KEY); struts.forwardName="success"; return; } // Validate the transactal control token errors = new ActionErrors(); if (log.isTraceEnabled()) { log.trace(" Checking transactal control token"); } if (!struts.action.isTokenValid(request)) { errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.transact.token")); } struts.action.resetToken(request); // Validate the request parameters specified by the user if (log.isTraceEnabled()) { log.trace(" Performing extra validations"); } value = form.getUsername(); if (("Create".equals(act)) && (database.findUser(value) != null)) { errors.add("username", new ActionError("error.username.unique", form.getUsername())); } if ("Create".equals(act)) { value = form.password; if ((value == null) || (value.length() <1)) { errors.add("password", new ActionError("error.password.required")); } value = form.password2; if ((value == null) || (value.length() < 1)) { errors.add("password2", new ActionError("error.password2.required")); } } // Report any errors we have discovered back to the original form if (!errors.isEmpty()) { struts.action.saveErrors(request, errors); struts.action.saveToken(request); struts.forward = struts.mapping.getInputForward(); return; } // Update the user's persistent profile information try { if ("Create".equals(act)) { user = database.createUser(form.username); } oldPassword = user.password; PropertyUtils.copyProperties(user, form); if ((form.password == null) || (form.password.length() < 1)) { user.password = oldPassword; } } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (t == null) { t = e; } log.error("Registration.populate", t); throw new ServletException("Registration.populate", t); } catch (Throwable t) { log.error("Registration.populate", t); throw new ServletException("Subscription.populate", t); } try { database.save(); } catch (e) { log.error("Database save", e); } // Log the user in if appropriate if ("Create".equals(act)) { session.setAttribute(Constants.USER_KEY, user); if (log.isTraceEnabled()) { log.trace(" User '" + user.getUsername() + "' logged on in session " + session.getId()); } } // Remove the obsolete form bean if (struts.mapping.getAttribute() != null) { if ("request".equals(struts.mapping.scope)) request.removeAttribute(struts.mapping.attribute); else session.removeAttribute(struts.mapping.attribute); } // Forward control to the specified success URI if (log.isTraceEnabled()) { log.trace(" Forwarding to success page"); } struts.forwardName = "success";