Search in sources :

Example 1 with ReadStream

use of org.vertx.java.core.streams.ReadStream in project fabric8 by jboss-fuse.

the class DetectingGateway method handle.

public void handle(final SocketWrapper socket) {
    try {
        shutdownTracker.retain();
        if (!socketsConnecting.add(socket)) {
            throw new AssertionError("Socket existed in the socketsConnecting set");
        }
    } catch (Throwable e) {
        LOG.debug("Could not accept connection from: " + socket.remoteAddress(), e);
        socket.close();
        // shutdownTracker.release();
        return;
    }
    receivedConnectionAttempts.incrementAndGet();
    if (connectionTimeout > 0) {
        vertx.setTimer(connectionTimeout, new Handler<Long>() {

            public void handle(Long timerID) {
                if (socketsConnecting.contains(socket)) {
                    handleConnectFailure(socket, String.format("Gateway client '%s' protocol detection timeout.", socket.remoteAddress()));
                }
            }
        });
    }
    ReadStream<ReadStream> readStream = socket.readStream();
    readStream.exceptionHandler(new Handler<Throwable>() {

        @Override
        public void handle(Throwable e) {
            handleConnectFailure(socket, String.format("Failed to route gateway client '%s' due to: %s", socket.remoteAddress(), e));
        }
    });
    readStream.endHandler(new Handler<Void>() {

        @Override
        public void handle(Void event) {
            handleConnectFailure(socket, String.format("Gateway client '%s' closed the connection before it could be routed.", socket.remoteAddress()));
        }
    });
    readStream.dataHandler(new Handler<Buffer>() {

        Buffer received = new Buffer();

        {
            LOG.debug("Inititalized new Handler[{}] for socket: {}", this, socket.remoteAddress());
        }

        @Override
        public void handle(Buffer event) {
            received.appendBuffer(event);
            if (LOG.isTraceEnabled()) {
                LOG.trace("Socket received following data: {}", event.copy().toString().replaceAll("\r", " "));
                LOG.trace("Data handled by Handler {}", this.toString());
            }
            for (final Protocol protocol : protocols) {
                if (protocol.matches(received)) {
                    if ("ssl".equals(protocol.getProtocolName())) {
                        LOG.info(String.format("SSL Connection from '%s'", socket.remoteAddress()));
                        String disabledCypherSuites = null;
                        String enabledCipherSuites = null;
                        if (sslConfig != null) {
                            disabledCypherSuites = sslConfig.getDisabledCypherSuites();
                            enabledCipherSuites = sslConfig.getEnabledCipherSuites();
                        }
                        if (sslContext == null) {
                            try {
                                if (sslConfig != null) {
                                    sslContext = SSLContext.getInstance(sslConfig.getProtocol());
                                    sslContext.init(sslConfig.getKeyManagers(), sslConfig.getTrustManagers(), null);
                                } else {
                                    sslContext = SSLContext.getDefault();
                                }
                            } catch (Exception e) {
                                handleConnectFailure(socket, "Could initialize SSL: " + e);
                                return;
                            }
                        }
                        // lets wrap it up in a SslSocketWrapper.
                        SslSocketWrapper sslSocketWrapper = new SslSocketWrapper(socket);
                        sslSocketWrapper.putBackHeader(received);
                        sslSocketWrapper.initServer(sslContext, clientAuth, disabledCypherSuites, enabledCipherSuites);
                        // Undo initial connection accounting since we will be redoing @ the SSL level.
                        boolean removed = socketsConnecting.remove(socket);
                        assert removed;
                        receivedConnectionAttempts.decrementAndGet();
                        try {
                            DetectingGateway.this.handle(sslSocketWrapper);
                        } finally {
                            shutdownTracker.release();
                        }
                        return;
                    } else if ("http".equals(protocol.getProtocolName())) {
                        InetSocketAddress target = getHttpGateway();
                        if (target != null) {
                            try {
                                URI url = new URI("http://" + target.getHostString() + ":" + target.getPort());
                                LOG.info(String.format("Connecting '%s' to '%s:%d' using the http protocol", socket.remoteAddress(), url.getHost(), url.getPort()));
                                ConnectionParameters params = new ConnectionParameters();
                                params.protocol = "http";
                                createClient(params, socket, url, received);
                                return;
                            } catch (URISyntaxException e) {
                                handleConnectFailure(socket, "Could not build valid connect URI: " + e);
                                return;
                            }
                        } else {
                            handleConnectFailure(socket, "No http gateway available for the http protocol");
                            return;
                        }
                    } else {
                        protocol.snoopConnectionParameters(socket, received, new Handler<ConnectionParameters>() {

                            @Override
                            public void handle(ConnectionParameters connectionParameters) {
                                // this will install a new dataHandler on the socket.
                                if (connectionParameters.protocol == null)
                                    connectionParameters.protocol = protocol.getProtocolName();
                                if (connectionParameters.protocolSchemes == null)
                                    connectionParameters.protocolSchemes = protocol.getProtocolSchemes();
                                route(socket, connectionParameters, received);
                            }
                        });
                        return;
                    }
                }
            }
            if (received.length() >= maxProtocolIdentificationLength) {
                handleConnectFailure(socket, "Connection did not use one of the enabled protocols " + getProtocolNames());
            }
        }
    });
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer) InetSocketAddress(java.net.InetSocketAddress) ConnectionParameters(io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) ReadStream(org.vertx.java.core.streams.ReadStream) AtomicLong(java.util.concurrent.atomic.AtomicLong) SslSocketWrapper(io.fabric8.gateway.handlers.detecting.protocol.ssl.SslSocketWrapper)

Aggregations

SslSocketWrapper (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslSocketWrapper)1 ConnectionParameters (io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters)1 InetSocketAddress (java.net.InetSocketAddress)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Buffer (org.vertx.java.core.buffer.Buffer)1 ReadStream (org.vertx.java.core.streams.ReadStream)1