Example usage for io.netty.channel ChannelPipeline remove

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

Introduction

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

Prototype

<T extends ChannelHandler> T remove(Class<T> handlerType);

Source Link

Document

Removes the ChannelHandler of the specified type from this pipeline.

Usage

From source file:com.linecorp.armeria.client.http.HttpClientPipelineConfigurator.java

License:Apache License

/**
 * @see <a href="https://http2.github.io/http2-spec/#discover-https">HTTP/2 specification</a>
 *///from  www  . j  av a2s  . c  o m
private void configureAsHttps(Channel ch) {
    final ChannelPipeline p = ch.pipeline();
    final SslHandler sslHandler = sslCtx.newHandler(ch.alloc());
    p.addLast(sslHandler);
    p.addLast(new ChannelInboundHandlerAdapter() {
        @Override
        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(p, 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(p, newHttp1Codec());
                protocol = H1;
            }
            finishSuccessfully(p, protocol);
            p.remove(this);
        }

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

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

License:Apache License

@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
        ChannelPromise promise) throws Exception {

    // Remember the requested remote address for later use.
    final InetSocketAddress inetRemoteAddr = (InetSocketAddress) remoteAddress;
    this.remoteAddress = inetRemoteAddr;

    // Configure the pipeline.
    final Channel ch = ctx.channel();

    final ChannelPipeline p = ch.pipeline();
    p.addLast(new FlushConsolidationHandler());
    p.addLast(ReadSuppressingHandler.INSTANCE);

    try {/*  w  w  w.j av a2  s  . c om*/
        if (sslCtx != null) {
            configureAsHttps(ch, inetRemoteAddr);
        } else {
            configureAsHttp(ch);
        }
    } catch (Throwable t) {
        promise.tryFailure(t);
        ctx.close();
    } finally {
        if (p.context(this) != null) {
            p.remove(this);
        }
    }

    ctx.connect(remoteAddress, localAddress, promise);
}

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

License:Apache License

/**
 * See <a href="https://http2.github.io/http2-spec/#discover-https">HTTP/2 specification</a>.
 *///from w  w  w. jav a2s . com
private void configureAsHttps(Channel ch, InetSocketAddress remoteAddr) {
    assert sslCtx != null;

    final ChannelPipeline p = ch.pipeline();
    final SslHandler sslHandler = sslCtx.newHandler(ch.alloc(), remoteAddr.getHostString(),
            remoteAddr.getPort());
    p.addLast(sslHandler);
    p.addLast(TrafficLoggingHandler.CLIENT);
    p.addLast(new ChannelInboundHandlerAdapter() {
        @Override
        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(p, 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(p, newHttp1Codec(clientFactory.maxHttp1InitialLineLength(),
                        clientFactory.maxHttp1HeaderSize(), clientFactory.maxHttp1ChunkSize()));
                protocol = H1;
            }
            finishSuccessfully(p, protocol);
            p.remove(this);
        }

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

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

License:Apache License

@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
        ChannelPromise promise) throws Exception {

    // Remember the requested remote address for later use.
    this.remoteAddress = (InetSocketAddress) remoteAddress;

    // Configure the pipeline.
    final Channel ch = ctx.channel();
    try {//w ww  .  ja v  a2 s .com
        if (sslCtx != null) {
            configureAsHttps(ch);
        } else {
            configureAsHttp(ch);
        }
    } catch (Throwable t) {
        promise.tryFailure(t);
        ctx.close();
    } finally {
        final ChannelPipeline pipeline = ch.pipeline();
        if (pipeline.context(this) != null) {
            pipeline.remove(this);
        }
    }

    ctx.connect(remoteAddress, localAddress, promise);
}

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);/*www  .j  a va2  s  .c  om*/
    pipeline.addLast(new ChannelInboundHandlerAdapter() {
        @Override
        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.common.http.Http1ClientCodec.java

License:Apache License

/**
 * Upgrades to another protocol from HTTP. Removes the {@link Decoder} and {@link Encoder} from
 * the pipeline.//from   w w  w  .  j  av a2  s. c om
 */
@Override
public void upgradeFrom(ChannelHandlerContext ctx) {
    final ChannelPipeline p = ctx.pipeline();
    p.remove(this);
}

From source file:com.netiq.websockify.PortUnificationHandler.java

License:Apache License

private void enableSsl(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.getPipeline();

    Logger.getLogger(PortUnificationHandler.class.getName())
            .fine("SSL request from " + ctx.getChannel().getRemoteAddress() + ".");

    SSLEngine engine = WebsockifySslContext.getInstance(keystore, keystorePassword).getServerContext()
            .createSSLEngine();//from   w  ww.  ja  v a  2  s  .c o  m
    engine.setUseClientMode(false);

    p.addLast("ssl", new SslHandler(engine));
    p.addLast("unificationA", new PortUnificationHandler(cf, resolver, SSLSetting.OFF, keystore,
            keystorePassword, webDirectory, ctx));
    p.remove(this);
}

From source file:com.netiq.websockify.PortUnificationHandler.java

License:Apache License

private void switchToWebsocketProxy(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.getPipeline();

    Logger.getLogger(PortUnificationHandler.class.getName())
            .fine("Websocket proxy request from " + ctx.getChannel().getRemoteAddress() + ".");

    p.addLast("decoder", new HttpRequestDecoder());
    p.addLast("aggregator", new HttpChunkAggregator(65536));
    p.addLast("encoder", new HttpResponseEncoder());
    p.addLast("chunkedWriter", new ChunkedWriteHandler());
    p.addLast("handler", new WebsockifyProxyHandler(cf, resolver, webDirectory));
    p.remove(this);
}

From source file:com.netiq.websockify.PortUnificationHandler.java

License:Apache License

private void switchToFlashPolicy(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.getPipeline();

    Logger.getLogger(PortUnificationHandler.class.getName())
            .fine("Flash policy request from " + ctx.getChannel().getRemoteAddress() + ".");

    p.addLast("flash", new FlashPolicyHandler());

    p.remove(this);
}

From source file:com.netiq.websockify.PortUnificationHandler.java

License:Apache License

private void switchToDirectProxy(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.getPipeline();

    Logger.getLogger(PortUnificationHandler.class.getName())
            .fine("Direct proxy request from " + ctx.getChannel().getRemoteAddress() + ".");

    p.addLast("proxy", new DirectProxyHandler(ctx.getChannel(), cf, resolver));

    p.remove(this);
}