Search in sources :

Example 1 with CONNECT

use of org.fusesource.mqtt.codec.CONNECT in project fabric8 by jboss-fuse.

the class MqttProtocol method snoopConnectionParameters.

@Override
public void snoopConnectionParameters(final SocketWrapper socket, final Buffer received, final Handler<ConnectionParameters> handler) {
    final MqttProtocolDecoder h = new MqttProtocolDecoder(this);
    h.errorHandler(new Handler<String>() {

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

        @Override
        public void handle(MQTTFrame event) {
            try {
                if (event.messageType() == org.fusesource.mqtt.codec.CONNECT.TYPE) {
                    CONNECT connect = new CONNECT().decode(event);
                    ConnectionParameters parameters = new ConnectionParameters();
                    if (connect.clientId() != null) {
                        parameters.protocolClientId = connect.clientId().toString();
                    }
                    if (connect.userName() != null) {
                        parameters.protocolUser = connect.userName().toString();
                        // containing the virtual host info.
                        if (parameters.protocolUser.contains("/")) {
                            // Strip off the virtual host part of the username..
                            String[] parts = parameters.protocolUser.split("/", 2);
                            parameters.protocolVirtualHost = parts[0];
                            parameters.protocolUser = parts[1];
                            // Update the connect frame to strip out the virtual host from the username field...
                            connect.userName(new UTF8Buffer(parameters.protocolUser));
                            // re-write the received buffer /w  the updated connect frame
                            Buffer tail = received.getBuffer((int) h.getBytesDecoded(), received.length());
                            BufferSupport.setLength(received, 0);
                            append(received, connect.encode());
                            received.appendBuffer(tail);
                        }
                    }
                    handler.handle(parameters);
                } else {
                    LOG.info("Expected a CONNECT frame");
                    socket.close();
                }
            } catch (java.net.ProtocolException e) {
                LOG.info("Invalid MQTT frame: " + e, e);
                socket.close();
            }
        }
    });
    socket.readStream().dataHandler(h);
    h.handle(received);
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer) UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer) UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer) ConnectionParameters(io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters) CONNECT(org.fusesource.mqtt.codec.CONNECT) MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame)

Aggregations

ConnectionParameters (io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters)1 UTF8Buffer (org.fusesource.hawtbuf.UTF8Buffer)1 CONNECT (org.fusesource.mqtt.codec.CONNECT)1 MQTTFrame (org.fusesource.mqtt.codec.MQTTFrame)1 Buffer (org.vertx.java.core.buffer.Buffer)1