View Javadoc

1   package org.appfuse.webapp.pages;
2   
3   import org.apache.tapestry5.EventContext;
4   import org.apache.tapestry5.Link;
5   import org.apache.tapestry5.alerts.AlertManager;
6   import org.apache.tapestry5.alerts.Duration;
7   import org.apache.tapestry5.alerts.Severity;
8   import org.apache.tapestry5.annotations.InjectPage;
9   import org.apache.tapestry5.ioc.Messages;
10  import org.apache.tapestry5.ioc.annotations.Inject;
11  import org.apache.tapestry5.services.PageRenderLinkSource;
12  import org.appfuse.service.UserManager;
13  import org.appfuse.webapp.services.EmailService;
14  import org.appfuse.webapp.util.RequestUtil;
15  import org.slf4j.Logger;
16  import org.springframework.security.core.userdetails.UsernameNotFoundException;
17  
18  import javax.servlet.http.HttpServletRequest;
19  
20  /**
21   * Generates a password recovery token and sends it via email to user.
22   *
23   */
24  public class PasswordRecoveryToken {
25  
26      @Inject
27      private Logger logger;
28  
29      @Inject
30      private Messages messages;
31  
32      @Inject
33      private UserManager userManager;
34  
35  
36      @Inject
37      private AlertManager alertManager;
38  
39      @Inject
40      private HttpServletRequest request;
41  
42  
43      @Inject
44      private PageRenderLinkSource pageRenderLinkSource;
45  
46  
47      private String username;
48  
49      Object onActivate(EventContext ctx) {
50          // ensure that the username has been set
51          if (ctx == null || ctx.getCount() == 0) {
52              logger.warn("Username not specified, notifying user that it's a required field.");
53              alertManager.alert(Duration.TRANSIENT,
54                      Severity.ERROR,
55                      messages.format("errors.required", messages.get("user.username")));
56  
57              return Login.class;
58          }
59  
60          // Expect username is the first item in the context
61          int userIdx = 0;
62          this.username = ctx.get(String.class, userIdx).trim();
63  
64          logger.debug("Sending recovery token for username: " + username);
65  
66          try {
67              userManager.sendPasswordRecoveryEmail(username, RequestUtil.getAppURL(request) + getLink());
68          } catch (final UsernameNotFoundException ignored) {
69              // lets ignore this
70              Throwable exceptionToLog = ignored.getCause() != null ? ignored.getCause() : ignored;
71              logger.error(exceptionToLog.getLocalizedMessage());
72          }
73  
74          alertManager.alert(Duration.TRANSIENT,
75                  Severity.SUCCESS,
76                  messages.get("updatePassword.recoveryToken.sent"));
77  
78  
79          return Login.class;
80      }
81  
82      /**
83       * Build link for for password reset
84       * @return URI
85       */
86      public String getLink() {
87          Link link = pageRenderLinkSource.createPageRenderLinkWithContext(PasswordUpdate.class);
88          link.addParameter("username", "{username}");
89          link.addParameter("token", "{token}");
90          return link.toURI();
91      }
92  
93  }