1 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 ; 40 import javax.servlet.ServletException ; 41 import javax.servlet.http.HttpServlet ; 42 import javax.servlet.http.HttpServletRequest ; 43 import javax.servlet.http.HttpServletResponse ; 44 import javax.servlet.http.HttpSession ; 45 import java.io.IOException ; 46 import java.util.HashMap ; 47 import java.util.Map ; 48 49 55 public class NewUserServlet extends HttpServlet { 56 private final static String ERR_EXISTS = "login.register.error.user.exists"; 57 private final static String ERR_TOO_SHORT = "login.register.error.user.short"; 58 private final static String ERR_ILLEGAL = "login.register.error.user.illegal"; 59 private final static String ERR_PASSWORD = "login.register.error.passwords"; 60 private final static String ERR_PASSWORD_TOO_SHORT = "login.register.error.password.short"; 61 private final static String ERR_NOT_ALLOWED = "login.register.error.not.allowed"; 62 63 public void doPost(HttpServletRequest request, HttpServletResponse response) 64 throws ServletException , IOException { 65 Configuration config = Application.get().getConfiguration(); 66 67 String 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 e) { 73 Logger.warn("FileUploadServlet: multipart/form-data wrapper:" + e.getMessage()); 74 } 75 } 76 77 HttpSession session = request.getSession(); 78 session.removeAttribute("errors"); 79 Map errors = new HashMap (); 80 81 if (!config.deny(Configuration.APP_PERM_REGISTER)) { 82 String login = request.getParameter("login"); 83 String email = request.getParameter("email"); 84 String password = request.getParameter("password"); 85 String 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 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 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 user = um.create(login, password, email); 124 Application.get().setUser(user, session); 125 HomePage.create(login); 126 127 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 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 session, Map errors, HttpServletRequest request, HttpServletResponse response) 144 throws ServletException , IOException { 145 session.setAttribute("errors", errors); 146 RequestDispatcher dispatcher = request.getRequestDispatcher("/exec/register.jsp"); 147 dispatcher.forward(request, response); 148 } 149 150 private String sanitize(String parameter) { 151 if (null != parameter) { 152 return parameter.split("[\r\n]")[0]; 153 } 154 return parameter; 155 } 156 } 157 | Popular Tags |