예제 #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);
    }
  }
예제 #4
0
  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()));
    }
  }
예제 #5
0
  @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));
    }
  }
예제 #6
0
  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);
 }