1 25 package org.snipsnap.net; 26 27 import org.snipsnap.app.Application; 28 import org.snipsnap.config.Configuration; 29 import org.snipsnap.snip.SnipLink; 30 import org.snipsnap.user.User; 31 import org.snipsnap.user.UserManager; 32 import org.snipsnap.user.PasswordService; 33 import org.snipsnap.user.UserManagerFactory; 34 import org.snipsnap.util.mail.Mail; 35 import org.snipsnap.container.Components; 36 37 import javax.servlet.RequestDispatcher ; 38 import javax.servlet.ServletException ; 39 import javax.servlet.http.HttpServlet ; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 import java.io.IOException ; 43 44 51 public class MailPasswordKeyServlet extends HttpServlet { 52 private final static String ERR_PASSWORD = "User name and password do not match!"; 53 54 public void doPost(HttpServletRequest request, HttpServletResponse response) 55 throws ServletException , IOException { 56 57 String login = request.getParameter("login"); 58 59 if (request.getParameter("cancel") == null) { 60 UserManager um = UserManagerFactory.getInstance(); 61 User user = um.load(login); 62 63 if (user == null) { 64 request.setAttribute("error", "User name '" + login + "' does not exist!"); 65 RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/forgot.jsp"); 66 dispatcher.forward(request, response); 67 return; 68 } 69 70 PasswordService passwordService = (PasswordService) Components.getComponent(PasswordService.class); 71 String key = passwordService.getPassWordKey(user); 72 Configuration configuration = Application.get().getConfiguration(); 73 String receiver = user.getEmail(); 74 if (receiver != null && receiver.length() > 0) { 75 String subject = "Forgotten password"; 76 String url = configuration.getUrl("/exec/changepass.jsp?key=" + key); 77 String content = "To change your password go to <a HREF=\"" + url + 78 "\">" + url + "</a>"; 79 80 Mail.getInstance().sendMail(receiver, subject, content); 81 } else { 82 request.setAttribute("error", "No email, please contact the SnipSnap administrator!"); 83 RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/forgot.jsp"); 84 dispatcher.forward(request, response); 85 return; 86 } 87 } else { 88 Configuration config = Application.get().getConfiguration(); 89 response.sendRedirect(config.getUrl("/space/"+config.getStartSnip())); 90 return; 91 } 92 93 94 request.setAttribute("error", "Check your inbox. You should receive an email with" 95 + " instructions soon if you registered with an Email."); 96 RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/forgot.jsp"); 97 dispatcher.forward(request, response); 98 } 99 } 100 | Popular Tags |