List of usage examples for io.netty.handler.ssl SslHandler handshakeFuture
public Future<Channel> handshakeFuture()
From source file:io.advantageous.conekt.net.impl.NetSocketImpl.java
License:Open Source License
@Override public synchronized NetSocket upgradeToSsl(final Handler<Void> handler) { SslHandler sslHandler = channel.pipeline().get(SslHandler.class); if (sslHandler == null) { sslHandler = helper.createSslHandler(vertx, client); channel.pipeline().addFirst(sslHandler); }/*ww w. ja v a2s . c o m*/ sslHandler.handshakeFuture().addListener(future -> context.executeFromIO(() -> { if (future.isSuccess()) { handler.handle(null); } else { log.error("", future.cause()); } })); return this; }
From source file:io.gatling.http.client.impl.DefaultHttpClient.java
License:Apache License
private Future<Channel> installSslHandler(HttpTx tx, Channel channel) { SslContext sslCtx = tx.request.isAlpnRequired() ? alpnSslContext : sslContext; try {/*from w w w. j ava 2 s. co m*/ SslHandler sslHandler = SslHandlers.newSslHandler(sslCtx, channel.alloc(), tx.request.getUri(), tx.request.getVirtualHost(), config); tx.listener.onTlsHandshakeAttempt(); ChannelPipeline pipeline = channel.pipeline(); String after = pipeline.get(PROXY_HANDLER) != null ? PROXY_HANDLER : PINNED_HANDLER; pipeline.addAfter(after, SSL_HANDLER, sslHandler); return sslHandler.handshakeFuture().addListener(f -> { SSLSession sslSession = sslHandler.engine().getHandshakeSession(); if (sslSession != null && sslSession.isValid() && config.isDisableSslSessionResumption()) { sslSession.invalidate(); } if (tx.requestTimeout.isDone()) { return; } if (f.isSuccess()) { tx.listener.onTlsHandshakeSuccess(); } else { tx.requestTimeout.cancel(); tx.listener.onTlsHandshakeFailure(f.cause()); tx.listener.onThrowable(f.cause()); } }); } catch (RuntimeException e) { tx.requestTimeout.cancel(); tx.listener.onThrowable(e); return new DefaultPromise<Channel>(ImmediateEventExecutor.INSTANCE).setFailure(e); } }
From source file:io.jsync.http.impl.DefaultHttpClient.java
License:Open Source License
void internalConnect(final Handler<ClientConnection> connectHandler, final Handler<Throwable> connectErrorHandler) { if (bootstrap == null) { // Share the event loop thread to also serve the HttpClient's network traffic. AsyncEventLoopGroup pool = new AsyncEventLoopGroup(); pool.addWorker(actualCtx.getEventLoop()); bootstrap = new Bootstrap(); bootstrap.group(pool);/*from w w w. j ava 2s .c o m*/ bootstrap.channel(NioSocketChannel.class); tcpHelper.checkSSL(async); bootstrap.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (tcpHelper.isSSL()) { pipeline.addLast("ssl", tcpHelper.createSslHandler(async, true, host, port)); } pipeline.addLast("codec", new HttpClientCodec(4096, 8192, 8192, false, false)); if (tryUseCompression) { pipeline.addLast("inflater", new HttpContentDecompressor(true)); } pipeline.addLast("handler", new ClientHandler()); } }); } tcpHelper.applyConnectionOptions(bootstrap); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); future.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture channelFuture) throws Exception { final Channel ch = channelFuture.channel(); if (channelFuture.isSuccess()) { if (tcpHelper.isSSL()) { // TCP connected, so now we must do the SSL handshake SslHandler sslHandler = ch.pipeline().get(SslHandler.class); Future<Channel> fut = sslHandler.handshakeFuture(); fut.addListener(new GenericFutureListener<Future<Channel>>() { @Override public void operationComplete(Future<Channel> future) throws Exception { if (future.isSuccess()) { connected(ch, connectHandler); } else { connectionFailed(ch, connectErrorHandler, new SSLHandshakeException("Failed to create SSL connection")); } } }); } else { connected(ch, connectHandler); } } else { connectionFailed(ch, connectErrorHandler, channelFuture.cause()); } } }); }
From source file:io.jsync.net.impl.DefaultNetClient.java
License:Open Source License
private void connect(final int port, final String host, final Handler<AsyncResult<NetSocket>> connectHandler, final int remainingAttempts) { if (bootstrap == null) { tcpHelper.checkSSL(async);/*from ww w .j a v a 2s. com*/ bootstrap = new Bootstrap(); bootstrap.group(actualCtx.getEventLoop()); bootstrap.channel(NioSocketChannel.class); bootstrap.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (tcpHelper.isSSL()) { SslHandler sslHandler = tcpHelper.createSslHandler(async, true); pipeline.addLast("ssl", sslHandler); } if (tcpHelper.isSSL()) { // only add ChunkedWriteHandler when SSL is enabled otherwise it is not needed as FileRegion is used. pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); // For large file / sendfile support } pipeline.addLast("handler", new AsyncNetHandler(async, socketMap)); } }); configurable = false; } tcpHelper.applyConnectionOptions(bootstrap); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); future.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture channelFuture) throws Exception { final Channel ch = channelFuture.channel(); if (channelFuture.isSuccess()) { if (tcpHelper.isSSL()) { // TCP connected, so now we must do the SSL handshake SslHandler sslHandler = ch.pipeline().get(SslHandler.class); Future<Channel> fut = sslHandler.handshakeFuture(); fut.addListener(new GenericFutureListener<Future<Channel>>() { @Override public void operationComplete(Future<Channel> future) throws Exception { if (future.isSuccess()) { connected(ch, connectHandler); } else { failed(ch, future.cause(), connectHandler); } } }); } else { connected(ch, connectHandler); } } else { if (remainingAttempts > 0 || remainingAttempts == -1) { actualCtx.execute(ch.eventLoop(), new Runnable() { public void run() { log.debug("Failed to create connection. Will retry in " + reconnectInterval + " milliseconds"); //Set a timer to retry connection async.setTimer(reconnectInterval, new Handler<Long>() { public void handle(Long timerID) { connect(port, host, connectHandler, remainingAttempts == -1 ? remainingAttempts : remainingAttempts - 1); } }); } }); } else { failed(ch, channelFuture.cause(), connectHandler); } } } }); }
From source file:io.jsync.net.impl.DefaultNetSocket.java
License:Open Source License
@Override public NetSocket ssl(final Handler<Void> handler) { SslHandler sslHandler = channel.pipeline().get(SslHandler.class); if (sslHandler == null) { sslHandler = helper.createSslHandler(async, client); channel.pipeline().addFirst(sslHandler); }//ww w . j a va 2 s . c o m sslHandler.handshakeFuture().addListener(new GenericFutureListener<Future<Channel>>() { @Override public void operationComplete(final Future<Channel> future) throws Exception { if (context.isOnCorrectWorker(channel.eventLoop())) { if (future.isSuccess()) { try { async.setContext(context); handler.handle(null); } catch (Throwable t) { context.reportException(t); } } else { context.reportException(future.cause()); } } else { context.execute(new Runnable() { public void run() { if (future.isSuccess()) { handler.handle(null); } else { context.reportException(future.cause()); } } }); } } }); return this; }
From source file:io.reactivex.netty.client.ClientChannelFactoryImpl.java
License:Apache License
@Override public ChannelFuture connect(final Subscriber<? super ObservableConnection<I, O>> subscriber, RxClient.ServerInfo serverInfo,// w ww.ja va2s. c o m final ClientConnectionFactory<I, O, ? extends ObservableConnection<I, O>> connectionFactory) { final long startTimeMillis = Clock.newStartTimeMillis(); eventsSubject.onEvent(ClientMetricsEvent.CONNECT_START); final ChannelFuture connectFuture = clientBootstrap.connect(serverInfo.getHost(), serverInfo.getPort()); subscriber.add(Subscriptions.create(new Action0() { @Override public void call() { if (!connectFuture.isDone()) { connectFuture.cancel(true); // Unsubscribe here means, no more connection is required. A close on connection is explicit. } } })); connectFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { try { if (!future.isSuccess()) { eventsSubject.onEvent(ClientMetricsEvent.CONNECT_FAILED, Clock.onEndMillis(startTimeMillis), future.cause()); subscriber.onError(future.cause()); } else { eventsSubject.onEvent(ClientMetricsEvent.CONNECT_SUCCESS, Clock.onEndMillis(startTimeMillis)); ChannelPipeline pipeline = future.channel().pipeline(); ChannelHandlerContext ctx = pipeline.lastContext(); // The connection uses the context for write which should always start from the tail. final ObservableConnection<I, O> newConnection = connectionFactory.newConnection(ctx); ChannelHandler lifecycleHandler = pipeline .get(RxRequiredConfigurator.CONN_LIFECYCLE_HANDLER_NAME); if (null == lifecycleHandler) { onNewConnection(newConnection, subscriber); } else { @SuppressWarnings("unchecked") ConnectionLifecycleHandler<I, O> handler = (ConnectionLifecycleHandler<I, O>) lifecycleHandler; SslHandler sslHandler = pipeline.get(SslHandler.class); if (null == sslHandler) { handler.setConnection(newConnection); onNewConnection(newConnection, subscriber); } else { sslHandler.handshakeFuture() .addListener(new GenericFutureListener<Future<? super Channel>>() { @Override public void operationComplete(Future<? super Channel> future) throws Exception { onNewConnection(newConnection, subscriber); } }); } } } } catch (Throwable throwable) { subscriber.onError(throwable); } } }); return connectFuture; }
From source file:io.vertx.core.http.impl.HttpChannelConnector.java
License:Open Source License
private void doConnect(ConnectionListener<HttpClientConnection> listener, ContextInternal context, Future<ConnectResult<HttpClientConnection>> future) { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(context.nettyEventLoop()); bootstrap.channelFactory(client.getVertx().transport().channelFactory(false)); applyConnectionOptions(bootstrap);//from ww w. ja v a 2 s . co m ChannelProvider channelProvider; // http proxy requests are handled in HttpClientImpl, everything else can use netty proxy handler if (options.getProxyOptions() == null || !ssl && options.getProxyOptions().getType() == ProxyType.HTTP) { channelProvider = ChannelProvider.INSTANCE; } else { channelProvider = ProxyChannelProvider.INSTANCE; } boolean useAlpn = options.isUseAlpn(); Handler<Channel> channelInitializer = ch -> { // Configure pipeline ChannelPipeline pipeline = ch.pipeline(); if (ssl) { SslHandler sslHandler = new SslHandler(sslHelper.createEngine(client.getVertx(), peerHost, port, options.isForceSni() ? peerHost : null)); ch.pipeline().addLast("ssl", sslHandler); // TCP connected, so now we must do the SSL handshake sslHandler.handshakeFuture().addListener(fut -> { if (fut.isSuccess()) { String protocol = sslHandler.applicationProtocol(); if (useAlpn) { if ("h2".equals(protocol)) { applyHttp2ConnectionOptions(ch.pipeline()); http2Connected(listener, context, ch, future); } else { applyHttp1xConnectionOptions(ch.pipeline()); HttpVersion fallbackProtocol = "http/1.0".equals(protocol) ? HttpVersion.HTTP_1_0 : HttpVersion.HTTP_1_1; http1xConnected(listener, fallbackProtocol, host, port, true, context, ch, http1Weight, future); } } else { applyHttp1xConnectionOptions(ch.pipeline()); http1xConnected(listener, version, host, port, true, context, ch, http1Weight, future); } } else { handshakeFailure(ch, fut.cause(), listener, future); } }); } else { if (version == HttpVersion.HTTP_2) { if (options.isHttp2ClearTextUpgrade()) { applyHttp1xConnectionOptions(pipeline); } else { applyHttp2ConnectionOptions(pipeline); } } else { applyHttp1xConnectionOptions(pipeline); } } }; Handler<AsyncResult<Channel>> channelHandler = res -> { if (res.succeeded()) { Channel ch = res.result(); if (!ssl) { if (version == HttpVersion.HTTP_2) { if (options.isHttp2ClearTextUpgrade()) { http1xConnected(listener, version, host, port, false, context, ch, http2Weight, future); } else { http2Connected(listener, context, ch, future); } } else { http1xConnected(listener, version, host, port, false, context, ch, http1Weight, future); } } } else { connectFailed(null, listener, res.cause(), future); } }; channelProvider.connect(client.getVertx(), bootstrap, options.getProxyOptions(), SocketAddress.inetSocketAddress(port, host), channelInitializer, channelHandler); }
From source file:io.vertx.core.http.impl.HttpClientImpl.java
License:Open Source License
private void internalConnect(ContextImpl context, int port, String host, Handler<ClientConnection> connectHandler, Handler<Throwable> connectErrorHandler, ConnectionLifeCycleListener listener) { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(context.eventLoop()); bootstrap.channelFactory(new VertxNioSocketChannelFactory()); sslHelper.validate(vertx);/* w w w . j av a 2s . c om*/ bootstrap.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (options.isSsl()) { pipeline.addLast("ssl", sslHelper.createSslHandler(vertx, true, host, port)); } pipeline.addLast("codec", new HttpClientCodec(4096, 8192, 8192, false, false)); if (options.isTryUseCompression()) { pipeline.addLast("inflater", new HttpContentDecompressor(true)); } if (options.getIdleTimeout() > 0) { pipeline.addLast("idle", new IdleStateHandler(0, 0, options.getIdleTimeout())); } pipeline.addLast("handler", new ClientHandler(vertx, context)); } }); applyConnectionOptions(bootstrap); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); future.addListener((ChannelFuture channelFuture) -> { Channel ch = channelFuture.channel(); if (channelFuture.isSuccess()) { if (options.isSsl()) { // TCP connected, so now we must do the SSL handshake SslHandler sslHandler = ch.pipeline().get(SslHandler.class); io.netty.util.concurrent.Future<Channel> fut = sslHandler.handshakeFuture(); fut.addListener(fut2 -> { if (fut2.isSuccess()) { connected(context, port, host, ch, connectHandler, connectErrorHandler, listener); } else { connectionFailed(context, ch, connectErrorHandler, new SSLHandshakeException("Failed to create SSL connection"), listener); } }); } else { connected(context, port, host, ch, connectHandler, connectErrorHandler, listener); } } else { connectionFailed(context, ch, connectErrorHandler, channelFuture.cause(), listener); } }); }
From source file:io.vertx.core.net.impl.NetClientBase.java
License:Open Source License
protected void doConnect(int port, String host, String serverName, Handler<AsyncResult<C>> connectHandler, int remainingAttempts) { checkClosed();/*from w ww .j a v a2 s . co m*/ Objects.requireNonNull(host, "No null host accepted"); Objects.requireNonNull(connectHandler, "No null connectHandler accepted"); ContextImpl context = vertx.getOrCreateContext(); sslHelper.validate(vertx); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(context.nettyEventLoop()); bootstrap.channel(NioSocketChannel.class); applyConnectionOptions(bootstrap); ChannelProvider channelProvider; if (options.getProxyOptions() == null) { channelProvider = ChannelProvider.INSTANCE; } else { channelProvider = ProxyChannelProvider.INSTANCE; } Handler<Channel> channelInitializer = ch -> { ChannelPipeline pipeline = ch.pipeline(); if (sslHelper.isSSL()) { SslHandler sslHandler = new SslHandler(sslHelper.createEngine(vertx, host, port, serverName)); ch.pipeline().addLast("ssl", sslHandler); } initChannel(pipeline); pipeline.addLast("handler", new VertxNetHandler<C>(ch, socketMap) { @Override protected Object safeObject(Object msg, ByteBufAllocator allocator) throws Exception { return NetClientBase.this.safeObject(msg, allocator); } @Override protected void handleMsgReceived(C conn, Object msg) { NetClientBase.this.handleMsgReceived(conn, msg); } }); }; Handler<AsyncResult<Channel>> channelHandler = res -> { if (res.succeeded()) { Channel ch = res.result(); if (sslHelper.isSSL()) { // TCP connected, so now we must do the SSL handshake SslHandler sslHandler = (SslHandler) ch.pipeline().get("ssl"); io.netty.util.concurrent.Future<Channel> fut = sslHandler.handshakeFuture(); fut.addListener(future2 -> { if (future2.isSuccess()) { connected(context, ch, connectHandler, host, port); } else { failed(context, ch, future2.cause(), connectHandler); } }); } else { connected(context, ch, connectHandler, host, port); } } else { if (remainingAttempts > 0 || remainingAttempts == -1) { context.executeFromIO(() -> { log.debug("Failed to create connection. Will retry in " + options.getReconnectInterval() + " milliseconds"); //Set a timer to retry connection vertx.setTimer(options.getReconnectInterval(), tid -> doConnect(port, host, serverName, connectHandler, remainingAttempts == -1 ? remainingAttempts : remainingAttempts - 1)); }); } else { failed(context, null, res.cause(), connectHandler); } } }; channelProvider.connect(vertx, bootstrap, options.getProxyOptions(), host, port, channelInitializer, channelHandler); }
From source file:io.vertx.core.net.impl.NetClientImpl.java
License:Open Source License
private void connect(int port, String host, Handler<AsyncResult<NetSocket>> connectHandler, int remainingAttempts) { Objects.requireNonNull(host, "No null host accepted"); Objects.requireNonNull(connectHandler, "No null connectHandler accepted"); ContextImpl context = vertx.getOrCreateContext(); sslHelper.validate(vertx);// w ww . j a va 2s . c o m Bootstrap bootstrap = new Bootstrap(); bootstrap.group(context.nettyEventLoop()); bootstrap.channel(NioSocketChannel.class); applyConnectionOptions(bootstrap); ChannelProvider channelProvider; if (options.getProxyOptions() == null) { channelProvider = ChannelProvider.INSTANCE; } else { channelProvider = ProxyChannelProvider.INSTANCE; } Handler<Channel> channelInitializer = ch -> { if (sslHelper.isSSL()) { SslHandler sslHandler = sslHelper.createSslHandler(vertx, host, port); ch.pipeline().addLast("ssl", sslHandler); } ChannelPipeline pipeline = ch.pipeline(); if (logEnabled) { pipeline.addLast("logging", new LoggingHandler()); } if (sslHelper.isSSL()) { // only add ChunkedWriteHandler when SSL is enabled otherwise it is not needed as FileRegion is used. pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); // For large file / sendfile support } if (options.getIdleTimeout() > 0) { pipeline.addLast("idle", new IdleStateHandler(0, 0, options.getIdleTimeout())); } pipeline.addLast("handler", new VertxNetHandler<NetSocketImpl>(ch, socketMap) { @Override protected void handleMsgReceived(NetSocketImpl conn, Object msg) { ByteBuf buf = (ByteBuf) msg; conn.handleDataReceived(Buffer.buffer(buf)); } }); }; Handler<AsyncResult<Channel>> channelHandler = res -> { if (res.succeeded()) { Channel ch = res.result(); if (sslHelper.isSSL()) { // TCP connected, so now we must do the SSL handshake SslHandler sslHandler = (SslHandler) ch.pipeline().get("ssl"); io.netty.util.concurrent.Future<Channel> fut = sslHandler.handshakeFuture(); fut.addListener(future2 -> { if (future2.isSuccess()) { connected(context, ch, connectHandler, host, port); } else { failed(context, ch, future2.cause(), connectHandler); } }); } else { connected(context, ch, connectHandler, host, port); } } else { if (remainingAttempts > 0 || remainingAttempts == -1) { context.executeFromIO(() -> { log.debug("Failed to create connection. Will retry in " + options.getReconnectInterval() + " milliseconds"); //Set a timer to retry connection vertx.setTimer(options.getReconnectInterval(), tid -> connect(port, host, connectHandler, remainingAttempts == -1 ? remainingAttempts : remainingAttempts - 1)); }); } else { failed(context, null, res.cause(), connectHandler); } } }; channelProvider.connect(vertx, bootstrap, options.getProxyOptions(), host, port, channelInitializer, channelHandler); }