private void sendResponse(
      ChannelHandlerContext channel,
      HttpRequest request,
      String messageBody,
      HttpResponseStatus status) {

    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);

    if (messageBody != null && !messageBody.isEmpty()) {
      response.setContent(ChannelBuffers.copiedBuffer(messageBody, Constants.DEFAULT_CHARSET));
    }

    Tracker.getInstance().trackResponse(request, response);
    HttpResponder.respond(channel, request, response);
  }
Пример #2
0
 /** @return {@link InputStream} from a {@link HttpResponse} */
 InputStream getStream(HttpResponse response) {
   try {
     return response.getEntity().getContent();
   } catch (Exception e) {
     log.error("Error reading response. " + e.getMessage());
     throw new CouchDbException(e);
   }
 }
Пример #3
0
 /**
  * Validates a HTTP response; on error cases logs status and throws relevant exceptions.
  *
  * @param response The HTTP response.
  */
 private void validate(HttpResponse response) throws IOException {
   int code = response.getStatusLine().getStatusCode();
   if (code == 200 || code == 201 || code == 202) { // success (ok | created | accepted)
     return;
   }
   String msg = format("<< Status: %s (%s) ", code, response.getStatusLine().getReasonPhrase());
   switch (code) {
     case HttpStatus.SC_NOT_FOUND:
       {
         log.info(msg);
         throw new NoDocumentException(msg);
       }
     case HttpStatus.SC_CONFLICT:
       {
         log.warn(msg);
         throw new DocumentConflictException(msg);
       }
     default:
       { // other errors: 400 | 401 | 500 etc.
         log.error(msg += EntityUtils.toString(response.getEntity()));
         throw new CouchDbException(msg);
       }
   }
 }