KickJava   Java API By Example, From Geeks To Geeks.

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


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.snip.SnipLink;
29 import org.snipsnap.user.User;
30 import org.snipsnap.user.UserManager;
31 import org.snipsnap.user.PasswordService;
32 import org.snipsnap.user.UserManagerFactory;
33 import org.snipsnap.container.Components;
34 import org.snipsnap.container.SessionService;
35 import org.snipsnap.config.Configuration;
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 javax.servlet.http.HttpSession JavaDoc;
43 import java.io.IOException JavaDoc;
44
45 /**
46  * Generates a password key to change the password. The key
47  * is mailed to the user.
48  *
49  * @author Stephan J. Schmidt
50  * @version $Id: ChangePasswordServlet.java 1256 2003-12-11 13:24:57Z leo $
51  */

52 public class ChangePasswordServlet extends HttpServlet JavaDoc {
53   private final static String JavaDoc ERR_PASSWORD = "User name and password do not match!";
54
55   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
56       throws ServletException JavaDoc, IOException JavaDoc {
57     Configuration config = Application.get().getConfiguration();
58
59     String JavaDoc password1 = request.getParameter("password");
60     String JavaDoc password2 = request.getParameter("password2");
61     String JavaDoc key = request.getParameter("key");
62
63     if (request.getParameter("cancel") == null) {
64       User user;
65       if (null != password1 && password1.equals(password2)) {
66         PasswordService passwordService = (PasswordService) Components.getComponent(PasswordService.class);
67         user = passwordService.changePassWord(key, password1);
68       } else {
69         request.setAttribute("error", "user.password.error.nomatch");
70         RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher("/exec/changepass.jsp");
71         dispatcher.forward(request, response);
72         return;
73       }
74
75       if (null != user) {
76         if (Application.getCurrentUsers().contains(user)) {
77           Application.getCurrentUsers().remove(user);
78         }
79         HttpSession JavaDoc session = request.getSession();
80         Application.get().setUser(user, session);
81
82         SessionService service = (SessionService) Components.getComponent(SessionService.class);
83         service.setUser(request, response, user);
84         response.sendRedirect(config.getUrl("/space/"+SnipLink.encode(user.getLogin())));
85       } else {
86         request.setAttribute("error", "user.password.error.keymismatch");
87         RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher("/exec/forgot.jsp");
88         dispatcher.forward(request, response);
89       }
90     } else {
91       response.sendRedirect(config.getUrl("/space/"+Application.get().getConfiguration().getStartSnip()));
92     }
93   }
94 }
95
Popular Tags