View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.webapp.action;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import com.gisgraphy.model.User;
29  import com.gisgraphy.webapp.util.RequestUtil;
30  
31  /**
32   * Action class to send password hints to registered users.
33   * 
34   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
35   */
36  public class PasswordHintAction extends BaseAction {
37      private static final long serialVersionUID = -4037514607101222025L;
38  
39      private String username;
40  
41      /**
42       * @param username
43       *                The username to set.
44       */
45      public void setUsername(String username) {
46  	this.username = username;
47      }
48  
49      /**
50       * Execute sending the password hint via e-mail.
51       * 
52       * @return success if username works, input if not
53       */
54      @Override
55      public String execute() {
56  	List<String> args = new ArrayList<String>();
57  
58  	// ensure that the username has been sent
59  	if (username == null) {
60  	    log
61  		    .warn("Username not specified, notifying user that it's a required field.");
62  
63  	    args.add(getText("user.username"));
64  	    addActionError(getText("errors.requiredField",args.toArray(new String[]{}) ));
65  	    return INPUT;
66  	}
67  
68  	if (log.isDebugEnabled()) {
69  	    log.debug("Processing Password Hint...");
70  	}
71  
72  	// look up the user's information
73  	try {
74  	    User user = userManager.getUserByUsername(username);
75  	    String hint = user.getPasswordHint();
76  
77  	    if (hint == null || hint.trim().equals("")) {
78  		log.warn("User '" + username
79  			+ "' found, but no password hint exists.");
80  		addActionError(getText("login.passwordHint.missing"));
81  		return INPUT;
82  	    }
83  
84  	    StringBuffer msg = new StringBuffer();
85  	    msg.append("Your password hint is: ").append(hint);
86  	    msg.append("\n\nLogin at: ").append(
87  		    RequestUtil.getAppURL(getRequest()));
88  
89  	    mailMessage.setTo(user.getEmail());
90  	    String subject = '[' + getText("webapp.name") + "] "
91  		    + getText("user.passwordHint");
92  	    mailMessage.setSubject(subject);
93  	    mailMessage.setText(msg.toString());
94  	    mailEngine.send(mailMessage);
95  
96  	    args.add(username);
97  	    args.add(user.getEmail());
98  
99  	    saveMessage(getText("login.passwordHint.sent", args.toArray(new String[]{})));
100 
101 	} catch (Exception e) {
102 	    log.warn("Username '" + username + "' not found in database.");
103 	    args.add(username);
104 	    addActionError(getText("login.passwordHint.error", args.toArray(new String[]{})));
105 	    return INPUT;
106 	}
107 
108 	return SUCCESS;
109     }
110 }