package p2.webclient.servlet;

import java.io.IOException;

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 p2.webclient.system.controller.RequestHandler;

@SuppressWarnings("serial")
public class WebClientServlet extends HttpServlet {

    @Override
    protected final void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        a(request, response);
    }

    @Override
    protected final void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        a(request, response);

    }

    private void a(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String str;
        if ((str = getInitParameter("char_encoding")) != null) {
            request.setCharacterEncoding(str);
        } else {
            str = request.getCharacterEncoding();
        }
        HttpSession session;
        if ((session = request.getSession()).getAttribute("handlingrequest") != null) {
            return;
        }
        response.setHeader("Pragma", "No-cache");
        response.setDateHeader("Expires", 0L);
        response.setHeader("Cache-Control", "no-cache");
        if ((str != null) && (!"".equals(str)) && (!"null".equalsIgnoreCase(str))) {
            str = ";charset=" + str;
        } else {
            str = "";
        }
        response.setContentType("text/html" + str);
        session.setAttribute("handlingrequest", "true");
        try {
            RequestHandler.handleRequest(request, response);
        } catch (Exception throwable) {
            throwable.printStackTrace();
        }
        session.removeAttribute("handlingrequest");
    }
}