KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > appfuse > webapp > action > SignupAction


1 package org.appfuse.webapp.action;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4 import javax.servlet.http.HttpServletResponse JavaDoc;
5
6 import org.acegisecurity.Authentication;
7 import org.acegisecurity.context.SecurityContextHolder;
8 import org.acegisecurity.providers.ProviderManager;
9 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
10
11 import org.apache.struts.action.ActionForm;
12 import org.apache.struts.action.ActionForward;
13 import org.apache.struts.action.ActionMapping;
14 import org.apache.struts.action.ActionMessage;
15 import org.apache.struts.action.ActionMessages;
16 import org.apache.struts.util.MessageResources;
17 import org.appfuse.Constants;
18 import org.appfuse.model.User;
19 import org.appfuse.service.MailEngine;
20 import org.appfuse.service.RoleManager;
21 import org.appfuse.service.UserExistsException;
22 import org.appfuse.service.UserManager;
23 import org.appfuse.util.StringUtil;
24 import org.appfuse.webapp.form.UserForm;
25 import org.appfuse.webapp.util.RequestUtil;
26 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
27 import org.springframework.mail.SimpleMailMessage;
28
29 /**
30  * Action class to allow users to self-register.
31  *
32  * <p/>
33  * <a HREF="SignupAction.java.htm"><i>View Source</i></a>
34  * </p>
35  *
36  * @author <a HREF="mailto:matt@raibledesigns.com">Matt Raible</a>
37  *
38  * @struts.action name="userForm" path="/signup" scope="request"
39  * validate="false" input="failure"
40  *
41  * @struts.action-forward name="failure" path="/WEB-INF/pages/signup.jsp"
42  * @struts.action-forward name="success" path="/mainMenu.html" redirect="true"
43  */

44 public final class SignupAction extends BaseAction {
45     
46     public ActionForward execute(ActionMapping mapping, ActionForm form,
47                                  HttpServletRequest JavaDoc request,
48                                  HttpServletResponse JavaDoc response)
49     throws Exception JavaDoc {
50         
51         // if it's an HTTP GET, simply forward to jsp
52
if (request.getMethod().equals("GET")) {
53             return mapping.findForward("failure");
54         // user clicked cancel button
55
} else if (isCancelled(request)) {
56             return new ActionForward("/");
57         // run validation
58
} else {
59             // run validation rules on this form
60
ActionMessages errors = form.validate(mapping, request);
61             if (!errors.isEmpty()) {
62                 saveErrors(request, errors);
63                 return mapping.findForward("failure");
64             }
65         }
66
67         if (log.isDebugEnabled()) {
68             log.debug("registering user...");
69         }
70
71         ActionMessages errors = new ActionMessages();
72         UserForm userForm = (UserForm) form;
73         User user = (User) convert(form);
74
75         // Set the default user role on this new user
76
RoleManager roleMgr = (RoleManager) getBean("roleManager");
77         user.addRole(roleMgr.getRole(Constants.USER_ROLE));
78
79         try {
80             Boolean JavaDoc encrypt = (Boolean JavaDoc) getConfiguration().get(Constants.ENCRYPT_PASSWORD);
81             
82             if (encrypt != null && encrypt.booleanValue()) {
83                 String JavaDoc algorithm = (String JavaDoc) getConfiguration().get(Constants.ENC_ALGORITHM);
84                 if (algorithm == null) { // should only happen for test case
85
log.debug("assuming testcase, setting algorigthm to 'SHA'");
86                     algorithm = "SHA";
87                 }
88
89                 user.setPassword(StringUtil.encodePassword(user.getPassword(), algorithm));
90             }
91             
92             user.setEnabled(true);
93             UserManager mgr = (UserManager) getBean("userManager");
94             mgr.saveUser(user);
95         } catch (UserExistsException e) {
96             log.warn(e.getMessage());
97             errors.add(ActionMessages.GLOBAL_MESSAGE,
98                     new ActionMessage("errors.existing.user",
99                             userForm.getUsername(),
100                             userForm.getEmail()));
101             saveErrors(request, errors);
102             return mapping.getInputForward();
103         }
104
105         ActionMessages messages = new ActionMessages();
106         MessageResources resources = getResources(request);
107
108         messages.add(ActionMessages.GLOBAL_MESSAGE,
109                 new ActionMessage("user.registered", userForm.getUsername()));
110
111         saveMessages(request.getSession(), messages);
112         request.getSession().setAttribute(Constants.REGISTERED, Boolean.TRUE);
113
114         // log user in automatically
115
Authentication auth = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getConfirmPassword());
116         try {
117             ProviderManager authenticationManager = (ProviderManager) getBean("authenticationManager");
118             SecurityContextHolder.getContext().setAuthentication(authenticationManager.doAuthentication(auth));
119         } catch (NoSuchBeanDefinitionException n) {
120             // ignore, should only happen when testing
121
}
122
123         // Send user an e-mail
124
if (log.isDebugEnabled()) {
125             log.debug("Sending user '" + userForm.getUsername()
126                     + "' an account information e-mail");
127         }
128
129         SimpleMailMessage message = (SimpleMailMessage) getBean("mailMessage");
130         message.setTo(user.getFullName() + "<" + user.getEmail() + ">");
131         
132         StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
133         msg.append(resources.getMessage("signup.email.message"));
134         msg.append("\n\n" + resources.getMessage("userForm.username"));
135         msg.append(": " + userForm.getUsername() + "\n");
136         msg.append(resources.getMessage("userForm.password") + ": ");
137         msg.append(userForm.getPassword());
138         msg.append("\n\nLogin at: " + RequestUtil.getAppURL(request));
139         message.setText(msg.toString());
140         
141         message.setSubject(resources.getMessage("signup.email.subject"));
142         
143         MailEngine engine = (MailEngine) getBean("mailEngine");
144         engine.send(message);
145
146         return mapping.findForward("success");
147     }
148
149 }
150
Popular Tags