本文整理匯總了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));
}