Example usage for io.netty.channel ChannelPipeline get

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

Introduction

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

Prototype

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

Source Link

Document

Returns the ChannelHandler of the specified type in this pipeline.

Usage

From source file:io.reactivex.netty.protocol.http.client.ClientRequiredConfigurator.java

License:Apache License

@Override
public void configureNewPipeline(ChannelPipeline pipeline) {
    ClientRequestResponseConverter converter = pipeline.get(ClientRequestResponseConverter.class);
    if (null == converter) {
        pipeline.addLast(HttpClientPipelineConfigurator.REQUEST_RESPONSE_CONVERTER_HANDLER_NAME,
                new ClientRequestResponseConverter(eventsSubject));
    }// w  w w .j a v a2s. c o m
}

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

License:Apache License

@Override
public void configureNewPipeline(ChannelPipeline pipeline) {
    httpClientPipelineConfigurator.configureNewPipeline(pipeline);
    if (null != pipeline.get(HttpClientPipelineConfigurator.REQUEST_RESPONSE_CONVERTER_HANDLER_NAME)) {
        pipeline.addBefore(HttpClientPipelineConfigurator.REQUEST_RESPONSE_CONVERTER_HANDLER_NAME,
                SseChannelHandler.NAME, SSE_CHANNEL_HANDLER);
    } else {/*from   ww w . j a  v  a 2 s .c  o  m*/
        // Assuming that the underlying HTTP configurator knows what its doing. It will mostly fail though.
        pipeline.addLast(SseChannelHandler.NAME, SSE_CHANNEL_HANDLER);
    }
}

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

License:Apache License

@Override
public void configureNewPipeline(ChannelPipeline pipeline) {
    httpClientPipelineConfigurator.configureNewPipeline(pipeline);
    if (null != pipeline.get(HttpClientPipelineConfigurator.REQUEST_RESPONSE_CONVERTER_HANDLER_NAME)) {
        pipeline.addBefore(HttpClientPipelineConfigurator.REQUEST_RESPONSE_CONVERTER_HANDLER_NAME,
                SSEInboundHandler.NAME, SSEClientPipelineConfigurator.SSE_INBOUND_HANDLER);
    } else {/*from w w  w . jav  a2 s.c om*/
        // Assuming that the underlying HTTP configurator knows what its doing. It will mostly fail though.
        pipeline.addLast(SSEInboundHandler.NAME, SSEClientPipelineConfigurator.SSE_INBOUND_HANDLER);
    }
}

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

License:Open Source License

public NetSocket createNetSocket() {
    // connection was upgraded to raw TCP socket
    NetSocketImpl socket = new NetSocketImpl(vertx, channel, context, client.getSslHelper(), metrics);
    socket.metric(metric());//from ww  w  . j  av  a 2s .c  om
    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 VertxNetHandler<NetSocketImpl>(channel, socket, connectionMap) {
        @Override
        public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception {
            // remove from the real mapping
            pool.removeChannel(channel);
            super.exceptionCaught(chctx, t);
        }

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

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

        @Override
        protected void handleMsgReceived(NetSocketImpl conn, Object msg) {
            ByteBuf buf = (ByteBuf) msg;
            conn.handleDataReceived(Buffer.buffer(buf));
        }
    });
    return socket;
}

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

License:Open Source License

private synchronized NetSocket upgrade(StreamImpl stream) {
    if (options.isPipelining()) {
        throw new IllegalStateException("Cannot upgrade a pipe-lined request");
    }//w ww.j a v a 2 s .  com
    if (upgraded) {
        throw new IllegalStateException("Request already upgraded to NetSocket");
    }
    upgraded = true;

    // connection was upgraded to raw TCP socket
    AtomicBoolean paused = new AtomicBoolean(false);
    NetSocketImpl socket = new NetSocketImpl(vertx, chctx, context, client.getSslHelper(), metrics) {
        {
            super.pause();
        }

        @Override
        public synchronized NetSocket handler(Handler<Buffer> dataHandler) {
            return super.handler(dataHandler);
        }

        @Override
        public synchronized NetSocket pause() {
            paused.set(true);
            return super.pause();
        }

        @Override
        public synchronized NetSocket resume() {
            paused.set(false);
            return super.resume();
        }
    };
    socket.metric(metric());

    // Flush out all pending data
    endReadAndFlush();

    // remove old http handlers and replace the old handler with one that handle plain sockets
    ChannelPipeline pipeline = chctx.pipeline();
    ChannelHandler inflater = pipeline.get(HttpContentDecompressor.class);
    if (inflater != null) {
        pipeline.remove(inflater);
    }
    pipeline.replace("handler", "handler", new VertxNetHandler(socket) {
        @Override
        public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
            if (msg instanceof HttpContent) {
                if (msg instanceof LastHttpContent) {
                    stream.endResponse((LastHttpContent) msg);
                }
                ReferenceCountUtil.release(msg);
                return;
            }
            super.channelRead(chctx, msg);
        }
    }.removeHandler(sock -> listener.onDiscard()));

    // Removing this codec might fire pending buffers in the HTTP decoder
    // this happens when the channel reads the HTTP response and the following data in a single buffer
    pipeline.remove("codec");

    // Async check to deliver the pending messages
    // because the netSocket access in HttpClientResponse is synchronous
    // we need to pause the NetSocket to avoid losing or reordering buffers
    // and then asynchronously un-pause it unless it was actually paused by the application
    context.runOnContext(v -> {
        if (!paused.get()) {
            socket.resume();
        }
    });

    return socket;
}

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

License:Open Source License

NetSocket createNetSocket() {
    NetSocketImpl socket = new NetSocketImpl(vertx, chctx, context, sslHelper, metrics);
    socket.metric(metric());/*from  w  w  w. j  a  v  a  2 s . c om*/
    Map<Channel, NetSocketImpl> connectionMap = new HashMap<>(1);
    connectionMap.put(chctx.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 = chctx.pipeline();
    ChannelHandler compressor = pipeline.get(HttpChunkContentCompressor.class);
    if (compressor != null) {
        pipeline.remove(compressor);
    }

    pipeline.remove("httpDecoder");
    if (pipeline.get("chunkedWriter") != null) {
        pipeline.remove("chunkedWriter");
    }

    chctx.pipeline().replace("handler", "handler", new VertxNetHandler(socket) {
        @Override
        public void channelRead(ChannelHandlerContext chctx, Object msg) throws Exception {
            if (msg instanceof HttpContent) {
                ReferenceCountUtil.release(msg);
                return;
            }
            super.channelRead(chctx, msg);
        }
    }.removeHandler(sock -> {
        if (metrics != null) {
            metrics.responseEnd(responseInProgress.metric(), responseInProgress.response());
        }
        connectionMap.remove(chctx.channel());
    }));

    // check if the encoder can be removed yet or not.
    chctx.pipeline().remove("httpEncoder");
    return socket;
}

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

License:Open Source License

NetSocket createNetSocket() {
    NetSocketImpl socket = new NetSocketImpl(vertx, channel, context, server.getSslHelper(), metrics);
    socket.metric(metric());//  w  ww  .  j a  v  a  2  s . com
    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 compressor = pipeline.get(HttpChunkContentCompressor.class);
    if (compressor != null) {
        pipeline.remove(compressor);
    }

    pipeline.remove("httpDecoder");
    if (pipeline.get("chunkedWriter") != null) {
        pipeline.remove("chunkedWriter");
    }

    channel.pipeline().replace("handler", "handler",
            new VertxNetHandler<NetSocketImpl>(channel, socket, connectionMap) {
                @Override
                public void exceptionCaught(ChannelHandlerContext chctx, Throwable t) throws Exception {
                    // remove from the real mapping
                    server.removeChannel(channel);
                    super.exceptionCaught(chctx, t);
                }

                @Override
                public void channelInactive(ChannelHandlerContext chctx) throws Exception {
                    // remove from the real mapping
                    server.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);
                }

                @Override
                protected void handleMsgReceived(NetSocketImpl conn, Object msg) {
                    ByteBuf buf = (ByteBuf) msg;
                    conn.handleDataReceived(Buffer.buffer(buf));
                }
            });

    // check if the encoder can be removed yet or not.
    if (lastWriteFuture == null) {
        channel.pipeline().remove("httpEncoder");
    } else {
        lastWriteFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                channel.pipeline().remove("httpEncoder");
            }
        });
    }
    return socket;
}

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

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse resp = (HttpResponse) msg;
        response = new DefaultFullHttpResponse(resp.protocolVersion(), resp.status());
        response.headers().add(resp.headers());
    }//from  w  w w  . ja v  a2 s.  co m
    if (msg instanceof HttpContent) {
        if (response != null) {
            response.content().writeBytes(((HttpContent) msg).content());
            if (msg instanceof LastHttpContent) {
                response.trailingHeaders().add(((LastHttpContent) msg).trailingHeaders());
                ChannelPipeline pipeline = chctx.pipeline();
                pipeline.remove(WebSocketHandshakeInboundHandler.this);
                ChannelHandler handler = pipeline.get(HttpContentDecompressor.class);
                if (handler != null) {
                    // remove decompressor as its not needed anymore once connection was upgraded to websockets
                    ctx.pipeline().remove(handler);
                }
                Future<Void> fut = handshakeComplete(response);
                wsHandler.handle(fut);
            }
        }
    }
}

From source file:io.viewserver.network.netty.NettyPipelineInitialiser.java

License:Apache License

@Override
protected void initChannel(Channel channel) throws Exception {
    ChannelPipeline pipeline = channel.pipeline();

    if (pipeline.get("frameDecoder") == null) {
        pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1 << 30, 0, 4, 0, 4));
    }/*w ww  . j  av a 2s. c  o  m*/
    pipeline.addLast(new IncomingMessageHandler(networkMessageWheel));

    if (pipeline.get("frameEncoder") == null) {
        pipeline.addLast("frameEncoder", new LengthFieldPrepender(4));
    }
    pipeline.addLast(new OutgoingMessageHandler(networkMessageWheel));
}

From source file:nikoladasm.aspark.server.ServerHandler.java

License:Open Source License

private static String getWebSocketLocation(ChannelPipeline cp, HttpRequest req, String path) {
    String protocol = "ws";
    if (cp.get(SslHandler.class) != null)
        protocol = "wss";
    return protocol + "://" + req.headers().get(HOST) + path;
}