Search in sources :

Example 36 with ChannelHandler

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler in project flink by apache.

the class Prio0InboundChannelHandlerFactory method createHandler.

@Override
public Optional<ChannelHandler> createHandler(Configuration configuration, Map<String, String> responseHeaders) throws ConfigurationException {
    String redirectFromUrl = configuration.getString(REDIRECT_FROM_URL);
    String redirectToUrl = configuration.getString(REDIRECT_TO_URL);
    if (!redirectFromUrl.isEmpty() && !redirectToUrl.isEmpty()) {
        return Optional.of(new ChannelInboundHandlerAdapter() {

            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) {
                if (msg instanceof HttpRequest) {
                    HttpRequest httpRequest = (HttpRequest) msg;
                    if (httpRequest.uri().equals(redirectFromUrl)) {
                        httpRequest.setUri(redirectToUrl);
                    }
                }
                ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
            }
        });
    }
    return Optional.empty();
}
Also used : HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) ChannelHandlerContext(org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(org.apache.flink.shaded.netty4.io.netty.channel.ChannelInboundHandlerAdapter)

Example 37 with ChannelHandler

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler in project netty by netty.

the class MessageToMessageEncoderTest method testIntermediateWriteFailures.

@Test
public void testIntermediateWriteFailures() {
    ChannelHandler encoder = new MessageToMessageEncoder<Object>() {

        @Override
        protected void encode(ChannelHandlerContext ctx, Object msg, List<Object> out) {
            out.add(new Object());
            out.add(msg);
        }
    };
    final Exception firstWriteException = new Exception();
    ChannelHandler writeThrower = new ChannelOutboundHandlerAdapter() {

        private boolean firstWritten;

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            if (firstWritten) {
                ctx.write(msg, promise);
            } else {
                firstWritten = true;
                promise.setFailure(firstWriteException);
            }
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(writeThrower, encoder);
    Object msg = new Object();
    ChannelFuture write = channel.writeAndFlush(msg);
    assertSame(firstWriteException, write.cause());
    assertSame(msg, channel.readOutbound());
    assertFalse(channel.finish());
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) List(java.util.List) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.jupiter.api.Test)

Example 38 with ChannelHandler

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler in project netty by netty.

the class OptionalSslHandlerTest method handlerReplaced.

@Test
public void handlerReplaced() throws Exception {
    final ChannelHandler nonSslHandler = Mockito.mock(ChannelHandler.class);
    OptionalSslHandler handler = new OptionalSslHandler(sslContext) {

        @Override
        protected ChannelHandler newNonSslHandler(ChannelHandlerContext context) {
            return nonSslHandler;
        }

        @Override
        protected String newNonSslHandlerName() {
            return HANDLER_NAME;
        }
    };
    final ByteBuf payload = Unpooled.copiedBuffer("plaintext".getBytes());
    try {
        handler.decode(context, payload, null);
        verify(pipeline).replace(handler, HANDLER_NAME, nonSslHandler);
    } finally {
        payload.release();
    }
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 39 with ChannelHandler

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler in project netty by netty.

the class Http2MultiplexTest method outboundStreamShouldWriteResetFrameOnClose_headersSent.

@Test
public void outboundStreamShouldWriteResetFrameOnClose_headersSent() {
    ChannelHandler handler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            ctx.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()));
            ctx.fireChannelActive();
        }
    };
    Http2StreamChannel childChannel = newOutboundStream(handler);
    assertTrue(childChannel.isActive());
    childChannel.close();
    verify(frameWriter).writeRstStream(eqCodecCtx(), eqStreamId(childChannel), eq(Http2Error.CANCEL.code()), anyChannelPromise());
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 40 with ChannelHandler

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelHandler in project netty by netty.

the class Http2MultiplexTest method endOfStreamDoesNotDiscardData.

@Test
public void endOfStreamDoesNotDiscardData() {
    AtomicInteger numReads = new AtomicInteger(1);
    final AtomicBoolean shouldDisableAutoRead = new AtomicBoolean();
    Consumer<ChannelHandlerContext> ctxConsumer = new Consumer<ChannelHandlerContext>() {

        @Override
        public void accept(ChannelHandlerContext obj) {
            if (shouldDisableAutoRead.get()) {
                obj.channel().config().setAutoRead(false);
            }
        }
    };
    LastInboundHandler inboundHandler = new LastInboundHandler(ctxConsumer);
    Http2StreamChannel childChannel = newInboundStream(3, false, numReads, inboundHandler);
    childChannel.config().setAutoRead(false);
    Http2DataFrame dataFrame1 = new DefaultHttp2DataFrame(bb("1")).stream(childChannel.stream());
    Http2DataFrame dataFrame2 = new DefaultHttp2DataFrame(bb("2")).stream(childChannel.stream());
    Http2DataFrame dataFrame3 = new DefaultHttp2DataFrame(bb("3")).stream(childChannel.stream());
    Http2DataFrame dataFrame4 = new DefaultHttp2DataFrame(bb("4")).stream(childChannel.stream());
    assertEquals(new DefaultHttp2HeadersFrame(request).stream(childChannel.stream()), inboundHandler.readInbound());
    ChannelHandler readCompleteSupressHandler = new ChannelInboundHandlerAdapter() {

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) {
        // We want to simulate the parent channel calling channelRead and delay calling channelReadComplete.
        }
    };
    parentChannel.pipeline().addFirst(readCompleteSupressHandler);
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("1"), 0, false);
    assertEqualsAndRelease(dataFrame1, inboundHandler.<Http2DataFrame>readInbound());
    // Deliver frames, and then a stream closed while read is inactive.
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("2"), 0, false);
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("3"), 0, false);
    frameInboundWriter.writeInboundData(childChannel.stream().id(), bb("4"), 0, false);
    shouldDisableAutoRead.set(true);
    childChannel.config().setAutoRead(true);
    numReads.set(1);
    frameInboundWriter.writeInboundRstStream(childChannel.stream().id(), Http2Error.NO_ERROR.code());
    // Detecting EOS should flush all pending data regardless of read calls.
    assertEqualsAndRelease(dataFrame2, inboundHandler.<Http2DataFrame>readInbound());
    assertNull(inboundHandler.readInbound());
    // As we limited the number to 1 we also need to call read() again.
    childChannel.read();
    assertEqualsAndRelease(dataFrame3, inboundHandler.<Http2DataFrame>readInbound());
    assertEqualsAndRelease(dataFrame4, inboundHandler.<Http2DataFrame>readInbound());
    Http2ResetFrame resetFrame = useUserEventForResetFrame() ? inboundHandler.<Http2ResetFrame>readUserEvent() : inboundHandler.<Http2ResetFrame>readInbound();
    assertEquals(childChannel.stream(), resetFrame.stream());
    assertEquals(Http2Error.NO_ERROR.code(), resetFrame.errorCode());
    assertNull(inboundHandler.readInbound());
    // Now we want to call channelReadComplete and simulate the end of the read loop.
    parentChannel.pipeline().remove(readCompleteSupressHandler);
    parentChannel.flushInbound();
    childChannel.closeFuture().syncUninterruptibly();
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(io.netty.handler.codec.http2.LastInboundHandler.Consumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test)

Aggregations

ChannelHandler (io.netty.channel.ChannelHandler)216 Test (org.junit.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)44 Channel (io.netty.channel.Channel)27 ChannelPipeline (io.netty.channel.ChannelPipeline)26 SslHandler (io.netty.handler.ssl.SslHandler)25 Test (org.junit.jupiter.api.Test)24 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)23 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)21 FilterChainMatchingHandler (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler)20 ChannelFuture (io.netty.channel.ChannelFuture)20 FilterChainSelector (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler.FilterChainSelector)19 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)18 InetSocketAddress (java.net.InetSocketAddress)18 DownstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext)17 FilterChain (io.grpc.xds.EnvoyServerProtoData.FilterChain)17 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 TcpIngester (com.wavefront.ingester.TcpIngester)15 ByteBuf (io.netty.buffer.ByteBuf)13