Example usage for io.netty.channel ChannelPipeline replace

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

Introduction

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

Prototype

<T extends ChannelHandler> T replace(Class<T> oldHandlerType, String newName, ChannelHandler newHandler);

Source Link

Document

Replaces the ChannelHandler of the specified type with a new handler in this pipeline.

Usage

From source file:dorkbox.network.connection.registration.remote.RegistrationRemoteHandler.java

License:Apache License

/**
 * upgrades a channel ONE channel at a time
 *//*w w  w.  ja v  a  2  s .c o  m*/
final void upgradeEncoders(final Channel channel, final MetaChannel metaChannel,
        final InetSocketAddress remoteAddress) {
    ChannelPipeline pipeline = channel.pipeline();

    try {
        if (metaChannel.tcpChannel == channel) {
            pipeline.replace(FRAME_AND_KRYO_ENCODER, FRAME_AND_KRYO_CRYPTO_ENCODER,
                    registrationWrapper.kryoTcpEncoderCrypto); // this is shared
        }

        if (metaChannel.udpChannel == channel) {
            pipeline.replace(KRYO_ENCODER, KRYO_CRYPTO_ENCODER, registrationWrapper.kryoUdpEncoderCrypto); // shared encoder
        }
    } catch (Exception e) {
        logger.error("Error during connection pipeline upgrade", e);
    }
}

From source file:dorkbox.network.connection.registration.remote.RegistrationRemoteHandler.java

License:Apache License

private void cleanupPipeline0(final int idleTimeout, final ChannelHandler connection, final Channel channel,
        final ChannelPromise channelPromise, final boolean isTcp) {
    final ChannelPipeline pipeline = channel.pipeline();

    // have to explicitly remove handlers based on the input type. Cannot use this.getClass()....
    boolean isClient = registrationWrapper.isClient();
    if (isClient) {
        if (isTcp) {
            pipeline.remove(RegistrationRemoteHandlerClientTCP.class);
        } else {// ww  w.j  a  v a2s  .co m
            pipeline.remove(RegistrationRemoteHandlerClientUDP.class);
        }
    } else {
        if (isTcp) {
            pipeline.remove(RegistrationRemoteHandlerServerTCP.class);
        } else {
            pipeline.remove(RegistrationRemoteHandlerServerUDP.class);
        }
    }

    pipeline.remove(ConnectionRegistrationImpl.class);

    if (idleTimeout > 0) {
        pipeline.replace(IDLE_HANDLER, IDLE_HANDLER_FULL,
                new IdleStateHandler(0, 0, idleTimeout, TimeUnit.MILLISECONDS));
    } else {
        pipeline.remove(IDLE_HANDLER);
    }

    pipeline.addLast(CONNECTION_HANDLER, connection);

    // we also DEREGISTER from the HANDSHAKE event-loop and run on the worker event-loop!
    ChannelFuture future = channel.deregister();
    future.addListener(new GenericFutureListener<Future<? super Void>>() {
        @Override
        public void operationComplete(final Future<? super Void> f) throws Exception {
            if (f.isSuccess()) {
                // TCP and UDP register on DIFFERENT event loops. The channel promise ONLY runs on 1 of them...
                if (channelPromise.channel() == channel) {
                    workerEventLoop.register(channelPromise);
                } else {
                    workerEventLoop.register(channel);
                }
            }
        }
    });
}

From source file:example.http2.helloworld.server.Http2ServerInitializer.java

License:Apache License

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

    p.addLast(cleartextHttp2ServerUpgradeHandler);
    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:http2.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *//* w  w  w . j  av a  2  s  . 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.
            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:io.advantageous.conekt.http.impl.ClientConnection.java

License:Open Source License

NetSocket createNetSocket() {
    // connection was upgraded to raw TCP socket
    NetSocketImpl socket = new NetSocketImpl(vertx, channel, context, client.getSslHelper(), true, metrics,
            metric);/*from w  ww. j ava  2  s  . co  m*/
    Map<Channel, NetSocketImpl> connectionMap = new HashMap<>(1);
    connectionMap.put(channel, socket);

    // Flush out all pending data
    endReadAndFlush();

    // remove old http handlers and replace the old handler with one that handle plain sockets
    ChannelPipeline pipeline = channel.pipeline();
    ChannelHandler inflater = pipeline.get(HttpContentDecompressor.class);
    if (inflater != null) {
        pipeline.remove(inflater);
    }
    pipeline.remove("codec");
    pipeline.replace("handler", "handler", new ConektNetHandler(connectionMap) {
        @Override
        public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception {
            // remove from the real mapping
            client.removeChannel(channel);
            super.exceptionCaught(chctx, t);
        }

        @Override
        public void channelInactive(ChannelHandlerContext chctx) throws Exception {
            // remove from the real mapping
            client.removeChannel(channel);
            super.channelInactive(chctx);
        }

        @Override
        public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
            if (msg instanceof HttpContent) {
                ReferenceCountUtil.release(msg);
                return;
            }
            super.channelRead(chctx, msg);
        }
    });
    return socket;
}

From source file:io.airlift.drift.transport.netty.server.OptionalSslHandler.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext context, ByteBuf in, List<Object> out) {
    // minimum bytes required to detect ssl
    if (in.readableBytes() < SSL_RECORD_HEADER_LENGTH) {
        return;//from  ww  w  .ja v a 2  s  . c  om
    }

    ChannelPipeline pipeline = context.pipeline();
    if (isTls(in, in.readerIndex())) {
        pipeline.replace(this, "ssl", sslContext.newHandler(context.alloc()));
    } else {
        pipeline.remove(this);
    }
}

From source file:io.higgs.http.client.ClientIntializer.java

License:Apache License

public void configurePipeline(ChannelPipeline pipeline) {
    if (ssl) {//from   w  w  w .j  av a  2 s  .co  m
        addSSL(pipeline, false, sslProtocols);
    }

    if (pipeline.get("codec") == null) {
        pipeline.addLast("codec", new HttpClientCodec());
    } else {
        pipeline.replace("codec", "codec", new HttpClientCodec());
    }
    if (pipeline.get("inflater") == null) {
        pipeline.addLast("inflater", new HttpContentDecompressor());
    } else {
        pipeline.replace("inflater", "inflater", new HttpContentDecompressor());
    }
    if (pipeline.get("chunkedWriter") == null) {
        pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
    } else {
        pipeline.replace("chunkedWriter", "chunkedWriter", new ChunkedWriteHandler());
    }
    //if a connect handler is provided then add it otherwise add the normal response handler
    if (pipeline.get("handler") == null) {
        pipeline.addLast("handler", connectHandler == null ? handler : connectHandler);
    } else {
        pipeline.replace("handler", "handler", connectHandler == null ? handler : connectHandler);
    }
}

From source file:io.jsync.http.impl.ClientConnection.java

License:Open Source License

NetSocket createNetSocket() {
    // connection was upgraded to raw TCP socket
    upgradedConnection = true;/* w ww. j a va2  s.com*/
    DefaultNetSocket socket = new DefaultNetSocket(async, channel, context, client.tcpHelper, true);
    Map<Channel, DefaultNetSocket> connectionMap = new HashMap<Channel, DefaultNetSocket>(1);
    connectionMap.put(channel, socket);

    // Flush out all pending data
    endReadAndFlush();

    // remove old http handlers and replace the old handler with one that handle plain sockets
    ChannelPipeline pipeline = channel.pipeline();
    ChannelHandler inflater = pipeline.get(HttpContentDecompressor.class);
    if (inflater != null) {
        pipeline.remove(inflater);
    }
    pipeline.remove("codec");
    pipeline.replace("handler", "handler", new AsyncNetHandler(client.async, connectionMap) {
        @Override
        public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception {
            // remove from the real mapping
            client.connectionMap.remove(channel);
            super.exceptionCaught(chctx, t);
        }

        @Override
        public void channelInactive(ChannelHandlerContext chctx) throws Exception {
            // remove from the real mapping
            client.connectionMap.remove(channel);
            super.channelInactive(chctx);
        }

        @Override
        public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
            if (msg instanceof HttpContent) {
                ReferenceCountUtil.release(msg);
                return;
            }
            super.channelRead(chctx, msg);
        }
    });
    return socket;
}

From source file:io.netty.example.http2.helloworld.frame.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *//*from www . ja  v a2s .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();
            pipeline.addAfter(ctx.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:io.netty.example.http2.helloworld.server.Http2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a cleartext upgrade from HTTP to HTTP/2.0
 *///from  w  w w . j a  v  a2s .  co  m
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();
    final HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(sourceCodec,
            upgradeCodecFactory);
    final CleartextHttp2ServerUpgradeHandler cleartextHttp2ServerUpgradeHandler = new CleartextHttp2ServerUpgradeHandler(
            sourceCodec, upgradeHandler, new HelloWorldHttp2HandlerBuilder().build());

    p.addLast(cleartextHttp2ServerUpgradeHandler);
    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();
            pipeline.addAfter(ctx.name(), null, new HelloWorldHttp1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
        }
    });

    p.addLast(new UserEventLogger());
}