public void exception(NHttpServerConnection conn, Exception ex) {
    if (ex instanceof IOException) {
      logIOException(conn, (IOException) ex);

      metrics.incrementFaultsReceiving();

      ProtocolState state = SourceContext.getState(conn);
      if (state == ProtocolState.REQUEST_BODY || state == ProtocolState.REQUEST_HEAD) {
        informReaderError(conn);
      } else if (state == ProtocolState.RESPONSE_BODY || state == ProtocolState.RESPONSE_HEAD) {
        informWriterError(conn);
      } else if (state == ProtocolState.REQUEST_DONE) {
        informWriterError(conn);
      } else if (state == ProtocolState.RESPONSE_DONE) {
        informWriterError(conn);
      }

      SourceContext.updateState(conn, ProtocolState.CLOSED);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    } else if (ex instanceof HttpException) {
      try {
        if (conn.isResponseSubmitted()) {
          sourceConfiguration.getSourceConnections().shutDownConnection(conn);
          return;
        }
        HttpContext httpContext = conn.getContext();

        HttpResponse response =
            new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "Bad request");
        response.setParams(
            new DefaultedHttpParams(sourceConfiguration.getHttpParams(), response.getParams()));
        response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);

        // Pre-process HTTP request
        httpContext.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
        httpContext.setAttribute(ExecutionContext.HTTP_REQUEST, null);
        httpContext.setAttribute(ExecutionContext.HTTP_RESPONSE, response);

        sourceConfiguration.getHttpProcessor().process(response, httpContext);

        conn.submitResponse(response);
        SourceContext.updateState(conn, ProtocolState.CLOSED);
        conn.close();
      } catch (Exception ex1) {
        log.error(ex.getMessage(), ex);
        SourceContext.updateState(conn, ProtocolState.CLOSED);
        sourceConfiguration.getSourceConnections().shutDownConnection(conn);
      }
    } else {
      log.error("Unexoected error: " + ex.getMessage(), ex);
      SourceContext.updateState(conn, ProtocolState.CLOSED);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    }
  }
 /**
  * Commit the response to the connection. Processes the response through the configured
  * HttpProcessor and submits it to be sent out. This method hides any exceptions and is targetted
  * for non critical (i.e. browser requests etc) requests, which are not core messages
  *
  * @param conn the connection being processed
  * @param response the response to commit over the connection
  */
 public void commitResponseHideExceptions(
     final NHttpServerConnection conn, final HttpResponse response) {
   try {
     conn.suspendInput();
     sourceConfiguration.getHttpProcessor().process(response, conn.getContext());
     conn.submitResponse(response);
   } catch (HttpException e) {
     handleException("Unexpected HTTP protocol error : " + e.getMessage(), e, conn);
   } catch (IOException e) {
     handleException("IO error submiting response : " + e.getMessage(), e, conn);
   }
 }