Skip to content

Commit

Permalink
Fixed issues in throwing an unhandled exception when payload is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashi1993 committed Oct 17, 2023
1 parent c6d2b11 commit 552047d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 552047d

Please sign in to comment.