Skip to content

Commit

Permalink
Raise exception for unexpected timestamp for OldJSON and SenML
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jun 7, 2024
1 parent 491017d commit 31dccaa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public <T extends LwM2mNode> T decode(byte[] content, LwM2mPath path, LwM2mModel
if (timestampedNodes.size() == 0) {
return null;
} else {
TimestampedLwM2mNode timestampedNode = timestampedNodes.get(0);
validateNoTimestampedValue(timestampedNode, path);

// return the most recent value
return (T) timestampedNodes.get(0).getNode();
}
Expand All @@ -112,6 +115,12 @@ public List<TimestampedLwM2mNode> decodeTimestampedData(byte[] content, LwM2mPat
}
}

protected void validateNoTimestampedValue(TimestampedLwM2mNode timestampedNode, LwM2mPath path) {
if (timestampedNode.getTimestamp() != null) {
throw new CodecException("Unable to decode node[path:%s] : value should not be timestamped", path);
}
}

private List<TimestampedLwM2mNode> parseJSON(JsonRootObject jsonObject, LwM2mPath requestPath, LwM2mModel model,
Class<? extends LwM2mNode> nodeClass) throws CodecException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ public <T extends LwM2mNode> T decode(byte[] content, LwM2mPath path, LwM2mModel
throw new CodecException("Invalid path [%s] for resource, it should start by %s",
resolvedRecord.getPath(), path);
}
if (resolvedRecord.getTimeStamp() != null) {
throw new CodecException("Unable to decode node[path:%s] : value should not be timestamped", path);
}
validateNoTimestampedRecord(resolvedRecord);
resolvedRecords.add(resolvedRecord);
}

Expand Down Expand Up @@ -141,6 +139,7 @@ public Map<LwM2mPath, LwM2mNode> decodeNodes(byte[] content, List<LwM2mPath> pat
// Meaning that a given path could have no corresponding value.
nodes.put(path, null);
} else {
validateNoTimestampedRecord(records);
LwM2mNode node = parseRecords(recordsByPath.get(path), path, model,
DefaultLwM2mDecoder.nodeClassFromPath(path));
nodes.put(path, node);
Expand All @@ -153,6 +152,7 @@ public Map<LwM2mPath, LwM2mNode> decodeNodes(byte[] content, List<LwM2mPath> pat
for (SenMLRecord record : pack.getRecords()) {
LwM2mResolvedSenMLRecord resolvedRecord = resolver.resolve(record);
LwM2mPath path = resolvedRecord.getPath();
validateNoTimestampedRecord(resolvedRecord);
LwM2mNode node = parseRecords(Arrays.asList(resolvedRecord), path, model,
DefaultLwM2mDecoder.nodeClassFromPath(path));
nodes.put(path, node);
Expand All @@ -165,6 +165,19 @@ public Map<LwM2mPath, LwM2mNode> decodeNodes(byte[] content, List<LwM2mPath> pat
}
}

protected void validateNoTimestampedRecord(Collection<LwM2mResolvedSenMLRecord> resolvedRecords) {
for (LwM2mResolvedSenMLRecord resolvedRecord : resolvedRecords) {
validateNoTimestampedRecord(resolvedRecord);
}
}

protected void validateNoTimestampedRecord(LwM2mResolvedSenMLRecord resolvedRecord) {
if (resolvedRecord.getTimeStamp() != null) {
throw new CodecException("Unable to decode node[path:%s] : value should not be timestamped",
resolvedRecord.getPath());
}
}

@Override
public List<TimestampedLwM2mNode> decodeTimestampedData(byte[] content, LwM2mPath path, LwM2mModel model,
Class<? extends LwM2mNode> nodeClass) throws CodecException {
Expand Down

0 comments on commit 31dccaa

Please sign in to comment.