Search in sources :

Example 1 with MqttConnectMessage

use of io.netty.handler.codec.mqtt.MqttConnectMessage in project activemq-artemis by apache.

the class MQTTUtil method traceMessage.

public static void traceMessage(MQTTSessionState state, MqttMessage message, boolean inbound) {
    StringBuilder log = new StringBuilder("MQTT(");
    if (state != null) {
        log.append(state.getClientId());
    }
    if (inbound) {
        log.append("): IN << ");
    } else {
        log.append("): OUT >> ");
    }
    if (message.fixedHeader() != null) {
        log.append(message.fixedHeader().messageType().toString());
        if (message.variableHeader() instanceof MqttMessageIdVariableHeader) {
            log.append("(" + ((MqttMessageIdVariableHeader) message.variableHeader()).messageId() + ")");
        }
        switch(message.fixedHeader().messageType()) {
            case PUBLISH:
                MqttPublishVariableHeader publishHeader = (MqttPublishVariableHeader) message.variableHeader();
                String publishPayload = ((MqttPublishMessage) message).payload().toString(StandardCharsets.UTF_8);
                final int maxPayloadLogSize = 256;
                log.append("(" + publishHeader.packetId() + ")").append(" topic=" + publishHeader.topicName()).append(", qos=" + message.fixedHeader().qosLevel()).append(", retain=" + message.fixedHeader().isRetain()).append(", dup=" + message.fixedHeader().isDup()).append(", payload=" + (publishPayload.length() > maxPayloadLogSize ? publishPayload.substring(0, maxPayloadLogSize) : publishPayload));
                break;
            case CONNECT:
                MqttConnectVariableHeader connectHeader = (MqttConnectVariableHeader) message.variableHeader();
                MqttConnectPayload payload = ((MqttConnectMessage) message).payload();
                log.append(" protocol=(").append(connectHeader.name()).append(", ").append(connectHeader.version()).append(")").append(", hasPassword=").append(connectHeader.hasPassword()).append(", isCleanSession=").append(connectHeader.isCleanSession()).append(", keepAliveTimeSeconds=").append(connectHeader.keepAliveTimeSeconds()).append(", clientIdentifier=").append(payload.clientIdentifier()).append(", hasUserName=").append(connectHeader.hasUserName());
                if (connectHeader.hasUserName()) {
                    log.append(", userName=").append(payload.userName());
                }
                log.append(", isWillFlag=").append(connectHeader.isWillFlag());
                if (connectHeader.isWillFlag()) {
                    log.append(", willQos=").append(connectHeader.willQos()).append(", isWillRetain=").append(connectHeader.isWillRetain()).append(", willTopic=").append(payload.willTopic());
                }
                break;
            case CONNACK:
                MqttConnAckVariableHeader connackHeader = (MqttConnAckVariableHeader) message.variableHeader();
                log.append(" connectReturnCode=").append(connackHeader.connectReturnCode().byteValue()).append(", sessionPresent=").append(connackHeader.isSessionPresent());
                break;
            case SUBSCRIBE:
                for (MqttTopicSubscription sub : ((MqttSubscribeMessage) message).payload().topicSubscriptions()) {
                    log.append("\n\t" + sub.topicName() + " : " + sub.qualityOfService());
                }
                break;
            case SUBACK:
                for (Integer qos : ((MqttSubAckMessage) message).payload().grantedQoSLevels()) {
                    log.append("\n\t" + qos);
                }
                break;
            case UNSUBSCRIBE:
                for (String topic : ((MqttUnsubscribeMessage) message).payload().topics()) {
                    log.append("\n\t" + topic);
                }
                break;
        }
        logger.trace(log.toString());
    }
}
Also used : MqttMessageIdVariableHeader(io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttConnAckVariableHeader(io.netty.handler.codec.mqtt.MqttConnAckVariableHeader) MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription) MqttPublishVariableHeader(io.netty.handler.codec.mqtt.MqttPublishVariableHeader) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MqttConnectPayload(io.netty.handler.codec.mqtt.MqttConnectPayload) MqttConnectVariableHeader(io.netty.handler.codec.mqtt.MqttConnectVariableHeader)

Example 2 with MqttConnectMessage

use of io.netty.handler.codec.mqtt.MqttConnectMessage in project activemq-artemis by apache.

the class SimpleMQTTInterceptor method intercept.

@Override
public boolean intercept(final MqttMessage mqttMessage, RemotingConnection connection) {
    System.out.println("MQTT control packet was intercepted " + mqttMessage.fixedHeader().messageType());
    // If you need to handle an specific packet type:
    if (mqttMessage instanceof MqttPublishMessage) {
        MqttPublishMessage message = (MqttPublishMessage) mqttMessage;
        String originalMessage = message.payload().toString(Charset.forName("UTF-8"));
        System.out.println("Original message: " + originalMessage);
        // The new message content must not be bigger that the original content.
        String modifiedMessage = "Modified message ";
        message.payload().setBytes(0, modifiedMessage.getBytes());
    } else {
        if (mqttMessage instanceof MqttConnectMessage) {
            MqttConnectMessage connectMessage = (MqttConnectMessage) mqttMessage;
            System.out.println("MQTT CONNECT control packet was intercepted " + connectMessage);
        }
    }
    // the target
    return true;
}
Also used : MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage)

Aggregations

MqttConnectMessage (io.netty.handler.codec.mqtt.MqttConnectMessage)2 MqttConnAckVariableHeader (io.netty.handler.codec.mqtt.MqttConnAckVariableHeader)1 MqttConnectPayload (io.netty.handler.codec.mqtt.MqttConnectPayload)1 MqttConnectVariableHeader (io.netty.handler.codec.mqtt.MqttConnectVariableHeader)1 MqttMessageIdVariableHeader (io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader)1 MqttPublishMessage (io.netty.handler.codec.mqtt.MqttPublishMessage)1 MqttPublishVariableHeader (io.netty.handler.codec.mqtt.MqttPublishVariableHeader)1 MqttTopicSubscription (io.netty.handler.codec.mqtt.MqttTopicSubscription)1 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)1