View Javadoc

1   package org.appfuse.webapp.action;
2   
3   import org.appfuse.model.User;
4   import org.appfuse.webapp.util.RequestUtil;
5   import org.springframework.mail.MailException;
6   import org.springframework.security.core.userdetails.UsernameNotFoundException;
7   
8   /**
9    * Managed Bean to send password hints to registered users.
10   *
11   * <p>
12   * <a href="PasswordHint.java.html"><i>View Source</i></a>
13   * </p>
14   *
15   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
16   */
17  public class PasswordHint extends BasePage {
18      private String username;
19      
20      public void setUsername(String username) {
21          this.username = username;
22      }
23      
24      public String execute() {
25          getFacesContext().getViewRoot().setViewId("/passwordHint.xhtml");
26  
27          // ensure that the username has been sent
28          if (username == null || "".equals(username)) {
29              log.warn("Username not specified, notifying user that it's a required field.");
30  
31              addError("errors.required", getText("user.username"));
32              return null;
33          } else if (username.endsWith(".xhtml")) {
34              username = username.substring(0, username.indexOf(".xhtml"));
35          }
36          
37          if (log.isDebugEnabled()) {
38              log.debug("Processing Password Hint...");
39          }
40          
41          // look up the user's information
42          try {
43              User user = userManager.getUserByUsername(username);
44  
45              StringBuilder msg = new StringBuilder();
46              msg.append("Your password hint is: ").append(user.getPasswordHint());
47              msg.append("\n\nLogin at: ").append(RequestUtil.getAppURL(getRequest()));
48  
49              message.setTo(user.getEmail());
50              String subject = '[' + getText("webapp.name") + "] " + getText("user.passwordHint");
51              message.setSubject(subject);
52              message.setText(msg.toString());
53              mailEngine.send(message);
54              
55              addMessage("login.passwordHint.sent", 
56                         new Object[] { username, user.getEmail() });
57              
58          } catch (UsernameNotFoundException e) {
59              log.warn(e.getMessage());
60              // If exception is expected do not rethrow
61              addError("login.passwordHint.error", username);
62          } catch (MailException me) {
63              addError(me.getCause().getLocalizedMessage());
64          }
65  
66          return "success";
67      }
68  }