Sends an HTTP reply to the client. @param status The status of the request (e.g. 200 OK or 404 Not Found). @param buf The content of the reply to send.
(final HttpResponseStatus status,
final ChannelBuffer buf,
final String contentType)
| 432 | * @param buf The content of the reply to send. |
| 433 | */ |
| 434 | public void sendBuffer(final HttpResponseStatus status, |
| 435 | final ChannelBuffer buf, |
| 436 | final String contentType) { |
| 437 | if (!chan.isConnected()) { |
| 438 | if(stats != null) { |
| 439 | stats.markSendFailed(); |
| 440 | } |
| 441 | done(); |
| 442 | return; |
| 443 | } |
| 444 | response.headers().set(HttpHeaders.Names.CONTENT_TYPE, contentType); |
| 445 | |
| 446 | // TODO(tsuna): Server, X-Backend, etc. headers. |
| 447 | // only reset the status if we have the default status, otherwise the user |
| 448 | // already set it |
| 449 | response.setStatus(status); |
| 450 | response.setContent(buf); |
| 451 | final boolean keepalive = HttpHeaders.isKeepAlive(request); |
| 452 | if (keepalive) { |
| 453 | HttpHeaders.setContentLength(response, buf.readableBytes()); |
| 454 | } |
| 455 | final ChannelFuture future = chan.write(response); |
| 456 | if (stats != null) { |
| 457 | future.addListener(new SendSuccess()); |
| 458 | } |
| 459 | if (!keepalive) { |
| 460 | future.addListener(ChannelFutureListener.CLOSE); |
| 461 | } |
| 462 | done(); |
| 463 | } |
| 464 | |
| 465 | /** A simple class that marks a query as complete when the stats are set */ |
| 466 | private class SendSuccess implements ChannelFutureListener { |
no test coverage detected