From 552047d5ad6d9e2481f90cfa8fd5af85a8cfb80e Mon Sep 17 00:00:00 2001 From: Ashirwada Date: Tue, 17 Oct 2023 13:51:20 +0530 Subject: [PATCH] Fixed issues in throwing an unhandled exception when payload is empty --- .../endpoint/api/EventPollingEndpoint.java | 60 ++++++++++--------- .../EventNotificationEndPointConstants.java | 1 + 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/api/EventPollingEndpoint.java b/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/api/EventPollingEndpoint.java index 213a7316..dd39d1bf 100644 --- a/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/api/EventPollingEndpoint.java +++ b/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/api/EventPollingEndpoint.java @@ -83,37 +83,43 @@ public Response pollEvents(@Context HttpServletRequest request, @Context HttpSer eventPollingData = parameterMap.get(EventNotificationEndPointConstants.REQUEST). toString().replaceAll("\\\\r|\\\\n|\\r|\\n|\\[|]| ", StringUtils.EMPTY); - byte[] decodedBytes = Base64.getDecoder().decode(eventPollingData); - String decodedString = new String(decodedBytes, StandardCharsets.UTF_8); - try { - eventPollingRequest = (JSONObject) new JSONParser(JSONParser.MODE_PERMISSIVE).parse(decodedString); - - //check if the client id is present in the header - String clientId = request.getHeader(EventNotificationConstants.X_WSO2_CLIENT_ID); - if (!StringUtils.isBlank(clientId)) { - eventPollingRequest.put(EventNotificationConstants.X_WSO2_CLIENT_ID, request. - getHeader(EventNotificationConstants.X_WSO2_CLIENT_ID)); - } else { + if (!eventPollingData.isEmpty()) { + byte[] decodedBytes = Base64.getDecoder().decode(eventPollingData); + String decodedString = new String(decodedBytes, StandardCharsets.UTF_8); + try { + eventPollingRequest = (JSONObject) new JSONParser(JSONParser.MODE_PERMISSIVE).parse(decodedString); + + //check if the client id is present in the header + String clientId = request.getHeader(EventNotificationConstants.X_WSO2_CLIENT_ID); + if (!StringUtils.isBlank(clientId)) { + eventPollingRequest.put(EventNotificationConstants.X_WSO2_CLIENT_ID, request. + getHeader(EventNotificationConstants.X_WSO2_CLIENT_ID)); + } else { + return Response.status(Response.Status.BAD_REQUEST).entity(EventNotificationUtils.getErrorDTO( + EventNotificationEndPointConstants.MISSING_REQUEST_HEADER, + EventNotificationConstants.MISSING_HEADER_PARAM_CLIENT_ID)).build(); + } + + EventPollingResponse eventPollingResponse = eventPollingServiceHandler. + pollEvents(eventPollingRequest); + + return EventNotificationUtils.mapEventPollingServiceResponse(eventPollingResponse); + + } catch (ParseException e) { + log.error("Exception when parsing the request payload", e); return Response.status(Response.Status.BAD_REQUEST).entity(EventNotificationUtils.getErrorDTO( - EventNotificationEndPointConstants.MISSING_REQUEST_HEADER, - EventNotificationConstants.MISSING_HEADER_PARAM_CLIENT_ID)).build(); + EventNotificationEndPointConstants.INVALID_REQUEST_PAYLOAD, + EventNotificationEndPointConstants.REQUEST_PAYLOAD_ERROR)).build(); + } catch (ClassCastException e) { + log.error(EventNotificationEndPointConstants.REQUEST_PAYLOAD_ERROR, e); + return Response.status(Response.Status.BAD_REQUEST).entity(EventNotificationUtils.getErrorDTO( + EventNotificationEndPointConstants.INVALID_REQUEST_PAYLOAD, + EventNotificationEndPointConstants.REQUEST_PAYLOAD_ERROR)).build(); } - - EventPollingResponse eventPollingResponse = eventPollingServiceHandler. - pollEvents(eventPollingRequest); - - return EventNotificationUtils.mapEventPollingServiceResponse(eventPollingResponse); - - } catch (ParseException e) { - log.error("Exception when parsing the request payload", e); - return Response.status(Response.Status.BAD_REQUEST).entity(EventNotificationUtils.getErrorDTO( - EventNotificationEndPointConstants.INVALID_REQUEST_PAYLOAD, - EventNotificationEndPointConstants.REQUEST_PAYLOAD_ERROR)).build(); - } catch (ClassCastException e) { - log.error(EventNotificationEndPointConstants.REQUEST_PAYLOAD_ERROR, e); + } else { return Response.status(Response.Status.BAD_REQUEST).entity(EventNotificationUtils.getErrorDTO( EventNotificationEndPointConstants.INVALID_REQUEST_PAYLOAD, - EventNotificationEndPointConstants.REQUEST_PAYLOAD_ERROR)).build(); + EventNotificationEndPointConstants.EMPTY_REQ_PAYLOAD)).build(); } } else { return Response.status(Response.Status.BAD_REQUEST).entity(EventNotificationUtils.getErrorDTO( diff --git a/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/constants/EventNotificationEndPointConstants.java b/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/constants/EventNotificationEndPointConstants.java index ea9b3523..90530c99 100644 --- a/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/constants/EventNotificationEndPointConstants.java +++ b/open-banking-accelerator/internal-apis/internal-webapps/com.wso2.openbanking.accelerator.event.notifications.endpoint/src/main/java/com/wso2/openbanking/accelerator/event/notifications/endpoint/constants/EventNotificationEndPointConstants.java @@ -29,6 +29,7 @@ public class EventNotificationEndPointConstants { public static final String POLLING_ERROR_RESPONSE = "OB Event Notification Polling error"; public static final String EVENT_CREATION_ERROR_RESPONSE = "OB Event Notification Creation error"; public static final String REQUEST_PAYLOAD_ERROR = "Error in the request payload"; + public static final String EMPTY_REQ_PAYLOAD = "Request payload cannot be empty"; public static final String INVALID_REQUEST = "invalid_request"; public static final String INVALID_REQUEST_PAYLOAD = "invalid_request_payload"; public static final String MISSING_REQUEST_PAYLOAD = "missing_request_payload";