示例#1
0
 @Override
 public final boolean trySend(WebDataMessage message) {
   if (!message.isBinary()) ctx.writeAndFlush(new TextWebSocketFrame(message.getStringBody()));
   else
     ctx.writeAndFlush(
         new BinaryWebSocketFrame(Unpooled.wrappedBuffer(message.getByteBufferBody())));
   return true;
 }
示例#2
0
 @Override
 public final boolean trySend(WebDataMessage res) {
   final ByteBuf buf;
   final String stringBody = res.getStringBody();
   if (stringBody != null) {
     byte[] bs = stringBody.getBytes(encoding);
     buf = Unpooled.wrappedBuffer(bs);
   } else {
     buf = Unpooled.wrappedBuffer(res.getByteBufferBody());
   }
   ctx.writeAndFlush(buf);
   return true;
 }
示例#3
0
  private static void writeHttpResponse(
      ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res, Boolean close) {
    if (!omitDateHeader && !res.headers().contains(DefaultHttpHeaders.Names.DATE))
      DefaultHttpHeaders.addDateHeader(res, DefaultHttpHeaders.Names.DATE, new Date());

    // Send the response and close the connection if necessary.
    if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200 || close == null || close) {
      res.headers().set(CONNECTION, HttpHeaders.Values.CLOSE);
      ctx.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE);
    } else {
      res.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
      write(ctx, res);
    }
  }
  private void processGet(ChannelHandlerContext context, FullHttpRequest request) {
    try {
      File file = getRequestedFile(request.getUri());

      RandomAccessFile raf = new RandomAccessFile(file, "r");
      long fileLength = raf.length();

      HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
      HttpHeaders.setContentLength(response, fileLength);
      setContentTypeHeader(response, file);

      context.write(response);

      context.write(new DefaultFileRegion(raf.getChannel(), 0, fileLength));

      // Write the end marker.
      ChannelFuture future = context.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
      future.addListener(ChannelFutureListener.CLOSE);

    } catch (IOException | URISyntaxException e) {
      context.writeAndFlush(getBadRequest(e.getMessage()));
    }
  }
  @Override
  protected void channelRead0(ChannelHandlerContext context, FullHttpRequest request)
      throws Exception {

    if (request.getMethod().equals(HttpMethod.HEAD)) {

      processHead(context, request);

    } else if (request.getMethod().equals(HttpMethod.GET)) {

      processGet(context, request);

    } else {
      // error
      String msg = "Not supported method: " + request.getMethod();
      LOG.error(msg);
      context.writeAndFlush(getBadRequest(msg));
    }
  }
  private void processHead(ChannelHandlerContext context, FullHttpRequest request) {
    HttpHeaders headers = request.headers();
    FullHttpResponse response = null;

    if (headers.contains(Names.CONTENT_LENGTH)) {

      try {
        File file = getRequestedFile(request.getUri());

        response =
            new DefaultFullHttpResponse(
                HTTP_1_1, request.getDecoderResult().isSuccess() ? OK : BAD_REQUEST);

        HttpHeaders.setContentLength(response, file.length());

      } catch (FileNotFoundException | URISyntaxException e) {
        response = getBadRequest(e.getMessage());
      }
    }

    context.writeAndFlush(response);
  }
示例#7
0
 private static ChannelFuture write(ChannelHandlerContext ctx, Object res) {
   return ctx.writeAndFlush(res);
   // : ctx.write(res);
 }