Search in sources :

Example 26 with HttpResponseEncoder

use of io.netty.handler.codec.http.HttpResponseEncoder in project BRFS by zhangnianli.

the class NettyChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    // server端发送的是httpResponse,所以要使用HttpResponseEncoder进行编码
    pipeline.addLast(new HttpResponseEncoder());
    // server端接收到的是httpRequest,所以要使用HttpRequestDecoder进行解码
    pipeline.addLast(new HttpRequestDecoder());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new ChunkedWriteHandler());
    contextHandlers.forEach((NettyHttpContextHandler handler) -> pipeline.addLast(handler));
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 27 with HttpResponseEncoder

use of io.netty.handler.codec.http.HttpResponseEncoder in project BRFS by zhangnianli.

the class NettyChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    // server端发送的是httpResponse,所以要使用HttpResponseEncoder进行编码
    pipeline.addLast(new HttpResponseEncoder());
    // server端接收到的是httpRequest,所以要使用HttpRequestDecoder进行解码
    pipeline.addLast(new HttpRequestDecoder());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new ChunkedWriteHandler());
    contextHandlers.forEach((NettyHttpContextHandler handler) -> pipeline.addLast(handler));
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 28 with HttpResponseEncoder

use of io.netty.handler.codec.http.HttpResponseEncoder in project camel by apache.

the class HttpServerInitializerFactory method initChannel.

@Override
protected void initChannel(Channel ch) throws Exception {
    // create a new pipeline
    ChannelPipeline pipeline = ch.pipeline();
    SslHandler sslHandler = configureServerSSLOnDemand();
    if (sslHandler != null) {
        //TODO must close on SSL exception
        // sslHandler.setCloseOnSSLException(true);
        LOG.debug("Server SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
        pipeline.addLast("ssl", sslHandler);
    }
    pipeline.addLast("decoder", new HttpRequestDecoder(4096, configuration.getMaxHeaderSize(), 8192));
    List<ChannelHandler> decoders = consumer.getConfiguration().getDecoders();
    for (int x = 0; x < decoders.size(); x++) {
        ChannelHandler decoder = decoders.get(x);
        if (decoder instanceof ChannelHandlerFactory) {
            // use the factory to create a new instance of the channel as it may not be shareable
            decoder = ((ChannelHandlerFactory) decoder).newChannelHandler();
        }
        pipeline.addLast("decoder-" + x, decoder);
    }
    pipeline.addLast("encoder", new HttpResponseEncoder());
    List<ChannelHandler> encoders = consumer.getConfiguration().getEncoders();
    for (int x = 0; x < encoders.size(); x++) {
        ChannelHandler encoder = encoders.get(x);
        if (encoder instanceof ChannelHandlerFactory) {
            // use the factory to create a new instance of the channel as it may not be shareable
            encoder = ((ChannelHandlerFactory) encoder).newChannelHandler();
        }
        pipeline.addLast("encoder-" + x, encoder);
    }
    pipeline.addLast("aggregator", new HttpObjectAggregator(configuration.getChunkedMaxContentLength()));
    if (supportCompressed()) {
        pipeline.addLast("deflater", new HttpContentCompressor());
    }
    int port = consumer.getConfiguration().getPort();
    ChannelHandler handler = consumer.getEndpoint().getComponent().getMultiplexChannelHandler(port).getChannelHandler();
    if (consumer.getConfiguration().isUsingExecutorService()) {
        EventExecutorGroup applicationExecutor = consumer.getEndpoint().getComponent().getExecutorService();
        pipeline.addLast(applicationExecutor, "handler", handler);
    } else {
        pipeline.addLast("handler", handler);
    }
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChannelHandlerFactory(org.apache.camel.component.netty4.ChannelHandlerFactory) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 29 with HttpResponseEncoder

use of io.netty.handler.codec.http.HttpResponseEncoder in project xian by happyyangyuan.

the class OAuthServer method startServer.

public void startServer() {
    synchronized (LOCK) {
        if (started.get()) {
            LOG.warn("已启动,不允许重复启动");
            return;
        }
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer() {

                protected void initChannel(Channel ch) throws Exception {
                    ch.pipeline().addLast("encoder", new HttpResponseEncoder()).addLast("decoder", new HttpRequestDecoder()).addLast("aggregator", new HttpObjectAggregator(4096)).addLast("handler", new OAuth20Handler());
                }
            }).option(ChannelOption.SO_BACKLOG, 10240).option(ChannelOption.SO_REUSEADDR, true);
            LOG.info(String.format("[OAuth] oauth server is about to start on port {%s} ", PORT));
            parentChannel = b.bind(PORT).sync().channel();
            LOG.info(String.format("[OAuth] oauth server started on port {%s} ", PORT));
            ThreadPoolManager.execute(() -> {
                try {
                    LOG.debug("[OAuth] Wait until the server socket is closed.");
                    parentChannel.closeFuture().sync();
                } catch (Throwable ee) {
                    LOG.error(ee);
                } finally {
                    LOG.info("[OAuth] 准备shutdown oauth server");
                    bossGroup.shutdownGracefully();
                    workerGroup.shutdownGracefully();
                    LOG.info("[OAuth] oauth server shutdown完毕!");
                }
            });
            started.set(true);
        } catch (Throwable e) {
            LOG.error("[OAuth] oauth server 启动失败", e);
            started.set(false);
        }
    }
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 30 with HttpResponseEncoder

use of io.netty.handler.codec.http.HttpResponseEncoder in project xian by happyyangyuan.

the class HttpFileServer method doStart.

private void doStart(int port) throws InterruptedException {
    EventLoopGroup boosGroup = new NioEventLoopGroup();
    EventLoopGroup workerGrop = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.group(boosGroup, workerGrop).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel channel) throws Exception {
            // 请求消息解码器
            channel.pipeline().addLast("http-decoder", new HttpRequestDecoder());
            channel.pipeline().addLast("http-aggregator", new HttpObjectAggregator(1000000));
            // 响应消息解码器
            channel.pipeline().addLast("http-encoder", new HttpResponseEncoder());
            channel.pipeline().addLast("fileServerHandler", new HttpFileHandler());
        }
    });
    ChannelFuture f = b.bind(port).sync();
    channel = f.channel();
    LOG.info("qcloud-xml-api 服务器启动成功,路径是:http://" + EnvUtil.getLocalIp() + ":" + port);
    ThreadPoolManager.execute(() -> {
        try {
            // 等待channel关闭通知/回调:客户端或者服务端主动关闭
            channel.closeFuture().sync();
            LOG.info("qcloud-xml-api 服务器完蛋了--");
        } catch (InterruptedException e) {
            LOG.error(e);
        } finally {
            boosGroup.shutdownGracefully();
            workerGrop.shutdownGracefully();
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)52 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)45 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)28 ChannelPipeline (io.netty.channel.ChannelPipeline)25 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)12 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)11 HttpContentCompressor (io.netty.handler.codec.http.HttpContentCompressor)11 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)10 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)10 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)10 SocketChannel (io.netty.channel.socket.SocketChannel)9 Channel (io.netty.channel.Channel)8 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)8 Test (org.junit.jupiter.api.Test)8 EventLoopGroup (io.netty.channel.EventLoopGroup)7 HttpContentDecompressor (io.netty.handler.codec.http.HttpContentDecompressor)7 SslHandler (io.netty.handler.ssl.SslHandler)7 InetSocketAddress (java.net.InetSocketAddress)7 Unpooled (io.netty.buffer.Unpooled)6 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)6