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));
}
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));
}
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);
}
}
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);
}
}
}
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();
}
});
}
Aggregations