/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package asistencia.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.json.JSONObject;
import asistencia.config.RequestPath;
import asistencia.utilities.HttpRequest;

/**
 *
 * @author sistem19user
 */
public class LogoutServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (request.getSession().getAttribute("codigo") != null && !request.getSession().getAttribute("codigo").toString().trim().equals("")) {
            HttpSession session = request.getSession();
            String authorization = (String) session.getAttribute("Authorization");
            Cookie cookieAuth = null;
            HttpRequest postRequest = new HttpRequest();
            String respuesta = postRequest.getRespuesta(RequestPath.LOG_OUT, HttpRequest.POST, new JSONObject("{}"), authorization);
            cookieAuth = new Cookie("Authorization", "");
            cookieAuth.setMaxAge(0);
            response.addCookie(cookieAuth);
            request.getSession().removeAttribute("codigo");//
            request.getSession().removeAttribute("Authorization");
            request.getSession().removeAttribute("menu");
            session.invalidate();
            response.sendRedirect("index.jsp");
        } else {
            response.sendRedirect("index.jsp");
        }
    }

}
