/**
   * Determine the HttpStatusCodedepending on the message type processed <br>
   * (normal response versus fault response) as well as Axis2 message context properties set via
   * Synapse configuration or MessageBuilders.
   *
   * @see org.apache.synapse.transport.nhttp.NhttpConstants#FAULTS_AS_HTTP_200
   * @see org.apache.synapse.transport.nhttp.NhttpConstants#HTTP_SC
   * @param msgContext the Axis2 message context
   * @param response the HTTP response object
   * @return the HTTP status code to set in the HTTP response object
   */
  private int determineHttpStatusCode(MessageContext msgContext, HttpResponse response) {

    int httpStatus = HttpStatus.SC_OK;

    // retrieve original status code (if present)
    if (response.getStatusLine() != null) {
      httpStatus = response.getStatusLine().getStatusCode();
    }

    // if this is a dummy message to handle http 202 case with non-blocking IO
    // set the status code to 202
    if (msgContext.isPropertyTrue(NhttpConstants.SC_ACCEPTED)) {
      httpStatus = HttpStatus.SC_ACCEPTED;
    } else {

      // is this a fault message
      boolean handleFault =
          msgContext.getEnvelope().getBody().hasFault() || msgContext.isProcessingFault();

      // shall faults be transmitted with HTTP 200
      boolean faultsAsHttp200 =
          NhttpConstants.TRUE.equalsIgnoreCase(
              (String) msgContext.getProperty(NhttpConstants.FAULTS_AS_HTTP_200));

      // Set HTTP status code to 500 if this is a fault case and we shall not use HTTP 200
      if (handleFault && !faultsAsHttp200) {
        httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
      } else if (handleFault & faultsAsHttp200) {
        return HttpStatus.SC_OK;
      }

      /*
       * Any status code previously set shall be overwritten with the value of the following
       * message context property if it is set.
       */
      Object statusCode = msgContext.getProperty(NhttpConstants.HTTP_SC);
      if (statusCode != null) {
        try {
          httpStatus = Integer.parseInt(msgContext.getProperty(NhttpConstants.HTTP_SC).toString());
        } catch (NumberFormatException e) {
          log.warn(
              "Unable to set the HTTP status code from the property "
                  + NhttpConstants.HTTP_SC
                  + " with value: "
                  + statusCode);
        }
      }
    }

    return httpStatus;
  }