///*
// * 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.filters;
//
//import java.io.IOException;
//import java.io.PrintStream;
//import java.io.PrintWriter;
//import java.io.StringWriter;
//import java.util.ArrayList;
//import java.util.List;
//import javax.servlet.Filter;
//import javax.servlet.FilterChain;
//import javax.servlet.FilterConfig;
//import javax.servlet.ServletException;
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.Cookie;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import javax.ws.rs.WebApplicationException;
//import javax.ws.rs.core.Response;
//import org.json.JSONArray;
//import org.json.JSONObject;
//import asistencia.config.RequestPath;
//import asistencia.utilities.GeneralUtils;
//import asistencia.utilities.HttpRequest;
//
///**
// *
// * @author sistem08user
// */
//@WebFilter(filterName = "RequestFilter", urlPatterns = {"/*"})
//public class RequestFilter implements Filter {
//
//    // The filter configuration object we are associated with.  If
//    // this value is null, this filter instance is not currently
//    // configured. 
//    private FilterConfig filterConfig = null;
//
//    public RequestFilter() {
//    }
//
//    private void doBeforeProcessing(ServletRequest req, ServletResponse resp, FilterChain chain)
//            throws IOException, ServletException {
//        HttpServletRequest request = (HttpServletRequest) req;
//        HttpServletResponse response = (HttpServletResponse) resp;
//        String uri = request.getRequestURI();
//
//        if (uri.endsWith("Asistencia") || uri.endsWith("Asistencia/")
//                || uri.contains("vistas/index.jsp")
//                || uri.contains("plantilla/assets") || uri.endsWith(".js") || uri.endsWith(".css")
//                || uri.endsWith(".png") || uri.endsWith(".jpg") || uri.endsWith(".gif")
//                || uri.contains("/login") || uri.contains("vistas/redireccionarServlet")
//                || uri.contains("vistas/interceptar")) {
//            chain.doFilter(request, response);
//        } else {
//            if (request.getSession().getAttribute("codigo") != null) {
//                if (uri.endsWith(".jsp")) {
//                    HttpSession session = request.getSession();
//                    HttpRequest httpRequest = new HttpRequest();
//                    JSONObject valid = null;
//                    String respuesta = "";
//                    String auth = "";
//                    try {
//                        if (session.getAttribute("Authorization") != null) {
//                            auth = (String) session.getAttribute("Authorization");
//                        } else {
//                            setError(response);
//                        }
//                        respuesta = httpRequest.getRespuesta(RequestPath.VERIFICAR_LOGIN, HttpRequest.POST, new JSONObject("{}"), auth);
//                        valid = new JSONObject(respuesta);
//                        if (valid.getBoolean("status")) {
//                            JSONObject menu = new JSONObject(valid.getString("menu"));//Obtiene el menu
//                            List<Object> vistas = new ArrayList<>();
//                            JSONObject rolvista = valid.getJSONObject("rolvista");
//                            JSONArray urls = rolvista.getJSONArray("vistas");
//                            for (int i = 0; i < urls.length(); i++) {
//                                vistas.add(urls.get(i));
//                            }
//                            vistas.add("main.jsp");
//                            String ruta = request.getRequestURI();
//                            int indice = GeneralUtils.obtenerIndex(ruta);
//                            String rutaJsp = ruta.substring(indice, ruta.length());
//                            boolean acceso = vistas.contains(rutaJsp);
//                            if (!acceso) {
//                                request.getSession().setAttribute("error", "no tiene acceso a la vista solicitada");//Esta session se elimina en el jsp (para que no ocupe memoria)
////                               request.getRequestDispatcher("/vistas/error.jsp").forward(request, response);
//                                response.sendRedirect("error/error.jsp");
//                                return;
//                            }
//                            session.setAttribute("menu", menu.toString());
//                        } else {
//                            deleteCredenciales(response, request);
//                            request.getSession().setAttribute("error", "no tiene credenciales validas");
////                           request.getRequestDispatcher("/vistas/error.jsp").forward(request, response);
//                            response.sendRedirect("error/error.jsp");
//                            return;
////                            setError(response);
//                        }
//                    } catch (Exception ex) {
//                        request.getSession().setAttribute("error", ex.getMessage());
////                       request.getRequestDispatcher("/vistas/error.jsp").forward(request, response);
//                        response.sendRedirect("error/error.jsp");
//                        return;
////                        setError(response);
//                    }
//                }
//                chain.doFilter(request, response);
//            } else {
//                deleteCredenciales(response, request);
//                response.sendRedirect("index.jsp");
//            }
//        }
//    }
//
//    private void deleteCredenciales(HttpServletResponse response, HttpServletRequest request) {
//        Cookie cookieAuth = new Cookie("Authorization", "");
//        cookieAuth.setMaxAge(0);
//        request.getSession().invalidate();
//        response.addCookie(cookieAuth);
//    }
//
//    private void sendError(HttpServletResponse response) throws IOException {
//        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//        response.sendError(401);
//    }
//
//    public void setError(HttpServletResponse response) throws IOException {
//        Response.ResponseBuilder builder = null;
//        sendError(response);
//        builder = Response.status(Response.Status.UNAUTHORIZED).entity(response);
//        throw new WebApplicationException(builder.build());
//    }
//
//    private void doAfterProcessing(ServletRequest request, ServletResponse response)
//            throws IOException, ServletException {
//
//    }
//
//    public void doFilter(ServletRequest req, ServletResponse resp,
//            FilterChain chain)
//            throws IOException, ServletException {
//        System.out.println("Entrando al filtro");
//        doBeforeProcessing(req, resp, chain);
//
////        chain.doFilter(req, resp);
//        doAfterProcessing(req, resp);
//
//    }
//
//    /**
//     * Return the filter configuration object for this filter.
//     */
//    public FilterConfig getFilterConfig() {
//        return (this.filterConfig);
//    }
//
//    /**
//     * Set the filter configuration object for this filter.
//     *
//     * @param filterConfig The filter configuration object
//     */
//    public void setFilterConfig(FilterConfig filterConfig) {
//        this.filterConfig = filterConfig;
//    }
//
//    /**
//     * Destroy method for this filter
//     */
//    public void destroy() {
//    }
//
//    /**
//     * Init method for this filter
//     */
//    public void init(FilterConfig filterConfig) {
//
//    }
//
//    private void sendProcessingError(Throwable t, ServletResponse response) {
//        String stackTrace = getStackTrace(t);
//
//        if (stackTrace != null && !stackTrace.equals("")) {
//            try {
//                response.setContentType("text/html");
//                PrintStream ps = new PrintStream(response.getOutputStream());
//                PrintWriter pw = new PrintWriter(ps);
//                pw.print("<html>\n<head>\n<title>Error</title>\n</head>\n<body>\n"); //NOI18N
//
//                // PENDING! Localize this for next official release
//                pw.print("<h1>The resource did not process correctly</h1>\n<pre>\n");
//                pw.print(stackTrace);
//                pw.print("</pre></body>\n</html>"); //NOI18N
//                pw.close();
//                ps.close();
//                response.getOutputStream().close();
//            } catch (Exception ex) {
//            }
//        } else {
//            try {
//                PrintStream ps = new PrintStream(response.getOutputStream());
//                t.printStackTrace(ps);
//                ps.close();
//                response.getOutputStream().close();
//            } catch (Exception ex) {
//            }
//        }
//    }
//
//    public static String getStackTrace(Throwable t) {
//        String stackTrace = null;
//        try {
//            StringWriter sw = new StringWriter();
//            PrintWriter pw = new PrintWriter(sw);
//            t.printStackTrace(pw);
//            pw.close();
//            sw.close();
//            stackTrace = sw.getBuffer().toString();
//        } catch (Exception ex) {
//        }
//        return stackTrace;
//    }
//
//    public void log(String msg) {
//        filterConfig.getServletContext().log(msg);
//    }
//
//}
