Search in sources :

Example 96 with ChannelHandler

use of io.netty.channel.ChannelHandler in project riposte by Nike-Inc.

the class HttpChannelInitializerTest method initChannel_adds_ExceptionHandlingHandler_immediately_before_ResponseSenderHandler_and_after_NonblockingEndpointExecutionHandler_and_uses_riposteErrorHandler_and_riposteUnhandledErrorHandler.

@Test
public void initChannel_adds_ExceptionHandlingHandler_immediately_before_ResponseSenderHandler_and_after_NonblockingEndpointExecutionHandler_and_uses_riposteErrorHandler_and_riposteUnhandledErrorHandler() {
    // given
    HttpChannelInitializer hci = basicHttpChannelInitializerNoUtilityHandlers();
    RiposteErrorHandler expectedRiposteErrorHandler = extractField(hci, "riposteErrorHandler");
    RiposteUnhandledErrorHandler expectedRiposteUnhandledErrorHandler = extractField(hci, "riposteUnhandledErrorHandler");
    DistributedTracingConfig<Span> distributedTracingConfigMock = mock(DistributedTracingConfig.class);
    ServerSpanNamingAndTaggingStrategy<Span> serverSpanNamingAndTaggingStrategyMock = mock(ServerSpanNamingAndTaggingStrategy.class);
    Whitebox.setInternalState(hci, "distributedTracingConfig", distributedTracingConfigMock);
    doReturn(serverSpanNamingAndTaggingStrategyMock).when(distributedTracingConfigMock).getServerSpanNamingAndTaggingStrategy();
    // 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, ResponseSenderHandler> responseSenderHandler = findChannelHandler(handlers, ResponseSenderHandler.class);
    Pair<Integer, NonblockingEndpointExecutionHandler> nonblockingEndpointExecutionHandler = findChannelHandler(handlers, NonblockingEndpointExecutionHandler.class);
    Pair<Integer, ExceptionHandlingHandler> exceptionHandlingHandler = findChannelHandler(handlers, ExceptionHandlingHandler.class);
    assertThat(responseSenderHandler, notNullValue());
    assertThat(nonblockingEndpointExecutionHandler, notNullValue());
    assertThat(exceptionHandlingHandler, notNullValue());
    assertThat(exceptionHandlingHandler.getLeft(), is(responseSenderHandler.getLeft() - 1));
    assertThat(exceptionHandlingHandler.getLeft(), is(greaterThan(nonblockingEndpointExecutionHandler.getLeft())));
    assertThat(extractField(exceptionHandlingHandler.getRight(), "spanNamingAndTaggingStrategy"), is(serverSpanNamingAndTaggingStrategyMock));
    // and then
    RiposteErrorHandler actualRiposteErrorHandler = (RiposteErrorHandler) Whitebox.getInternalState(exceptionHandlingHandler.getRight(), "riposteErrorHandler");
    RiposteUnhandledErrorHandler actualRiposteUnhandledErrorHandler = (RiposteUnhandledErrorHandler) Whitebox.getInternalState(exceptionHandlingHandler.getRight(), "riposteUnhandledErrorHandler");
    assertThat(actualRiposteErrorHandler, is(expectedRiposteErrorHandler));
    assertThat(actualRiposteUnhandledErrorHandler, is(expectedRiposteUnhandledErrorHandler));
}
Also used : RiposteUnhandledErrorHandler(com.nike.riposte.server.error.handler.RiposteUnhandledErrorHandler) ExceptionHandlingHandler(com.nike.riposte.server.handler.ExceptionHandlingHandler) ChannelHandler(io.netty.channel.ChannelHandler) Span(com.nike.wingtips.Span) RiposteErrorHandler(com.nike.riposte.server.error.handler.RiposteErrorHandler) NonblockingEndpointExecutionHandler(com.nike.riposte.server.handler.NonblockingEndpointExecutionHandler) ResponseSenderHandler(com.nike.riposte.server.handler.ResponseSenderHandler) Test(org.junit.Test)

Example 97 with ChannelHandler

use of io.netty.channel.ChannelHandler in project riposte by Nike-Inc.

the class HttpChannelInitializerTest method initChannel_adds_HttpServerCodec_with_config_values_coming_from_httpRequestDecoderConfig.

@Test
public void initChannel_adds_HttpServerCodec_with_config_values_coming_from_httpRequestDecoderConfig() {
    // given
    HttpChannelInitializer hci = basicHttpChannelInitializerNoUtilityHandlers();
    HttpRequestDecoderConfig configWithCustomValues = new HttpRequestDecoderConfig() {

        @Override
        public int maxInitialLineLength() {
            return 123;
        }

        @Override
        public int maxHeaderSize() {
            return 456;
        }

        @Override
        public int maxChunkSize() {
            return 789;
        }
    };
    Whitebox.setInternalState(hci, "httpRequestDecoderConfig", configWithCustomValues);
    // 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, HttpServerCodec> httpServerCodecHandlerPair = findChannelHandler(handlers, HttpServerCodec.class);
    Assertions.assertThat(httpServerCodecHandlerPair).isNotNull();
    HttpServerCodec httpServerCodecHandler = httpServerCodecHandlerPair.getValue();
    HttpRequestDecoder decoderHandler = (HttpRequestDecoder) Whitebox.getInternalState(httpServerCodecHandler, "inboundHandler");
    int actualMaxInitialLineLength = extractField(extractField(decoderHandler, "lineParser"), "maxLength");
    int actualMaxHeaderSize = extractField(extractField(decoderHandler, "headerParser"), "maxLength");
    int actualMaxChunkSize = extractField(decoderHandler, "maxChunkSize");
    Assertions.assertThat(actualMaxInitialLineLength).isEqualTo(configWithCustomValues.maxInitialLineLength());
    Assertions.assertThat(actualMaxHeaderSize).isEqualTo(configWithCustomValues.maxHeaderSize());
    Assertions.assertThat(actualMaxChunkSize).isEqualTo(configWithCustomValues.maxChunkSize());
}
Also used : HttpRequestDecoderConfig(com.nike.riposte.server.config.ServerConfig.HttpRequestDecoderConfig) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelHandler(io.netty.channel.ChannelHandler) Endpoint(com.nike.riposte.server.http.Endpoint) Test(org.junit.Test)

Example 98 with ChannelHandler

use of io.netty.channel.ChannelHandler in project riposte by Nike-Inc.

the class HttpChannelInitializerTest method initChannel_adds_RequestContentDeserializerHandler_after_RequestInfoSetterHandler_and_RoutingHandler_and_uses_requestContentDeserializer.

@Test
public void initChannel_adds_RequestContentDeserializerHandler_after_RequestInfoSetterHandler_and_RoutingHandler_and_uses_requestContentDeserializer() {
    // given
    HttpChannelInitializer hci = basicHttpChannelInitializerNoUtilityHandlers();
    ObjectMapper expectedRequestContentDeserializer = mock(ObjectMapper.class);
    Whitebox.setInternalState(hci, "requestContentDeserializer", expectedRequestContentDeserializer);
    // 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, RequestInfoSetterHandler> requestInfoSetterHandler = findChannelHandler(handlers, RequestInfoSetterHandler.class);
    Pair<Integer, RoutingHandler> routingHandler = findChannelHandler(handlers, RoutingHandler.class);
    Pair<Integer, RequestContentDeserializerHandler> requestContentDeserializerHandler = findChannelHandler(handlers, RequestContentDeserializerHandler.class);
    assertThat(requestInfoSetterHandler, notNullValue());
    assertThat(routingHandler, notNullValue());
    assertThat(requestContentDeserializerHandler, notNullValue());
    assertThat(requestContentDeserializerHandler.getLeft(), is(greaterThan(requestInfoSetterHandler.getLeft())));
    assertThat(requestContentDeserializerHandler.getLeft(), is(greaterThan(routingHandler.getLeft())));
    // and then
    ObjectMapper actualRequestContentDeserializer = (ObjectMapper) Whitebox.getInternalState(requestContentDeserializerHandler.getRight(), "defaultRequestContentDeserializer");
    assertThat(actualRequestContentDeserializer, is(expectedRequestContentDeserializer));
}
Also used : RoutingHandler(com.nike.riposte.server.handler.RoutingHandler) RequestInfoSetterHandler(com.nike.riposte.server.handler.RequestInfoSetterHandler) RequestContentDeserializerHandler(com.nike.riposte.server.handler.RequestContentDeserializerHandler) ChannelHandler(io.netty.channel.ChannelHandler) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 99 with ChannelHandler

use of io.netty.channel.ChannelHandler in project spring-framework by spring-projects.

the class RxNettyWebSocketSession method aggregateFrames.

/**
	 * Insert an {@link WebSocketFrameAggregator} after the
	 * {@code WebSocketFrameDecoder} for receiving full messages.
	 * @param channel the channel for the session
	 * @param frameDecoderName the name of the WebSocketFrame decoder
	 */
public RxNettyWebSocketSession aggregateFrames(Channel channel, String frameDecoderName) {
    ChannelPipeline pipeline = channel.pipeline();
    if (pipeline.context(FRAME_AGGREGATOR_NAME) != null) {
        return this;
    }
    ChannelHandlerContext frameDecoder = pipeline.context(frameDecoderName);
    if (frameDecoder == null) {
        throw new IllegalArgumentException("WebSocketFrameDecoder not found: " + frameDecoderName);
    }
    ChannelHandler frameAggregator = new WebSocketFrameAggregator(DEFAULT_FRAME_MAX_SIZE);
    pipeline.addAfter(frameDecoder.name(), FRAME_AGGREGATOR_NAME, frameAggregator);
    return this;
}
Also used : WebSocketFrameAggregator(io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 100 with ChannelHandler

use of io.netty.channel.ChannelHandler in project netty by netty.

the class EmbeddedChannel method setup.

private void setup(final ChannelHandler... handlers) {
    ObjectUtil.checkNotNull(handlers, "handlers");
    ChannelPipeline p = pipeline();
    p.addLast(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            for (ChannelHandler h : handlers) {
                if (h == null) {
                    break;
                }
                pipeline.addLast(h);
            }
        }
    });
    ChannelFuture future = loop.register(this);
    assert future.isDone();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) AbstractChannel(io.netty.channel.AbstractChannel) Channel(io.netty.channel.Channel) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) DefaultChannelPipeline(io.netty.channel.DefaultChannelPipeline) ClosedChannelException(java.nio.channels.ClosedChannelException)

Aggregations

ChannelHandler (io.netty.channel.ChannelHandler)186 Test (org.junit.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)44 Channel (io.netty.channel.Channel)26 ChannelPipeline (io.netty.channel.ChannelPipeline)25 SslHandler (io.netty.handler.ssl.SslHandler)25 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FilterChainMatchingHandler (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler)20 ChannelFuture (io.netty.channel.ChannelFuture)20 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)20 FilterChainSelector (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler.FilterChainSelector)19 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)18 DownstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext)17 FilterChain (io.grpc.xds.EnvoyServerProtoData.FilterChain)17 InetSocketAddress (java.net.InetSocketAddress)16 Test (org.junit.jupiter.api.Test)16 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Bootstrap (io.netty.bootstrap.Bootstrap)11 ArrayList (java.util.ArrayList)11