Package org.xnio.ssl

Examples of org.xnio.ssl.SslConnection


    /**
     * Not really part of the public API, but is used by the HTTP client to initiate a SPDY connection for HTTPS requests.
     */
    public static void handlePotentialSpdyConnection(final StreamConnection connection, final ClientCallback<ClientConnection> listener, final Pool<ByteBuffer> bufferPool, final OptionMap options, final ChannelListener<SslConnection> spdyFailedListener) {
        final SpdySelectionProvider spdySelectionProvider = new SpdySelectionProvider(listener, connection, options, bufferPool);
        final SslConnection sslConnection = (SslConnection) connection;


        try {
            NPN_PUT_METHOD.invoke(null, JsseXnioSsl.getSslEngine(sslConnection), spdySelectionProvider);
        } catch (Exception e) {
            spdyFailedListener.handleEvent(sslConnection);
            return;
        }

        try {
            sslConnection.startHandshake();
            sslConnection.getSourceChannel().getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
                @Override
                public void handleEvent(StreamSourceChannel channel) {

                    if (spdySelectionProvider.selected != null) {
                        if (spdySelectionProvider.selected.equals(HTTP_1_1)) {
                            sslConnection.getSourceChannel().suspendReads();
                            spdyFailedListener.handleEvent(sslConnection);
                            return;
                        } else if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
                            listener.completed(createSpdyChannel());
                        }
                    } else {
                        ByteBuffer buf = ByteBuffer.allocate(100);
                        try {
                            int read = channel.read(buf);
                            if (read > 0) {
                                PushBackStreamSourceConduit pb = new PushBackStreamSourceConduit(connection.getSourceChannel().getConduit());
                                pb.pushBack(new ImmediatePooled<ByteBuffer>(buf));
                                connection.getSourceChannel().setConduit(pb);
                            }
                            if ((spdySelectionProvider.selected == null && read > 0) || HTTP_1_1.equals(spdySelectionProvider.selected)) {
                                sslConnection.getSourceChannel().suspendReads();
                                spdyFailedListener.handleEvent(sslConnection);
                                return;
                            } else if (spdySelectionProvider.selected != null) {
                                //we have spdy
                                if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
                                    listener.completed(createSpdyChannel());
                                }
                            }
                        } catch (IOException e) {
                            listener.failed(e);
                        }
                    }
                }

                private SpdyClientConnection createSpdyChannel() {
                    return new SpdyClientConnection(new SpdyChannel(connection, bufferPool, null, new ByteBufferSlicePool(BufferAllocator.BYTE_BUFFER_ALLOCATOR, 1024, 1024)));
                }
            });
            sslConnection.getSourceChannel().resumeReads();
        } catch (IOException e) {
            listener.failed(e);
        }

    }
View Full Code Here


    /**
     * Not really part of the public API, but is used by the HTTP client to initiate a SPDY connection for HTTPS requests.
     */
    public static void handlePotentialSpdyConnection(final StreamConnection connection, final ClientCallback<ClientConnection> listener, final Pool<ByteBuffer> bufferPool, final OptionMap options, final ChannelListener<SslConnection> spdyFailedListener) {

        final SslConnection sslConnection = (SslConnection) connection;
        final SSLEngine sslEngine = JsseXnioSsl.getSslEngine(sslConnection);

        String existing = (String) sslEngine.getSession().getValue(PROTOCOL_KEY);
        if(existing != null) {
            if (existing.equals(SPDY_3) || existing.equals(SPDY_3_1)) {
                listener.completed(createSpdyChannel(connection, bufferPool));
            } else {
                sslConnection.getSourceChannel().suspendReads();
                spdyFailedListener.handleEvent(sslConnection);
            }
        } else {

            final SpdySelectionProvider spdySelectionProvider = new SpdySelectionProvider(sslEngine);
            try {
                ALPN_PUT_METHOD.invoke(null, sslEngine, spdySelectionProvider);
            } catch (Exception e) {
                spdyFailedListener.handleEvent(sslConnection);
                return;
            }

            try {
                sslConnection.startHandshake();
                sslConnection.getSourceChannel().getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
                    @Override
                    public void handleEvent(StreamSourceChannel channel) {

                        if (spdySelectionProvider.selected != null) {
                            if (spdySelectionProvider.selected.equals(HTTP_1_1)) {
                                sslConnection.getSourceChannel().suspendReads();
                                spdyFailedListener.handleEvent(sslConnection);
                                return;
                            } else if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
                                listener.completed(createSpdyChannel(connection, bufferPool));
                            }
                        } else {
                            ByteBuffer buf = ByteBuffer.allocate(100);
                            try {
                                int read = channel.read(buf);
                                if (read > 0) {
                                    PushBackStreamSourceConduit pb = new PushBackStreamSourceConduit(connection.getSourceChannel().getConduit());
                                    pb.pushBack(new ImmediatePooled<>(buf));
                                    connection.getSourceChannel().setConduit(pb);
                                }
                                if ((spdySelectionProvider.selected == null && read > 0) || HTTP_1_1.equals(spdySelectionProvider.selected)) {
                                    sslConnection.getSourceChannel().suspendReads();
                                    spdyFailedListener.handleEvent(sslConnection);
                                    return;
                                } else if (spdySelectionProvider.selected != null) {
                                    //we have spdy
                                    if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
                                        listener.completed(createSpdyChannel(connection, bufferPool));
                                    }
                                }
                            } catch (IOException e) {
                                listener.failed(e);
                            }
                        }
                    }

                });
                sslConnection.getSourceChannel().resumeReads();
            } catch (IOException e) {
                listener.failed(e);
            }
        }

View Full Code Here

    /**
     * Not really part of the public API, but is used by the HTTP client to initiate a HTTP2 connection for HTTPS requests.
     */
    public static void handlePotentialHttp2Connection(final StreamConnection connection, final ClientCallback<ClientConnection> listener, final Pool<ByteBuffer> bufferPool, final OptionMap options, final ChannelListener<SslConnection> http2FailedListener) {

        final SslConnection sslConnection = (SslConnection) connection;
        final SSLEngine sslEngine = JsseXnioSsl.getSslEngine(sslConnection);

        final SpdySelectionProvider spdySelectionProvider = new SpdySelectionProvider(sslEngine);
        try {
            ALPN_PUT_METHOD.invoke(null, sslEngine, spdySelectionProvider);
        } catch (Exception e) {
            http2FailedListener.handleEvent(sslConnection);
            return;
        }

        try {
            sslConnection.startHandshake();
            sslConnection.getSourceChannel().getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
                @Override
                public void handleEvent(StreamSourceChannel channel) {

                    if (spdySelectionProvider.selected != null) {
                        if (spdySelectionProvider.selected.equals(HTTP_1_1)) {
                            sslConnection.getSourceChannel().suspendReads();
                            http2FailedListener.handleEvent(sslConnection);
                            return;
                        } else if (spdySelectionProvider.selected.equals(HTTP2)) {
                            listener.completed(createHttp2Channel(connection, bufferPool, options));
                        }
                    } else {
                        ByteBuffer buf = ByteBuffer.allocate(100);
                        try {
                            int read = channel.read(buf);
                            if (read > 0) {
                                PushBackStreamSourceConduit pb = new PushBackStreamSourceConduit(connection.getSourceChannel().getConduit());
                                pb.pushBack(new ImmediatePooled<>(buf));
                                connection.getSourceChannel().setConduit(pb);
                            }
                            if (spdySelectionProvider.selected == null) {
                                spdySelectionProvider.selected = (String) sslEngine.getSession().getValue(PROTOCOL_KEY);
                            }
                            if ((spdySelectionProvider.selected == null && read > 0) || HTTP_1_1.equals(spdySelectionProvider.selected)) {
                                sslConnection.getSourceChannel().suspendReads();
                                http2FailedListener.handleEvent(sslConnection);
                                return;
                            } else if (spdySelectionProvider.selected != null) {
                                //we have spdy
                                if (spdySelectionProvider.selected.equals(HTTP2)) {
                                    listener.completed(createHttp2Channel(connection, bufferPool, options));
                                }
                            }
                        } catch (IOException e) {
                            listener.failed(e);
                        }
                    }
                }

            });
            sslConnection.getSourceChannel().resumeReads();
        } catch (IOException e) {
            listener.failed(e);
        } catch (Throwable e) {
            listener.failed(new IOException(e));
        }
View Full Code Here

    /**
     * Not really part of the public API, but is used by the HTTP client to initiate a SPDY connection for HTTPS requests.
     */
    public static void handlePotentialSpdyConnection(final StreamConnection connection, final ClientCallback<ClientConnection> listener, final Pool<ByteBuffer> bufferPool, final OptionMap options, final ChannelListener<SslConnection> spdyFailedListener) {

        final SslConnection sslConnection = (SslConnection) connection;
        final SSLEngine sslEngine = JsseXnioSsl.getSslEngine(sslConnection);

        final SpdySelectionProvider spdySelectionProvider = new SpdySelectionProvider(sslEngine);
        try {
            ALPN_PUT_METHOD.invoke(null, sslEngine, spdySelectionProvider);
        } catch (Exception e) {
            spdyFailedListener.handleEvent(sslConnection);
            return;
        }

        try {
            sslConnection.startHandshake();
            sslConnection.getSourceChannel().getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
                @Override
                public void handleEvent(StreamSourceChannel channel) {

                    if (spdySelectionProvider.selected != null) {
                        if (spdySelectionProvider.selected.equals(HTTP_1_1)) {
                            sslConnection.getSourceChannel().suspendReads();
                            spdyFailedListener.handleEvent(sslConnection);
                            return;
                        } else if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
                            listener.completed(createSpdyChannel(connection, bufferPool));
                        }
                    } else {
                        ByteBuffer buf = ByteBuffer.allocate(100);
                        try {
                            int read = channel.read(buf);
                            if (read > 0) {
                                PushBackStreamSourceConduit pb = new PushBackStreamSourceConduit(connection.getSourceChannel().getConduit());
                                pb.pushBack(new ImmediatePooled<>(buf));
                                connection.getSourceChannel().setConduit(pb);
                            }
                            if(spdySelectionProvider.selected == null) {
                                spdySelectionProvider.selected = (String) sslEngine.getSession().getValue(PROTOCOL_KEY);
                            }
                            if ((spdySelectionProvider.selected == null && read > 0) || HTTP_1_1.equals(spdySelectionProvider.selected)) {
                                sslConnection.getSourceChannel().suspendReads();
                                spdyFailedListener.handleEvent(sslConnection);
                                return;
                            } else if (spdySelectionProvider.selected != null) {
                                //we have spdy
                                if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
                                    listener.completed(createSpdyChannel(connection, bufferPool));
                                }
                            }
                        } catch (IOException e) {
                            listener.failed(e);
                        }
                    }
                }

            });
            sslConnection.getSourceChannel().resumeReads();
        } catch (IOException e) {
            listener.failed(e);
        }


View Full Code Here

    /**
     * Not really part of the public API, but is used by the HTTP client to initiate a HTTP2 connection for HTTPS requests.
     */
    public static void handlePotentialHttp2Connection(final StreamConnection connection, final ClientCallback<ClientConnection> listener, final Pool<ByteBuffer> bufferPool, final OptionMap options, final ChannelListener<SslConnection> http2FailedListener) {

        final SslConnection sslConnection = (SslConnection) connection;
        final SSLEngine sslEngine = JsseXnioSsl.getSslEngine(sslConnection);

        String existing = (String) sslEngine.getSession().getValue(PROTOCOL_KEY);
        if(existing != null) {
            if (existing.equals(HTTP2)) {
                listener.completed(createHttp2Channel(connection, bufferPool, options));
            } else {
                sslConnection.getSourceChannel().suspendReads();
                http2FailedListener.handleEvent(sslConnection);
            }
        } else {

            final SpdySelectionProvider spdySelectionProvider = new SpdySelectionProvider(sslEngine);
            try {
                ALPN_PUT_METHOD.invoke(null, sslEngine, spdySelectionProvider);
            } catch (Exception e) {
                http2FailedListener.handleEvent(sslConnection);
                return;
            }

            try {
                sslConnection.startHandshake();
                sslConnection.getSourceChannel().getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
                    @Override
                    public void handleEvent(StreamSourceChannel channel) {

                        if (spdySelectionProvider.selected != null) {
                            if (spdySelectionProvider.selected.equals(HTTP_1_1)) {
                                sslConnection.getSourceChannel().suspendReads();
                                http2FailedListener.handleEvent(sslConnection);
                                return;
                            } else if (spdySelectionProvider.selected.equals(HTTP2)) {
                                listener.completed(createHttp2Channel(connection, bufferPool, options));
                            }
                        } else {
                            ByteBuffer buf = ByteBuffer.allocate(100);
                            try {
                                int read = channel.read(buf);
                                if (read > 0) {
                                    PushBackStreamSourceConduit pb = new PushBackStreamSourceConduit(connection.getSourceChannel().getConduit());
                                    pb.pushBack(new ImmediatePooled<>(buf));
                                    connection.getSourceChannel().setConduit(pb);
                                }
                                if ((spdySelectionProvider.selected == null && read > 0) || HTTP_1_1.equals(spdySelectionProvider.selected)) {
                                    sslConnection.getSourceChannel().suspendReads();
                                    http2FailedListener.handleEvent(sslConnection);
                                    return;
                                } else if (spdySelectionProvider.selected != null) {
                                    //we have spdy
                                    if (spdySelectionProvider.selected.equals(HTTP2)) {
                                        listener.completed(createHttp2Channel(connection, bufferPool, options));
                                    }
                                }
                            } catch (IOException e) {
                                listener.failed(e);
                            }
                        }
                    }

                });
                sslConnection.getSourceChannel().resumeReads();
            } catch (IOException e) {
                listener.failed(e);
            } catch (Throwable e) {
                listener.failed(new IOException(e));
            }
View Full Code Here

    /**
     * Not really part of the public API, but is used by the HTTP client to initiate a SPDY connection for HTTPS requests.
     */
    public static void handlePotentialSpdyConnection(final StreamConnection connection, final ClientCallback<ClientConnection> listener, final Pool<ByteBuffer> bufferPool, final OptionMap options, final ChannelListener<SslConnection> spdyFailedListener) {

        final SslConnection sslConnection = (SslConnection) connection;
        final SSLEngine sslEngine = JsseXnioSsl.getSslEngine(sslConnection);

        String existing = (String) sslEngine.getSession().getValue(PROTOCOL_KEY);
        if(existing != null) {
            if (existing.equals(SPDY_3) || existing.equals(SPDY_3_1)) {
                listener.completed(createSpdyChannel(connection, bufferPool));
            } else {
                sslConnection.getSourceChannel().suspendReads();
                spdyFailedListener.handleEvent(sslConnection);
            }
        } else {

            final SpdySelectionProvider spdySelectionProvider = new SpdySelectionProvider(sslEngine);
            try {
                ALPN_PUT_METHOD.invoke(null, sslEngine, spdySelectionProvider);
            } catch (Exception e) {
                spdyFailedListener.handleEvent(sslConnection);
                return;
            }

            try {
                sslConnection.startHandshake();
                sslConnection.getSourceChannel().getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
                    @Override
                    public void handleEvent(StreamSourceChannel channel) {

                        if (spdySelectionProvider.selected != null) {
                            if (spdySelectionProvider.selected.equals(HTTP_1_1)) {
                                sslConnection.getSourceChannel().suspendReads();
                                spdyFailedListener.handleEvent(sslConnection);
                                return;
                            } else if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
                                listener.completed(createSpdyChannel(connection, bufferPool));
                            }
                        } else {
                            ByteBuffer buf = ByteBuffer.allocate(100);
                            try {
                                int read = channel.read(buf);
                                if (read > 0) {
                                    PushBackStreamSourceConduit pb = new PushBackStreamSourceConduit(connection.getSourceChannel().getConduit());
                                    pb.pushBack(new ImmediatePooled<>(buf));
                                    connection.getSourceChannel().setConduit(pb);
                                }
                                if ((spdySelectionProvider.selected == null && read > 0) || HTTP_1_1.equals(spdySelectionProvider.selected)) {
                                    sslConnection.getSourceChannel().suspendReads();
                                    spdyFailedListener.handleEvent(sslConnection);
                                    return;
                                } else if (spdySelectionProvider.selected != null) {
                                    //we have spdy
                                    if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
                                        listener.completed(createSpdyChannel(connection, bufferPool));
                                    }
                                }
                            } catch (IOException e) {
                                listener.failed(e);
                            }
                        }
                    }

                });
                sslConnection.getSourceChannel().resumeReads();
            } catch (IOException e) {
                listener.failed(e);
            }
        }

View Full Code Here

    /**
     * Not really part of the public API, but is used by the HTTP client to initiate a SPDY connection for HTTPS requests.
     */
    public static void handlePotentialSpdyConnection(final StreamConnection connection, final ClientCallback<ClientConnection> listener, final Pool<ByteBuffer> bufferPool, final OptionMap options, final ChannelListener<SslConnection> spdyFailedListener) {

        final SslConnection sslConnection = (SslConnection) connection;
        final SSLEngine sslEngine = JsseXnioSsl.getSslEngine(sslConnection);

        String existing = (String) sslEngine.getSession().getValue(PROTOCOL_KEY);
        if(existing != null) {
            if (existing.equals(SPDY_3) || existing.equals(SPDY_3_1)) {
                listener.completed(createSpdyChannel(connection, bufferPool));
            } else {
                sslConnection.getSourceChannel().suspendReads();
                spdyFailedListener.handleEvent(sslConnection);
            }
        } else {

            final SpdySelectionProvider spdySelectionProvider = new SpdySelectionProvider(sslEngine);
            try {
                ALPN_PUT_METHOD.invoke(null, sslEngine, spdySelectionProvider);
            } catch (Exception e) {
                spdyFailedListener.handleEvent(sslConnection);
                return;
            }

            try {
                sslConnection.startHandshake();
                sslConnection.getSourceChannel().getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
                    @Override
                    public void handleEvent(StreamSourceChannel channel) {

                        if (spdySelectionProvider.selected != null) {
                            if (spdySelectionProvider.selected.equals(HTTP_1_1)) {
                                sslConnection.getSourceChannel().suspendReads();
                                spdyFailedListener.handleEvent(sslConnection);
                                return;
                            } else if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
                                listener.completed(createSpdyChannel(connection, bufferPool));
                            }
                        } else {
                            ByteBuffer buf = ByteBuffer.allocate(100);
                            try {
                                int read = channel.read(buf);
                                if (read > 0) {
                                    PushBackStreamSourceConduit pb = new PushBackStreamSourceConduit(connection.getSourceChannel().getConduit());
                                    pb.pushBack(new ImmediatePooled<>(buf));
                                    connection.getSourceChannel().setConduit(pb);
                                }
                                if ((spdySelectionProvider.selected == null && read > 0) || HTTP_1_1.equals(spdySelectionProvider.selected)) {
                                    sslConnection.getSourceChannel().suspendReads();
                                    spdyFailedListener.handleEvent(sslConnection);
                                    return;
                                } else if (spdySelectionProvider.selected != null) {
                                    //we have spdy
                                    if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
                                        listener.completed(createSpdyChannel(connection, bufferPool));
                                    }
                                }
                            } catch (IOException e) {
                                listener.failed(e);
                            }
                        }
                    }

                });
                sslConnection.getSourceChannel().resumeReads();
            } catch (IOException e) {
                listener.failed(e);
            }
        }

View Full Code Here

    /**
     * Not really part of the public API, but is used by the HTTP client to initiate a HTTP2 connection for HTTPS requests.
     */
    public static void handlePotentialHttp2Connection(final StreamConnection connection, final ClientCallback<ClientConnection> listener, final Pool<ByteBuffer> bufferPool, final OptionMap options, final ChannelListener<SslConnection> http2FailedListener) {

        final SslConnection sslConnection = (SslConnection) connection;
        final SSLEngine sslEngine = JsseXnioSsl.getSslEngine(sslConnection);

        String existing = (String) sslEngine.getSession().getValue(PROTOCOL_KEY);
        if(existing != null) {
            if (existing.equals(HTTP2)) {
                listener.completed(createHttp2Channel(connection, bufferPool, options));
            } else {
                sslConnection.getSourceChannel().suspendReads();
                http2FailedListener.handleEvent(sslConnection);
            }
        } else {

            final SpdySelectionProvider spdySelectionProvider = new SpdySelectionProvider(sslEngine);
            try {
                ALPN_PUT_METHOD.invoke(null, sslEngine, spdySelectionProvider);
            } catch (Exception e) {
                http2FailedListener.handleEvent(sslConnection);
                return;
            }

            try {
                sslConnection.startHandshake();
                sslConnection.getSourceChannel().getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
                    @Override
                    public void handleEvent(StreamSourceChannel channel) {

                        if (spdySelectionProvider.selected != null) {
                            if (spdySelectionProvider.selected.equals(HTTP_1_1)) {
                                sslConnection.getSourceChannel().suspendReads();
                                http2FailedListener.handleEvent(sslConnection);
                                return;
                            } else if (spdySelectionProvider.selected.equals(HTTP2)) {
                                listener.completed(createHttp2Channel(connection, bufferPool, options));
                            }
                        } else {
                            ByteBuffer buf = ByteBuffer.allocate(100);
                            try {
                                int read = channel.read(buf);
                                if (read > 0) {
                                    PushBackStreamSourceConduit pb = new PushBackStreamSourceConduit(connection.getSourceChannel().getConduit());
                                    pb.pushBack(new ImmediatePooled<>(buf));
                                    connection.getSourceChannel().setConduit(pb);
                                }
                                if ((spdySelectionProvider.selected == null && read > 0) || HTTP_1_1.equals(spdySelectionProvider.selected)) {
                                    sslConnection.getSourceChannel().suspendReads();
                                    http2FailedListener.handleEvent(sslConnection);
                                    return;
                                } else if (spdySelectionProvider.selected != null) {
                                    //we have spdy
                                    if (spdySelectionProvider.selected.equals(HTTP2)) {
                                        listener.completed(createHttp2Channel(connection, bufferPool, options));
                                    }
                                }
                            } catch (IOException e) {
                                listener.failed(e);
                            }
                        }
                    }

                });
                sslConnection.getSourceChannel().resumeReads();
            } catch (IOException e) {
                listener.failed(e);
            }
        }

View Full Code Here

    /**
     * Not really part of the public API, but is used by the HTTP client to initiate a SPDY connection for HTTPS requests.
     */
    public static void handlePotentialSpdyConnection(final StreamConnection connection, final ClientCallback<ClientConnection> listener, final Pool<ByteBuffer> bufferPool, final OptionMap options, final ChannelListener<SslConnection> spdyFailedListener) {

        final SslConnection sslConnection = (SslConnection) connection;
        final SSLEngine sslEngine = JsseXnioSsl.getSslEngine(sslConnection);

        String existing = (String) sslEngine.getSession().getValue(PROTOCOL_KEY);
        if(existing != null) {
            if (existing.equals(SPDY_3) || existing.equals(SPDY_3_1)) {
                listener.completed(createSpdyChannel(connection, bufferPool));
            } else {
                sslConnection.getSourceChannel().suspendReads();
                spdyFailedListener.handleEvent(sslConnection);
            }
        } else {

            final SpdySelectionProvider spdySelectionProvider = new SpdySelectionProvider(sslEngine);
            try {
                NPN_PUT_METHOD.invoke(null, sslEngine, spdySelectionProvider);
            } catch (Exception e) {
                spdyFailedListener.handleEvent(sslConnection);
                return;
            }

            try {
                sslConnection.startHandshake();
                sslConnection.getSourceChannel().getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
                    @Override
                    public void handleEvent(StreamSourceChannel channel) {

                        if (spdySelectionProvider.selected != null) {
                            if (spdySelectionProvider.selected.equals(HTTP_1_1)) {
                                sslConnection.getSourceChannel().suspendReads();
                                spdyFailedListener.handleEvent(sslConnection);
                                return;
                            } else if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
                                listener.completed(createSpdyChannel(connection, bufferPool));
                            }
                        } else {
                            ByteBuffer buf = ByteBuffer.allocate(100);
                            try {
                                int read = channel.read(buf);
                                if (read > 0) {
                                    PushBackStreamSourceConduit pb = new PushBackStreamSourceConduit(connection.getSourceChannel().getConduit());
                                    pb.pushBack(new ImmediatePooled<>(buf));
                                    connection.getSourceChannel().setConduit(pb);
                                }
                                if ((spdySelectionProvider.selected == null && read > 0) || HTTP_1_1.equals(spdySelectionProvider.selected)) {
                                    sslConnection.getSourceChannel().suspendReads();
                                    spdyFailedListener.handleEvent(sslConnection);
                                    return;
                                } else if (spdySelectionProvider.selected != null) {
                                    //we have spdy
                                    if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
                                        listener.completed(createSpdyChannel(connection, bufferPool));
                                    }
                                }
                            } catch (IOException e) {
                                listener.failed(e);
                            }
                        }
                    }

                });
                sslConnection.getSourceChannel().resumeReads();
            } catch (IOException e) {
                listener.failed(e);
            }
        }

View Full Code Here

    /**
     * Not really part of the public API, but is used by the HTTP client to initiate a HTTP2 connection for HTTPS requests.
     */
    public static void handlePotentialHttp2Connection(final StreamConnection connection, final ClientCallback<ClientConnection> listener, final Pool<ByteBuffer> bufferPool, final OptionMap options, final ChannelListener<SslConnection> http2FailedListener) {

        final SslConnection sslConnection = (SslConnection) connection;
        final SSLEngine sslEngine = JsseXnioSsl.getSslEngine(sslConnection);

        String existing = (String) sslEngine.getSession().getValue(PROTOCOL_KEY);
        if(existing != null) {
            if (existing.equals(HTTP2)) {
                listener.completed(createHttp2Channel(connection, bufferPool, options));
            } else {
                sslConnection.getSourceChannel().suspendReads();
                http2FailedListener.handleEvent(sslConnection);
            }
        } else {

            final SpdySelectionProvider spdySelectionProvider = new SpdySelectionProvider(sslEngine);
            try {
                ALPN_PUT_METHOD.invoke(null, sslEngine, spdySelectionProvider);
            } catch (Exception e) {
                http2FailedListener.handleEvent(sslConnection);
                return;
            }

            try {
                sslConnection.startHandshake();
                sslConnection.getSourceChannel().getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
                    @Override
                    public void handleEvent(StreamSourceChannel channel) {

                        if (spdySelectionProvider.selected != null) {
                            if (spdySelectionProvider.selected.equals(HTTP_1_1)) {
                                sslConnection.getSourceChannel().suspendReads();
                                http2FailedListener.handleEvent(sslConnection);
                                return;
                            } else if (spdySelectionProvider.selected.equals(HTTP2)) {
                                listener.completed(createHttp2Channel(connection, bufferPool, options));
                            }
                        } else {
                            ByteBuffer buf = ByteBuffer.allocate(100);
                            try {
                                int read = channel.read(buf);
                                if (read > 0) {
                                    PushBackStreamSourceConduit pb = new PushBackStreamSourceConduit(connection.getSourceChannel().getConduit());
                                    pb.pushBack(new ImmediatePooled<>(buf));
                                    connection.getSourceChannel().setConduit(pb);
                                }
                                if ((spdySelectionProvider.selected == null && read > 0) || HTTP_1_1.equals(spdySelectionProvider.selected)) {
                                    sslConnection.getSourceChannel().suspendReads();
                                    http2FailedListener.handleEvent(sslConnection);
                                    return;
                                } else if (spdySelectionProvider.selected != null) {
                                    //we have spdy
                                    if (spdySelectionProvider.selected.equals(HTTP2)) {
                                        listener.completed(createHttp2Channel(connection, bufferPool, options));
                                    }
                                }
                            } catch (IOException e) {
                                listener.failed(e);
                            }
                        }
                    }

                });
                sslConnection.getSourceChannel().resumeReads();
            } catch (IOException e) {
                listener.failed(e);
            } catch (Throwable e) {
                listener.failed(new IOException(e));
            }
View Full Code Here

TOP

Related Classes of org.xnio.ssl.SslConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.