Search in sources :

Example 6 with SocketWrapper

use of io.fabric8.gateway.SocketWrapper in project fabric8 by jboss-fuse.

the class DetectingGateway method route.

public void route(final SocketWrapper socket, ConnectionParameters params, final Buffer received) {
    NetClient client = null;
    if (params.protocolVirtualHost == null) {
        params.protocolVirtualHost = defaultVirtualHost;
    }
    HashSet<String> schemes = new HashSet<String>(Arrays.asList(params.protocolSchemes));
    if (params.protocolVirtualHost != null) {
        List<ServiceDetails> services = serviceMap.getServices(params.protocolVirtualHost);
        // Lets try again with the defaultVirtualHost
        if (services.isEmpty() && !params.protocolVirtualHost.equals(defaultVirtualHost)) {
            params.protocolVirtualHost = defaultVirtualHost;
            services = serviceMap.getServices(params.protocolVirtualHost);
        }
        LOG.debug(String.format("%d services match the virtual host", services.size()));
        if (!services.isEmpty()) {
            ClientRequestFacade clientRequestFacade = clientRequestFacadeFactory.create(socket, params);
            ServiceDetails serviceDetails = serviceLoadBalancer.choose(services, clientRequestFacade);
            if (serviceDetails != null) {
                List<String> urlStrings = serviceDetails.getServices();
                LOG.debug("Selected service exposes the following URLS: {}", urlStrings);
                for (String urlString : urlStrings) {
                    if (Strings.notEmpty(urlString)) {
                        // lets create a client for this request...
                        try {
                            URI uri = new URI(urlString);
                            // URL url = new URL(urlString);
                            String urlProtocol = uri.getScheme();
                            if (schemes.contains(urlProtocol)) {
                                if (!socket.remoteAddress().toString().equals(clientRequestFacade.getClientRequestKey())) {
                                    LOG.info(String.format("Connecting client from '%s' (with key '%s') requesting virtual host '%s' to '%s:%d' using the %s protocol", socket.remoteAddress(), clientRequestFacade.getClientRequestKey(), params.protocolVirtualHost, uri.getHost(), uri.getPort(), params.protocol));
                                } else {
                                    LOG.info(String.format("Connecting client from '%s' requesting virtual host '%s' to '%s:%d' using the %s protocol", socket.remoteAddress(), params.protocolVirtualHost, uri.getHost(), uri.getPort(), params.protocol));
                                }
                                client = createClient(params, socket, uri, received);
                                break;
                            }
                        } catch (URISyntaxException e) {
                            LOG.warn("Failed to parse URI: " + urlString + ". " + e, e);
                        }
                    }
                }
            }
        }
    }
    if (client == null) {
        // failed to route
        handleConnectFailure(socket, String.format("No endpoint available for virtual host '%s' and protocol %s", params.protocolVirtualHost, params.protocol));
    }
}
Also used : NetClient(org.vertx.java.core.net.NetClient) ServiceDetails(io.fabric8.gateway.ServiceDetails) ClientRequestFacade(io.fabric8.gateway.loadbalancer.ClientRequestFacade) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 7 with SocketWrapper

use of io.fabric8.gateway.SocketWrapper in project fabric8 by jboss-fuse.

the class AmqpProtocol method snoopConnectionParameters.

@Override
public void snoopConnectionParameters(final SocketWrapper socket, final Buffer received, final Handler<ConnectionParameters> handler) {
    // We can't yet snoop the virtual host info from a AMQP connection..
    final AmqpProtocolDecoder h = new AmqpProtocolDecoder(this);
    handler.handle(new ConnectionParameters());
}
Also used : ConnectionParameters(io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters)

Aggregations

ConnectionParameters (io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters)5 SslSocketWrapper (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslSocketWrapper)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Buffer (org.vertx.java.core.buffer.Buffer)2 ServiceDetails (io.fabric8.gateway.ServiceDetails)1 SocketWrapper (io.fabric8.gateway.SocketWrapper)1 Command (io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command)1 WireFormatInfo (io.fabric8.gateway.handlers.detecting.protocol.openwire.command.WireFormatInfo)1 ClientRequestFacade (io.fabric8.gateway.loadbalancer.ClientRequestFacade)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 UTF8Buffer (org.fusesource.hawtbuf.UTF8Buffer)1 CONNECT (org.fusesource.mqtt.codec.CONNECT)1 MQTTFrame (org.fusesource.mqtt.codec.MQTTFrame)1 NetClient (org.vertx.java.core.net.NetClient)1 ReadStream (org.vertx.java.core.streams.ReadStream)1