Skip to content

Commit

Permalink
#1253: Do not allow Overlapped paths for ObserveCompositeRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsero authored and sbernard31 committed Jun 3, 2022
1 parent 32aceb2 commit 7bf2d77
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,24 @@ public static List<LwM2mPath> getLwM2mPathList(List<String> paths) {
}
return res;
}

/**
* Checks if any pair of paths from a list of paths is overlapping i.e. one path contains the other
*
* @param paths list of paths to be validated
*
* @exception IllegalArgumentException if there exists any pair of overlapping paths in the list
*/
public static void validateNotOverlapping(List<LwM2mPath> paths) {
for (int i = 0; i < paths.size(); i++) {
LwM2mPath firstPath = paths.get(i);
for (int j = i + 1; j < paths.size(); j++) {
LwM2mPath secondPath = paths.get(j);
if (firstPath.startWith(secondPath) || secondPath.startWith(firstPath)) {
throw new IllegalArgumentException(String
.format("Invalid path list : %s and %s are overlapped paths", firstPath, secondPath));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.request.exception.InvalidRequestException;
import org.eclipse.leshan.core.response.ObserveCompositeResponse;
import org.eclipse.leshan.core.util.Validate;

/**
* A Lightweight M2M request for observing changes of multiple Resources, Resources within an Object Instance or for all
Expand Down Expand Up @@ -88,6 +89,13 @@ public ObserveCompositeRequest(ContentFormat requestContentFormat, ContentFormat
List<LwM2mPath> paths, Object coapRequest) {
super(coapRequest);

try {
Validate.notEmpty(paths, "paths are mandatory");
LwM2mPath.validateNotOverlapping(paths);
} catch (IllegalArgumentException exception) {
throw new InvalidRequestException(exception.getMessage());
}

this.requestContentFormat = requestContentFormat;
this.responseContentFormat = responseContentFormat;
this.paths = paths;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.request.exception.InvalidRequestException;
import org.eclipse.leshan.core.response.ReadCompositeResponse;
import org.eclipse.leshan.core.util.Validate;

/**
* The "Read-Composite" operation can be used by the LwM2M Server to selectively read any combination of Objects, Object
Expand Down Expand Up @@ -86,20 +87,14 @@ private static List<LwM2mPath> getLwM2mPathList(List<String> paths) {
public ReadCompositeRequest(List<LwM2mPath> paths, ContentFormat requestContentFormat,
ContentFormat responseContentFormat, Object coapRequest) {
super(coapRequest);
if (paths == null || paths.size() == 0)
throw new InvalidRequestException("path is mandatory");

// Ensure there is no overlapped Path (e.g. "3/0" and "/3/0/1")
for (int i = 0; i < paths.size(); i++) {
LwM2mPath firstPath = paths.get(i);
for (int j = i + 1; j < paths.size(); j++) {
LwM2mPath secondPath = paths.get(j);
if (firstPath.startWith(secondPath) || secondPath.startWith(firstPath)) {
throw new InvalidRequestException("Invalid path list : %s and %s are overlapped paths", firstPath,
secondPath);
}
}

try {
Validate.notEmpty(paths, "paths are mandatory");
LwM2mPath.validateNotOverlapping(paths);
} catch (IllegalArgumentException exception) {
throw new InvalidRequestException(exception.getMessage());
}

this.paths = paths;
this.requestContentFormat = requestContentFormat;
this.responseContentFormat = responseContentFormat;
Expand Down

0 comments on commit 7bf2d77

Please sign in to comment.