Ejemplo n.º 1
0
 private static HttpResponse createResponseTemplate(
     HttpResponseStatus status, ChannelBuffer data) {
   HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
   if (data != null) {
     response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, Integer.toString(data.readableBytes()));
     response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "application/octet-stream");
     response.setContent(data);
   } else {
     response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, "0");
     response.setContent(null);
   }
   return response;
 }
Ejemplo n.º 2
0
 public static HttpResponse createRejection(HttpRequest request, String reason) {
   HttpVersion version = request != null ? request.getProtocolVersion() : HttpVersion.HTTP_1_1;
   HttpResponse response = new DefaultHttpResponse(version, HttpResponseStatus.BAD_REQUEST);
   response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=\"utf-8\"");
   ChannelBuffer reasonBuffer = ChannelBuffers.wrappedBuffer(toBytes(reason));
   response.setHeader(
       HttpHeaders.Names.CONTENT_LENGTH, Integer.toString(reasonBuffer.readableBytes()));
   response.setContent(reasonBuffer);
   return response;
 }
Ejemplo n.º 3
0
  private void sendHttpResponse(ChannelHandlerContext ctx, HttpRequest req, HttpResponse res) {
    // Generate an error page if response status code is not OK (200).
    if (res.getStatus().getCode() != 200) {
      res.setContent(ChannelBuffers.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8));
      setContentLength(res, res.getContent().readableBytes());
    }

    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.getChannel().write(res);
    if (!isKeepAlive(req) || res.getStatus().getCode() != 200) {
      f.addListener(ChannelFutureListener.CLOSE);
    }
  }