Example usage for io.netty.channel ChannelPipeline addFirst

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

Introduction

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

Prototype

ChannelPipeline addFirst(EventExecutorGroup group, ChannelHandler... handlers);

Source Link

Document

Inserts ChannelHandler s at the first position of this pipeline.

Usage

From source file:io.moquette.server.netty.NettyAcceptor__.java

License:Open Source License

private void initializeWSSTransport(final NettyMQTTHandler__ handler, IConfig props,
        final SSLContext sslContext) throws IOException {
    String sslPortProp = props.getProperty(WSS_PORT_PROPERTY_NAME, DISABLED_PORT_BIND);
    if (DISABLED_PORT_BIND.equals(sslPortProp)) {
        //Do nothing no SSL configured
        LOG.info("SSL websocket is disabled because there is no value in properties for key {}",
                BrokerConstants.WSS_PORT_PROPERTY_NAME);
        return;//w  ww.  j  ava2 s.c om
    }
    int sslPort = Integer.parseInt(sslPortProp);
    final MoquetteIdleTimeoutHandler timeoutHandler = new MoquetteIdleTimeoutHandler();
    String host = props.getProperty(BrokerConstants.HOST_PROPERTY_NAME);
    String sNeedsClientAuth = props.getProperty(BrokerConstants.NEED_CLIENT_AUTH, "false");
    final boolean needsClientAuth = Boolean.valueOf(sNeedsClientAuth);
    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, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler);
            pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
            pipeline.addLast("messageLogger", new MQTTMessageLogger());
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:io.moquette.spi.impl.ProtocolProcessor.java

License:Open Source License

private void setupAutoFlusher(ChannelPipeline pipeline, int flushIntervalMs) {
    AutoFlushHandler autoFlushHandler = new AutoFlushHandler(flushIntervalMs, TimeUnit.MILLISECONDS);
    try {//  w  w  w. jav a  2 s  .  c o  m
        pipeline.addAfter("idleEventHandler", "autoFlusher", autoFlushHandler);
    } catch (NoSuchElementException nseex) {
        //the idleEventHandler is not present on the pipeline
        pipeline.addFirst("autoFlusher", autoFlushHandler);
    }
}

From source file:io.moquette.spi.impl.ProtocolProcessor.java

License:Open Source License

private void setIdleTime(ChannelPipeline pipeline, int idleTime) {
    if (pipeline.names().contains("idleStateHandler")) {
        pipeline.remove("idleStateHandler");
    }//from   ww  w  .j a  v  a 2 s  . c  o m
    pipeline.addFirst("idleStateHandler", new IdleStateHandler(0, 0, idleTime));
}

From source file:io.moquette.spi.impl.ProtocolProcessor__.java

License:Open Source License

private void setupAutoFlusher(ChannelPipeline pipeline, int flushIntervalMs) {
    try {// w  ww.ja  va2 s.c  o  m
        pipeline.addAfter("idleEventHandler", "autoFlusher",
                new AutoFlushHandler(flushIntervalMs, TimeUnit.MILLISECONDS));
    } catch (NoSuchElementException nseex) {
        //the idleEventHandler is not present on the pipeline
        pipeline.addFirst("autoFlusher", new AutoFlushHandler(flushIntervalMs, TimeUnit.MILLISECONDS));
    }
}

From source file:io.reactivex.netty.pipeline.ReadTimeoutPipelineConfigurator.java

License:Apache License

@Override
public void configureNewPipeline(ChannelPipeline pipeline) {
    pipeline.addFirst(READ_TIMEOUT_LIFECYCLE_MANAGER_HANDLER_NAME, new ReadTimeoutHandlerLifecycleManager());
}

From source file:io.reactivex.netty.pipeline.RxRequiredConfigurator.java

License:Apache License

@Override
public void configureNewPipeline(ChannelPipeline pipeline) {

    /**/*from  w ww .  j  a v a  2 s  . c om*/
     * This method is called for each new connection & the following two channel handlers are not shareable, so
     * we need to create a new instance every time.
     */
    ChannelHandler lifecycleHandler = newConnectionLifecycleHandler(pipeline);
    ObservableAdapter observableAdapter = new ObservableAdapter();

    pipeline.addFirst(BYTES_INSPECTOR_HANDLER_NAME, bytesInspector);
    pipeline.addLast(getConnectionLifecycleHandlerExecutor(), CONN_LIFECYCLE_HANDLER_NAME, lifecycleHandler);
    pipeline.addLast(getObservableAdapterExecutor(), NETTY_OBSERVABLE_ADAPTER_NAME, observableAdapter);
}

From source file:io.reactivex.netty.protocol.http.sse.SseChannelHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpResponse) {

        /**/*from  w  w  w . ja va 2s .  c  o m*/
         * Since SSE is an endless stream, we can never reuse a connection and hence as soon as SSE traffic is
         * received, the connection is marked as discardable on close.
         */
        ctx.channel().attr(ClientRequestResponseConverter.DISCARD_CONNECTION).set(true); // SSE traffic should always discard connection on close.

        ChannelPipeline pipeline = ctx.channel().pipeline();
        if (!HttpHeaders.isTransferEncodingChunked((HttpResponse) msg)) {
            pipeline.addFirst(SSE_DECODER_HANDLER_NAME, new ServerSentEventDecoder());
            /*
             * If there are buffered messages in the previous handler at the time this message is read, we would
             * not be able to convert the content into an SseEvent. For this reason, we also add the decoder after
             * this handler, so that we can handle the buffered messages.
             * See the class level javadoc for more details.
             */
            pipeline.addAfter(NAME, SSE_DECODER_POST_INBOUND_HANDLER, new ServerSentEventDecoder());
        } else {
            pipeline.addAfter(NAME, SSE_DECODER_HANDLER_NAME, new ServerSentEventDecoder());
        }
        ctx.fireChannelRead(msg);
    } else if (msg instanceof LastHttpContent) {
        LastHttpContent lastHttpContent = (LastHttpContent) msg;

        /**
         * The entire pipeline is set based on the assumption that LastHttpContent signals the end of the stream.
         * Since, here we are only passing the content to the rest of the pipeline, it becomes imperative to
         * also pass LastHttpContent as such.
         * For this reason, we send the LastHttpContent again in the pipeline. For this event sent, the content
         * buffer will already be read and hence will not be read again. This message serves as only containing
         * the trailing headers.
         * However, we need to increment the ref count of the content so that the assumptions down the line of the
         * ByteBuf always being released by the last pipeline handler will not break (as ServerSentEventDecoder releases
         * the ByteBuf after read).
         */
        lastHttpContent.content().retain(); // pseudo retain so that the last handler of the pipeline can release it.

        if (lastHttpContent.content().isReadable()) {
            ctx.fireChannelRead(lastHttpContent.content());
        }

        ctx.fireChannelRead(msg); // Since the content is already consumed above (by the SSEDecoder), this is just
        // as sending just trailing headers. This is critical to mark the end of stream.

    } else if (msg instanceof HttpContent) {
        ctx.fireChannelRead(((HttpContent) msg).content());
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:io.reactivex.netty.protocol.http.sse.SSEInboundHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpResponse) {
        ChannelPipeline pipeline = ctx.channel().pipeline();
        if (!HttpHeaders.isTransferEncodingChunked((HttpResponse) msg)) {
            pipeline.addFirst(SSE_DECODER_HANDLER_NAME, new ServerSentEventDecoder());
            /*/*from   w  w w.j  a v a 2 s  .  c o m*/
             * If there are buffered messages in the previous handler at the time this message is read, we would
             * not be able to convert the content into an SseEvent. For this reason, we also add the decoder after
             * this handler, so that we can handle the buffered messages.
             * See the class level javadoc for more details.
             */
            pipeline.addAfter(NAME, SSE_DECODER_POST_INBOUND_HANDLER, new ServerSentEventDecoder());
        } else {
            pipeline.addAfter(NAME, SSE_DECODER_HANDLER_NAME, new ServerSentEventDecoder());
        }
        ctx.fireChannelRead(msg);
    } else if (msg instanceof LastHttpContent) {
        LastHttpContent lastHttpContent = (LastHttpContent) msg;

        /**
         * The entire pipeline is set based on the assumption that LastHttpContent signals the end of the stream.
         * Since, here we are only passing the content to the rest of the pipeline, it becomes imperative to
         * also pass LastHttpContent as such.
         * For this reason, we send the LastHttpContent again in the pipeline. For this event sent, the content
         * buffer will already be read and hence will not be read again. This message serves as only containing
         * the trailing headers.
         * However, we need to increment the ref count of the content so that the assumptions down the line of the
         * ByteBuf always being released by the last pipeline handler will not break (as ServerSentEventDecoder releases
         * the ByteBuf after read).
         */
        lastHttpContent.content().retain(); // pseudo retain so that the last handler of the pipeline can release it.

        if (lastHttpContent.content().isReadable()) {
            ctx.fireChannelRead(lastHttpContent.content());
        }

        ctx.fireChannelRead(msg); // Since the content is already consumed above (by the SSEDecoder), this is just
                                  // as sending just trailing headers. This is critical to mark the end of stream.

    } else if (msg instanceof HttpContent) {
        ctx.fireChannelRead(((HttpContent) msg).content());
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:io.vertx.core.http.impl.HttpChannelConnector.java

License:Open Source License

@Override
public void activate(HttpClientConnection conn) {
    if (options.getIdleTimeout() > 0) {
        ChannelPipeline pipeline = conn.channelHandlerContext().pipeline();
        pipeline.addFirst("idle",
                new IdleStateHandler(0, 0, options.getIdleTimeout(), options.getIdleTimeoutUnit()));
    }//from w  ww  .j  a  va 2 s.  c  om
}

From source file:me.ferrybig.javacoding.webmapper.netty.WebSslServerInitializer.java

@Override
public void initChannel(SocketChannel ch) throws Exception {
    super.initChannel(ch);
    ChannelPipeline pipeline = ch.pipeline();

    // handle ssl
    pipeline.addFirst("ssl-translator", sslCtx.newHandler(ch.alloc()));

}