90 lines
3.6 KiB
Java
90 lines
3.6 KiB
Java
|
|
package zall.page;
|
||
|
|
|
||
|
|
import zall.ZalleryServlet;
|
||
|
|
import zall.bean.User;
|
||
|
|
import zall.util.ZalleryEmail;
|
||
|
|
import zall.util.UserMessage;
|
||
|
|
import zutil.db.DBConnection;
|
||
|
|
import zutil.log.LogUtil;
|
||
|
|
|
||
|
|
import javax.servlet.ServletException;
|
||
|
|
import javax.servlet.annotation.WebServlet;
|
||
|
|
import javax.servlet.http.*;
|
||
|
|
import java.io.IOException;
|
||
|
|
import java.sql.SQLException;
|
||
|
|
import java.util.Locale;
|
||
|
|
import java.util.PropertyResourceBundle;
|
||
|
|
import java.util.ResourceBundle;
|
||
|
|
import java.util.logging.Logger;
|
||
|
|
|
||
|
|
import static zall.ZalleryConstant.LANG_BASENAME;
|
||
|
|
|
||
|
|
@WebServlet("/register")
|
||
|
|
public class RegisterServlet extends ZalleryServlet {
|
||
|
|
private static Logger logger = LogUtil.getLogger();
|
||
|
|
|
||
|
|
private static final String JSP_FILE = "register.jsp";
|
||
|
|
|
||
|
|
private static final String INPUT_ID_FIRST_NAME = "first-name";
|
||
|
|
private static final String INPUT_ID_LAST_NAME = "last-name";
|
||
|
|
private static final String INPUT_ID_EMAIL = "email";
|
||
|
|
private static final String INPUT_ID_PASSWORD = "password";
|
||
|
|
private static final String INPUT_ID_PASSWORD_REPEATE = "password-re";
|
||
|
|
|
||
|
|
|
||
|
|
public void doGet(HttpServletRequest request, HttpServletResponse response, DBConnection db) throws ServletException, IOException {
|
||
|
|
include(JSP_FILE, request, response);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
public void doPost(HttpServletRequest request, HttpServletResponse response, DBConnection db) throws SQLException, IOException, ServletException {
|
||
|
|
UserMessage msgHandler = UserMessage.getUserMessage(request.getSession());
|
||
|
|
ResourceBundle lang = PropertyResourceBundle.getBundle(LANG_BASENAME, Locale.getDefault());
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Check inputs
|
||
|
|
*/
|
||
|
|
|
||
|
|
String firstName = (String)request.getAttribute(INPUT_ID_FIRST_NAME);
|
||
|
|
String lastName = (String)request.getAttribute(INPUT_ID_LAST_NAME);
|
||
|
|
String email = (String)request.getAttribute(INPUT_ID_EMAIL);
|
||
|
|
String password = (String)request.getAttribute(INPUT_ID_PASSWORD);
|
||
|
|
String passwordRe = (String)request.getAttribute(INPUT_ID_PASSWORD_REPEATE);
|
||
|
|
|
||
|
|
if (firstName == null)
|
||
|
|
msgHandler.add(UserMessage.MessageType.ERROR, lang.getString("error.first_name_missing"));
|
||
|
|
if (lastName == null)
|
||
|
|
msgHandler.add(UserMessage.MessageType.ERROR, lang.getString("error.last_name_missing"));
|
||
|
|
if (email == null)
|
||
|
|
msgHandler.add(UserMessage.MessageType.ERROR, lang.getString("error.email_missing"));
|
||
|
|
if (password == null)
|
||
|
|
msgHandler.add(UserMessage.MessageType.ERROR, lang.getString("error.password_missing"));
|
||
|
|
if (passwordRe == null)
|
||
|
|
msgHandler.add(UserMessage.MessageType.ERROR, lang.getString("error.password_missing"));
|
||
|
|
else if (passwordRe.equals(password))
|
||
|
|
msgHandler.add(UserMessage.MessageType.ERROR, lang.getString("error.password_not_matching"));
|
||
|
|
|
||
|
|
if(User.load(db, (String)request.getAttribute(INPUT_ID_EMAIL)) != null){
|
||
|
|
msgHandler.add(UserMessage.MessageType.ERROR, lang.getString("error.email_exsists"));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
* Create new user
|
||
|
|
*/
|
||
|
|
|
||
|
|
User user = new User();
|
||
|
|
user.setName(firstName + " " + lastName);
|
||
|
|
user.setEmail(email);
|
||
|
|
user.setPassword(password);
|
||
|
|
user.save(db);
|
||
|
|
ZalleryEmail.sendVerificationEmail( user );
|
||
|
|
request.getSession().setAttribute("user", user);
|
||
|
|
logger.info("Registered new user: "+user.getName()+".");
|
||
|
|
msgHandler.add(UserMessage.MessageType.INFO,
|
||
|
|
lang.getString("info.account_created") + lang.getString("info.verification_email_sent"));
|
||
|
|
|
||
|
|
include(JSP_FILE, request, response);
|
||
|
|
}
|
||
|
|
}
|