本文整理汇总了Java中io.netty.handler.codec.http.HttpResponseEncoder类的典型用法代码示例。如果您正苦于以下问题:Java HttpResponseEncoder类的具体用法?Java HttpResponseEncoder怎么用?Java HttpResponseEncoder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpResponseEncoder类属于io.netty.handler.codec.http包,在下文中一共展示了HttpResponseEncoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: appendHttpPipeline
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
public final ChannelPipeline appendHttpPipeline(ChannelPipeline channelPipeline) {
// 服务端,对响应编码。属于ChannelOutboundHandler,逆序执行
channelPipeline.addLast("encoder", new HttpResponseEncoder());
// 服务端,对请求解码。属于ChannelIntboundHandler,按照顺序执行
channelPipeline.addLast("decoder", new HttpRequestDecoder());
//即通过它可以把 HttpMessage 和 HttpContent 聚合成一个 FullHttpRequest,并定义可以接受的数据大小,在文件上传时,可以支持params+multipart
channelPipeline.addLast("aggregator", new HttpObjectAggregator(maxConentLength));
//块写入写出Handler
channelPipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
// 对传输数据进行压缩,这里在客户端需要解压缩处理
// channelPipeline.addLast("deflater", new HttpContentCompressor());
HttpServletHandler servletHandler = new HttpServletHandler();
servletHandler.addInterceptor(new ChannelInterceptor());
//servletHandler.addInterceptor(new HttpSessionInterceptor(getHttpSessionStore()));
// 自定义Handler
channelPipeline.addLast("handler", servletHandler);
// 异步
// channelPipeline.addLast(businessExecutor, new AsyncHttpServletHandler());
return channelPipeline;
}
示例2: initChannel
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
@Override
protected void initChannel(Channel ch) throws Exception {
// create a new pipeline
ChannelPipeline pipeline = ch.pipeline();
SslHandler sslHandler = configureServerSSLOnDemand();
if (sslHandler != null) {
LOG.debug("Server SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
pipeline.addLast("ssl", sslHandler);
}
pipeline.addLast("decoder", new HttpRequestDecoder(409, configuration.getMaxHeaderSize(), 8192));
pipeline.addLast("encoder", new HttpResponseEncoder());
if (configuration.isChunked()) {
pipeline.addLast("aggregator", new HttpObjectAggregator(configuration.getChunkedMaxContentLength()));
}
if (configuration.isCompression()) {
pipeline.addLast("deflater", new HttpContentCompressor());
}
pipeline.addLast("handler", channelFactory.getChannelHandler());
}
示例3: initChannel
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
if (sslCtx != null) {
p.addLast(sslCtx.newHandler(ch.alloc()));
}
p.addLast(new HttpRequestDecoder());
// Uncomment the following line if you don't want to handle HttpChunks.
//p.addLast(new HttpObjectAggregator(1048576));
p.addLast(new HttpResponseEncoder());
// Remove the following line if you don't want automatic content compression.
//p.addLast(new HttpContentCompressor());
p.addLast(new MockingFCMServerHandler());
}
示例4: initChannel
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
if(sslCtx!=null)
{
p.addLast(new SslHandler(sslCtx.newEngine(ch.alloc())));
}
p.addLast(new HttpResponseEncoder());//必须放在最前面,如果decoder途中需要回复消息,则decoder前面需要encoder
p.addLast(new HttpRequestDecoder());
p.addLast(new HttpObjectAggregator(65536));//限制contentLength
//大文件传输处理
// p.addLast(new ChunkedWriteHandler());
// p.addLast(new HttpContentCompressor());
//跨域配置
CorsConfig corsConfig = CorsConfigBuilder.forAnyOrigin().allowNullOrigin().allowCredentials().build();
p.addLast(new CorsHandler(corsConfig));
p.addLast(new DefaultListenerHandler<HttpRequest>(listener));
}
示例5: init
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
@Override
public void init() {
super.init();
b.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, false)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.SO_BACKLOG, 1024)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(defLoopGroup,
new HttpRequestDecoder(), //请求解码器
new HttpObjectAggregator(65536),//将多个消息转换成单一的消息对象
new HttpResponseEncoder(), // 响应编码器
new HttpServerHandler(snowFlake)//自定义处理器
);
}
});
}
示例6: newNonSslHandler
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
@Override
protected ChannelHandler newNonSslHandler(ChannelHandlerContext context) {
return new ChannelInboundHandlerAdapter() {
private HttpResponseEncoder encoder = new HttpResponseEncoder();
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
LOG.trace("Received non-SSL request, returning redirect");
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.MOVED_PERMANENTLY, Unpooled.EMPTY_BUFFER);
response.headers().set(Names.LOCATION, redirectAddress);
LOG.trace(Constants.LOG_RETURNING_RESPONSE, response);
encoder.write(ctx, response, ctx.voidPromise());
ctx.flush();
}
};
}
示例7: initChannel
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
@Override
public void initChannel(SocketChannel ch) throws Exception {
// Create a default pipeline implementation.
ChannelPipeline p = ch.pipeline();
// Uncomment the following line if you want HTTPS
//SSLEngine engine = SecureChatSslContextFactory.getServerContext().createSSLEngine();
//engine.setUseClientMode(false);
//p.addLast("ssl", new SslHandler(engine));
p.addLast("decoder", new HttpRequestDecoder());
// Uncomment the following line if you don't want to handle HttpChunks.
//p.addLast("aggregator", new HttpObjectAggregator(1048576));
p.addLast("encoder", new HttpResponseEncoder());
// Remove the following line if you don't want automatic content compression.
//p.addLast("deflater", new HttpContentCompressor());
p.addLast("handler", new HttpSnoopServerHandler());
}
示例8: initServer
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
/**
* Start WebImageViewer.
* @param fsimage the fsimage to load.
* @throws IOException if fail to load the fsimage.
*/
@VisibleForTesting
public void initServer(String fsimage)
throws IOException, InterruptedException {
final FSImageLoader loader = FSImageLoader.load(fsimage);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new HttpRequestDecoder(),
new StringEncoder(),
new HttpResponseEncoder(),
new FSImageHandler(loader, allChannels));
}
});
channel = bootstrap.bind(address).sync().channel();
allChannels.add(channel);
address = (InetSocketAddress) channel.localAddress();
LOG.info("WebImageViewer started. Listening on " + address.toString() + ". Press Ctrl+C to stop the viewer.");
}
示例9: channelRead
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof ByteBuf && ctx.channel().isActive()) {
boolean isHttpRequest = false;
ByteBuf buffer = (ByteBuf) msg;
final int len = 11;
if (buffer.readableBytes() > len) {
byte[] dst = new byte[len];
buffer.getBytes(buffer.readerIndex(), dst, 0, len);
int n = HttpMethodUtil.method(dst);
isHttpRequest = n > 2;
}
if (isHttpRequest) {
ChannelPipeline cp = ctx.pipeline();
String currentName = ctx.name();
cp.addAfter(currentName, "HttpRequestDecoder", new HttpRequestDecoder());
cp.addAfter("HttpRequestDecoder", "HttpResponseEncoder", new HttpResponseEncoder());
cp.addAfter("HttpResponseEncoder", "HttpObjectAggregator", new HttpObjectAggregator(512 * 1024));
ChannelHandler handler = serverDef.httpHandlerFactory.create(serverDef);
cp.addAfter("HttpObjectAggregator", "HttpThriftBufDecoder", handler);
cp.remove(currentName);
}
}
ctx.fireChannelRead(msg);
}
示例10: start
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
public void start(int port) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
// server端发送的是httpResponse,所以要使用HttpResponseEncoder进行编码
ch.pipeline().addLast(new HttpResponseEncoder());
// server端接收到的是httpRequest,所以要使用HttpRequestDecoder进行解码
ch.pipeline().addLast(new HttpRequestDecoder());
ch.pipeline().addLast(new HttpServerInboundHandler());
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
示例11: initChannel_adds_HttpResponseEncoder_as_the_last_outbound_handler_before_sslCtx
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
@Test
public void initChannel_adds_HttpResponseEncoder_as_the_last_outbound_handler_before_sslCtx() {
// given
HttpChannelInitializer hci = basicHttpChannelInitializerNoUtilityHandlers();
// when
hci.initChannel(socketChannelMock);
// then
ArgumentCaptor<ChannelHandler> channelHandlerArgumentCaptor = ArgumentCaptor.forClass(ChannelHandler.class);
verify(channelPipelineMock, atLeastOnce()).addLast(anyString(), channelHandlerArgumentCaptor.capture());
List<ChannelHandler> handlers = channelHandlerArgumentCaptor.getAllValues();
Pair<Integer, HttpResponseEncoder> foundHandler = findChannelHandler(handlers, HttpResponseEncoder.class);
assertThat(foundHandler, notNullValue());
// No SSL Context was passed, so HttpResponseEncoder should be the handler at index 0 in the list (corresponding to the "last" outbound handler since they go in reverse order).
assertThat(foundHandler.getLeft(), is(0));
}
示例12: initChannel_adds_HttpContentCompressor_before_HttpResponseEncoder_for_outbound_handler
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
@Test
public void initChannel_adds_HttpContentCompressor_before_HttpResponseEncoder_for_outbound_handler() {
// given
HttpChannelInitializer hci = basicHttpChannelInitializerNoUtilityHandlers();
// when
hci.initChannel(socketChannelMock);
// then
ArgumentCaptor<ChannelHandler> channelHandlerArgumentCaptor = ArgumentCaptor.forClass(ChannelHandler.class);
verify(channelPipelineMock, atLeastOnce()).addLast(anyString(), channelHandlerArgumentCaptor.capture());
List<ChannelHandler> handlers = channelHandlerArgumentCaptor.getAllValues();
Pair<Integer, HttpContentCompressor> httpContentCompressor = findChannelHandler(handlers, HttpContentCompressor.class);
Pair<Integer, HttpResponseEncoder> httpResponseEncoder = findChannelHandler(handlers, HttpResponseEncoder.class);
assertThat(httpContentCompressor, notNullValue());
assertThat(httpResponseEncoder, notNullValue());
// HttpContentCompressor's index should be later than HttpResponseEncoder's index to verify that it comes BEFORE HttpResponseEncoder on the OUTBOUND handlers
// (since the outbound handlers are processed in reverse order).
assertThat(httpContentCompressor.getLeft(), is(greaterThan(httpResponseEncoder.getLeft())));
}
示例13: prematureCancel
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
@Test
public void prematureCancel() throws Exception {
DirectProcessor<Void> signal = DirectProcessor.create();
NettyContext x = TcpServer.create("localhost", 0)
.newHandler((in, out) -> {
signal.onComplete();
return out.context(c -> c.addHandlerFirst(
new HttpResponseEncoder()))
.sendObject(Mono.delay(Duration
.ofSeconds(2))
.map(t ->
new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1,
HttpResponseStatus
.PROCESSING)))
.neverComplete();
})
.block(Duration.ofSeconds(30));
StepVerifier.create(createHttpClientForContext(x)
.get("/")
.timeout(signal)
)
.verifyError(TimeoutException.class);
// Thread.sleep(1000000);
}
示例14: initChannel
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
if (sslCtx != null) {
pipeline.addLast(sslCtx.newHandler(ch.alloc()));
}
pipeline.addLast(new HttpRequestDecoder());
pipeline.addLast(new HttpResponseEncoder());
// Remove the following line if you don't want automatic content compression.
pipeline.addLast(new HttpContentCompressor());
pipeline.addLast(new HttpUploadServerHandler());
}
示例15: initChannel
import io.netty.handler.codec.http.HttpResponseEncoder; //导入依赖的package包/类
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
if (sslCtx != null) {
pipeline.addLast(sslCtx.newHandler(ch.alloc()));
}
pipeline.addLast(new HttpResponseEncoder());
pipeline.addLast(new HttpRequestDecoder());
// Uncomment the following line if you don't want to handle HttpChunks.
//pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
//p.addLast(new HttpObjectAggregator(1048576));
// Remove the following line if you don't want automatic content compression.
//pipeline.addLast(new HttpContentCompressor());
// Uncomment the following line if you don't want to handle HttpContents.
pipeline.addLast(new HttpObjectAggregator(65536));
pipeline.addLast("readTimeoutHandler", new ReadTimeoutHandler(READ_TIMEOUT));
pipeline.addLast("myHandler", new MyHandler());
pipeline.addLast("handler", new HttpServerHandler(listener));
}