Example usage for io.netty.handler.codec.http2 Http2Connection stream

List of usage examples for io.netty.handler.codec.http2 Http2Connection stream

Introduction

In this page you can find the example usage for io.netty.handler.codec.http2 Http2Connection stream.

Prototype

Http2Stream stream(int streamId);

Source Link

Document

Gets the stream if it exists.

Usage

From source file:io.vertx.core.http.Http2ClientTest.java

License:Open Source License

private ServerBootstrap createH2CServer(
        BiFunction<Http2ConnectionDecoder, Http2ConnectionEncoder, Http2FrameListener> handler,
        boolean upgrade) {
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.group(new NioEventLoopGroup());
    bootstrap.childHandler(new ChannelInitializer<Channel>() {
        @Override/*from   www . j a v a  2s.c o  m*/
        protected void initChannel(Channel ch) throws Exception {
            if (upgrade) {
                HttpServerCodec sourceCodec = new HttpServerCodec();
                HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory = protocol -> {
                    if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
                        Http2ConnectionHandler httpConnectionHandler = createHttpConnectionHandler((a, b) -> {
                            return new Http2FrameListenerDecorator(handler.apply(a, b)) {
                                @Override
                                public void onSettingsRead(ChannelHandlerContext ctx,
                                        io.netty.handler.codec.http2.Http2Settings settings)
                                        throws Http2Exception {
                                    super.onSettingsRead(ctx, settings);
                                    Http2Connection conn = a.connection();
                                    Http2Stream stream = conn.stream(1);
                                    DefaultHttp2Headers blah = new DefaultHttp2Headers();
                                    blah.status("200");
                                    b.frameWriter().writeHeaders(ctx, 1, blah, 0, true, ctx.voidPromise());
                                }
                            };
                        });
                        return new Http2ServerUpgradeCodec(httpConnectionHandler);
                    } else {
                        return null;
                    }
                };
                ch.pipeline().addLast(sourceCodec);
                ch.pipeline().addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
            } else {
                Http2ConnectionHandler clientHandler = createHttpConnectionHandler(handler);
                ch.pipeline().addLast("handler", clientHandler);
            }
        }
    });
    return bootstrap;
}