Example usage for io.netty.handler.ssl SslHandshakeCompletionEvent isSuccess

List of usage examples for io.netty.handler.ssl SslHandshakeCompletionEvent isSuccess

Introduction

In this page you can find the example usage for io.netty.handler.ssl SslHandshakeCompletionEvent isSuccess.

Prototype

public final boolean isSuccess() 

Source Link

Document

Return true if the completion was successful

Usage

From source file:com.linecorp.armeria.client.http.HttpClientPipelineConfigurator.java

License:Apache License

/**
 * @see <a href="https://http2.github.io/http2-spec/#discover-https">HTTP/2 specification</a>
 *///from  w  w  w  .  ja va  2 s . co  m
private void configureAsHttps(Channel ch) {
    final ChannelPipeline p = ch.pipeline();
    final SslHandler sslHandler = sslCtx.newHandler(ch.alloc());
    p.addLast(sslHandler);
    p.addLast(new ChannelInboundHandlerAdapter() {
        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            if (!(evt instanceof SslHandshakeCompletionEvent)) {
                ctx.fireUserEventTriggered(evt);
                return;
            }

            final SslHandshakeCompletionEvent handshakeEvent = (SslHandshakeCompletionEvent) evt;
            if (!handshakeEvent.isSuccess()) {
                // The connection will be closed automatically by SslHandler.
                return;
            }

            final SessionProtocol protocol;
            if (isHttp2Protocol(sslHandler)) {
                if (httpPreference == HttpPreference.HTTP1_REQUIRED) {
                    finishWithNegotiationFailure(ctx, H1, H2, "unexpected protocol negotiation result");
                    return;
                }

                addBeforeSessionHandler(p, newHttp2ConnectionHandler(ch));
                protocol = H2;
            } else {
                if (httpPreference != HttpPreference.HTTP1_REQUIRED) {
                    SessionProtocolNegotiationCache.setUnsupported(ctx.channel().remoteAddress(), H2);
                }

                if (httpPreference == HttpPreference.HTTP2_REQUIRED) {
                    finishWithNegotiationFailure(ctx, H2, H1, "unexpected protocol negotiation result");
                    return;
                }

                addBeforeSessionHandler(p, newHttp1Codec());
                protocol = H1;
            }
            finishSuccessfully(p, protocol);
            p.remove(this);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            Exceptions.logIfUnexpected(logger, ctx.channel(), cause);
            ctx.close();
        }
    });
}

From source file:com.linecorp.armeria.client.HttpClientPipelineConfigurator.java

License:Apache License

/**
 * See <a href="https://http2.github.io/http2-spec/#discover-https">HTTP/2 specification</a>.
 *///from www . j a  v a  2 s.  c  o m
private void configureAsHttps(Channel ch, InetSocketAddress remoteAddr) {
    assert sslCtx != null;

    final ChannelPipeline p = ch.pipeline();
    final SslHandler sslHandler = sslCtx.newHandler(ch.alloc(), remoteAddr.getHostString(),
            remoteAddr.getPort());
    p.addLast(sslHandler);
    p.addLast(TrafficLoggingHandler.CLIENT);
    p.addLast(new ChannelInboundHandlerAdapter() {
        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            if (!(evt instanceof SslHandshakeCompletionEvent)) {
                ctx.fireUserEventTriggered(evt);
                return;
            }

            final SslHandshakeCompletionEvent handshakeEvent = (SslHandshakeCompletionEvent) evt;
            if (!handshakeEvent.isSuccess()) {
                // The connection will be closed automatically by SslHandler.
                return;
            }

            final SessionProtocol protocol;
            if (isHttp2Protocol(sslHandler)) {
                if (httpPreference == HttpPreference.HTTP1_REQUIRED) {
                    finishWithNegotiationFailure(ctx, H1, H2, "unexpected protocol negotiation result");
                    return;
                }

                addBeforeSessionHandler(p, newHttp2ConnectionHandler(ch));
                protocol = H2;
            } else {
                if (httpPreference != HttpPreference.HTTP1_REQUIRED) {
                    SessionProtocolNegotiationCache.setUnsupported(ctx.channel().remoteAddress(), H2);
                }

                if (httpPreference == HttpPreference.HTTP2_REQUIRED) {
                    finishWithNegotiationFailure(ctx, H2, H1, "unexpected protocol negotiation result");
                    return;
                }

                addBeforeSessionHandler(p, newHttp1Codec(clientFactory.maxHttp1InitialLineLength(),
                        clientFactory.maxHttp1HeaderSize(), clientFactory.maxHttp1ChunkSize()));
                protocol = H1;
            }
            finishSuccessfully(p, protocol);
            p.remove(this);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            Exceptions.logIfUnexpected(logger, ctx.channel(), cause);
            ctx.close();
        }
    });
}

From source file:com.linecorp.armeria.client.HttpConfigurator.java

License:Apache License

private void configureAsHttps(Channel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    SslHandler sslHandler = sslCtx.newHandler(ch.alloc());
    pipeline.addLast(sslHandler);//from  ww w.  j  a  v a  2s .co m
    pipeline.addLast(new ChannelInboundHandlerAdapter() {
        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            if (!(evt instanceof SslHandshakeCompletionEvent)) {
                ctx.fireUserEventTriggered(evt);
                return;
            }

            final SslHandshakeCompletionEvent handshakeEvent = (SslHandshakeCompletionEvent) evt;
            if (!handshakeEvent.isSuccess()) {
                // The connection will be closed automatically by SslHandler.
                return;
            }

            final SessionProtocol protocol;
            if (isHttp2Protocol(sslHandler)) {
                if (httpPreference == HttpPreference.HTTP1_REQUIRED) {
                    finishWithNegotiationFailure(ctx, H1, H2, "unexpected protocol negotiation result");
                    return;
                }

                addBeforeSessionHandler(pipeline, newHttp2ConnectionHandler(ch));
                protocol = H2;
            } else {
                if (httpPreference != HttpPreference.HTTP1_REQUIRED) {
                    SessionProtocolNegotiationCache.setUnsupported(ctx.channel().remoteAddress(), H2);
                }

                if (httpPreference == HttpPreference.HTTP2_REQUIRED) {
                    finishWithNegotiationFailure(ctx, H2, H1, "unexpected protocol negotiation result");
                    return;
                }

                addBeforeSessionHandler(pipeline, newHttp1Codec());
                protocol = H1;
            }
            finishSuccessfully(pipeline, protocol);
            pipeline.remove(this);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            Exceptions.logIfUnexpected(logger, ctx.channel(), null, cause);
            ctx.close();
        }
    });
}

From source file:com.linkedin.r2.transport.http.client.Http2AlpnHandler.java

License:Apache License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof SslHandshakeCompletionEvent) {
        SslHandshakeCompletionEvent handshakeEvent = (SslHandshakeCompletionEvent) evt;
        if (handshakeEvent.isSuccess()) {
            LOG.debug("SSL handshake succeeded");
            SslHandler sslHandler = ctx.pipeline().get(SslHandler.class);
            if (sslHandler == null) {
                ctx.fireExceptionCaught(
                        new IllegalStateException("cannot find a SslHandler in the pipeline (required for "
                                + "application-level protocol negotiation)"));
                return;
            }//from  w w  w.j av a2  s .  com
            String protocol = sslHandler.applicationProtocol();
            if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
                LOG.debug("HTTP/2 is negotiated");

                // Add HTTP/2 handler
                ctx.pipeline().addAfter("sslHandler", "http2Handler", _http2Handler);

                // Remove handler from pipeline after negotiation is complete
                ctx.pipeline().remove(this);
                _alpnPromise.setSuccess();
            } else {
                LOG.error("Protocol {}, instead of HTTP/2, is negotiated through ALPN", protocol);
                _alpnPromise.setFailure(new IllegalStateException("HTTP/2 ALPN negotiation failed"));
            }
        } else {
            LOG.error("SSL handshake failed", handshakeEvent.cause());
            _alpnPromise.setFailure(handshakeEvent.cause());
        }
    }

    ctx.fireUserEventTriggered(evt);
}

From source file:reactor.ipc.netty.channel.SslReadHandler.java

License:Open Source License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof SslHandshakeCompletionEvent) {
        handshakeDone = true;//www  . j  av  a2s.  c  o  m
        if (ctx.pipeline().context(this) != null) {
            ctx.pipeline().remove(this);
        }
        SslHandshakeCompletionEvent handshake = (SslHandshakeCompletionEvent) evt;
        if (handshake.isSuccess()) {
            ctx.fireChannelActive();
        } else {
            sink.error(handshake.cause());
        }
    }
    super.userEventTriggered(ctx, evt);
}

From source file:reactor.ipc.netty.tcp.SslReadHandler.java

License:Open Source License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof SslHandshakeCompletionEvent) {
        handshakeDone = true;//  w  w w  .  j a va 2 s .c om
        SslHandshakeCompletionEvent handshake = (SslHandshakeCompletionEvent) evt;
        if (handshake.isSuccess()) {
            ctx.fireChannelActive();
        } else {
            sink.error(handshake.cause());
        }
    }
    super.userEventTriggered(ctx, evt);
}