/* (non-Javadoc)
  * @see com.kanchan.java.designpatterns.chainofresponsibilities.RequestHandler#processRequest()
  */
 @Override
 public void processRequest(RequestObject requestObject) {
   if (null != requestObject && requestObject.getRequestName().equalsIgnoreCase("RequestThree")) {
     System.out.println(ConcreteHandlerThree.class.getSimpleName() + " Is Handling the request");
   } else {
     System.out.println(
         "Passing request to the handler " + getRequestHandler().getClass().getSimpleName());
     getRequestHandler().processRequest(requestObject);
   }
 }
  private void handleRequest(RequestObject reqObj) {
    final Path path = reqObj.getPath();
    final HttpServerConnection conn = reqObj.getConn();
    final HttpRequest req = reqObj.getRequest();

    WebServerLog.log(this, "Request handler got a request:\n" + req.toString());

    if (!req.getRequestLine().getMethod().toUpperCase().equals("GET")) {
      return;
    }

    final HttpResponse response =
        new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, ReasonPhrases.OK);

    boolean applyFilters;
    applyFilters = handleFileRequest(path, response);

    if (applyFilters) {
      filterChain.reset(path.toString());
      filterChain.doFilter(reqObj.getRequest(), response);
    }
    try {
      proc.process(response, new BasicHttpContext());
      addContentType(path, response);
    } catch (HttpException | IOException e1) {
      WebServerLog.log(this, "Request handler failed to 'process' response after filtering");
      e1.printStackTrace();
    }
    WebServerLog.log(this, "Request handler is sending a response:\n" + response.toString());

    try {
      conn.sendResponseHeader(response);
      conn.sendResponseEntity(response);
    } catch (HttpException | IOException e) {
      WebServerLog.log(this, "Request handler has encountered error while sending response");
      e.printStackTrace();
    }
  }
  @Override
  public void run() {

    buildProcessor();

    WebServerLog.log(this, "Request handler has started running");
    RequestObject reqObj = null;
    while (true) {
      try {
        reqObj = requestsQueue.take();

        // timer.schedule(new MyTimerTask(Thread.currentThread()),
        // // 5 * 60 * 1000);
        // 5 * 1000);

        WebServerLog.log(this, "Request handler got a new request");

        handleRequest(reqObj);

        reqObj.getConn().shutdown();
      } catch (final InterruptedException e) {
        WebServerLog.log(this, "Request handler was interrupted");
        System.err.println("Request handler was interrupted");
        try {
          reqObj.getConn().shutdown();
        } catch (final IOException e1) {
          System.err.println("Request handler failed to shutdown connection");
          e1.printStackTrace();
        }
        e.printStackTrace();
      } catch (final IOException e) {
        WebServerLog.log(this, "Request handler failed to close connection");
        System.err.println("Request handler failed to close connection");
        e.printStackTrace();
      }
    }
  }