Search in sources :

Example 6 with ConnectionParameters

use of io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters in project fabric8 by jboss-fuse.

the class AmqpProtocol method experimentalSnoopConnectionParameters.

public void experimentalSnoopConnectionParameters(final NetSocket socket, final Buffer received, final Handler<ConnectionParameters> handler) {
    final AmqpProtocolDecoder h = new AmqpProtocolDecoder(this);
    final ConnectionParameters parameters = new ConnectionParameters();
    h.errorHandler(new Handler<String>() {

        @Override
        public void handle(String error) {
            LOG.info("STOMP protocol decoding error: " + error);
            socket.close();
        }
    });
    h.codecHandler(new Handler<AmqpEvent>() {

        EngineFactory engineFactory = new EngineFactoryImpl();

        Transport protonTransport = engineFactory.createTransport();

        Connection protonConnection = engineFactory.createConnection();

        Sasl sasl;

        @Override
        public void handle(AmqpEvent event) {
            switch(event.type) {
                case HEADER:
                    AmqpHeader header = (AmqpHeader) event.decodedFrame;
                    switch(header.getProtocolId()) {
                        case 0:
                            // amqpTransport.sendToAmqp(new AmqpHeader());
                            break;
                        // nothing to do..
                        case 3:
                            // Client will be using SASL for auth..
                            sasl = protonTransport.sasl();
                            // sasl.setMechanisms(new String[] { "ANONYMOUS", "PLAIN" });
                            sasl.server();
                            break;
                        default:
                    }
                    processEvent(event);
                    // Les send back the AMQP response headers so that the client
                    // can send us the SASL init or AMQP open frames.
                    Buffer buffer = toBuffer(protonTransport.getOutputBuffer());
                    protonTransport.outputConsumed();
                    socket.write(buffer);
                    break;
                default:
                    processEvent(event);
            }
        }

        private void processEvent(AmqpEvent event) {
            byte[] buffer = event.encodedFrame.getBytes();
            int offset = 0;
            int remaining = buffer.length;
            while (remaining > 0) {
                try {
                    int count = protonTransport.input(buffer, offset, remaining);
                    offset += count;
                    remaining -= count;
                } catch (Throwable e) {
                    LOG.info("Could not decode AMQP frame: " + e, e);
                    socket.close();
                    return;
                }
                if (sasl != null) {
                    // TODO: add timeout in case the client is waiting for SASL negotiation
                    if (sasl.getRemoteMechanisms().length > 0) {
                        parameters.protocolVirtualHost = getHostname(sasl);
                        if ("PLAIN".equals(sasl.getRemoteMechanisms()[0])) {
                            byte[] data = new byte[sasl.pending()];
                            sasl.recv(data, 0, data.length);
                            Buffer[] parts = split(new Buffer(data), (byte) 0);
                            if (parts.length > 0) {
                                parameters.protocolUser = parts[0].toString();
                            }
                            // We are done!
                            handler.handle(parameters);
                        }
                    }
                }
                if (protonConnection.getLocalState() == EndpointState.UNINITIALIZED && protonConnection.getRemoteState() != EndpointState.UNINITIALIZED) {
                    // If we get here them the connection was not using SASL.. we can get the hostname
                    // info from the open frame.
                    parameters.protocolVirtualHost = protonConnection.getRemoteHostname();
                    // We are done!
                    handler.handle(parameters);
                }
            }
        }
    });
    socket.dataHandler(h);
    h.handle(received);
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer) ConnectionParameters(io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters) EngineFactoryImpl(org.apache.qpid.proton.engine.impl.EngineFactoryImpl)

Example 7 with ConnectionParameters

use of io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters 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)6 Buffer (org.vertx.java.core.buffer.Buffer)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 ServiceDetails (io.fabric8.gateway.ServiceDetails)1 Command (io.fabric8.gateway.handlers.detecting.protocol.openwire.command.Command)1 WireFormatInfo (io.fabric8.gateway.handlers.detecting.protocol.openwire.command.WireFormatInfo)1 SslSocketWrapper (io.fabric8.gateway.handlers.detecting.protocol.ssl.SslSocketWrapper)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 EngineFactoryImpl (org.apache.qpid.proton.engine.impl.EngineFactoryImpl)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