Add ActivationCodeServlet

This commit is contained in:
Sofia Baltzi 2017-10-23 13:14:39 +00:00
parent e656c06fae
commit fe8cd8a9b2
1 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,57 @@
package eu.dnetlib.openaire.usermanagement;
import eu.dnetlib.openaire.user.utils.VerificationActions;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by sofia on 23/10/2017.
*/
public class ActivationCodeServlet extends HttpServlet{
public void init(ServletConfig config) throws ServletException {
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
config.getServletContext());
}
@Autowired
private VerificationActions verificationActions;
private Logger logger = Logger.getLogger(ActivationCodeServlet.class);
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();
String formUsername = request.getParameter("username");
String formVerificationCode = request.getParameter("verification_code");
if (verificationActions.verificationEntryExists(formUsername) && verificationActions.verificationCodeIsCorrect(formUsername, formVerificationCode)) {
if (!verificationActions.verificationCodeHasExpired(formUsername)) {
HttpSession session = request.getSession();
session.setAttribute("username", formUsername);
response.sendRedirect("./addPassword.jsp");
} else {
logger.info("Verification code has expired!");
response.sendRedirect("./expiredVerificationCode.jsp");
}
} else {
request.getSession().setAttribute("message", "Username or activation code are not valid.");
response.sendRedirect("./activate.jsp");
}
printWriter.close();
}
}