Skip to content

Commit

Permalink
#1082: Create dedicated LwM2mPathInvalidException.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Dec 1, 2021
1 parent e7a6f92 commit 0cba977
Show file tree
Hide file tree
Showing 24 changed files with 219 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.eclipse.leshan.core.node.LwM2mObject;
import org.eclipse.leshan.core.node.LwM2mObjectInstance;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.InvalidLwM2mPathException;
import org.eclipse.leshan.core.node.LwM2mResource;
import org.eclipse.leshan.core.node.codec.CodecException;
import org.eclipse.leshan.core.node.codec.LwM2mDecoder;
Expand All @@ -63,6 +64,7 @@
import org.eclipse.leshan.core.request.WriteAttributesRequest;
import org.eclipse.leshan.core.request.WriteRequest;
import org.eclipse.leshan.core.request.WriteRequest.Mode;
import org.eclipse.leshan.core.request.exception.InvalidRequestException;
import org.eclipse.leshan.core.response.BootstrapDeleteResponse;
import org.eclipse.leshan.core.response.BootstrapDiscoverResponse;
import org.eclipse.leshan.core.response.BootstrapReadResponse;
Expand Down Expand Up @@ -149,7 +151,7 @@ public void handleGET(CoapExchange exchange) {
ObserveRequest observeRequest = new ObserveRequest(requestedContentFormat, URI, coapRequest);
ObserveResponse response = nodeEnabler.observe(identity, observeRequest);
if (response.getCode() == org.eclipse.leshan.core.ResponseCode.CONTENT) {
LwM2mPath path = new LwM2mPath(URI);
LwM2mPath path = getPath(URI);
LwM2mNode content = response.getContent();
LwM2mModel model = new StaticModel(nodeEnabler.getObjectModel());
ContentFormat format = getContentFormat(observeRequest, requestedContentFormat);
Expand All @@ -167,7 +169,7 @@ public void handleGET(CoapExchange exchange) {
coapRequest);
BootstrapReadResponse response = nodeEnabler.read(identity, readRequest);
if (response.getCode() == org.eclipse.leshan.core.ResponseCode.CONTENT) {
LwM2mPath path = new LwM2mPath(URI);
LwM2mPath path = getPath(URI);
LwM2mNode content = response.getContent();
LwM2mModel model = new StaticModel(nodeEnabler.getObjectModel());
ContentFormat format = getContentFormat(readRequest, requestedContentFormat);
Expand All @@ -183,7 +185,7 @@ public void handleGET(CoapExchange exchange) {
ReadRequest readRequest = new ReadRequest(requestedContentFormat, URI, coapRequest);
ReadResponse response = nodeEnabler.read(identity, readRequest);
if (response.getCode() == org.eclipse.leshan.core.ResponseCode.CONTENT) {
LwM2mPath path = new LwM2mPath(URI);
LwM2mPath path = getPath(URI);
LwM2mNode content = response.getContent();
LwM2mModel model = new StaticModel(nodeEnabler.getObjectModel());
ContentFormat format = getContentFormat(readRequest, requestedContentFormat);
Expand Down Expand Up @@ -238,7 +240,7 @@ public void handlePUT(CoapExchange coapExchange) {
}
// Manage Write and Bootstrap Write Request (replace)
else {
LwM2mPath path = new LwM2mPath(URI);
LwM2mPath path = getPath(URI);

if (!coapExchange.getRequestOptions().hasContentFormat()) {
handleInvalidRequest(coapExchange, "Content Format is mandatory");
Expand Down Expand Up @@ -290,7 +292,7 @@ public void handlePOST(CoapExchange exchange) {
String URI = exchange.getRequestOptions().getUriPathString();
Request coapRequest = exchange.advanced().getRequest();

LwM2mPath path = new LwM2mPath(URI);
LwM2mPath path = getPath(URI);

// Manage Execute Request
if (path.isResource()) {
Expand Down Expand Up @@ -444,4 +446,12 @@ public void objectInstancesAdded(LwM2mObjectEnabler object, int... instanceIds)
@Override
public void objectInstancesRemoved(LwM2mObjectEnabler object, int... instanceIds) {
}

protected LwM2mPath getPath(String URI) throws InvalidRequestException {
try {
return new LwM2mPath(URI);
} catch (InvalidLwM2mPathException e) {
throw new InvalidRequestException(e, "Invalid path : %s", e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2021 Sierra Wireless and others.
*
* 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
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.core.node;

/**
* Raise if you try to create an Invalid {@link LwM2mPath}
*/
public class InvalidLwM2mPathException extends RuntimeException {
private static final long serialVersionUID = 1L;

public InvalidLwM2mPathException(String message) {
super(message);
}

public InvalidLwM2mPathException(String message, Object... args) {
super(String.format(message, args));
}

public InvalidLwM2mPathException(Exception e, String message, Object... args) {
super(String.format(message, args), e);
}

public InvalidLwM2mPathException(String message, Exception cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public class LwM2mIncompletePath extends LwM2mPath {
* Create a path to an object instance without object instance id
*
* @param objectId the object identifier
*
* @throws InvalidLwM2mPathException if you try to create path with invalid inputs
*/
public LwM2mIncompletePath(int objectId) {
public LwM2mIncompletePath(int objectId) throws InvalidLwM2mPathException {
super(objectId, LwM2mObjectInstance.UNDEFINED, null, null);
validate();
}
Expand All @@ -38,8 +40,10 @@ public LwM2mIncompletePath(int objectId) {
*
* @param objectId the object identifier
* @param resourceId the resource identifier
*
* @throws InvalidLwM2mPathException if you try to create path with invalid inputs
*/
public LwM2mIncompletePath(int objectId, int resourceId) {
public LwM2mIncompletePath(int objectId, int resourceId) throws InvalidLwM2mPathException {
super(objectId, LwM2mObjectInstance.UNDEFINED, resourceId, null);
validate();
}
Expand All @@ -50,8 +54,10 @@ public LwM2mIncompletePath(int objectId, int resourceId) {
* @param objectId the object identifier
* @param resourceId the resource identifier
* @param resourceInstanceId the resource instance identifier
*
* @throws InvalidLwM2mPathException if you try to create path with invalid inputs
*/
public LwM2mIncompletePath(int objectId, int resourceId, int resourceInstanceId) {
public LwM2mIncompletePath(int objectId, int resourceId, int resourceInstanceId) throws InvalidLwM2mPathException {
super(objectId, LwM2mObjectInstance.UNDEFINED, resourceId, resourceInstanceId);
validate();
}
Expand Down Expand Up @@ -86,7 +92,7 @@ public LwM2mPath toResourcePath() {
}

@Override
protected void validate() {
protected void validate() throws InvalidLwM2mPathException {
LwM2mNodeUtil.validateIncompletePath(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ private static String getInvalidPathCause(LwM2mPath path) {
return null;
}

public static void validatePath(LwM2mPath path) throws LwM2mNodeException {
public static void validatePath(LwM2mPath path) throws InvalidLwM2mPathException {
String err = getInvalidPathCause(path);
if (err != null)
throw new LwM2mNodeException(err);
throw new InvalidLwM2mPathException(err);
}

private static String getInvalidIncompletePathCause(LwM2mPath path) {
Expand Down Expand Up @@ -261,9 +261,9 @@ private static String getInvalidIncompletePathCause(LwM2mPath path) {
return null;
}

public static void validateIncompletePath(LwM2mPath path) throws LwM2mNodeException {
public static void validateIncompletePath(LwM2mPath path) throws InvalidLwM2mPathException {
String err = getInvalidIncompletePathCause(path);
if (err != null)
throw new LwM2mNodeException(err);
throw new InvalidLwM2mPathException(err);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ private LwM2mPath() {
* Create a path to an object
*
* @param objectId the object identifier
*
* @throws InvalidLwM2mPathException if you try to create path with invalid inputs
*/
public LwM2mPath(int objectId) {
public LwM2mPath(int objectId) throws InvalidLwM2mPathException {
this(objectId, null, null, null);
validate();
}
Expand All @@ -57,8 +59,10 @@ public LwM2mPath(int objectId) {
*
* @param objectId the object identifier
* @param objectInstanceId the instance identifier
*
* @throws InvalidLwM2mPathException if you try to create path with invalid inputs
*/
public LwM2mPath(int objectId, int objectInstanceId) {
public LwM2mPath(int objectId, int objectInstanceId) throws InvalidLwM2mPathException {
this(objectId, objectInstanceId, null, null);
validate();
}
Expand All @@ -69,8 +73,10 @@ public LwM2mPath(int objectId, int objectInstanceId) {
* @param objectId the object identifier
* @param objectInstanceId the instance identifier
* @param resourceId the resource identifier
*
* @throws InvalidLwM2mPathException if you try to create path with invalid inputs
*/
public LwM2mPath(int objectId, int objectInstanceId, int resourceId) {
public LwM2mPath(int objectId, int objectInstanceId, int resourceId) throws InvalidLwM2mPathException {
this(objectId, objectInstanceId, resourceId, null);
validate();
}
Expand All @@ -82,8 +88,11 @@ public LwM2mPath(int objectId, int objectInstanceId, int resourceId) {
* @param objectInstanceId the instance identifier
* @param resourceId the resource identifier
* @param resourceInstanceId the resource instance identifier
*
* @throws InvalidLwM2mPathException if you try to create path with invalid inputs
*/
public LwM2mPath(int objectId, int objectInstanceId, int resourceId, int resourceInstanceId) {
public LwM2mPath(int objectId, int objectInstanceId, int resourceId, int resourceInstanceId)
throws InvalidLwM2mPathException {
this((Integer) objectId, (Integer) objectInstanceId, (Integer) resourceId, (Integer) resourceInstanceId);
validate();
}
Expand All @@ -92,8 +101,10 @@ public LwM2mPath(int objectId, int objectInstanceId, int resourceId, int resourc
* Constructs a {@link LwM2mPath} from a string representation
*
* @param path the path (e.g. "/3/0/1" or "/3")
*
* @throws InvalidLwM2mPathException if you try to create path with invalid inputs
*/
public LwM2mPath(String path) {
public LwM2mPath(String path) throws InvalidLwM2mPathException {
Validate.notNull(path);
if (path.startsWith("/")) {
path = path.substring(1);
Expand All @@ -103,7 +114,7 @@ public LwM2mPath(String path) {
}
String[] p = path.split("/");
if (0 > p.length || p.length > 4) {
throw new IllegalArgumentException("Invalid length for path: " + path);
throw new InvalidLwM2mPathException("Invalid length for path: ", path);
}
try {
this.objectId = (p.length >= 1 && !p[0].isEmpty()) ? Integer.valueOf(p[0]) : null;
Expand All @@ -112,7 +123,7 @@ public LwM2mPath(String path) {
this.resourceInstanceId = (p.length == 4) ? Integer.valueOf(p[3]) : null;
validate();
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid elements in path: " + path, e);
throw new InvalidLwM2mPathException(e, "Invalid elements in path: ", path);
}
}

Expand All @@ -127,8 +138,9 @@ protected LwM2mPath(Integer objectId, Integer objectInstanceId, Integer resource
* Validate the current path and raise {@link IllegalArgumentException} is path is not valid
*
* @see LwM2mNodeUtil#validatePath(LwM2mPath)
* @throws InvalidLwM2mPathException if you try to create path with invalid inputs
*/
protected void validate() {
protected void validate() throws InvalidLwM2mPathException {
LwM2mNodeUtil.validatePath(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ public ObjectLink(int objectId, int objectInstanceId) {
* Create a Object Link referencing an object instance with the given path.
*/
public static ObjectLink fromPath(String path) {
LwM2mPath lwM2mPath = new LwM2mPath(path);
if (lwM2mPath.isRoot()) {
return new ObjectLink(); // create null link
} else if (lwM2mPath.isObjectInstance()) {
return new ObjectLink(lwM2mPath.getObjectId(), lwM2mPath.getObjectInstanceId());
} else {
throw new IllegalArgumentException("Invalid path: ObjectLink should reference an object instance");
try {
LwM2mPath lwM2mPath = new LwM2mPath(path);
if (lwM2mPath.isRoot()) {
return new ObjectLink(); // create null link
} else if (lwM2mPath.isObjectInstance()) {
return new ObjectLink(lwM2mPath.getObjectId(), lwM2mPath.getObjectInstanceId());
} else {
throw new IllegalArgumentException("Invalid path: ObjectLink should reference an object instance");
}
} catch (InvalidLwM2mPathException e) {
throw new IllegalArgumentException("Invalid path " + path, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.eclipse.leshan.core.node.LwM2mObject;
import org.eclipse.leshan.core.node.LwM2mObjectInstance;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.InvalidLwM2mPathException;
import org.eclipse.leshan.core.node.LwM2mResource;
import org.eclipse.leshan.core.node.LwM2mResourceInstance;
import org.eclipse.leshan.core.node.LwM2mSingleResource;
Expand Down Expand Up @@ -82,7 +83,7 @@ public <T extends LwM2mNode> T decode(byte[] content, LwM2mPath path, LwM2mModel
// return the most recent value
return (T) timestampedNodes.get(0).getNode();
}
} catch (LwM2mJsonException | LwM2mNodeException e) {
} catch (LwM2mJsonException | LwM2mNodeException | InvalidLwM2mPathException e) {
throw new CodecException(e, "Unable to deserialize json [path:%s]", path);
}
}
Expand All @@ -94,7 +95,7 @@ public List<TimestampedLwM2mNode> decodeTimestampedData(byte[] content, LwM2mPat
String jsonStrValue = new String(content);
JsonRootObject json = decoder.fromJsonLwM2m(jsonStrValue);
return parseJSON(json, path, model, nodeClass);
} catch (LwM2mJsonException e) {
} catch (LwM2mJsonException | InvalidLwM2mPathException e) {
throw new CodecException(e, "Unable to deserialize json [path:%s]", path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*******************************************************************************/
package org.eclipse.leshan.core.node.codec.senml;

import org.eclipse.leshan.core.node.LwM2mNodeException;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.InvalidLwM2mPathException;
import org.eclipse.leshan.senml.ResolvedSenMLRecord;
import org.eclipse.leshan.senml.SenMLRecord;

Expand All @@ -31,10 +31,10 @@ public class LwM2mResolvedSenMLRecord extends ResolvedSenMLRecord {

/**
* @throws IllegalArgumentException if path is invalid
* @throws LwM2mNodeException if path is invalid
* @throws InvalidLwM2mPathException if path is invalid
*/
public LwM2mResolvedSenMLRecord(SenMLRecord unresolvedRecord, String resolvedName, Long resolvedTimestamp)
throws IllegalArgumentException, LwM2mNodeException {
throws InvalidLwM2mPathException {
super(unresolvedRecord, resolvedName, resolvedTimestamp);
this.path = new LwM2mPath(resolvedName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*******************************************************************************/
package org.eclipse.leshan.core.node.codec.senml;

import org.eclipse.leshan.core.node.LwM2mNodeException;
import org.eclipse.leshan.core.node.InvalidLwM2mPathException;
import org.eclipse.leshan.senml.SenMLException;
import org.eclipse.leshan.senml.SenMLRecord;
import org.eclipse.leshan.senml.SenMLResolver;
Expand All @@ -30,7 +30,7 @@ protected LwM2mResolvedSenMLRecord createResolvedRecord(SenMLRecord unresolvedRe
Long resolvedTimestamp) throws SenMLException {
try {
return new LwM2mResolvedSenMLRecord(unresolvedRecord, resolvedName, resolvedTimestamp);
} catch (IllegalArgumentException | LwM2mNodeException e) {
} catch (InvalidLwM2mPathException e) {
throw new SenMLException(e, "Unable to resolve record, invalid path", resolvedName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.leshan.core.node.LwM2mObject;
import org.eclipse.leshan.core.node.LwM2mObjectInstance;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.InvalidLwM2mPathException;
import org.eclipse.leshan.core.node.LwM2mResource;
import org.eclipse.leshan.core.node.LwM2mResourceInstance;
import org.eclipse.leshan.core.node.LwM2mSingleResource;
Expand All @@ -54,7 +55,7 @@ public <T extends LwM2mNode> T decode(byte[] content, LwM2mPath path, LwM2mModel
try {
Tlv[] tlvs = TlvDecoder.decode(ByteBuffer.wrap(content != null ? content : new byte[0]));
return parseTlv(tlvs, path, model, nodeClass);
} catch (TlvException | LwM2mNodeException e) {
} catch (TlvException | LwM2mNodeException | InvalidLwM2mPathException e) {
throw new CodecException(String.format("Unable to decode tlv for path [%s]", path), e);
}
}
Expand Down
Loading

0 comments on commit 0cba977

Please sign in to comment.