Project

General

Profile

1
package eu.dnetlib.openaire.usermanagement;
2

    
3
import eu.dnetlib.openaire.user.utils.VerificationActions;
4
import eu.dnetlib.openaire.user.utils.InputValidator;
5
import eu.dnetlib.openaire.usermanagement.utils.UrlConstructor;
6
import org.apache.log4j.Logger;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
9

    
10
import javax.servlet.ServletConfig;
11
import javax.servlet.ServletException;
12
import javax.servlet.http.HttpServlet;
13
import javax.servlet.http.HttpServletRequest;
14
import javax.servlet.http.HttpServletResponse;
15
import javax.servlet.http.HttpSession;
16
import java.io.IOException;
17
import java.io.PrintWriter;
18

    
19
/**
20
 * Created by kiatrop on 28/9/2017.
21
 */
22
public class VerificationCodeServlet extends HttpServlet {
23

    
24
    public void init(ServletConfig config) throws ServletException {
25
        super.init(config);
26
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
27
                config.getServletContext());
28
    }
29

    
30
    @Autowired
31
    private VerificationActions verificationActions;
32

    
33
    private Logger logger = Logger.getLogger(VerificationCodeServlet.class);
34

    
35
    @Override
36
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
37
        response.setContentType("text/html");
38
        PrintWriter printWriter = response.getWriter();
39

    
40
        String formUsername = request.getParameter("username").trim();
41
        String formVerificationCode = request.getParameter("verification_code").trim();
42

    
43
        if (InputValidator.isFilled(formUsername) && InputValidator.isFilled(formVerificationCode)) {
44
            if (verificationActions.verificationEntryExists(formUsername) && verificationActions.verificationCodeIsCorrect(formUsername, formVerificationCode)) {
45
                if (!verificationActions.verificationCodeHasExpired(formUsername)) {
46
                    HttpSession session = request.getSession();
47
                    session.setAttribute("username", formUsername);
48
                    response.sendRedirect("./resetPassword.jsp");
49
                } else {
50
                    logger.info("Verification code has expired!");
51
                    response.sendRedirect(UrlConstructor.getRedirectUrl(request, "expiredVerificationCode.jsp"));
52
                }
53
            } else {
54
                logger.info("Username or verification code are not valid!");
55
                request.getSession().setAttribute("message", "Username or verification code are not valid.");
56
                response.sendRedirect("./verify.jsp");
57
            }
58
        } else {
59
            if (!InputValidator.isFilled(formUsername)) {
60
                logger.info("No username");
61
                request.getSession().setAttribute("msg_username_error", "Please enter your username.");
62
            }
63
            if (!InputValidator.isFilled(formVerificationCode)) {
64
                logger.info("No verification code");
65
                request.getSession().setAttribute("msg_verification_code_error", "Please enter your verification code.");
66
            }
67
            response.sendRedirect("./verify.jsp");
68
            }
69

    
70
        printWriter.close();
71
    }
72
}
(9-9/10)