List of usage examples for io.netty.handler.codec.mqtt MqttPublishMessage payload
@Override
public ByteBuf payload()
From source file:com.caricah.iotracah.server.mqttserver.transform.MqttIOTTransformerImpl.java
License:Apache License
@Override public IOTMessage toIOTMessage(MqttMessage serverMessage) { MqttFixedHeader fxH = serverMessage.fixedHeader(); if (null == fxH) { return null; }/* w w w . ja va2 s .c om*/ switch (fxH.messageType()) { case PUBLISH: MqttPublishMessage publishMessage = (MqttPublishMessage) serverMessage; MqttPublishVariableHeader pubVH = publishMessage.variableHeader(); ByteBuffer byteBuffer = publishMessage.payload().nioBuffer(); return PublishMessage.from(pubVH.messageId(), fxH.isDup(), fxH.qosLevel().value(), fxH.isRetain(), pubVH.topicName(), byteBuffer, true); case PUBACK: MqttPubAckMessage pubAckMessage = (MqttPubAckMessage) serverMessage; MqttMessageIdVariableHeader msgIdVH = pubAckMessage.variableHeader(); return AcknowledgeMessage.from(msgIdVH.messageId()); case PUBREC: msgIdVH = (MqttMessageIdVariableHeader) serverMessage.variableHeader(); return PublishReceivedMessage.from(msgIdVH.messageId()); case PUBREL: msgIdVH = (MqttMessageIdVariableHeader) serverMessage.variableHeader(); return ReleaseMessage.from(msgIdVH.messageId(), fxH.isDup()); case PUBCOMP: msgIdVH = (MqttMessageIdVariableHeader) serverMessage.variableHeader(); return CompleteMessage.from(msgIdVH.messageId()); case PINGREQ: case PINGRESP: return Ping.from(fxH.isDup(), fxH.qosLevel().value(), fxH.isRetain()); case CONNECT: MqttConnectMessage mqttConnectMessage = (MqttConnectMessage) serverMessage; MqttConnectVariableHeader conVH = mqttConnectMessage.variableHeader(); MqttConnectPayload conPayload = mqttConnectMessage.payload(); boolean isAnnonymousConnect = (!conVH.hasPassword() && !conVH.hasUserName()); ConnectMessage connectionMessage = ConnectMessage.from(fxH.isDup(), fxH.qosLevel().value(), fxH.isRetain(), conVH.name(), conVH.version(), conVH.isCleanSession(), isAnnonymousConnect, conPayload.clientIdentifier(), conPayload.userName(), conPayload.password(), conVH.keepAliveTimeSeconds(), ""); connectionMessage.setHasWill(conVH.isWillFlag()); connectionMessage.setRetainWill(conVH.isWillRetain()); connectionMessage.setWillQos(conVH.willQos()); connectionMessage.setWillTopic(conPayload.willTopic()); connectionMessage.setWillMessage(conPayload.willMessage()); return connectionMessage; case CONNACK: MqttConnAckMessage connAckMessage = (MqttConnAckMessage) serverMessage; MqttConnAckVariableHeader connAckVH = connAckMessage.variableHeader(); return ConnectAcknowledgeMessage.from(fxH.isDup(), fxH.qosLevel().value(), fxH.isRetain(), 20, connAckVH.connectReturnCode()); case SUBSCRIBE: MqttSubscribeMessage subMsg = (MqttSubscribeMessage) serverMessage; msgIdVH = subMsg.variableHeader(); MqttSubscribePayload subPayload = subMsg.payload(); SubscribeMessage subscribeMessage = SubscribeMessage.from(msgIdVH.messageId(), fxH.isDup(), fxH.qosLevel().value(), fxH.isRetain()); subPayload.topicSubscriptions().forEach(tSub -> { subscribeMessage.getTopicFilterList() .add(new AbstractMap.SimpleEntry<>(tSub.topicName(), tSub.qualityOfService().value())); }); return subscribeMessage; case UNSUBSCRIBE: MqttUnsubscribeMessage unSubMsg = (MqttUnsubscribeMessage) serverMessage; msgIdVH = unSubMsg.variableHeader(); MqttUnsubscribePayload unsubscribePayload = unSubMsg.payload(); return UnSubscribeMessage.from(msgIdVH.messageId(), fxH.isDup(), fxH.qosLevel().value(), fxH.isRetain(), unsubscribePayload.topics()); case DISCONNECT: return DisconnectMessage.from(false); default: return null; } }
From source file:io.moquette.spi.impl.AbstractProtocolProcessorCommonUtils.java
License:Open Source License
protected void verifyPublishIsReceived(EmbeddedChannel channel) { final MqttPublishMessage publishReceived = channel.readOutbound(); String payloadMessage = new String(publishReceived.payload().array(), UTF_8); assertEquals("Sent and received payload must be identical", HELLO_WORLD_MQTT, payloadMessage); }
From source file:io.moquette.spi.impl.AbstractProtocolProcessorCommonUtils.java
License:Open Source License
protected void verifyPublishIsReceived(EmbeddedChannel channel, String expectedPayload, MqttQoS expectedQoS) { final MqttPublishMessage publishReceived = channel.readOutbound(); String payloadMessage = new String(publishReceived.payload().array(), UTF_8); assertEquals("Sent and received payload must be identical", expectedPayload, payloadMessage); assertEquals("Expected QoS don't match", expectedQoS, publishReceived.fixedHeader().qosLevel()); }
From source file:net.anyflow.lannister.message.Message.java
License:Apache License
public static Message newMessage(String clientId, MqttPublishMessage published) { return new Message(published.variableHeader().messageId(), published.variableHeader().topicName(), clientId, NettyUtil.copy(published.payload()), published.fixedHeader().qosLevel(), published.fixedHeader().isRetain()); }
From source file:org.apache.activemq.artemis.core.protocol.mqtt.MQTTProtocolHandler.java
License:Apache License
void handlePublish(MqttPublishMessage message) throws Exception { session.getMqttPublishManager().handleMessage(message.variableHeader().packetId(), message.variableHeader().topicName(), message.fixedHeader().qosLevel().value(), message.payload(), message.fixedHeader().isRetain()); }
From source file:org.apache.activemq.artemis.mqtt.example.SimpleMQTTInterceptor.java
License:Apache License
@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 {/*from www . j ava 2 s. com*/ if (mqttMessage instanceof MqttConnectMessage) { MqttConnectMessage connectMessage = (MqttConnectMessage) mqttMessage; System.out.println("MQTT CONNECT control packet was intercepted " + connectMessage); } } // We return true which means "call next interceptor" (if there is one) or target. // If we returned false, it means "abort call" - no more interceptors would be called and neither would // the target return true; }
From source file:org.thingsboard.mqtt.MqttChannelHandler.java
License:Apache License
private void invokeHandlersForIncomingPublish(MqttPublishMessage message) { boolean handlerInvoked = false; for (MqttSubscription subscription : ImmutableSet.copyOf(this.client.getSubscriptions().values())) { if (subscription.matches(message.variableHeader().topicName())) { if (subscription.isOnce() && subscription.isCalled()) { continue; }//from w w w . j ava2 s.co m message.payload().markReaderIndex(); subscription.setCalled(true); subscription.getHandler().onMessage(message.variableHeader().topicName(), message.payload()); if (subscription.isOnce()) { this.client.off(subscription.getTopic(), subscription.getHandler()); } message.payload().resetReaderIndex(); handlerInvoked = true; } } if (!handlerInvoked && client.getDefaultHandler() != null) { client.getDefaultHandler().onMessage(message.variableHeader().topicName(), message.payload()); } message.payload().release(); }
From source file:org.thingsboard.mqtt.MqttChannelHandler.java
License:Apache License
private void handlePublish(Channel channel, MqttPublishMessage message) { switch (message.fixedHeader().qosLevel()) { case AT_MOST_ONCE: invokeHandlersForIncomingPublish(message); break;/*w w w.jav a2s . co m*/ case AT_LEAST_ONCE: invokeHandlersForIncomingPublish(message); if (message.variableHeader().messageId() != -1) { MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBACK, false, MqttQoS.AT_MOST_ONCE, false, 0); MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader .from(message.variableHeader().messageId()); channel.writeAndFlush(new MqttPubAckMessage(fixedHeader, variableHeader)); } break; case EXACTLY_ONCE: if (message.variableHeader().messageId() != -1) { MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBREC, false, MqttQoS.AT_MOST_ONCE, false, 0); MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader .from(message.variableHeader().messageId()); MqttMessage pubrecMessage = new MqttMessage(fixedHeader, variableHeader); MqttIncomingQos2Publish incomingQos2Publish = new MqttIncomingQos2Publish(message, pubrecMessage); this.client.getQos2PendingIncomingPublishes().put(message.variableHeader().messageId(), incomingQos2Publish); message.payload().retain(); incomingQos2Publish.startPubrecRetransmitTimer(this.client.getEventLoop().next(), this.client::sendAndFlushPacket); channel.writeAndFlush(pubrecMessage); } break; } }
From source file:org.thingsboard.server.transport.mqtt.adaptors.JsonMqttAdaptor.java
License:Apache License
@Override public TransportProtos.PostTelemetryMsg convertToPostTelemetry(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException { String payload = validatePayload(ctx.getSessionId(), inbound.payload()); try {//from www . ja v a 2s .c o m return JsonConverter.convertToTelemetryProto(new JsonParser().parse(payload)); } catch (IllegalStateException | JsonSyntaxException ex) { throw new AdaptorException(ex); } }
From source file:org.thingsboard.server.transport.mqtt.adaptors.JsonMqttAdaptor.java
License:Apache License
@Override public TransportProtos.PostAttributeMsg convertToPostAttributes(MqttDeviceAwareSessionContext ctx, MqttPublishMessage inbound) throws AdaptorException { String payload = validatePayload(ctx.getSessionId(), inbound.payload()); try {/*from w w w.j a va 2s . c o m*/ return JsonConverter.convertToAttributesProto(new JsonParser().parse(payload)); } catch (IllegalStateException | JsonSyntaxException ex) { throw new AdaptorException(ex); } }