Skip to content

Commit

Permalink
validate object are registered in Send Request.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Sep 26, 2023
1 parent 9a095b9 commit 13e78c0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
Expand Up @@ -57,6 +57,10 @@ public static SendResponse notFound() {
return new SendResponse(ResponseCode.NOT_FOUND, null);
}

public static SendResponse notFound(String errorMessage) {
return new SendResponse(ResponseCode.NOT_FOUND, errorMessage);
}

public static SendResponse internalServerError(String errorMessage) {
return new SendResponse(ResponseCode.INTERNAL_SERVER_ERROR, errorMessage);
}
Expand Down
Expand Up @@ -273,7 +273,7 @@ public TimestampedLwM2mNodes waitForData(String clientEndpoint, int timeout, Tim
public Exception waitForSendDataError(String clientEndpoint, int timeout, TimeUnit unit) {
final ArgumentCaptor<Exception> c = ArgumentCaptor.forClass(Exception.class);
sendEventInOrder.verify(sendListener, timeout(unit.toMillis(timeout)).times(1))
.onError(assertArg(reg -> assertThat(reg.getEndpoint()).isEqualTo(clientEndpoint)), c.capture());
.onError(assertArg(reg -> assertThat(reg.getEndpoint()).isEqualTo(clientEndpoint)), any(), c.capture());
awakeEventInOrder.verifyNoMoreInteractions();
return c.getValue();
}
Expand Down
Expand Up @@ -45,7 +45,8 @@ public DefaultUplinkRequestReceiver(RegistrationHandler registrationHandler, Sen
public void onError(LwM2mPeer sender, ClientProfile senderProfile, Exception exception,
Class<? extends UplinkRequest<? extends LwM2mResponse>> requestType, URI serverEndpointUri) {
if (requestType.equals(SendRequest.class)) {
sendHandler.onError(senderProfile.getRegistration(), exception);
sendHandler.onError(senderProfile.getRegistration(),
exception.getMessage() != null ? exception.getMessage() : null, exception);
}
}

Expand Down
Expand Up @@ -16,8 +16,11 @@
package org.eclipse.leshan.server.send;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.leshan.core.node.LwM2mNode;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.TimestampedLwM2mNodes;
import org.eclipse.leshan.core.peer.LwM2mPeer;
import org.eclipse.leshan.core.request.SendRequest;
Expand Down Expand Up @@ -67,22 +70,33 @@ public SendableResponse<SendResponse> handleSend(LwM2mPeer sender, Registration
try {
updatedRegistration = updateRegistration(sender, registration);
} catch (Exception e) {
SendableResponse<SendResponse> response = new SendableResponse<>(
SendResponse.internalServerError("unable to update registration"), new Runnable() {
String errMsg = "unable to update registration";
SendableResponse<SendResponse> response = new SendableResponse<>(SendResponse.internalServerError(errMsg),
new Runnable() {
@Override
public void run() {
onError(registration, e);
onError(registration, errMsg, e);
}
});
return response;
}

// Send Response to send request on success
SendableResponse<SendResponse> response = new SendableResponse<>(SendResponse.success(), new Runnable() {
final SendResponse sendResponse = validateSendRequest(updatedRegistration, request);
SendableResponse<SendResponse> response = new SendableResponse<>(sendResponse, new Runnable() {

@Override
public void run() {
fireDataReceived(updatedRegistration, request.getTimestampedNodes(), request);
if (sendResponse.isSuccess()) {
fireDataReceived(updatedRegistration, request.getTimestampedNodes(), request);
} else {
onError(updatedRegistration, String.format("Invalid Send Request, server returns %s %s", //
sendResponse.getCode().getName(), //
sendResponse.getErrorMessage() != null ? "because" + sendResponse.getErrorMessage() : ""),
null);
}
}

});
return response;

Expand Down Expand Up @@ -111,9 +125,31 @@ protected void fireDataReceived(Registration registration, TimestampedLwM2mNodes
}
}

public void onError(Registration registration, Exception error) {
public void onError(Registration registration, String errorMessage, Exception error) {
for (SendListener listener : listeners) {
listener.onError(registration, error);
listener.onError(registration, errorMessage, error);
}
}

protected SendResponse validateSendRequest(Registration registration, SendRequest request) {
// check if all data of Send request are registered by LwM2M device.
// see : https://github.com/eclipse-leshan/leshan/issues/1472
TimestampedLwM2mNodes timestampedNodes = request.getTimestampedNodes();
Map<LwM2mPath, LwM2mNode> nodes = timestampedNodes.getNodes();
for (LwM2mPath path : nodes.keySet()) {
if (path.isRoot()) {
continue;
} else if (path.isObject()) {
if (registration.getSupportedVersion(path.getObjectId()) == null) {
return SendResponse.notFound(String.format("object %s not registered", path));
}
} else {
LwM2mPath instancePath = path.toObjectInstancePath();
if (!registration.getAvailableInstances().contains(instancePath)) {
return SendResponse.notFound(String.format("object instance %s not registered", instancePath));
}
}
}
return SendResponse.success();
}
}
Expand Up @@ -40,7 +40,8 @@ public interface SendListener {
* payload or any unexpected error).
*
* @param registration Registration of the client which send the data.
* @param error The cause of the error.
* @param errorMessage the cause of the error.
* @param error the related exception which caused the error, it could be {@code null}.
*/
void onError(Registration registration, Exception error);
void onError(Registration registration, String errorMessage, Exception error);
}
Expand Up @@ -266,9 +266,10 @@ public void dataReceived(Registration registration, TimestampedLwM2mNodes data,
}

@Override
public void onError(Registration registration, Exception error) {
public void onError(Registration registration, String errorMessage, Exception error) {
if (LOG.isWarnEnabled()) {
LOG.warn(String.format("Unable to handle Send Request from [%s].", registration), error);
LOG.warn(String.format("Unable to handle Send Request from [%s] : %s.", registration, errorMessage),
error);
}
}
};
Expand Down

0 comments on commit 13e78c0

Please sign in to comment.