Example usage for io.netty.handler.ssl SslContext newClientContext

List of usage examples for io.netty.handler.ssl SslContext newClientContext

Introduction

In this page you can find the example usage for io.netty.handler.ssl SslContext newClientContext.

Prototype

@Deprecated
public static SslContext newClientContext(File certChainFile, TrustManagerFactory trustManagerFactory,
        Iterable<String> ciphers, Iterable<String> nextProtocols, long sessionCacheSize, long sessionTimeout)
        throws SSLException 

Source Link

Document

Creates a new client-side SslContext .

Usage

From source file:Http2Client.java

License:Apache License

public Http2Client(final String host, final int port, final boolean ssl) throws Exception {
    this.host = host;
    this.port = port;
    this.ssl = ssl;

    // Configure SSL.
    if (ssl) {/*ww  w.j  av a 2 s  .c  o m*/
        this.sslCtx = SslContext.newClientContext(null, InsecureTrustManagerFactory.INSTANCE, null,
                Arrays.asList(SelectedProtocol.HTTP_2.protocolName(), SelectedProtocol.HTTP_1_1.protocolName()),
                0, 0);
    } else {
        this.sslCtx = null;
    }

    // XXX (dano): Http2Connection does not seem to be thread safe, use one thread only
    this.workerGroup = new NioEventLoopGroup(1);
    Http2ClientInitializer initializer = new Http2ClientInitializer(sslCtx);

    // Configure the client.
    Bootstrap b = new Bootstrap();
    b.group(workerGroup);
    b.channel(NioSocketChannel.class);
    b.option(ChannelOption.SO_KEEPALIVE, true);
    b.remoteAddress(host, port);
    b.handler(initializer);

    // Start the client.
    this.channel = b.connect().syncUninterruptibly().channel();
    System.out.println("Connected to [" + host + ':' + port + ']');

    // Wait for the HTTP/2 upgrade to occur.
    this.connectionHandler = initializer.connectionHandler();
    connectionHandler.awaitInitialization();
}

From source file:io.aos.netty5.http2.client.Http2Client.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {/* w  ww .ja  v  a 2s . c  o m*/
        sslCtx = SslContext.newClientContext(null, InsecureTrustManagerFactory.INSTANCE, null,
                Arrays.asList(SelectedProtocol.HTTP_2.protocolName(), SelectedProtocol.HTTP_1_1.protocolName()),
                0, 0);
    } else {
        sslCtx = null;
    }

    EventLoopGroup workerGroup = new NioEventLoopGroup();
    Http2ClientInitializer initializer = new Http2ClientInitializer(sslCtx, Integer.MAX_VALUE);

    try {
        // Configure the client.
        Bootstrap b = new Bootstrap();
        b.group(workerGroup);
        b.channel(NioSocketChannel.class);
        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.remoteAddress(HOST, PORT);
        b.handler(initializer);

        // Start the client.
        Channel channel = b.connect().syncUninterruptibly().channel();
        System.out.println("Connected to [" + HOST + ':' + PORT + ']');

        // Wait for the HTTP/2 upgrade to occur.
        Http2SettingsHandler http2SettingsHandler = initializer.settingsHandler();
        http2SettingsHandler.awaitSettings(5, TimeUnit.SECONDS);

        HttpResponseHandler responseHandler = initializer.responseHandler();
        int streamId = 3;
        URI hostName = URI.create((SSL ? "https" : "http") + "://" + HOST + ':' + PORT);
        System.err.println("Sending request(s)...");
        if (URL != null) {
            // Create a simple GET request.
            FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, URL);
            request.headers().add(HttpHeaders.Names.HOST, hostName);
            request.headers().add(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
            channel.writeAndFlush(request);
            responseHandler.put(streamId, channel.newPromise());
            streamId += 2;
        }
        if (URL2 != null) {
            // Create a simple POST request with a body.
            FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, URL2,
                    Unpooled.copiedBuffer(URL2DATA.getBytes(CharsetUtil.UTF_8)));
            request.headers().add(HttpHeaders.Names.HOST, hostName);
            request.headers().add(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
            channel.writeAndFlush(request);
            responseHandler.put(streamId, channel.newPromise());
            streamId += 2;
        }
        responseHandler.awaitResponses(5, TimeUnit.SECONDS);
        System.out.println("Finished HTTP/2 request(s)");

        // Wait until the connection is closed.
        channel.close().syncUninterruptibly();
    } finally {
        workerGroup.shutdownGracefully();
    }
}

From source file:io.aos.netty5.memcache.binary.MemcacheClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//from  w w  w.  ja  v a  2 s  .  co m
        sslCtx = SslContext.newClientContext(null, InsecureTrustManagerFactory.INSTANCE, null,
                Arrays.asList(SelectedProtocol.HTTP_2.protocolName(), SelectedProtocol.HTTP_1_1.protocolName()),
                0, 0);
    } else {
        sslCtx = null;
    }

    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                if (sslCtx != null) {
                    p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                }
                p.addLast(new BinaryMemcacheClientCodec());
                p.addLast(new BinaryMemcacheObjectAggregator(Integer.MAX_VALUE));
                p.addLast(new MemcacheClientHandler());
            }
        });

        // Start the connection attempt.
        Channel ch = b.connect(HOST, PORT).sync().channel();

        // Read commands from the stdin.
        System.out.println("Enter commands (quit to end)");
        System.out.println("get <key>");
        System.out.println("set <key> <value>");
        ChannelFuture lastWriteFuture = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        for (;;) {
            String line = in.readLine();
            if (line == null) {
                break;
            }
            if ("quit".equals(line.toLowerCase())) {
                ch.close().sync();
                break;
            }
            // Sends the received line to the server.
            lastWriteFuture = ch.writeAndFlush(line);
        }

        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }
    } finally {
        group.shutdownGracefully();
    }
}