From 6e842952058fc21d2f404be4c073b77f3ccf22b4 Mon Sep 17 00:00:00 2001 From: Manzarul Date: Tue, 12 Jun 2018 20:01:13 +0530 Subject: [PATCH] Issues #SB-00 fix: chnages to support telemetry with request and without request env --- .../dispatcher/EkstepTelemetryDispatcher.java | 3 ++ .../KafkaTelemetryDispatcherActor.java | 11 +++++++ .../app/util/TelemetryRequestValidator.java | 30 ++++++++++++------- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/service/app/org/sunbird/telemetry/dispatcher/EkstepTelemetryDispatcher.java b/service/app/org/sunbird/telemetry/dispatcher/EkstepTelemetryDispatcher.java index 1b2240d..4245c75 100644 --- a/service/app/org/sunbird/telemetry/dispatcher/EkstepTelemetryDispatcher.java +++ b/service/app/org/sunbird/telemetry/dispatcher/EkstepTelemetryDispatcher.java @@ -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); diff --git a/service/app/org/sunbird/telemetry/dispatcher/KafkaTelemetryDispatcherActor.java b/service/app/org/sunbird/telemetry/dispatcher/KafkaTelemetryDispatcherActor.java index 9038261..13d898b 100644 --- a/service/app/org/sunbird/telemetry/dispatcher/KafkaTelemetryDispatcherActor.java +++ b/service/app/org/sunbird/telemetry/dispatcher/KafkaTelemetryDispatcherActor.java @@ -126,6 +126,17 @@ private List getEvents(String body) throws Exception { events.add(mapper.writeValueAsString(obj)); } } + } else { + // data is coming with out request wrapper + Map map = mapper.readValue(body, Map.class); + if (map != null) { + List objList = (List) map.get(JsonKey.EVENTS); + if (CollectionUtils.isNotEmpty(objList)) { + for (Object obj : objList) { + events.add(mapper.writeValueAsString(obj)); + } + } + } } return events; } diff --git a/service/app/util/TelemetryRequestValidator.java b/service/app/util/TelemetryRequestValidator.java index f76c3ce..2d80e44 100644 --- a/service/app/util/TelemetryRequestValidator.java +++ b/service/app/util/TelemetryRequestValidator.java @@ -66,21 +66,13 @@ private static void validateProperData(Request request) { if (map == null) { throw createProjectException(""); } - Map innerMap = (Map) 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> eventMap = (List>) 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> eventList) { @@ -99,4 +91,22 @@ private static ProjectCommonException createProjectException(String message) { return new ProjectCommonException( ResponseCode.invalidRequestData.getErrorCode(), message, ERROR_CODE); } + + private static boolean isDataHasRequestOrEvent(Map requestMap) { + Map innerMap = (Map) 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 requestMap) { + List> eventMap = (List>) requestMap.get(JsonKey.EVENTS); + if (eventMap == null || eventMap.size() == 0) { + return false; + } + return true; + } }