示例#1
0
  // FIXME: rather than isError, we might was to pass in the status code to give more flexibility
  private void writeResponse(
      HttpResponse resp,
      final String responseText,
      final int statusCode,
      String responseType,
      String reasonPhrase) {
    try {
      resp.setStatusCode(statusCode);
      resp.setReasonPhrase(reasonPhrase);

      BasicHttpEntity body = new BasicHttpEntity();
      if (BaseCmd.RESPONSE_TYPE_JSON.equalsIgnoreCase(responseType)) {
        // JSON response
        body.setContentType(jsonContentType);
        if (responseText == null) {
          body.setContent(
              new ByteArrayInputStream(
                  "{ \"error\" : { \"description\" : \"Internal Server Error\" } }"
                      .getBytes("UTF-8")));
        }
      } else {
        body.setContentType("text/xml");
        if (responseText == null) {
          body.setContent(
              new ByteArrayInputStream("<error>Internal Server Error</error>".getBytes("UTF-8")));
        }
      }

      if (responseText != null) {
        body.setContent(new ByteArrayInputStream(responseText.getBytes("UTF-8")));
      }
      resp.setEntity(body);
    } catch (Exception ex) {
      s_logger.error("error!", ex);
    }
  }