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.github.sinsinpub.pero.manual.proxyhandler.HttpProxyServer.java

License:Apache License

@Override
protected void configure(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();
    switch (testMode) {
    case INTERMEDIARY:
        p.addLast(new HttpServerCodec());
        p.addLast(new HttpObjectAggregator(1));
        p.addLast(new HttpIntermediaryHandler());
        break;//w w w  .j av a 2  s .c  o m
    case TERMINAL:
        p.addLast(new HttpServerCodec());
        p.addLast(new HttpObjectAggregator(1));
        p.addLast(new HttpTerminalHandler());
        break;
    case UNRESPONSIVE:
        p.addLast(UnresponsiveHandler.INSTANCE);
        break;
    }
}

From source file:com.github.sinsinpub.pero.manual.proxyhandler.ProxyServer.java

License:Apache License

/**
 * Starts a new proxy server with disabled authentication for testing purpose.
 * /*from  w  ww. j  a  v a2 s .co  m*/
 * @param useSsl {@code true} if and only if implicit SSL is enabled
 * @param testMode the test mode
 * @param username the expected username. If the client tries to authenticate with a different
 *            username, this server will fail the authentication request.
 * @param password the expected password. If the client tries to authenticate with a different
 *            password, this server will fail the authentication request.
 * @param destination the expected destination. If the client requests proxying to a different
 *            destination, this server will reject the connection request.
 */
protected ProxyServer(final boolean useSsl, TestMode testMode, InetSocketAddress destination, String username,
        String password, int bindPort, boolean logging) {

    this.testMode = testMode;
    this.destination = destination;
    this.username = username;
    this.password = password;

    ServerBootstrap b = new ServerBootstrap();
    b.channel(NioServerSocketChannel.class);
    if (logging) {
        b.handler(new LoggingHandler(LogLevel.INFO));
    }
    b.group(StaticContextProvider.group);
    b.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            if (useSsl) {
                p.addLast(StaticContextProvider.serverSslCtx.newHandler(ch.alloc()));
            }

            configure(ch);
        }
    });

    ch = (ServerSocketChannel) b.bind(NetUtil.LOCALHOST, bindPort).syncUninterruptibly().channel();
}

From source file:com.github.unafraid.signer.server.ServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    if (_sslCtx != null) {
        p.addLast(_sslCtx.newHandler(ch.alloc()));
    }//w  w w  .j av  a2 s  . c  o  m
    p.addLast(new HttpServerCodec());
    p.addLast(new ServerHandler());
}

From source file:com.google.devtools.build.lib.remote.blobstore.http.HttpBlobStore.java

License:Open Source License

public HttpBlobStore(URI uri, int timeoutMillis, @Nullable final Credentials creds) throws Exception {
    boolean useTls = uri.getScheme().equals("https");
    if (uri.getPort() == -1) {
        int port = useTls ? 443 : 80;
        uri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), port, uri.getPath(), uri.getQuery(),
                uri.getFragment());// w w  w  . ja  v  a  2  s  . co  m
    }
    this.uri = uri;
    final SslContext sslCtx;
    if (useTls) {
        // OpenSsl gives us a > 2x speed improvement on fast networks, but requires netty tcnative
        // to be there which is not available on all platforms and environments.
        SslProvider sslProvider = OpenSsl.isAvailable() ? SslProvider.OPENSSL : SslProvider.JDK;
        sslCtx = SslContextBuilder.forClient().sslProvider(sslProvider).build();
    } else {
        sslCtx = null;
    }
    Bootstrap clientBootstrap = new Bootstrap().channel(NioSocketChannel.class)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeoutMillis).group(eventLoop)
            .remoteAddress(uri.getHost(), uri.getPort());
    downloadChannels = new SimpleChannelPool(clientBootstrap, new ChannelPoolHandler() {
        @Override
        public void channelReleased(Channel ch) {
            ch.pipeline().remove("read-timeout-handler");
        }

        @Override
        public void channelAcquired(Channel ch) {
            ch.pipeline().addFirst("read-timeout-handler", new ReadTimeoutHandler(timeoutMillis));
        }

        @Override
        public void channelCreated(Channel ch) {
            ChannelPipeline p = ch.pipeline();
            p.addFirst("read-timeout-handler", new ReadTimeoutHandler(timeoutMillis));
            if (sslCtx != null) {
                SSLEngine engine = sslCtx.newEngine(ch.alloc());
                engine.setUseClientMode(true);
                p.addFirst(new SslHandler(engine));
            }
            p.addLast(new HttpClientCodec());
            p.addLast(new HttpDownloadHandler(creds));
        }
    });
    uploadChannels = new SimpleChannelPool(clientBootstrap, new ChannelPoolHandler() {
        @Override
        public void channelReleased(Channel ch) {
        }

        @Override
        public void channelAcquired(Channel ch) {
        }

        @Override
        public void channelCreated(Channel ch) {
            ChannelPipeline p = ch.pipeline();
            if (sslCtx != null) {
                SSLEngine engine = sslCtx.newEngine(ch.alloc());
                engine.setUseClientMode(true);
                p.addFirst(new SslHandler(engine));
            }
            p.addLast(new HttpResponseDecoder());
            // The 10KiB limit was chosen at random. We only expect HTTP servers to respond with
            // an error message in the body and that should always be less than 10KiB.
            p.addLast(new HttpObjectAggregator(10 * 1024));
            p.addLast(new HttpRequestEncoder());
            p.addLast(new ChunkedWriteHandler());
            p.addLast(new HttpUploadHandler(creds));
        }
    });
    this.creds = creds;
}

From source file:com.google.devtools.build.remote.worker.HttpCacheServerInitializer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();
    p.addLast(new HttpServerCodec());
    p.addLast(new HttpObjectAggregator(100 * 1024 * 1024));
    p.addLast(new HttpCacheServerHandler());
}

From source file:com.gw.services.client.HttpsClientInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();

    // Enable HTTPS if necessary.
    if (sslCtx != null) {
        SSLEngine sslEngine = sslCtx.createSSLEngine();
        sslEngine.setUseClientMode(true);
        SslHandler sslHandler = new SslHandler(sslEngine);
        p.addLast(sslHandler);
    }//w w w.  j ava 2  s  .  c  om

    p.addLast(new HttpClientCodec());

    // Remove the following line if you don't want automatic content decompression.
    p.addLast(new HttpContentDecompressor());

    // Uncomment the following line if you don't want to handle HttpContents.
    //p.addLast(new HttpObjectAggregator(1048576));

    p.addLast(new HttpsClientHandler());
}

From source file:com.hazelcast.openshift.TunnelClient.java

License:Open Source License

public ServerBootstrap createBootstrap(int localPort) throws Exception {
    System.out.println(//w  w  w.  ja  va 2  s . c  o m
            "Creating clientside plain-socket: (" + localPort + ") => (" + httpHost + ":" + httpPort + ")");
    return new ServerBootstrap().option(ChannelOption.SO_BACKLOG, 20).group(getBossGroup(), getWorkerGroup())
            .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

                @Override
                protected void initChannel(SocketChannel channel) throws Exception {
                    ChannelPipeline pipeline = channel.pipeline();
                    pipeline.addLast(new TunnelServerConnector(getWorkerGroup(), httpHost, httpPort, httpSsl));
                }
            });

}

From source file:com.hazelcast.openshift.TunnelClientAcceptor.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
    HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    ctx.writeAndFlush(response).sync();/*ww w . j  av a  2s .co m*/

    ChannelPipeline pipeline = ctx.pipeline();

    Channel forward = createRemoteChannel(ctx.channel());
    if (forward == null) {
        ctx.close();
        return;
    }

    forward.closeFuture().addListener(f -> ctx.close());
    ctx.channel().closeFuture().addListener(f -> forward.close());

    pipeline.addLast(new ProxyForwardHandler(forward));
    pipeline.remove(HttpRequestDecoder.class);
    pipeline.remove(HttpResponseEncoder.class);
    pipeline.remove(this);
}

From source file:com.hazelcast.openshift.TunnelClientAcceptor.java

License:Open Source License

protected Bootstrap createBootstrap(Channel socket) {
    return new Bootstrap().channel(NioSocketChannel.class).group(workerGroup)
            .option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {

                @Override/*from w w w . j a  va2  s . c o m*/
                protected void initChannel(SocketChannel channel) throws Exception {
                    System.out.println("Configure plain-socket: (" + socket + ") => (" + forwardHost + ":"
                            + forwardPort + ")");
                    ChannelPipeline pipeline = channel.pipeline();
                    pipeline.addLast(new ProxyForwardHandler(socket));
                }
            });
}

From source file:com.hazelcast.openshift.TunnelServerConnector.java

License:Open Source License

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
    Promise promise = new Promise();
    Bootstrap bootstrap = createBootstrap(ctx.channel(), promise);
    ChannelFuture future = bootstrap.connect(httpHost, httpPort);
    Channel forward = future.sync().channel();
    forward.closeFuture().addListener(f -> ctx.close());
    ctx.channel().closeFuture().addListener(f -> forward.close());

    HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.CONNECT, "/");
    request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    forward.writeAndFlush(request);/*from ww w .  j  a v  a  2s . c o m*/

    // Wait for the initial http response (ssl established)
    promise.sync(10, TimeUnit.SECONDS);

    // Exchange the HazelcastClient -> TunnelClient handler to proxy
    ChannelPipeline pipeline = ctx.pipeline();
    pipeline.addLast(new ProxyForwardHandler(forward));
    pipeline.remove(this);

    ctx.fireChannelRegistered();
}