Skip to content

Commit 3f321cd

Browse files
committed
Add validation to the period enum
1 parent a627b05 commit 3f321cd

File tree

2 files changed

+26
-0
lines changed
  • components/org.wso2.openbanking.cds.metrics/src/main/java/org/wso2/openbanking/cds/metrics/util
  • internal-apis/internal-webapps/org.wso2.openbanking.cds.metrics.endpoint/src/main/java/org/wso2/openbanking/cds/metrics/endpoint/impl

2 files changed

+26
-0
lines changed

components/org.wso2.openbanking.cds.metrics/src/main/java/org/wso2/openbanking/cds/metrics/util/PeriodEnum.java

+17
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,21 @@ public String toString() {
4848
return String.valueOf(text);
4949
}
5050

51+
/**
52+
* Check if the given period is valid enum value
53+
* Valid values : CURRENT, HISTORIC, ALL
54+
* As defined in the specification values MUST be in all caps.
55+
*
56+
* @param period - Period to be checked
57+
* @return - true if the period is valid, false otherwise
58+
*/
59+
public static boolean isValidPeriodEnum(String period) {
60+
for (PeriodEnum periodEnum : PeriodEnum.values()) {
61+
if (periodEnum.name().equals(period)) {
62+
return true;
63+
}
64+
}
65+
return false;
66+
}
67+
5168
}

internal-apis/internal-webapps/org.wso2.openbanking.cds.metrics.endpoint/src/main/java/org/wso2/openbanking/cds/metrics/endpoint/impl/MetricsApiImpl.java

+9
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ public Response getMetrics(String xV, String period, String xMinV) {
6363
"Requested x-v version is not supported", new CDSErrorMeta()));
6464
return Response.status(Response.Status.NOT_ACCEPTABLE).entity(ErrorUtil.getErrorJson(errorList)).build();
6565
}
66+
67+
if (!PeriodEnum.isValidPeriodEnum(period)) {
68+
log.error("Error occurred due to invalid period value.");
69+
JSONArray errorList = new JSONArray();
70+
errorList.add(ErrorUtil.getErrorObject(ErrorConstants.AUErrorEnum.INVALID_FIELD,
71+
"Requested period value is invalid", new CDSErrorMeta()));
72+
return Response.status(Response.Status.BAD_REQUEST).entity(ErrorUtil.getErrorJson(errorList)).build();
73+
}
74+
6675
PeriodEnum periodEnum = PeriodEnum.fromString(period);
6776

6877
try {

0 commit comments

Comments
 (0)