package com.fp.frontend.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fp.frontend.controller.security.LoginController;

/**
 * Clase que se encarga de convertir cerrar la session del usuario al monento de
 * cerrar el browser..
 *
 * @author Jorge Vaca.
 * @version 2.1
 */
@WebServlet(name = "closeSessionMaia", urlPatterns = {"/closeSessionMaia"})
public class CloseSessionMaia extends HttpServlet {

    private static final long serialVersionUID = 1L;

    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            LoginController loginController = (LoginController) request.getSession().getAttribute("loginController");
            if (loginController != null) {
                loginController.updateusesession();
                request.getSession().invalidate();
                loginController = null;
            }
//            loginController.logout();
        } catch (Exception e) {
            // no hace nada la session esta cerrada.
            e.printStackTrace();
        }
    }
}
