Exemplo n.º 1
0
  @Override
  public NextAction handleWrite(FilterChainContext context) throws IOException {
    Connection<?> connection = context.getConnection();
    GrizzlyChannel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
    try {
      UnsafeByteArrayOutputStream output = new UnsafeByteArrayOutputStream(1024); // 不需要关闭

      if (!(context.getMessage() instanceof Response)) {
        downstreamCodec.encode(channel, output, context.getMessage());
      } else {
        upstreamCodec.encode(channel, output, context.getMessage());
      }

      GrizzlyChannel.removeChannelIfDisconnectd(connection);
      byte[] bytes = output.toByteArray();
      Buffer buffer = connection.getTransport().getMemoryManager().allocate(bytes.length);
      buffer.put(bytes);
      buffer.flip();
      buffer.allowBufferDispose(true);
      context.setMessage(buffer);
    } finally {
      GrizzlyChannel.removeChannelIfDisconnectd(connection);
    }
    return context.getInvokeAction();
  }
    @Override
    public NextAction handleRead(final FilterChainContext ctx) throws IOException {

      String message = (String) ctx.getMessage();

      logger.log(Level.INFO, "First chunk come: {0}", message);
      intermResultQueue.add(message);

      Connection connection = ctx.getConnection();
      connection.setBlockingReadTimeout(10, TimeUnit.SECONDS);

      try {
        for (int i = 0; i < clientMsgs - 1; i++) {
          final ReadResult rr = ctx.read();
          final String blckMsg = (String) rr.getMessage();

          rr.recycle();
          logger.log(Level.INFO, "Blocking chunk come: {0}", blckMsg);
          intermResultQueue.add(blckMsg);
          message += blckMsg;
        }
      } catch (Exception e) {
        intermResultQueue.add(e);
        return ctx.getStopAction();
      }

      ctx.setMessage(message);

      return ctx.getInvokeAction();
    }
Exemplo n.º 3
0
    @Override
    public NextAction handleRead(FilterChainContext ctx) throws IOException {
      // Take ADD-service response
      final AddResponseMessage addResponseMessage = ctx.getMessage();

      // do output
      LOGGER.log(Level.INFO, "Result={0}", addResponseMessage.getResult());

      return ctx.getStopAction();
    }
Exemplo n.º 4
0
    @Override
    public NextAction handleRead(FilterChainContext ctx) throws IOException {
      final Buffer content = ctx.getMessage();
      try {
        future.result(content);
      } catch (Exception e) {
        future.failure(e);
        e.printStackTrace();
      }

      return ctx.getStopAction();
    }
  @Override
  public NextAction handleRead(final FilterChainContext ctx) throws IOException {
    final HttpContent httpContent = ctx.getMessage();
    if (httpContent.isLast()) {
      // Perform the cleanup logic if it's the last chunk of the payload
      final HttpResponsePacket response = (HttpResponsePacket) httpContent.getHttpHeader();

      recycleRequestResponsePackets(ctx.getConnection(), response);
      return ctx.getStopAction();
    }

    return ctx.getInvokeAction();
  }
  @Override
  public NextAction handleWrite(final FilterChainContext ctx) throws IOException {

    Object message = ctx.getMessage();
    if (message instanceof RequestInfoHolder) {
      ctx.setMessage(null);
      if (!sendAsGrizzlyRequest((RequestInfoHolder) message, ctx)) {
        return ctx.getSuspendAction();
      }
    } else if (message instanceof Buffer) {
      return ctx.getInvokeAction();
    }

    return ctx.getStopAction();
  }
  private static FilterChainContext obtainProtocolChainContext(
      final FilterChainContext ctx, final FilterChain completeProtocolFilterChain) {

    final FilterChainContext newFilterChainContext =
        completeProtocolFilterChain.obtainFilterChainContext(
            ctx.getConnection(),
            ctx.getStartIdx() + 1,
            completeProtocolFilterChain.size(),
            ctx.getFilterIdx() + 1);

    newFilterChainContext.setAddressHolder(ctx.getAddressHolder());
    newFilterChainContext.setMessage(ctx.getMessage());
    newFilterChainContext.getInternalContext().setIoEvent(ctx.getInternalContext().getIoEvent());
    ctx.getConnection().setProcessor(completeProtocolFilterChain);
    return newFilterChainContext;
  }
      @Override
      public NextAction handleRead(FilterChainContext ctx) throws IOException {

        HttpContent c = (HttpContent) ctx.getMessage();
        Buffer b = c.getContent();
        if (b.hasRemaining()) {
          sb.append(b.toStringContent());
        }

        // Last content from the server, set the future result so
        // the client can display the result and gracefully exit.
        if (c.isLast()) {
          future.result(sb.toString());
        }
        return ctx.getStopAction(); // discontinue filter chain execution
      }
  public Result find(PUContext puc, FilterChainContext fcc) {
    final Buffer buffer = fcc.getMessage();
    if (buffer.remaining() >= signature.length) {
      final int start = buffer.position();

      for (int i = 0; i < signature.length; i++) {
        if (buffer.get(start + i) != signature[i]) {
          return Result.NOT_FOUND;
        }
      }

      return Result.FOUND;
    }

    return Result.NEED_MORE_DATA;
  }
Exemplo n.º 10
0
 @Override
 public NextAction handleRead(FilterChainContext context) throws IOException {
   Object message = context.getMessage();
   Connection<?> connection = context.getConnection();
   Channel channel = GrizzlyChannel.getOrAddChannel(connection, url, handler);
   try {
     if (message instanceof Buffer) { // 收到新的数据包
       Buffer buffer = (Buffer) message; // 缓存
       int readable = buffer.capacity(); // 本次可读取新数据的大小
       if (readable == 0) {
         return context.getStopAction();
       }
       byte[] bytes; // byte[]缓存区,将Buffer转成byte[],再转成UnsafeByteArrayInputStream
       int offset; // 指向已用数据的偏移量,off之前的数据都是已用过的
       int limit; // 有效长度,limit之后的长度是空白或无效数据,off到limit之间的数据是准备使用的有效数据
       Object[] remainder = (Object[]) channel.getAttribute(BUFFER_KEY); // 上次序列化剩下的数据
       channel.removeAttribute(BUFFER_KEY);
       if (remainder == null) { // 如果没有,创建新的bytes缓存
         bytes = new byte[bufferSize];
         offset = 0;
         limit = 0;
       } else { // 如果有,使用剩下的bytes缓存
         bytes = (byte[]) remainder[0];
         offset = (Integer) remainder[1];
         limit = (Integer) remainder[2];
       }
       return receive(context, channel, buffer, readable, bytes, offset, limit);
     } else if (message instanceof Object[]) { // 同一Buffer多轮Filter,即:一个Buffer里有多个请求
       Object[] remainder = (Object[]) message;
       Buffer buffer = (Buffer) remainder[0];
       int readable = (Integer) remainder[1];
       byte[] bytes = (byte[]) remainder[2];
       int offset = (Integer) remainder[3];
       int limit = (Integer) remainder[4];
       return receive(context, channel, buffer, readable, bytes, offset, limit);
     } else { // 其它事件直接往下传
       return context.getInvokeAction();
     }
   } finally {
     GrizzlyChannel.removeChannelIfDisconnectd(connection);
   }
 }
Exemplo n.º 11
0
  @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();
  }
Exemplo n.º 12
0
    @Override
    public NextAction handleRead(FilterChainContext ctx) throws IOException {
      // Get the parsed HttpContent (we assume prev. filter was HTTP)
      HttpContent message = ctx.getMessage();
      Socket tunnelSocket = tunnelSockets.get(ctx.getConnection());

      if (tunnelSocket == null) {
        // handle connection procedure
        return GrizzlyModProxy.this.handleConnect(ctx, message);
      }

      if (message.getContent().hasRemaining()) {
        // relay the content to the tunnel connection

        Buffer buffer = message.getContent();
        message.recycle();

        tunnelSocket
            .getOutputStream()
            .write(buffer.array(), buffer.arrayOffset(), buffer.remaining());
      }

      return ctx.getStopAction();
    }
Exemplo n.º 13
0
  /**
   * Per-request initialization required for the InputBuffer to function properly.
   *
   * @param httpHeader the header from which input will be obtained.
   * @param ctx the FilterChainContext for the chain processing this request
   */
  public void initialize(final HttpHeader httpHeader, final FilterChainContext ctx) {

    if (ctx == null) {
      throw new IllegalArgumentException("ctx cannot be null.");
    }
    this.httpHeader = httpHeader;

    this.ctx = ctx;
    connection = ctx.getConnection();
    final Object message = ctx.getMessage();
    if (message instanceof HttpContent) {
      final HttpContent content = (HttpContent) message;

      // Check if HttpContent is chunked message trailer w/ headers
      checkHttpTrailer(content);
      updateInputContentBuffer(content.getContent());
      contentRead = content.isLast();
      content.recycle();

      if (LOGGER.isLoggable(LOGGER_LEVEL)) {
        log("InputBuffer %s initialize with ready content: %s", this, inputContentBuffer);
      }
    }
  }
Exemplo n.º 14
0
 private HttpRequestPacket getHttpRequest(FilterChainContext ctx) {
   return (HttpRequestPacket) ((HttpContent) ctx.getMessage()).getHttpHeader();
 }
    @Override
    public Result find(PUContext puContext, FilterChainContext ctx) {
      final String requestedProtocolName = ctx.getMessage();

      return requestedProtocolName.startsWith(name) ? Result.FOUND : Result.NOT_FOUND;
    }