KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > MailPasswordKeyServlet


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
38 import javax.servlet.ServletException JavaDoc;
39 import javax.servlet.http.HttpServlet JavaDoc;
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 /**
45  * Generates a password key to change the password. The key
46  * is mailed to the user.
47  *
48  * @author Stephan J. Schmidt
49  * @version $Id: MailPasswordKeyServlet.java 1606 2004-05-17 10:56:18Z leo $
50  */

51 public class MailPasswordKeyServlet extends HttpServlet JavaDoc {
52   private final static String JavaDoc ERR_PASSWORD = "User name and password do not match!";
53
54   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
55     throws ServletException JavaDoc, IOException JavaDoc {
56
57     String JavaDoc 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 JavaDoc 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 JavaDoc key = passwordService.getPassWordKey(user);
72       Configuration configuration = Application.get().getConfiguration();
73       String JavaDoc receiver = user.getEmail();
74       if (receiver != null && receiver.length() > 0) {
75         String JavaDoc subject = "Forgotten password";
76         String JavaDoc url = configuration.getUrl("/exec/changepass.jsp?key=" + key);
77         String JavaDoc 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 JavaDoc 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 JavaDoc dispatcher = request.getRequestDispatcher("/exec/forgot.jsp");
97     dispatcher.forward(request, response);
98   }
99 }
100
Popular Tags