Skip to content

Commit

Permalink
Fix SendRequest validation (not directly linked to this PR ...)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Sep 3, 2024
1 parent 7554715 commit 05d6d10
Showing 1 changed file with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*******************************************************************************/
package org.eclipse.leshan.core.request;

import java.time.Instant;
import java.util.Map;
import java.util.Map.Entry;

Expand Down Expand Up @@ -71,37 +72,41 @@ public SendRequest(ContentFormat format, TimestampedLwM2mNodes timestampedNodes,
throw new InvalidRequestException("Content format MUST be SenML_CBOR or SenML_JSON but was " + format);
}
// Validate Nodes
validateNodes(timestampedNodes.getFlattenNodes());
validateNodes(timestampedNodes);

this.format = format;
}

private void validateNodes(Map<LwM2mPath, LwM2mNode> nodes) {
if (nodes == null || nodes.size() == 0) {
private void validateNodes(TimestampedLwM2mNodes timestampedNodes) {
if (timestampedNodes == null || timestampedNodes.isEmpty() || timestampedNodes.getFlattenNodes().isEmpty()) {
throw new InvalidRequestException(
"SendRequest MUST NOT have empty payload (at least 1 node should be present)");
}
for (Entry<LwM2mPath, LwM2mNode> entry : nodes.entrySet()) {
LwM2mPath path = entry.getKey();
LwM2mNode node = entry.getValue();
if (path == null) {
throw new InvalidRequestException("Invalid key for entry (null, %s) : path MUST NOT be null", node);
}
if (node == null) {
throw new InvalidRequestException("Invalid value for entry (%s, null) : node MUST NOT be null ", path);
}

if (path.isObject() && node instanceof LwM2mObject)
return;
if (path.isObjectInstance() && node instanceof LwM2mObjectInstance)
return;
if (path.isResource() && node instanceof LwM2mSingleResource)
return;
if (path.isResourceInstance() && node instanceof LwM2mResourceInstance)
return;

throw new InvalidRequestException("Invalid value : path (%s) should not refer to a %s value", path,
node.getClass().getSimpleName());
for (Instant t : timestampedNodes.getTimestamps()) {
for (Entry<LwM2mPath, LwM2mNode> entry : timestampedNodes.getNodesAt(t).entrySet()) {
LwM2mPath path = entry.getKey();
LwM2mNode node = entry.getValue();
if (path == null) {
throw new InvalidRequestException("Invalid key for entry (null, %s) : path MUST NOT be null", node);
}
if (node == null) {
throw new InvalidRequestException("Invalid value for entry (%s, null) : node MUST NOT be null ",
path);
}

if (path.isObject() && node instanceof LwM2mObject)
return;
if (path.isObjectInstance() && node instanceof LwM2mObjectInstance)
return;
if (path.isResource() && node instanceof LwM2mSingleResource)
return;
if (path.isResourceInstance() && node instanceof LwM2mResourceInstance)
return;

throw new InvalidRequestException("Invalid value : path (%s) should not refer to a %s value", path,
node.getClass().getSimpleName());
}
}
}

Expand Down

0 comments on commit 05d6d10

Please sign in to comment.