/******************************************************************************* * Copyright (c) 2016 IBM Corp. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * and Eclipse Distribution License v1.0 which accompany this distribution. * * The Eclipse Public License is available at * https://www.eclipse.org/legal/epl-2.0 * and the Eclipse Distribution License is available at * https://www.eclipse.org/org/documents/edl-v10.php * * Contributors: * Dave Locke - Original MQTTv3 implementation * James Sutton - Initial MQTTv5 implementation */ package org.eclipse.paho.mqttv5.common; import org.eclipse.paho.mqttv5.common.packet.MqttProperties; /** * An MQTT message holds the application payload and options specifying how the * message is to be delivered The message includes a "payload" (the body of the * message) represented as a byte[]. */ public class MqttMessage { private boolean mutable = true; private byte[] payload; private int qos = 1; private boolean retained = false; private boolean dup = false; private int messageId; private MqttProperties properties; /** * Utility method to validate the supplied QoS value. * * @param qos * The Quality Of Service Level to validate * @throws IllegalArgumentException * if value of QoS is not 0, 1 or 2. * */ public static void validateQos(int qos) { if ((qos < 0) || (qos > 2)) { throw new IllegalArgumentException(); } } /** * Constructs a message with an empty payload, and all other values set to * defaults. * * The defaults are: *
true
if the message should be, or was, retained by the
* server.
* @see #setRetained(boolean)
*/
public boolean isRetained() {
return retained;
}
/**
* Whether or not the publish message should be retained by the messaging
* engine. Sending a message with retained set to true
and with an
* empty byte array as the payload e.g. new byte[0]
will clear the
* retained message from the server. The default value is false
*
* @param retained
* whether or not the messaging engine should retain the message.
* @throws IllegalStateException
* if this message cannot be edited
*/
public void setRetained(boolean retained) {
checkMutable();
this.retained = retained;
}
/**
* Returns the quality of service for this message.
*
* @return the quality of service to use, either 0, 1, or 2.
* @see #setQos(int)
*/
public int getQos() {
return qos;
}
/**
* Sets the quality of service for this message.
* MqttConnectOptions
. If a persistence mechanism is not
* specified, the message will not be delivered in the event of a client
* failure. The message will be acknowledged across the network. This is the
* default QoS.MqttConnectOptions
. If a persistence mechanism is not
* specified, the message will not be delivered in the event of a client
* failure.true
if the values can be changed, false
* to prevent them from being changed.
*/
public void setMutable(boolean mutable) {
this.mutable = mutable;
}
protected void checkMutable() throws IllegalStateException {
if (!mutable) {
throw new IllegalStateException();
}
}
public void setDuplicate(boolean dup) {
this.dup = dup;
}
/**
* Returns whether or not this message might be a duplicate of one which has
* already been received. This will only be set on messages received from the
* server.
*
* @return true
if the message might be a duplicate.
*/
public boolean isDuplicate() {
return this.dup;
}
/**
* This is only to be used internally to provide the MQTT id of a message
* received from the server. Has no effect when publishing messages.
*
* @param messageId
* the Message Identifier
*/
public void setId(int messageId) {
this.messageId = messageId;
}
/**
* Returns the MQTT id of the message. This is only applicable to messages
* received from the server.
*
* @return the MQTT id of the message
*/
public int getId() {
return this.messageId;
}
public MqttProperties getProperties() {
return properties;
}
public void setProperties(MqttProperties properties) {
this.properties = properties;
}
/**
* Returns a string representation of this message's payload. Makes an attempt
* to return the payload as a string. As the MQTT client has no control over the
* content of the payload it may fail.
*
* @return a string representation of this message.
*/
public String toString() {
return new String(payload);
}
public String toDebugString() {
return "MqttMessage [mutable=" + mutable + ", payload=" + new String(payload) + ", qos=" + qos + ", retained="
+ retained + ", dup=" + dup + ", messageId=" + messageId + "]";
}
}