@SuppressWarnings({"unchecked"})
      @Override
      public NextAction handleConnect(FilterChainContext ctx) throws IOException {
        System.out.println("\nClient connected!\n");

        HttpRequestPacket request = createRequest();
        System.out.println("Writing request:\n");
        System.out.println(request.toString());
        ctx.write(request); // write the request

        // for each of the content parts in CONTENT, wrap in a Buffer,
        // create the HttpContent to wrap the buffer and write the
        // content.
        MemoryManager mm = ctx.getMemoryManager();
        for (int i = 0, len = CONTENT.length; i < len; i++) {
          HttpContent.Builder contentBuilder = request.httpContentBuilder();
          Buffer b = Buffers.wrap(mm, CONTENT[i]);
          contentBuilder.content(b);
          HttpContent content = contentBuilder.build();
          System.out.printf("(Client writing: %s)\n", b.toStringContent());
          ctx.write(content);
          try {
            Thread.sleep(2000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }

        // since the request created by createRequest() is chunked,
        // we need to write the trailer to signify the end of the
        // POST data
        ctx.write(request.httpTrailerBuilder().build());

        System.out.println("\n");

        return ctx.getStopAction(); // discontinue filter chain execution
      }
  @Override
  public NextAction handleRead(FilterChainContext ctx) throws IOException {

    final Object message = ctx.getMessage();

    if (_logger.isDebugEnabled()) {
      _logger.debug(String.format("%s: Got [%s] from [%s].", this, message, ctx.getAddress()));
    }

    String responseXML = _coreCommandProcessor.processCommand((String) message);

    if (_logger.isDebugEnabled()) {
      _logger.debug(
          String.format("%s: Returning [%s] to [%s].", this, responseXML, ctx.getAddress()));
    }

    ctx.write(ctx.getAddress(), responseXML, null);

    //  Close the connection
    ctx.getConnection().closeSilently();

    return ctx.getStopAction();
  }
  @SuppressWarnings("unchecked")
  public boolean sendRequest(
      final FilterChainContext ctx, final Request request, final HttpRequestPacket requestPacket)
      throws IOException {

    boolean isWriteComplete = true;

    if (Utils.requestHasEntityBody(request)) {
      final HttpTxContext context = HttpTxContext.get(ctx);
      BodyHandler handler = bodyHandlerFactory.getBodyHandler(request);
      if (requestPacket.getHeaders().contains(Header.Expect)
          && requestPacket.getHeaders().getValue(1).equalsIgnoreCase("100-Continue")) {
        // We have to set the content-length now as the headers will be flushed
        // before the FileBodyHandler is invoked.  If we don't do it here, and
        // the user didn't explicitly set the length, then the transfer-encoding
        // will be chunked and zero-copy file transfer will not occur.
        final File f = request.getFile();
        if (f != null) {
          requestPacket.setContentLengthLong(f.length());
        }
        handler = new ExpectHandler(handler);
      }
      context.setBodyHandler(handler);
      if (logger.isDebugEnabled()) {
        logger.debug("REQUEST: {}", requestPacket);
      }
      isWriteComplete = handler.doHandle(ctx, request, requestPacket);
    } else {
      HttpContent content = HttpContent.builder(requestPacket).last(true).build();
      if (logger.isDebugEnabled()) {
        logger.debug("REQUEST: {}", requestPacket);
      }
      ctx.write(content, ctx.getTransportContext().getCompletionHandler());
    }

    return isWriteComplete;
  }
Beispiel #4
0
 private void writeHttpResponse(FilterChainContext ctx, int status) {
   HttpResponsePacket responsePacket = getHttpRequest(ctx).getResponse();
   responsePacket.setProtocol(Protocol.HTTP_1_1);
   responsePacket.setStatus(status);
   ctx.write(HttpContent.builder(responsePacket).build());
 }