Java Code Examples for io.netty.channel.ChannelHandler

The following examples show how to use io.netty.channel.ChannelHandler. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: grpc-nebula-java   Source File: ProtocolNegotiators.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Handler newHandler(GrpcHttp2ConnectionHandler handler) {
  final HostPort hostPort = parseAuthority(handler.getAuthority());

  ChannelHandler sslBootstrap = new ChannelHandlerAdapter() {
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
      SSLEngine sslEngine = sslContext.newEngine(ctx.alloc(), hostPort.host, hostPort.port);
      SSLParameters sslParams = sslEngine.getSSLParameters();
      sslParams.setEndpointIdentificationAlgorithm("HTTPS");
      sslEngine.setSSLParameters(sslParams);
      ctx.pipeline().replace(this, null, new SslHandler(sslEngine, false));
    }
  };
  return new BufferUntilTlsNegotiatedHandler(sslBootstrap, handler);
}
 
Example 2
Source Project: grpc-nebula-java   Source File: ProtocolNegotiatorsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void tlsHandler_userEventTriggeredSslEvent_unsupportedProtocol() throws Exception {
  SslHandler badSslHandler = new SslHandler(engine, false) {
    @Override
    public String applicationProtocol() {
      return "badprotocol";
    }
  };

  ChannelHandler handler = new ServerTlsHandler(sslContext, grpcHandler);
  pipeline.addLast(handler);

  pipeline.replace(SslHandler.class, null, badSslHandler);
  channelHandlerCtx = pipeline.context(handler);
  Object sslEvent = SslHandshakeCompletionEvent.SUCCESS;

  pipeline.fireUserEventTriggered(sslEvent);

  // No h2 protocol was specified, so this should be closed.
  assertFalse(channel.isOpen());
  ChannelHandlerContext grpcHandlerCtx = pipeline.context(grpcHandler);
  assertNull(grpcHandlerCtx);
}
 
Example 3
Source Project: joyqueue   Source File: TransportServerSupport.java    License: Apache License 2.0 6 votes vote down vote up
protected ServerBootstrap newBootstrap(ChannelHandler channelHandler, EventLoopGroup acceptEventGroup, EventLoopGroup ioEventGroup) throws Exception {
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.channel(Epoll.isAvailable() ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
            .group(acceptEventGroup, ioEventGroup)
            .childHandler(channelHandler)
            .option(ChannelOption.SO_REUSEADDR, config.isReuseAddress())
            .option(ChannelOption.SO_RCVBUF, config.getSocketBufferSize())
            .option(ChannelOption.SO_BACKLOG, config.getBacklog())
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.SO_SNDBUF, config.getSocketBufferSize())
            .childOption(ChannelOption.TCP_NODELAY, config.isTcpNoDelay())
            .childOption(ChannelOption.SO_KEEPALIVE, config.isKeepAlive())
            .childOption(ChannelOption.SO_LINGER, config.getSoLinger())
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    return serverBootstrap;
}
 
Example 4
Source Project: simple-rpc-framework   Source File: NettyClient.java    License: Apache License 2.0 6 votes vote down vote up
private synchronized Channel createChannel(SocketAddress address, long connectionTimeout) throws InterruptedException, TimeoutException {
    if (address == null) {
        throw new IllegalArgumentException("address must not be null!");
    }
    if (ioEventGroup == null) {
        ioEventGroup = newIoEventGroup();
    }
    if (bootstrap == null){
        ChannelHandler channelHandlerPipeline = newChannelHandlerPipeline();
        bootstrap = newBootstrap(channelHandlerPipeline, ioEventGroup);
    }
        ChannelFuture channelFuture;
        Channel channel;
        channelFuture = bootstrap.connect(address);
        if (!channelFuture.await(connectionTimeout)) {
            throw new TimeoutException();
        }
        channel = channelFuture.channel();
        if (channel == null || !channel.isActive()) {
            throw new IllegalStateException();
        }
        channels.add(channel);
        return channel;
}
 
Example 5
Source Project: litchi   Source File: WebSocketServerInitializer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new IdleStateHandler(0, 0, 60));
    pipeline.addLast(new WebSocketServerProtocolHandler(WEB_SOCKET_PATH, null, true));
    pipeline.addLast(new WebSocketHandler(litchi));

    for (ChannelHandler handler : handlers) {
        pipeline.addLast(handler);
    }
}
 
Example 6
Source Project: ViaFabric   Source File: MixinClientConnection.java    License: MIT License 6 votes vote down vote up
@Redirect(method = "setCompressionThreshold", at = @At(
        value = "INVOKE",
        remap = false,
        target = "Lio/netty/channel/ChannelPipeline;addBefore(Ljava/lang/String;Ljava/lang/String;Lio/netty/channel/ChannelHandler;)Lio/netty/channel/ChannelPipeline;"
))
private ChannelPipeline decodeEncodePlacement(ChannelPipeline instance, String base, String newHandler, ChannelHandler handler) {
    // Fixes the handler order
    switch (base) {
        case "decoder": {
            if (instance.get(CommonTransformer.HANDLER_DECODER_NAME) != null)
                base = CommonTransformer.HANDLER_DECODER_NAME;
            break;
        }
        case "encoder": {
            if (instance.get(CommonTransformer.HANDLER_ENCODER_NAME) != null)
                base = CommonTransformer.HANDLER_ENCODER_NAME;
            break;
        }
    }
    return instance.addBefore(base, newHandler, handler);
}
 
Example 7
Source Project: netty-4.1.22   Source File: OcspTest.java    License: Apache License 2.0 6 votes vote down vote up
private static ChannelHandler newServerHandler(final SslContext context,
        final byte[] response, final ChannelHandler handler) {
    return new ChannelInitializer<Channel>() {
        @Override
        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            SslHandler sslHandler = context.newHandler(ch.alloc());

            if (response != null) {
                ReferenceCountedOpenSslEngine engine = (ReferenceCountedOpenSslEngine) sslHandler.engine();
                engine.setOcspResponse(response);
            }

            pipeline.addLast(sslHandler);

            if (handler != null) {
                pipeline.addLast(handler);
            }
        }
    };
}
 
Example 8
Source Project: journalkeeper   Source File: TransportClientSupport.java    License: Apache License 2.0 5 votes vote down vote up
protected Bootstrap newBootstrap(ChannelHandler channelHandler, EventLoopGroup ioEventGroup) {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.channel(Epoll.isAvailable() ? EpollSocketChannel.class : NioSocketChannel.class)
            .group(ioEventGroup)
            .handler(channelHandler)
            .option(ChannelOption.SO_REUSEADDR, config.isReuseAddress())
            .option(ChannelOption.SO_RCVBUF, config.getSocketBufferSize())
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    return bootstrap;
}
 
Example 9
Source Project: journalkeeper   Source File: DefaultTransportServer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected ChannelHandler newChannelHandlerPipeline() {
    final CommandDispatcher commandDispatcher = new DefaultCommandDispatcher(requestBarrier, requestHandler, responseHandler);
    return new ChannelInitializer<Channel>() {
        @Override
        protected void initChannel(Channel channel) throws Exception {
            channel.pipeline()
                    .addLast(new NettyDecoder(codec))
                    .addLast(new NettyEncoder(codec))
                    .addLast(new TransportEventHandler(requestBarrier, transportEventBus))
                    .addLast(new ExceptionChannelHandler(exceptionHandler, requestBarrier))
                    .addLast(new CommandInvocation(commandDispatcher));
        }
    };
}
 
Example 10
Source Project: grpc-nebula-java   Source File: NettyServerTransport.java    License: Apache License 2.0 5 votes vote down vote up
public void start(ServerTransportListener listener) {
  Preconditions.checkState(this.listener == null, "Handler already registered");
  this.listener = listener;

  // Create the Netty handler for the pipeline.
  grpcHandler = createHandler(listener, channelUnused);
  NettyHandlerSettings.setAutoWindow(grpcHandler);

  // Notify when the channel closes.
  final class TerminationNotifier implements ChannelFutureListener {
    boolean done;

    @Override
    public void operationComplete(ChannelFuture future) throws Exception {
      if (!done) {
        done = true;
        notifyTerminated(grpcHandler.connectionError());
      }
    }
  }

  ChannelFutureListener terminationNotifier = new TerminationNotifier();
  channelUnused.addListener(terminationNotifier);
  channel.closeFuture().addListener(terminationNotifier);

  ChannelHandler negotiationHandler = protocolNegotiator.newHandler(grpcHandler);
  channel.pipeline().addLast(negotiationHandler);
}
 
Example 11
Source Project: joyqueue   Source File: TransportServerSupport.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void doStart() throws Exception {
    EventLoopGroup acceptEventGroup = newAcceptEventGroup();
    EventLoopGroup ioEventGroup = newIoEventGroup();
    ChannelHandler channelHandlerPipeline = newChannelHandlerPipeline();
    ServerBootstrap serverBootstrap = newBootstrap(channelHandlerPipeline, acceptEventGroup, ioEventGroup);
    Channel channel = doBind(serverBootstrap);

    this.acceptEventGroup = acceptEventGroup;
    this.ioEventGroup = ioEventGroup;
    this.serverBootstrap = serverBootstrap;
    this.channel = channel;
}
 
Example 12
Source Project: grpc-nebula-java   Source File: ProtocolNegotiatorsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void tlsHandler_handlerAddedAddsSslHandler() throws Exception {
  ChannelHandler handler = new ServerTlsHandler(sslContext, grpcHandler);

  pipeline.addLast(handler);

  assertTrue(pipeline.first() instanceof SslHandler);
}
 
Example 13
Source Project: grpc-nebula-java   Source File: ProtocolNegotiatorsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void tlsHandler_userEventTriggeredNonSslEvent() throws Exception {
  ChannelHandler handler = new ServerTlsHandler(sslContext, grpcHandler);
  pipeline.addLast(handler);
  channelHandlerCtx = pipeline.context(handler);
  Object nonSslEvent = new Object();

  pipeline.fireUserEventTriggered(nonSslEvent);

  // A non ssl event should not cause the grpcHandler to be in the pipeline yet.
  ChannelHandlerContext grpcHandlerCtx = pipeline.context(grpcHandler);
  assertNull(grpcHandlerCtx);
}
 
Example 14
Source Project: netty-4.1.22   Source File: Http2ServerUpgradeCodec.java    License: Apache License 2.0 5 votes vote down vote up
private Http2ServerUpgradeCodec(String handlerName, Http2ConnectionHandler connectionHandler,
        ChannelHandler... handlers) {
    this.handlerName = handlerName;
    this.connectionHandler = connectionHandler;
    this.handlers = handlers;
    frameReader = new DefaultHttp2FrameReader();
}
 
Example 15
Source Project: grpc-nebula-java   Source File: AltsProtocolNegotiator.java    License: Apache License 2.0 5 votes vote down vote up
BufferUntilAltsNegotiatedHandler(
    GrpcHttp2ConnectionHandler grpcHandler, ChannelHandler... negotiationhandlers) {
  super(negotiationhandlers);
  // Save the gRPC handler. The ALTS handler doesn't support buffering before the handshake
  // completes, so we wait until the handshake was successful before adding the grpc handler.
  this.grpcHandler = grpcHandler;
}
 
Example 16
public MqttOverWebsocketProtocolHandlerPipeline(Protocol protocol, ChannelHandler channelHandler, BrokerContext brokerContext) {
    super(brokerContext);
    this.protocol = protocol;
    this.brokerContext = brokerContext;
    if (channelHandler instanceof DefaultProtocolHandlerPipeline) {
        DefaultProtocolHandlerPipeline handlerPipeline = (DefaultProtocolHandlerPipeline) channelHandler;
        // todo
    }
}
 
Example 17
Source Project: netty-4.1.22   Source File: Http2MultiplexCodec.java    License: Apache License 2.0 5 votes vote down vote up
Http2MultiplexCodec(Http2ConnectionEncoder encoder,
                    Http2ConnectionDecoder decoder,
                    Http2Settings initialSettings,
                    ChannelHandler inboundStreamHandler) {
    super(encoder, decoder, initialSettings);
    this.inboundStreamHandler = inboundStreamHandler;
}
 
Example 18
Source Project: iot-mqtt   Source File: NettySslHandler.java    License: Apache License 2.0 5 votes vote down vote up
public static ChannelHandler getSslHandler(SocketChannel channel, boolean useClientCA, String sslKeyStoreType, String sslKeyFilePath, String sslManagerPwd, String sslStorePwd) {

        SslContext sslContext = createSSLContext(useClientCA, sslKeyStoreType, sslKeyFilePath, sslManagerPwd, sslStorePwd);
        SSLEngine sslEngine = sslContext.newEngine(
                channel.alloc(),
                channel.remoteAddress().getHostString(),
                channel.remoteAddress().getPort());
        sslEngine.setUseClientMode(false); // server mode
        if (useClientCA) {
            sslEngine.setNeedClientAuth(true);
        }
        return new SslHandler(sslEngine);
    }
 
Example 19
Source Project: simple-rpc-framework   Source File: NettyClient.java    License: Apache License 2.0 5 votes vote down vote up
private Bootstrap newBootstrap(ChannelHandler channelHandler, EventLoopGroup ioEventGroup) {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.channel(Epoll.isAvailable() ? EpollSocketChannel.class : NioSocketChannel.class)
            .group(ioEventGroup)
            .handler(channelHandler)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    return bootstrap;
}
 
Example 20
Source Project: simple-rpc-framework   Source File: NettyServer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void start(RequestHandlerRegistry requestHandlerRegistry, int port) throws Exception {
    this.port = port;
    this.requestHandlerRegistry = requestHandlerRegistry;
    EventLoopGroup acceptEventGroup = newEventLoopGroup();
    EventLoopGroup ioEventGroup = newEventLoopGroup();
    ChannelHandler channelHandlerPipeline = newChannelHandlerPipeline();
    ServerBootstrap serverBootstrap = newBootstrap(channelHandlerPipeline, acceptEventGroup, ioEventGroup);
    Channel channel = doBind(serverBootstrap);

    this.acceptEventGroup = acceptEventGroup;
    this.ioEventGroup = ioEventGroup;
    this.channel = channel;

}
 
Example 21
Source Project: simple-rpc-framework   Source File: NettyServer.java    License: Apache License 2.0 5 votes vote down vote up
private ChannelHandler newChannelHandlerPipeline() {
    return new ChannelInitializer<Channel>() {
        @Override
        protected void initChannel(Channel channel) {
            channel.pipeline()
                    .addLast(new RequestDecoder())
                    .addLast(new ResponseEncoder())
                    .addLast(new RequestInvocation(requestHandlerRegistry));
        }
    };
}
 
Example 22
@Override
public ChannelHandler createPipeline(Protocol protocol) {
    CommandDispatcher commandDispatcher = commandDispatcherFactory.getCommandDispatcher(protocol);
    ChannelHandler handlerPipeline = new DefaultProtocolHandlerPipeline(protocol, commandDispatcher, transportEventHandler, connectionHandler);

    if (protocol instanceof ChannelHandlerProvider) {
        ChannelHandler customHandlerPipeline = ((ChannelHandlerProvider) protocol).getChannelHandler(handlerPipeline);
        if (customHandlerPipeline != null) {
            return customHandlerPipeline;
        }
    }

    return handlerPipeline;
}
 
Example 23
private static EmbeddedChannel createServerChannel(ChannelHandler handler) {
    return new EmbeddedChannel(
            new HttpServerCodec(),
            new HttpObjectAggregator(8192),
            new WebSocketServerProtocolHandler("/test", "test-proto-1, test-proto-2", false),
            handler);
}
 
Example 24
Source Project: openAGV   Source File: RxtxServerChannelManager.java    License: Apache License 2.0 5 votes vote down vote up
public RxtxServerChannelManager(String serialport, int baudrate,
                                Map<String, ClientEntry> clientEntries,
                                Supplier<List<ChannelHandler>> channelSupplier,
                                int readTimeout,
                                boolean loggingInitially) {
    super(serialport, baudrate, clientEntries, channelSupplier, readTimeout, loggingInitially);
}
 
Example 25
Source Project: openAGV   Source File: RxtxClientChannelManager.java    License: Apache License 2.0 5 votes vote down vote up
public RxtxClientChannelManager(@Nonnull ConnectionEventListener connectionEventListener,
                                Supplier<List<ChannelHandler>> channelSupplier,
                                int readTimeout,
                                boolean enableLogging) {
    this.connectionEventListener = requireNonNull(connectionEventListener, "connEventListener");
    this.channelSupplier = requireNonNull(channelSupplier, "channelSupplier");
    this.readTimeout = readTimeout;
    this.loggingEnabled = enableLogging;
}
 
Example 26
Source Project: litchi   Source File: WebSocketServerInitializer.java    License: Apache License 2.0 5 votes vote down vote up
public WebSocketServerInitializer(Litchi litchi, boolean openSSL, ChannelHandler... handlers) {
    this.litchi = litchi;
    if (openSSL) {
        this.sslCtx = createSSLContext();
    }
    this.handlers = handlers;
}
 
Example 27
Source Project: joyqueue   Source File: ProtocolServiceWrapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ChannelHandler getChannelHandler(ChannelHandler channelHandler) {
    if (delegate instanceof ChannelHandlerProvider) {
        return ((ChannelHandlerProvider) delegate).getChannelHandler(channelHandler);
    }
    return null;
}
 
Example 28
Source Project: netty-4.1.22   Source File: ProxyHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void test() throws Exception {
    final long TIMEOUT = 2000;
    for (ChannelHandler h: clientHandlers) {
        if (h instanceof ProxyHandler) {
            ((ProxyHandler) h).setConnectTimeoutMillis(TIMEOUT);
        }
    }

    final FailureTestHandler testHandler = new FailureTestHandler();
    Bootstrap b = new Bootstrap();
    b.group(group);
    b.channel(NioSocketChannel.class);
    b.resolver(NoopAddressResolverGroup.INSTANCE);
    b.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(clientHandlers);
            p.addLast(new LineBasedFrameDecoder(64));
            p.addLast(testHandler);
        }
    });

    ChannelFuture cf = b.connect(DESTINATION).channel().closeFuture();
    boolean finished = cf.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);
    finished &= testHandler.latch.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);

    logger.debug("Recorded exceptions: {}", testHandler.exceptions);

    assertProxyHandlers(false);

    assertThat(testHandler.exceptions.size(), is(1));
    Throwable e = testHandler.exceptions.poll();
    assertThat(e, is(instanceOf(ProxyConnectException.class)));
    assertThat(String.valueOf(e), containsString("timeout"));
    assertThat(finished, is(true));
}
 
Example 29
private static EmbeddedChannel createClientChannel(ChannelHandler handler) throws Exception {
    return new EmbeddedChannel(
            new HttpClientCodec(),
            new HttpObjectAggregator(8192),
            new WebSocketClientProtocolHandler(new URI("ws://localhost:1234/test"),
                                               WebSocketVersion.V13, "test-proto-2",
                                               false, null, 65536),
            handler);
}
 
Example 30
Source Project: netty-4.1.22   Source File: Http2MultiplexCodecBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static ChannelHandler checkSharable(ChannelHandler handler) {
    if ((handler instanceof ChannelHandlerAdapter && !((ChannelHandlerAdapter) handler).isSharable()) &&
            !handler.getClass().isAnnotationPresent(ChannelHandler.Sharable.class)) {
        throw new IllegalArgumentException("The handler must be Sharable");
    }
    return handler;
}