Example usage for io.netty.channel ChannelPipeline addAfter

List of usage examples for io.netty.channel ChannelPipeline addAfter

Introduction

In this page you can find the example usage for io.netty.channel ChannelPipeline addAfter.

Prototype

ChannelPipeline addAfter(String baseName, String name, ChannelHandler handler);

Source Link

Document

Inserts a ChannelHandler after an existing handler of this pipeline.

Usage

From source file:com.flysoloing.learning.network.netty.http2.helloworld.multiplex.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *//*from   ww  w  .j a v  a 2s.  com*/
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();

    p.addLast(sourceCodec);
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null,
                    new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:com.google.cloud.pubsub.proxy.moquette.NettyAcceptor.java

License:Open Source License

private void initializePlainTcpTransport(IMessaging messaging, Properties props) throws IOException {
    final NettyMQTTHandler mqttHandler = new NettyMQTTHandler();
    final PubsubHandler handler = new PubsubHandler(pubsub, mqttHandler);
    handler.setMessaging(messaging);//from www.j  av a2 s . c o m
    String host = props.getProperty(Constants.HOST_PROPERTY_NAME);
    int port = Integer.parseInt(props.getProperty(Constants.PORT_PROPERTY_NAME));
    initFactory(host, port, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) {
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimeoutHandler());
            //pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
            pipeline.addFirst("bytemetrics", new BytesMetricsHandler(bytesMetricsCollector));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:com.google.cloud.pubsub.proxy.moquette.NettyAcceptor.java

License:Open Source License

private void initializeWebSocketTransport(IMessaging messaging, Properties props) throws IOException {
    String webSocketPortProp = props.getProperty(Constants.WEB_SOCKET_PORT_PROPERTY_NAME);
    if (webSocketPortProp == null) {
        //Do nothing no WebSocket configured
        LOG.info("WebSocket is disabled");
        return;//from ww  w . j  a v  a2 s .co m
    }
    int port = Integer.parseInt(webSocketPortProp);

    final NettyMQTTHandler mqttHandler = new NettyMQTTHandler();
    final PubsubHandler handler = new PubsubHandler(pubsub, mqttHandler);
    handler.setMessaging(messaging);

    String host = props.getProperty(Constants.HOST_PROPERTY_NAME);
    initFactory(host, port, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) {
            pipeline.addLast("httpEncoder", new HttpResponseEncoder());
            pipeline.addLast("httpDecoder", new HttpRequestDecoder());
            pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
            pipeline.addLast("webSocketHandler",
                    new WebSocketServerProtocolHandler("/mqtt"/*"/mqtt"*/, "mqttv3.1, mqttv3.1.1"));
            //pipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler(null, "mqtt"));
            pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
            pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimeoutHandler());
            pipeline.addFirst("bytemetrics", new BytesMetricsHandler(bytesMetricsCollector));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:com.google.cloud.pubsub.proxy.moquette.NettyAcceptor.java

License:Open Source License

private void initializeSslTcpTransport(IMessaging messaging, Properties props, final SslHandler sslHandler)
        throws IOException {
    String sslPortProp = props.getProperty(Constants.SSL_PORT_PROPERTY_NAME);
    if (sslPortProp == null) {
        //Do nothing no SSL configured
        LOG.info("SSL is disabled");
        return;// w w  w.j av a 2 s .  co  m
    }

    int sslPort = Integer.parseInt(sslPortProp);
    LOG.info("Starting SSL on port {}", sslPort);

    final NettyMQTTHandler mqttHandler = new NettyMQTTHandler();
    final PubsubHandler handler = new PubsubHandler(pubsub, mqttHandler);
    handler.setMessaging(messaging);
    String host = props.getProperty(Constants.HOST_PROPERTY_NAME);
    initFactory(host, sslPort, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) throws Exception {
            pipeline.addLast("ssl", sslHandler);
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimeoutHandler());
            //pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
            pipeline.addFirst("bytemetrics", new BytesMetricsHandler(bytesMetricsCollector));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:com.google.cloud.pubsub.proxy.moquette.NettyAcceptor.java

License:Open Source License

private void initializeWssTransport(IMessaging messaging, Properties props, final SslHandler sslHandler)
        throws IOException {
    String sslPortProp = props.getProperty(Constants.WSS_PORT_PROPERTY_NAME);
    if (sslPortProp == null) {
        //Do nothing no SSL configured
        LOG.info("SSL is disabled");
        return;/* www .j a va 2s . com*/
    }
    int sslPort = Integer.parseInt(sslPortProp);
    final NettyMQTTHandler mqttHandler = new NettyMQTTHandler();
    final PubsubHandler handler = new PubsubHandler(pubsub, mqttHandler);
    handler.setMessaging(messaging);
    String host = props.getProperty(Constants.HOST_PROPERTY_NAME);
    initFactory(host, sslPort, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) throws Exception {
            pipeline.addLast("ssl", sslHandler);
            pipeline.addLast("httpEncoder", new HttpResponseEncoder());
            pipeline.addLast("httpDecoder", new HttpRequestDecoder());
            pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
            pipeline.addLast("webSocketHandler",
                    new WebSocketServerProtocolHandler("/mqtt", "mqttv3.1, mqttv3.1.1"));
            pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
            pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimeoutHandler());
            pipeline.addFirst("bytemetrics", new BytesMetricsHandler(bytesMetricsCollector));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:com.googlecode.protobuf.pro.duplex.client.DuplexTcpClientPipelineFactory.java

License:Apache License

/**
 * After RPC handshake has taken place, remove the RPC handshake
 * {@link ClientConnectResponseHandler} and add a {@link RpcClientHandler}
 * and {@link RpcServerHandler} to complete the Netty client side Pipeline.
 * // w w  w. j a  v a 2 s . co m
 * @param rpcClient
 * @return
 */
protected RpcClientHandler completePipeline(RpcClient rpcClient) {
    ChannelPipeline p = rpcClient.getChannel().pipeline();

    if (rpcClient.isCompression()) {
        p.addBefore(Handler.FRAME_DECODER, Handler.COMPRESSOR,
                ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
        p.addAfter(Handler.COMPRESSOR, Handler.DECOMPRESSOR, ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    }

    TcpConnectionEventListener informer = new TcpConnectionEventListener() {
        @Override
        public void connectionClosed(RpcClientChannel client) {
            for (TcpConnectionEventListener listener : getListenersCopy()) {
                listener.connectionClosed(client);
            }
        }

        @Override
        public void connectionOpened(RpcClientChannel client) {
            for (TcpConnectionEventListener listener : getListenersCopy()) {
                listener.connectionOpened(client);
            }
        }
    };
    RpcClientHandler rpcClientHandler = new RpcClientHandler(rpcClient, informer);
    p.replace(Handler.CLIENT_CONNECT, Handler.RPC_CLIENT, rpcClientHandler);

    RpcServer rpcServer = new RpcServer(rpcClient, rpcServiceRegistry, rpcServerCallExecutor, logger);
    RpcServerHandler rpcServerHandler = new RpcServerHandler(rpcServer, rpcClientRegistry);
    p.addAfter(Handler.RPC_CLIENT, Handler.RPC_SERVER, rpcServerHandler);

    return rpcClientHandler;
}

From source file:com.googlecode.protobuf.pro.duplex.server.DuplexTcpServerPipelineFactory.java

License:Apache License

public RpcClientHandler completePipeline(RpcClient rpcClient) {
    ChannelPipeline p = rpcClient.getChannel().pipeline();

    if (rpcClient.isCompression()) {
        p.addBefore(Handler.FRAME_DECODER, Handler.COMPRESSOR,
                ZlibCodecFactory.newZlibEncoder(ZlibWrapper.GZIP));
        p.addAfter(Handler.COMPRESSOR, Handler.DECOMPRESSOR, ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
    }/*from   ww  w  .  ja  v  a 2  s. c  o m*/

    TcpConnectionEventListener informer = new TcpConnectionEventListener() {
        @Override
        public void connectionClosed(RpcClientChannel client) {
            for (TcpConnectionEventListener listener : getListenersCopy()) {
                listener.connectionClosed(client);
            }
        }

        @Override
        public void connectionOpened(RpcClientChannel client) {
            for (TcpConnectionEventListener listener : getListenersCopy()) {
                listener.connectionOpened(client);
            }
        }
    };

    RpcClientHandler rpcClientHandler = new RpcClientHandler(rpcClient, informer);
    p.replace(Handler.SERVER_CONNECT, Handler.RPC_CLIENT, rpcClientHandler);

    RpcServer rpcServer = new RpcServer(rpcClient, getRpcServiceRegistry(), getRpcServerCallExecutor(),
            getLogger());
    RpcServerHandler rpcServerHandler = new RpcServerHandler(rpcServer, getRpcClientRegistry());
    p.addAfter(Handler.RPC_CLIENT, Handler.RPC_SERVER, rpcServerHandler);

    if (log.isDebugEnabled()) {
        log.debug("completed Pipeline to " + rpcClient.getPeerInfo());
    }
    return rpcClientHandler;
}

From source file:com.heliosapm.streams.onramp.GZipDetector.java

License:Apache License

private void enableGzip(final ChannelHandlerContext ctx) {
    final ChannelPipeline p = ctx.pipeline();
    try {/*ww w  .ja  va  2 s. co  m*/
        //         p.addAfter("connmgr", "gzipdeflater", new JZlibEncoder(ZlibWrapper.GZIP){
        //            // TODO
        //         });            
        p.addAfter("gzipdetector", "gzipinflater", ZlibCodecFactory.newZlibDecoder(ZlibWrapper.GZIP));
        p.remove(this);
    } catch (Exception ex) {
        log.error("Failed to add gzip handlers", ex);
    }
}

From source file:com.hop.hhxx.example.http2.helloworld.multiplex.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *//*from   w  w  w .ja  va2 s.co m*/
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();

    p.addLast(sourceCodec);
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            System.err.println("Directly talking: " + msg.protocolVersion() + " (no upgrade was attempted)");
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null,
                    new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(msg);
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:com.jansegre.jwar.webapi.ApiInitializer.java

License:Open Source License

@Override
protected void initChannel(Channel ch) throws Exception {
    super.initChannel(ch);
    ChannelPipeline pipeline = ch.pipeline();
    //pipeline.remove(RESOURCE_HANDLER);
    pipeline.addAfter(HTTP_ENCODER, HTTP_STATIC_HANDLER, new FileServerHandler());
}