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());
}
}
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;
}
Aggregations