KickJava   Java API By Example, From Geeks To Geeks.

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


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.radeox.util.logging.Logger;
28 import org.snipsnap.app.Application;
29 import org.snipsnap.config.Configuration;
30 import org.snipsnap.container.Components;
31 import org.snipsnap.container.SessionService;
32 import org.snipsnap.net.filter.MultipartWrapper;
33 import org.snipsnap.snip.HomePage;
34 import org.snipsnap.snip.SnipLink;
35 import org.snipsnap.user.User;
36 import org.snipsnap.user.UserManager;
37 import org.snipsnap.user.UserManagerFactory;
38
39 import javax.servlet.RequestDispatcher JavaDoc;
40 import javax.servlet.ServletException JavaDoc;
41 import javax.servlet.http.HttpServlet JavaDoc;
42 import javax.servlet.http.HttpServletRequest JavaDoc;
43 import javax.servlet.http.HttpServletResponse JavaDoc;
44 import javax.servlet.http.HttpSession JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Map JavaDoc;
48
49 /**
50  * Servlet to register a new user.
51  *
52  * @author Matthias L. Jugel
53  * @version $Id: NewUserServlet.java 1801 2005-01-29 17:26:09Z leo $
54  */

55 public class NewUserServlet extends HttpServlet JavaDoc {
56   private final static String JavaDoc ERR_EXISTS = "login.register.error.user.exists";
57   private final static String JavaDoc ERR_TOO_SHORT = "login.register.error.user.short";
58   private final static String JavaDoc ERR_ILLEGAL = "login.register.error.user.illegal";
59   private final static String JavaDoc ERR_PASSWORD = "login.register.error.passwords";
60   private final static String JavaDoc ERR_PASSWORD_TOO_SHORT = "login.register.error.password.short";
61   private final static String JavaDoc ERR_NOT_ALLOWED = "login.register.error.not.allowed";
62
63   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
64           throws ServletException JavaDoc, IOException JavaDoc {
65     Configuration config = Application.get().getConfiguration();
66
67     // If this is not a multipart/form-data request continue
68
String JavaDoc type = request.getHeader("Content-Type");
69     if (type != null && type.startsWith("multipart/form-data")) {
70       try {
71         request = new MultipartWrapper(request, config.getEncoding() != null ? config.getEncoding() : "UTF-8");
72       } catch (IllegalArgumentException JavaDoc e) {
73         Logger.warn("FileUploadServlet: multipart/form-data wrapper:" + e.getMessage());
74       }
75     }
76
77     HttpSession JavaDoc session = request.getSession();
78     session.removeAttribute("errors");
79     Map JavaDoc errors = new HashMap JavaDoc();
80
81     if (!config.deny(Configuration.APP_PERM_REGISTER)) {
82       String JavaDoc login = request.getParameter("login");
83       String JavaDoc email = request.getParameter("email");
84       String JavaDoc password = request.getParameter("password");
85       String JavaDoc password2 = request.getParameter("password2");
86
87       login = login != null ? login : "";
88       email = email != null ? email : "";
89
90
91       if (request.getParameter("cancel") == null) {
92         UserManager um = UserManagerFactory.getInstance();
93         User user = um.load(login);
94         // check whether user exists or not
95
if (user != null) {
96           errors.put("login", ERR_EXISTS);
97           sendError(session, errors, request, response);
98           return;
99         }
100
101         if (login.length() < 3) {
102           errors.put("login", ERR_TOO_SHORT);
103           sendError(session, errors, request, response);
104           return;
105         }
106
107         login = login.trim();
108
109         // check whether the password is correctly typed
110
if (!password.equals(password2)) {
111           errors.put("password", ERR_PASSWORD);
112           sendError(session, errors, request, response);
113           return;
114         }
115
116         if (password.length() < 3) {
117           errors.put("password", ERR_PASSWORD_TOO_SHORT);
118           sendError(session, errors, request, response);
119           return;
120         }
121
122         // create user ...
123
user = um.create(login, password, email);
124         Application.get().setUser(user, session);
125         HomePage.create(login);
126
127         // store user name and app in cookie and session
128
SessionService sessionService = (SessionService) Components.getComponent(SessionService.class);
129         sessionService.setCookie(request, response, user);
130
131         response.sendRedirect(config.getUrl("/space/" + SnipLink.encode(login)));
132         return;
133       }
134
135       String JavaDoc referer = sanitize(request.getParameter("referer"));
136       response.sendRedirect(referer != null ? referer : config.getUrl("/space/" + config.getStartSnip()));
137     } else {
138       errors.put("Fatal", ERR_NOT_ALLOWED);
139       sendError(session, errors, request, response);
140     }
141   }
142
143   private void sendError(HttpSession JavaDoc session, Map JavaDoc errors, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
144           throws ServletException JavaDoc, IOException JavaDoc {
145     session.setAttribute("errors", errors);
146     RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher("/exec/register.jsp");
147     dispatcher.forward(request, response);
148   }
149
150   private String JavaDoc sanitize(String JavaDoc parameter) {
151     if (null != parameter) {
152       return parameter.split("[\r\n]")[0];
153     }
154     return parameter;
155   }
156 }
157
Popular Tags