Java 类io.netty.handler.codec.http2.HttpUtil 实例源码

项目:aliyun-oss-hadoop-fs    文件:Http2ResponseHandler.java   
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg)
    throws Exception {
  Integer streamId =
      msg.headers().getInt(HttpUtil.ExtensionHeaderNames.STREAM_ID.text());
  if (streamId == null) {
    System.err.println("HttpResponseHandler unexpected message received: "
        + msg);
    return;
  }
  if (streamId.intValue() == 1) {
    // this is the upgrade response message, just ignore it.
    return;
  }
  Promise<FullHttpResponse> promise;
  synchronized (this) {
    promise = streamId2Promise.get(streamId);
  }
  if (promise == null) {
    System.err.println("Message received for unknown stream id " + streamId);
  } else {
    // Do stuff with the message (for now just print it)
    promise.setSuccess(msg.retain());

  }
}
项目:aliyun-oss-hadoop-fs    文件:TestDtpHttp2.java   
@Test
public void test() throws InterruptedException, ExecutionException {
  int streamId = 3;
  FullHttpRequest request =
      new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
  request.headers().add(HttpUtil.ExtensionHeaderNames.STREAM_ID.text(),
    streamId);
  Promise<FullHttpResponse> promise = CHANNEL.eventLoop().newPromise();
  synchronized (RESPONSE_HANDLER) {
    CHANNEL.writeAndFlush(request);
    RESPONSE_HANDLER.put(streamId, promise);
  }
  assertEquals(HttpResponseStatus.OK, promise.get().status());
  ByteBuf content = promise.get().content();
  assertEquals("HTTP/2 DTP", content.toString(StandardCharsets.UTF_8));
}
项目:netty-cookbook    文件:HttpResponseHandler.java   
@Override
protected void messageReceived(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
    Integer streamId = msg.headers().getInt(HttpUtil.ExtensionHeaderNames.STREAM_ID.text());
    if (streamId == null) {
        System.err.println("HttpResponseHandler unexpected message received: " + msg);
        return;
    }

    ChannelPromise promise = streamidPromiseMap.get(streamId);
    if (promise == null) {
        System.err.println("Message received for unknown stream id " + streamId);
    } else {
        // Do stuff with the message (for now just print it)
        ByteBuf content = msg.content();
        if (content.isReadable()) {
            int contentLength = content.readableBytes();
            byte[] arr = new byte[contentLength];
            content.readBytes(arr);
            System.out.println(new String(arr, 0, contentLength, CharsetUtil.UTF_8));
        }

        promise.setSuccess();
    }
}
项目:http2-examples    文件:HttpResponseHandler.java   
@Override
protected void messageReceived(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
    Integer streamId = msg.headers().getInt(HttpUtil.ExtensionHeaderNames.STREAM_ID.text());
    if (streamId == null) {
        System.err.println("HttpResponseHandler unexpected message received: " + msg);
        return;
    }

    ChannelPromise promise = streamidPromiseMap.get(streamId);
    if (promise == null) {
        System.err.println("Message received for unknown stream id " + streamId);
    } else {
        // Do stuff with the message (for now just print it)
        ByteBuf content = msg.content();
        if (content.isReadable()) {
            int contentLength = content.readableBytes();
            byte[] arr = new byte[contentLength];
            content.readBytes(arr);
            System.out.println("After " + StopWatch.getInstance().currentTimeInSeconds() + " seconds: " + new String(arr, 0, contentLength));
        }

        promise.setSuccess();
    }
}
项目:jmeter-http2-plugin    文件:HttpResponseHandler.java   
@Override
protected void messageReceived(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
    Integer streamId = msg.headers().getInt(HttpUtil.ExtensionHeaderNames.STREAM_ID.text());
    if (streamId == null) {
        System.err.println("HttpResponseHandler unexpected message received: " + msg);
        return;
    }

    ChannelPromise promise = streamidPromiseMap.get(streamId);
    if (promise == null) {
        System.err.println("Message received for unknown stream id " + streamId);
    } else {
        // Do stuff with the message (for now just print it)
        ByteBuf content = msg.content();
        if (content.isReadable()) {
            int contentLength = content.readableBytes();
            byte[] arr = new byte[contentLength];
            content.readBytes(arr);
            System.out.println(new String(arr, 0, contentLength, CharsetUtil.UTF_8));
        }

        promise.setSuccess();

        // Set result
        streamidResponseMap.put(streamId, msg);
    }
}