public ByteBuf getPayload() { return msg.payload(); }
private static ByteBuf encodePublishMessage( ByteBufAllocator byteBufAllocator, MqttPublishMessage message) { MqttFixedHeader mqttFixedHeader = message.fixedHeader(); MqttPublishVariableHeader variableHeader = message.variableHeader(); ByteBuf payload = message.payload().duplicate(); String topicName = variableHeader.topicName(); byte[] topicNameBytes = encodeStringUtf8(topicName); int variableHeaderBufferSize = 2 + topicNameBytes.length + (mqttFixedHeader.qosLevel().value() > 0 ? 2 : 0); int payloadBufferSize = payload.readableBytes(); int variablePartSize = variableHeaderBufferSize + payloadBufferSize; int fixedHeaderBufferSize = 1 + getVariableLengthInt(variablePartSize); ByteBuf buf = byteBufAllocator.buffer(fixedHeaderBufferSize + variablePartSize); buf.writeByte(getFixedHeaderByte1(mqttFixedHeader)); writeVariableLengthInt(buf, variablePartSize); buf.writeShort(topicNameBytes.length); buf.writeBytes(topicNameBytes); if (mqttFixedHeader.qosLevel().value() > 0) { buf.writeShort(variableHeader.messageId()); } buf.writeBytes(payload); return buf; }
void sendPublish(MqttPublishMessage publishMsg) { final int packetId = publishMsg.variableHeader().packetId(); final String topicName = publishMsg.variableHeader().topicName(); final String clientId = getClientId(); MqttQoS qos = publishMsg.fixedHeader().qosLevel(); if (LOG.isTraceEnabled()) { LOG.trace("Sending PUBLISH({}) message. MessageId={}, CId={}, topic={}, payload={}", qos, packetId, clientId, topicName, DebugUtils.payload2Str(publishMsg.payload())); } else { LOG.debug("Sending PUBLISH({}) message. MessageId={}, CId={}, topic={}", qos, packetId, clientId, topicName); } sendIfWritableElseDrop(publishMsg); }
/** * Intended usage is only for embedded versions of the broker, where the hosting application * want to use the broker to send a publish message. Like normal external publish message but * with some changes to avoid security check, and the handshake phases for Qos1 and Qos2. It * also doesn't notifyTopicPublished because using internally the owner should already know * where it's publishing. * * @param msg * the message to publish */ public void internalPublish(MqttPublishMessage msg) { final MqttQoS qos = msg.fixedHeader().qosLevel(); final Topic topic = new Topic(msg.variableHeader().topicName()); final ByteBuf payload = msg.payload(); LOG.info("Sending internal PUBLISH message Topic={}, qos={}", topic, qos); publish2Subscribers(payload, topic, qos); if (!msg.fixedHeader().isRetain()) { return; } if (qos == AT_MOST_ONCE || msg.payload().readableBytes() == 0) { // QoS == 0 && retain => clean old retained retainedRepository.cleanRetained(topic); return; } retainedRepository.retain(topic, msg); }
/** * First phase of a publish QoS2 protocol, sent by publisher to the broker. Publish to all interested * subscribers. */ void receivedPublishQos2(MQTTConnection connection, MqttPublishMessage mqttPublishMessage, String username) { LOG.trace("Processing PUBREL message on connection: {}", connection); final Topic topic = new Topic(mqttPublishMessage.variableHeader().topicName()); final ByteBuf payload = mqttPublishMessage.payload(); final String clientId = connection.getClientId(); if (!authorizator.canWrite(topic, username, clientId)) { LOG.error("MQTT client is not authorized to publish on topic. CId={}, topic: {}", clientId, topic); return; } publish2Subscribers(payload, topic, EXACTLY_ONCE); final boolean retained = mqttPublishMessage.fixedHeader().isRetain(); if (retained) { if (!payload.isReadable()) { retainedRepository.cleanRetained(topic); } else { // before wasn't stored retainedRepository.retain(topic, mqttPublishMessage); } } String clientID = connection.getClientId(); interceptor.notifyTopicPublished(mqttPublishMessage, clientID, username); }
private static void assertPublishIsCorrect(String expectedTopic, String expectedContent, MqttPublishMessage receivedPublish) { assertNotNull("Expecting a PUBLISH message", receivedPublish); final String decodedPayload = DebugUtils.payload2Str(receivedPublish.payload()); assertEquals(expectedContent, decodedPayload); assertEquals(expectedTopic, receivedPublish.variableHeader().topicName()); }
LOG.trace("Processing PUBLISH message. CId={}, topic: {}, messageId: {}, qos: {}", clientId, topicName, msg.variableHeader().packetId(), qos); ByteBuf payload = msg.payload(); final boolean retain = msg.fixedHeader().isRetain(); final Topic topic = new Topic(topicName);
static void verifyPublishIsReceived(EmbeddedChannel embCh, MqttQoS expectedQos, String expectedPayload) { final MqttPublishMessage publishReceived = embCh.flushOutbound().readOutbound(); final String payloadMessage = DebugUtils.payload2Str(publishReceived.payload()); assertEquals("Sent and received payload must be identical", expectedPayload, payloadMessage); assertEquals("Expected QoS don't match", expectedQos, publishReceived.fixedHeader().qosLevel()); }
public ByteBuf getPayload() { return msg.payload(); }
public static Message<MqttPublishVariableHeader, MqttPublishPayload> fromMqttMessage(MqttPublishMessage msg, MqttVersion version, String clientId, String userName, String brokerId) { // forge bytes payload byte[] bytes = new byte[0]; if (msg.payload() != null && msg.payload().readableBytes() > 0) { ByteBuf buf = msg.payload().duplicate(); bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); } return new Message<>(msg.fixedHeader(), new MqttAdditionalHeader(version, clientId, userName, brokerId), msg.variableHeader(), new MqttPublishPayload(bytes)); }
private static ByteBuf encodePublishMessage( ByteBufAllocator byteBufAllocator, MqttPublishMessage message) { MqttFixedHeader mqttFixedHeader = message.fixedHeader(); MqttPublishVariableHeader variableHeader = message.variableHeader(); ByteBuf payload = message.payload().duplicate(); String topicName = variableHeader.topicName(); byte[] topicNameBytes = encodeStringUtf8(topicName); int variableHeaderBufferSize = 2 + topicNameBytes.length + (mqttFixedHeader.qos().value() > 0 ? 2 : 0); int payloadBufferSize = payload.readableBytes(); int variablePartSize = variableHeaderBufferSize + payloadBufferSize; int fixedHeaderBufferSize = 1 + getVariableLengthInt(variablePartSize); ByteBuf buf = byteBufAllocator.buffer(fixedHeaderBufferSize + variablePartSize); buf.writeByte(getFixedHeaderByte1(mqttFixedHeader)); writeVariableLengthInt(buf, variablePartSize); buf.writeShort(topicNameBytes.length); buf.writeBytes(topicNameBytes); if (mqttFixedHeader.qos().value() > 0) { buf.writeShort(variableHeader.packetId()); } buf.writeBytes(payload); return buf; }
@Test public void testPublishMessage() throws Exception { final MqttPublishMessage message = createPublishMessage(); ByteBuf byteBuf = MqttEncoder.doEncode(ALLOCATOR, message); final List<Object> out = new LinkedList<>(); mqttDecoder.decode(ctx, byteBuf, out); assertEquals("Expected one object bout got " + out.size(), 1, out.size()); final MqttPublishMessage decodedMessage = (MqttPublishMessage) out.get(0); validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader()); validatePublishVariableHeader(message.variableHeader(), decodedMessage.variableHeader()); validatePublishPayload(message.payload(), decodedMessage.payload()); }
private void publish(Channel channel,MqttPublishMessage mqttMessage) { MqttFixedHeader mqttFixedHeader = mqttMessage.fixedHeader(); MqttPublishVariableHeader mqttPublishVariableHeader = mqttMessage.variableHeader(); ByteBuf payload = mqttMessage.payload(); byte[] bytes = ByteBufUtil.copyByteBuf(payload); // if(mqttListener!=null){ mqttListener.callBack(mqttPublishVariableHeader.topicName(),new String(bytes)); } switch (mqttFixedHeader.qosLevel()){ case AT_MOST_ONCE: break; case AT_LEAST_ONCE: mqttHandlerApi.pubBackMessage(channel,mqttPublishVariableHeader.messageId()); break; case EXACTLY_ONCE: mqttProducer.pubRecMessage(channel,mqttPublishVariableHeader.messageId()); break; } }
public static IMessagesStore.StoredMessage asStoredMessage(MqttPublishMessage msg) { // TODO ugly, too much array copy ByteBuf payload = msg.payload(); byte[] payloadContent = readBytesAndRewind(payload); IMessagesStore.StoredMessage stored = new IMessagesStore.StoredMessage(payloadContent, msg.fixedHeader().qosLevel(), msg.variableHeader().topicName()); stored.setRetained(msg.fixedHeader().isRetain()); return stored; }
void handlePublish(MqttPublishMessage message) throws Exception { session.getMqttPublishManager().handleMessage(message.variableHeader().packetId(), message.variableHeader().topicName(), message.fixedHeader().qosLevel().value(), message.payload(), message.fixedHeader().isRetain()); }
void handlePublish(MqttPublishMessage message) throws Exception { session.getMqttPublishManager().handleMessage(message.variableHeader().packetId(), message.variableHeader().topicName(), message.fixedHeader().qosLevel().value(), message.payload(), message.fixedHeader().isRetain()); }
void receivedPublishQos1(Channel channel, MqttPublishMessage msg) { // verify if topic can be write final Topic topic = new Topic(msg.variableHeader().topicName()); String clientID = NettyUtils.clientID(channel); String username = NettyUtils.userName(channel); if (!m_authorizator.canWrite(topic, username, clientID)) { LOG.error("MQTT client is not authorized to publish on topic. CId={}, topic={}", clientID, topic); return; } final int messageID = msg.variableHeader().messageId(); // route message to subscribers IMessagesStore.StoredMessage toStoreMsg = asStoredMessage(msg); toStoreMsg.setClientID(clientID); this.publisher.publish2Subscribers(toStoreMsg, topic, messageID); sendPubAck(clientID, messageID); if (msg.fixedHeader().isRetain()) { if (!msg.payload().isReadable()) { m_messagesStore.cleanRetained(topic); } else { // before wasn't stored m_messagesStore.storeRetained(topic, toStoreMsg); } } m_interceptor.notifyTopicPublished(msg, clientID, username); }
public static NutMap transPublishToMapBeta(SessionStore store) { try { NutMap sessionStore = new NutMap(); sessionStore.addv("clientId", store.getClientId()); sessionStore.addv("channelId", store.getChannelId()); sessionStore.addv("cleanSession", store.isCleanSession()); MqttPublishMessage msg = store.getWillMessage(); if (null != msg) { sessionStore.addv("payload", new String(msg.payload().array(), "UTF-8")); sessionStore.addv("messageType", msg.fixedHeader().messageType().value()); sessionStore.addv("isDup", msg.fixedHeader().isDup()); sessionStore.addv("qosLevel", msg.fixedHeader().qosLevel().value()); sessionStore.addv("isRetain", msg.fixedHeader().isRetain()); sessionStore.addv("remainingLength", msg.fixedHeader().remainingLength()); sessionStore.addv("topicName", msg.variableHeader().topicName()); sessionStore.addv("packetId", msg.variableHeader().packetId()); sessionStore.addv("hasWillMessage", true); } return sessionStore; } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return null; }