Skip to content

Commit

Permalink
Added MqttPayload.hasBody() method
Browse files Browse the repository at this point in the history
Signed-off-by: coduz <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Apr 14, 2020
1 parent 2bffe49 commit bccc35b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
Expand Up @@ -57,13 +57,16 @@ private KuraDataChannel translate(MqttTopic mqttTopic)

private KuraDataPayload translate(MqttPayload mqttPayload)
throws KapuaException {
byte[] mqttBody = mqttPayload.getBody();

KuraDataPayload kuraPayload = new KuraDataPayload();
try {
kuraPayload.readFromByteArray(mqttBody);
} catch (MessageException ex) {
kuraPayload.setBody(mqttBody);

if (mqttPayload.hasBody()) {
byte[] mqttBody = mqttPayload.getBody();
try {
kuraPayload.readFromByteArray(mqttBody);
} catch (MessageException ex) {
kuraPayload.setBody(mqttBody);
}
}

//
Expand Down
Expand Up @@ -25,9 +25,8 @@

/**
* Messages translator implementation from {@link org.eclipse.kapua.transport.message.mqtt.MqttMessage} to {@link KuraResponseMessage}
*
* @since 1.0
*
* @since 1.0
*/
public class TranslatorResponseMqttKura extends Translator<MqttMessage, KuraResponseMessage> {

Expand Down Expand Up @@ -76,10 +75,12 @@ private KuraResponseChannel translate(MqttTopic mqttTopic)

private KuraResponsePayload translate(MqttPayload mqttPayload)
throws KapuaException {
byte[] mqttBody = mqttPayload.getBody();

KuraResponsePayload kuraResponsePayload = new KuraResponsePayload();
kuraResponsePayload.readFromByteArray(mqttBody);

if (mqttPayload.hasBody()) {
byte[] mqttBody = mqttPayload.getBody();
kuraResponsePayload.readFromByteArray(mqttBody);
}

//
// Return Kura Payload
Expand Down
Expand Up @@ -22,6 +22,8 @@ public class MqttPayload implements TransportPayload {

/**
* The raw body of this {@link MqttPayload}.
*
* @since 1.0.0
*/
private byte[] body;

Expand Down Expand Up @@ -54,4 +56,16 @@ public byte[] getBody() {
public void setBody(byte[] body) {
this.body = body;
}

/**
* Says whether or not the {@link #getBody()} has value.
* <p>
* Checks for {@code null} and size equals to 0
*
* @return {@code true} if {@link #getBody()} is not {@code null} and {@link #getBody()}{@code length > 0}, {@code false} otherwise.
* @since 1.2.0
*/
public boolean hasBody() {
return getBody() != null && getBody().length > 0;
}
}
Expand Up @@ -45,7 +45,7 @@ public class MqttClientPublishException extends MqttClientException {
* @since 1.2.0
*/
public MqttClientPublishException(Throwable cause, String clientId, String topic, MqttMessage mqttMessage) {
super(MqttClientErrorCodes.PUBLISH_EXCEPTION, cause, clientId, topic, mqttMessage.getPayload().getBody().length);
super(MqttClientErrorCodes.PUBLISH_EXCEPTION, cause, clientId, topic, mqttMessage.getPayload().hasBody() ? mqttMessage.getPayload().getBody().length : null);

this.topic = topic;
this.mqttMessage = mqttMessage;
Expand Down

0 comments on commit bccc35b

Please sign in to comment.