Пример #1
0
  private static void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
    FullHttpResponse response =
        new DefaultFullHttpResponse(
            HttpVersion.HTTP_1_1,
            status,
            Unpooled.copiedBuffer("Failure: " + status.toString() + "\r\n", CharsetUtil.UTF_8));
    response.headers().set(HttpHeaderConstants.CONTENT_TYPE, "text/plain; charset=UTF-8");

    // Close the connection as soon as the error message is sent.
    ctx.write(response).addListener(ChannelFutureListener.CLOSE);
  }
Пример #2
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()));
    }
  }
Пример #3
0
    public void messageReceived(ChannelHandlerContext ctx, Object o) throws Exception {

      if (o instanceof HttpRequest) { // DefaultHttpRequest ) {
        queue.add((HttpRequest) o);
      } else if (o instanceof LastHttpContent) {

        HttpRequest req = queue.remove();

        req.getMethod();
        req.getUri();
        req.headers();

        LastHttpContent content = (LastHttpContent) o;

        ByteBuf buf = content.content();

        if (buf.readableBytes() > 0) {
          Gson gson = GsonFactory.createBuilder().create();

          Reader in = new InputStreamReader(new ByteBufInputStream(buf), "utf-8");
          Object v =
              gson.fromJson(in, Class.forName("com.logbook.logbook.resources.logs.LogEntryDTO"));
          System.out.println("v = " + v);
        }

        System.out.println(
            req.getMethod() + " " + req.getUri() + "    -- " + buf.readableBytes() + " bytes");

        HttpResponse r = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        r.headers().add("Content-Length", "0");
        ctx.write(r);

      } else {
        System.out.println("o = " + o + " : " + o.getClass());
      }
    }