From 3112fd92a621dac6a2c6c6aa44139375447b2646 Mon Sep 17 00:00:00 2001 From: "osh.silva" Date: Tue, 23 Jul 2024 11:59:13 +0530 Subject: [PATCH] Validate billing cost --- .../impl/util/RequestValidationUtil.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/util/RequestValidationUtil.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/util/RequestValidationUtil.java index 71d875a6b9..ab66e75d1c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/util/RequestValidationUtil.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/util/RequestValidationUtil.java @@ -23,6 +23,8 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpStatus; +import org.json.JSONArray; +import org.json.JSONObject; import io.entgra.device.mgt.core.device.mgt.common.Base64File; import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import io.entgra.device.mgt.core.device.mgt.common.Feature; @@ -821,6 +823,30 @@ public class RequestValidationUtil { new ErrorResponse.ErrorResponseBuilder() .setCode(HttpStatus.SC_BAD_REQUEST).setMessage(msg).build()); } + if (metadata.getMetaKey().equals("PER_DEVICE_COST")) { + String metaValue = metadata.getMetaValue(); + JSONArray jsonArray = new JSONArray(metaValue); + + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject jsonObject = jsonArray.getJSONObject(i); + if (jsonObject.has("cost")) { + Object costObj = jsonObject.get("cost"); + if (costObj == JSONObject.NULL || (costObj instanceof Number && ((Number) costObj).doubleValue() < 0)) { + String msg = "Billing cost cannot be a minus value or null."; + log.error(msg); + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder() + .setCode(HttpStatus.SC_BAD_REQUEST).setMessage(msg).build()); + } + } else { + String msg = "Billing cost is missing."; + log.error(msg); + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder() + .setCode(HttpStatus.SC_BAD_REQUEST).setMessage(msg).build()); + } + } + } if (metadata.getMetaValue() == null) { String msg = "Request parameter metaValue should be non empty."; log.error(msg);