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:org.jmqtt.broker.acceptor.NettyAcceptor.java

License:Open Source License

private void initializePlainTCPTransport(final NettyMQTTHandler handler, AcceptorProperties props)
        throws IOException {
    final IdleTimeoutHandler timeoutHandler = new IdleTimeoutHandler();
    String host = props.getHost();
    boolean mqttEnable = props.getMqttEnable();
    if (!mqttEnable) {
        LOG.info("tcp MQTT is disabled");
        return;//from  w  ww. j a  v a2  s.  c  o m
    }
    int port = props.getMqttPort();
    initFactory(host, port, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) {
            pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, props.getConnectTimeout()));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
            //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:org.jmqtt.broker.acceptor.NettyAcceptor.java

License:Open Source License

private void initializeWebSocketTransport(final NettyMQTTHandler handler, AcceptorProperties props)
        throws IOException {
    if (!props.getWsEnable()) {
        //Do nothing no WebSocket configured
        LOG.info("WebSocket is disabled");
        return;//from  w  w w.j a  v a  2  s  . c om
    }
    int port = props.getWsPort();

    final IdleTimeoutHandler timeoutHandler = new IdleTimeoutHandler();

    String host = props.getHost();
    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_SUBPROTOCOL_CSV_LIST));
            pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
            pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
            pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, props.getConnectTimeout()));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
            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:org.jmqtt.broker.acceptor.NettyAcceptor.java

License:Open Source License

private void initializeSSLTCPTransport(final NettyMQTTHandler handler, AcceptorProperties props,
        final SSLContext sslContext) throws IOException {
    if (props.getMqttsEnable()) {
        //Do nothing no SSL configured
        LOG.info("SSL MQTT is disabled");
        return;/* ww w .j av  a 2  s.com*/
    }

    int sslPort = props.getMqttsPort();
    LOG.info("Starting SSL on port {}", sslPort);

    final IdleTimeoutHandler timeoutHandler = new IdleTimeoutHandler();
    String host = props.getHost();
    final boolean needsClientAuth = props.getNeedsClientAuth();
    initFactory(host, sslPort, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) throws Exception {
            pipeline.addLast("ssl", createSslHandler(sslContext, needsClientAuth));
            pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, props.getConnectTimeout()));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
            //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:org.jmqtt.broker.acceptor.NettyAcceptor.java

License:Open Source License

private void initializeWSSTransport(final NettyMQTTHandler handler, AcceptorProperties props,
        final SSLContext sslContext) throws IOException {
    if (!props.getWssEnable()) {
        //Do nothing no SSL configured
        LOG.info("SSL websocket is disabled");
        return;//from  w ww  .  j a  va2 s.  c om
    }
    int sslPort = props.getWssPort();
    final IdleTimeoutHandler timeoutHandler = new IdleTimeoutHandler();
    String host = props.getHost();
    final boolean needsClientAuth = props.getNeedsClientAuth();
    initFactory(host, sslPort, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) throws Exception {
            pipeline.addLast("ssl", createSslHandler(sslContext, needsClientAuth));
            pipeline.addLast("httpEncoder", new HttpResponseEncoder());
            pipeline.addLast("httpDecoder", new HttpRequestDecoder());
            pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
            pipeline.addLast("webSocketHandler",
                    new WebSocketServerProtocolHandler("/mqtt", MQTT_SUBPROTOCOL_CSV_LIST));
            pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
            pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
            pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, props.getConnectTimeout()));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
            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:org.jooby.internal.netty.NettyResponse.java

License:Apache License

private void chunkHandler(final ChannelPipeline pipeline) {
    if (pipeline.get("chunker") == null) {
        pipeline.addAfter("codec", "chunker", new ChunkedWriteHandler());
    }/* w  w w  .j av  a2  s.  c o m*/
}

From source file:org.opendaylight.openflowjava.protocol.impl.core.TlsDetector.java

License:Open Source License

private static void enableSsl(ChannelHandlerContext ctx) {
    if (ctx.pipeline().get(COMPONENT_NAMES.SSL_HANDLER.name()) == null) {
        LOGGER.trace("Engaging TLS handler");
        ChannelPipeline p = ctx.channel().pipeline();
        SSLEngine engine = SslContextFactory.getServerContext().createSSLEngine();
        engine.setUseClientMode(false);//from   w w  w .  ja va  2  s. co  m
        p.addAfter(COMPONENT_NAMES.TLS_DETECTOR.name(), COMPONENT_NAMES.SSL_HANDLER.name(),
                new SslHandler(engine));
    }
}

From source file:org.springframework.web.reactive.socket.adapter.RxNettyWebSocketSession.java

License:Apache License

/**
 * 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
 *//*from   w  w w  . ja  v  a 2s.  c om*/
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;
}

From source file:org.wso2.custom.inbound.InboundHttp2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a clear text upgrade from HTTP to HTTP/2.0
 *//*www. j a v  a2s.c  o  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.
            log.info("No upgrade done: continue with " + msg.protocolVersion());
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null, new InboundHttpSourceHandler(config));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(msg);
        }
    });
    p.addLast(new UserEventLogger());
}

From source file:org.wso2.esb.integration.common.utils.servers.http2.Http2ServerInitializer.java

License:Open Source License

private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();

    p.addLast(sourceCodec);//from  ww w .  j a  va 2  s .  c om
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null, new Http1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(msg);
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:reactor.ipc.netty.channel.ContextHandler.java

License:Open Source License

static void addSslAndLogHandlers(NettyOptions<?, ?> options, MonoSink<NettyContext> sink,
        LoggingHandler loggingHandler, boolean secure, ChannelPipeline pipeline) {
    SslHandler sslHandler = secure ? options.getSslHandler(pipeline.channel().alloc()) : null;
    if (sslHandler != null) {
        if (log.isDebugEnabled()) {
            log.debug("SSL enabled using engine {}", sslHandler.engine().getClass().getSimpleName());
        }/*  w ww  .java 2  s . c o  m*/
        if (log.isTraceEnabled()) {
            pipeline.addFirst(NettyPipeline.SslLoggingHandler, new LoggingHandler(SslReadHandler.class));
            pipeline.addAfter(NettyPipeline.SslLoggingHandler, NettyPipeline.SslHandler, sslHandler);
        } else {
            pipeline.addFirst(NettyPipeline.SslHandler, sslHandler);
        }
        if (log.isDebugEnabled()) {
            pipeline.addAfter(NettyPipeline.SslHandler, NettyPipeline.LoggingHandler, loggingHandler);
            pipeline.addAfter(NettyPipeline.LoggingHandler, NettyPipeline.SslReader, new SslReadHandler(sink));
        } else {
            pipeline.addAfter(NettyPipeline.SslHandler, NettyPipeline.SslReader, new SslReadHandler(sink));
        }
    } else if (log.isDebugEnabled()) {
        pipeline.addFirst(NettyPipeline.LoggingHandler, loggingHandler);
    }
}