Example usage for io.netty.channel ChannelPipeline addLast

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

Introduction

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

Prototype

ChannelPipeline addLast(ChannelHandler... handlers);

Source Link

Document

Inserts ChannelHandler s at the last position of this pipeline.

Usage

From source file:com.linecorp.armeria.client.HttpConfigurator.java

License:Apache License

private void configureAsHttps(Channel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    SslHandler sslHandler = sslCtx.newHandler(ch.alloc());
    pipeline.addLast(sslHandler);
    pipeline.addLast(new ChannelInboundHandlerAdapter() {
        @Override//from w  ww . j a v a2  s .c o m
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            if (!(evt instanceof SslHandshakeCompletionEvent)) {
                ctx.fireUserEventTriggered(evt);
                return;
            }

            final SslHandshakeCompletionEvent handshakeEvent = (SslHandshakeCompletionEvent) evt;
            if (!handshakeEvent.isSuccess()) {
                // The connection will be closed automatically by SslHandler.
                return;
            }

            final SessionProtocol protocol;
            if (isHttp2Protocol(sslHandler)) {
                if (httpPreference == HttpPreference.HTTP1_REQUIRED) {
                    finishWithNegotiationFailure(ctx, H1, H2, "unexpected protocol negotiation result");
                    return;
                }

                addBeforeSessionHandler(pipeline, newHttp2ConnectionHandler(ch));
                protocol = H2;
            } else {
                if (httpPreference != HttpPreference.HTTP1_REQUIRED) {
                    SessionProtocolNegotiationCache.setUnsupported(ctx.channel().remoteAddress(), H2);
                }

                if (httpPreference == HttpPreference.HTTP2_REQUIRED) {
                    finishWithNegotiationFailure(ctx, H2, H1, "unexpected protocol negotiation result");
                    return;
                }

                addBeforeSessionHandler(pipeline, newHttp1Codec());
                protocol = H1;
            }
            finishSuccessfully(pipeline, protocol);
            pipeline.remove(this);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            Exceptions.logIfUnexpected(logger, ctx.channel(), null, cause);
            ctx.close();
        }
    });
}

From source file:com.linecorp.armeria.client.HttpConfigurator.java

License:Apache License

private void configureAsHttp(Channel ch) {
    final ChannelPipeline pipeline = ch.pipeline();

    final boolean attemptUpgrade;
    switch (httpPreference) {
    case HTTP1_REQUIRED:
        attemptUpgrade = false;/*from  w  ww .  j a  v  a 2s. c o m*/
        break;
    case HTTP2_PREFERRED:
        attemptUpgrade = !SessionProtocolNegotiationCache.isUnsupported(remoteAddress, H2C);
        break;
    case HTTP2_REQUIRED:
        attemptUpgrade = true;
        break;
    default:
        // Should never reach here.
        throw new Error();
    }

    if (attemptUpgrade) {
        if (options.useHttp2Preface()) {
            pipeline.addLast(new DowngradeHandler());
            pipeline.addLast(newHttp2ConnectionHandler(ch));
        } else {
            Http1ClientCodec http1Codec = newHttp1Codec();
            Http2ClientUpgradeCodec http2ClientUpgradeCodec = new Http2ClientUpgradeCodec(
                    newHttp2ConnectionHandler(ch));
            HttpClientUpgradeHandler http2UpgradeHandler = new HttpClientUpgradeHandler(http1Codec,
                    http2ClientUpgradeCodec, options.maxFrameLength());

            pipeline.addLast(http1Codec);
            pipeline.addLast(new WorkaroundHandler());
            pipeline.addLast(http2UpgradeHandler);
            pipeline.addLast(new UpgradeRequestHandler());
        }
    } else {
        pipeline.addLast(newHttp1Codec());

        // NB: We do not call finishSuccessfully() immediately here
        //     because it assumes HttpSessionHandler to be in the pipeline,
        //     which is only true after the connection attempt is successful.
        pipeline.addLast(new ChannelInboundHandlerAdapter() {
            @Override
            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                ctx.pipeline().remove(this);
                finishSuccessfully(pipeline, H1C);
                ctx.fireChannelActive();
            }
        });
    }
}

From source file:com.linecorp.armeria.server.http.HttpServerPipelineConfigurator.java

License:Apache License

@Override
protected void initChannel(Channel ch) throws Exception {
    final ChannelPipeline p = ch.pipeline();
    p.addLast(new FlushConsolidationHandler());
    p.addLast(ReadSuppressingHandler.INSTANCE);

    if (port.protocol().isTls()) {
        p.addLast(new SniHandler(sslContexts));
        configureHttps(p);//w w  w  .  j ava  2 s .  c o  m
    } else {
        configureHttp(p);
    }
}

From source file:com.linecorp.armeria.server.http.HttpServerPipelineConfigurator.java

License:Apache License

private void configureHttp(ChannelPipeline p) {
    p.addLast(new Http2PrefaceOrHttpHandler());
    configureRequestCountingHandlers(p);
    p.addLast(new HttpServerHandler(config, SessionProtocol.H1C));
}

From source file:com.linecorp.armeria.server.http.HttpServerPipelineConfigurator.java

License:Apache License

private void configureRequestCountingHandlers(ChannelPipeline p) {
    if (config.idleTimeoutMillis() > 0) {
        p.addFirst(new HttpServerIdleTimeoutHandler(config.idleTimeoutMillis()));
    }/*  www.  j av a  2  s . com*/
    gracefulShutdownHandler.ifPresent(h -> {
        h.reset();
        p.addLast(h);
    });
}

From source file:com.linecorp.armeria.server.http.HttpServerPipelineConfigurator.java

License:Apache License

private void configureHttps(ChannelPipeline p) {
    p.addLast(new Http2OrHttpHandler());
}

From source file:com.linecorp.armeria.server.HttpServerPipelineConfigurator.java

License:Apache License

@Override
protected void initChannel(Channel ch) throws Exception {
    final ChannelPipeline p = ch.pipeline();
    p.addLast(new FlushConsolidationHandler());
    p.addLast(ReadSuppressingHandler.INSTANCE);
    configurePipeline(p, port.protocols(), null);
}

From source file:com.linecorp.armeria.server.HttpServerPipelineConfigurator.java

License:Apache License

private void configurePipeline(ChannelPipeline p, Set<SessionProtocol> protocols,
        @Nullable ProxiedAddresses proxiedAddresses) {
    if (protocols.size() == 1) {
        switch (Iterables.getFirst(protocols, null)) {
        case HTTP:
            configureHttp(p, proxiedAddresses);
            break;
        case HTTPS:
            configureHttps(p, proxiedAddresses);
            break;
        default://from w  ww .  j  a  v a  2 s.  c o  m
            // Should never reach here.
            throw new Error();
        }
        return;
    }

    // More than one protocol were specified. Detect the protocol.
    p.addLast(new ProtocolDetectionHandler(protocols, proxiedAddresses));
}

From source file:com.linecorp.armeria.server.HttpServerPipelineConfigurator.java

License:Apache License

private void configureHttp(ChannelPipeline p, @Nullable ProxiedAddresses proxiedAddresses) {
    p.addLast(TrafficLoggingHandler.SERVER);
    p.addLast(new Http2PrefaceOrHttpHandler());
    configureIdleTimeoutHandler(p);/*  www. j a  v  a  2s .co m*/
    p.addLast(new HttpServerHandler(config, gracefulShutdownSupport, SessionProtocol.H1C, proxiedAddresses));
}

From source file:com.linecorp.armeria.server.HttpServerPipelineConfigurator.java

License:Apache License

private void configureHttps(ChannelPipeline p, @Nullable ProxiedAddresses proxiedAddresses) {
    assert sslContexts != null;
    p.addLast(new SniHandler(sslContexts));
    p.addLast(TrafficLoggingHandler.SERVER);
    p.addLast(new Http2OrHttpHandler(proxiedAddresses));
}