View Javadoc

1   package org.appfuse.webapp.controller;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import org.appfuse.model.User;
6   import org.appfuse.service.MailEngine;
7   import org.appfuse.service.UserManager;
8   import org.appfuse.webapp.util.RequestUtil;
9   import org.springframework.beans.factory.annotation.Autowired;
10  import org.springframework.context.MessageSource;
11  import org.springframework.context.support.MessageSourceAccessor;
12  import org.springframework.mail.MailException;
13  import org.springframework.mail.SimpleMailMessage;
14  import org.springframework.security.core.userdetails.UsernameNotFoundException;
15  import org.springframework.stereotype.Controller;
16  import org.springframework.web.bind.annotation.RequestMapping;
17  import org.springframework.web.bind.annotation.RequestMethod;
18  import org.springframework.web.servlet.ModelAndView;
19  import org.springframework.web.servlet.view.RedirectView;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  /**
26   * Simple class to retrieve and send a password hint to users.
27   *
28   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
29   */
30  @Controller
31  @RequestMapping("/passwordHint*")
32  public class PasswordHintController {
33      private final Log log = LogFactory.getLog(PasswordHintController.class);
34      private UserManager userManager = null;
35      private MessageSource messageSource = null;
36      protected MailEngine mailEngine = null;
37      protected SimpleMailMessage message = null;
38  
39      @Autowired
40      public void setUserManager(UserManager userManager) {
41          this.userManager = userManager;
42      }
43  
44      @Autowired
45      public void setMessageSource(MessageSource messageSource) {
46          this.messageSource = messageSource;
47      }
48  
49      @Autowired
50      public void setMailEngine(MailEngine mailEngine) {
51          this.mailEngine = mailEngine;
52      }
53  
54      @Autowired
55      public void setMessage(SimpleMailMessage message) {
56          this.message = message;
57      }
58  
59      @RequestMapping(method = RequestMethod.GET)
60      public ModelAndView handleRequest(HttpServletRequest request)
61      throws Exception {
62          log.debug("entering 'handleRequest' method...");
63  
64          String username = request.getParameter("username");
65          MessageSourceAccessor text = new MessageSourceAccessor(messageSource, request.getLocale());
66  
67          // ensure that the username has been sent
68          if (username == null) {
69              log.warn("Username not specified, notifying user that it's a required field.");
70              request.setAttribute("error", text.getMessage("errors.required", text.getMessage("user.username")));
71              return new ModelAndView("login");
72          }
73  
74          log.debug("Processing Password Hint...");
75  
76          // look up the user's information
77          try {
78              User user = userManager.getUserByUsername(username);
79  
80              StringBuffer msg = new StringBuffer();
81              msg.append("Your password hint is: ").append(user.getPasswordHint());
82              msg.append("\n\nLogin at: ").append(RequestUtil.getAppURL(request));
83  
84              message.setTo(user.getEmail());
85              String subject = '[' + text.getMessage("webapp.name") + "] " + 
86                               text.getMessage("user.passwordHint");
87              message.setSubject(subject);
88              message.setText(msg.toString());
89              mailEngine.send(message);
90  
91              saveMessage(request, text.getMessage("login.passwordHint.sent", new Object[] { username, user.getEmail() }));
92          } catch (UsernameNotFoundException e) {
93              log.warn(e.getMessage());
94              saveError(request, text.getMessage("login.passwordHint.error", new Object[] { username }));
95          } catch (MailException me) {
96              log.warn(me.getMessage());
97              saveError(request, me.getCause().getLocalizedMessage());
98          }
99  
100         return new ModelAndView(new RedirectView(request.getContextPath()));
101     }
102 
103     @SuppressWarnings("unchecked")
104     public void saveError(HttpServletRequest request, String error) {
105         List errors = (List) request.getSession().getAttribute("errors");
106         if (errors == null) {
107             errors = new ArrayList();
108         }
109         errors.add(error);
110         request.getSession().setAttribute("errors", errors);
111     }
112 
113     // this method is also in BaseForm Controller
114     @SuppressWarnings("unchecked")
115     public void saveMessage(HttpServletRequest request, String msg) {
116         List messages = (List) request.getSession().getAttribute(BaseFormController.MESSAGES_KEY);
117         if (messages == null) {
118             messages = new ArrayList();
119         }
120         messages.add(msg);
121         request.getSession().setAttribute(BaseFormController.MESSAGES_KEY, messages);
122     }
123 }