Skip to content

Commit

Permalink
Issues #SB-00 fix: chnages to support telemetry with request and with…
Browse files Browse the repository at this point in the history
…out request env
  • Loading branch information
manzarul committed Jun 12, 2018
1 parent 7be18ca commit 6e84295
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ private static String getEvents(String body) throws Exception {
if (map != null) {
return mapper.writeValueAsString(map);
}
} else {
// Here data is coming without request body.
return body;
}
}
throw emptyRequestError(Message.INVALID_REQ_BODY_MSG_ERROR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ private List<String> getEvents(String body) throws Exception {
events.add(mapper.writeValueAsString(obj));
}
}
} else {
// data is coming with out request wrapper
Map<String, Object> map = mapper.readValue(body, Map.class);
if (map != null) {
List<Object> objList = (List<Object>) map.get(JsonKey.EVENTS);
if (CollectionUtils.isNotEmpty(objList)) {
for (Object obj : objList) {
events.add(mapper.writeValueAsString(obj));
}
}
}
}
return events;
}
Expand Down
30 changes: 20 additions & 10 deletions service/app/util/TelemetryRequestValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,13 @@ private static void validateProperData(Request request) {
if (map == null) {
throw createProjectException("");
}
Map<String, Object> innerMap = (Map<String, Object>) map.get(JsonKey.REQUEST);
if (innerMap == null || innerMap.size() == 0) {
boolean response = isDataHasRequestOrEvent(map);
if (!response) {
ProjectLogger.log(
"TelemetryRequestValidator:validateProperData Request object missing : " + map,
LoggerEnum.INFO.name());
throw createProjectException("");
}
List<Map<String, Object>> eventMap = (List<Map<String, Object>>) innerMap.get(JsonKey.EVENTS);
if (eventMap == null || eventMap.size() == 0) {
ProjectLogger.log(
"TelemetryRequestValidator:validateProperData Events object missing : " + map,
LoggerEnum.INFO.name());
throw createProjectException(Message.TELEMETRY_EVENT_MISSING_MSG_ERROR);
}
validateMaxSize(eventMap);
}

private static void validateMaxSize(List<Map<String, Object>> eventList) {
Expand All @@ -99,4 +91,22 @@ private static ProjectCommonException createProjectException(String message) {
return new ProjectCommonException(
ResponseCode.invalidRequestData.getErrorCode(), message, ERROR_CODE);
}

private static boolean isDataHasRequestOrEvent(Map<String, Object> requestMap) {
Map<String, Object> innerMap = (Map<String, Object>) requestMap.get(JsonKey.REQUEST);
if (innerMap == null || innerMap.size() == 0) {
return isMapContainsEvents(requestMap);
} else {
// if data is wrap with request then it should have events list as well
return isMapContainsEvents(innerMap);
}
}

private static boolean isMapContainsEvents(Map<String, Object> requestMap) {
List<Map<String, Object>> eventMap = (List<Map<String, Object>>) requestMap.get(JsonKey.EVENTS);
if (eventMap == null || eventMap.size() == 0) {
return false;
}
return true;
}
}

0 comments on commit 6e84295

Please sign in to comment.