From bd4ca63b1f2d8a2182417777d74ba7a4f4c52989 Mon Sep 17 00:00:00 2001 From: AMIT KUMAR <amit.kumar@tarento.com> Date: Tue, 25 Jan 2022 12:13:28 +0530 Subject: [PATCH] Issue #SB-28384 feat: Merge release-4.5 to release-4.6 (#1039) --- .../telemetry/util/TelemetryGenerator.java | 3 - .../client/NotificationServiceClient.java | 263 +++++++++--------- 2 files changed, 139 insertions(+), 127 deletions(-) diff --git a/core/platform-common/src/main/java/org/sunbird/telemetry/util/TelemetryGenerator.java b/core/platform-common/src/main/java/org/sunbird/telemetry/util/TelemetryGenerator.java index 5e69ea094..d132ed39b 100644 --- a/core/platform-common/src/main/java/org/sunbird/telemetry/util/TelemetryGenerator.java +++ b/core/platform-common/src/main/java/org/sunbird/telemetry/util/TelemetryGenerator.java @@ -54,7 +54,6 @@ public class TelemetryGenerator { } Map<String, Object> edata = generateAuditEdata(params); - edata.put(JsonKey.REQUEST_ID, reqId); Telemetry telemetry = new Telemetry(TelemetryEvents.AUDIT.getName(), actor, eventContext, edata, targetObject); telemetry.setMid(reqId); @@ -208,7 +207,6 @@ public class TelemetryGenerator { eventContext.getCdata().add(map); } Map<String, Object> edata = generateSearchEdata(params); - edata.put(JsonKey.REQUEST_ID, reqId); Telemetry telemetry = new Telemetry(TelemetryEvents.SEARCH.getName(), actor, eventContext, edata); telemetry.setMid(reqId); @@ -264,7 +262,6 @@ public class TelemetryGenerator { } Map<String, Object> edata = generateLogEdata(params); - edata.put(JsonKey.REQUEST_ID, reqId); Telemetry telemetry = new Telemetry(TelemetryEvents.LOG.getName(), actor, eventContext, edata); telemetry.setMid(reqId); return getTelemetry(telemetry); diff --git a/service/src/main/java/org/sunbird/client/NotificationServiceClient.java b/service/src/main/java/org/sunbird/client/NotificationServiceClient.java index b776ba22d..eadcbf4f7 100644 --- a/service/src/main/java/org/sunbird/client/NotificationServiceClient.java +++ b/service/src/main/java/org/sunbird/client/NotificationServiceClient.java @@ -2,7 +2,9 @@ package org.sunbird.client; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.commons.codec.binary.StringUtils; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; import org.sunbird.exception.ProjectCommonException; import org.sunbird.exception.ResponseCode; import org.sunbird.http.HttpClientUtil; @@ -14,137 +16,150 @@ import org.sunbird.response.Response; import org.sunbird.util.ProjectUtil; import org.sunbird.util.PropertiesCache; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; - public class NotificationServiceClient { - private final LoggerUtil logger = new LoggerUtil(NotificationServiceClient.class); - private ObjectMapper mapper = new ObjectMapper(); - - - private Map<String,String> getHeader(RequestContext context){ - Map<String, String> headers = new HashMap<>(); - headers.put("Accept", "application/json"); - headers.put("Content-type", "application/json"); - ProjectUtil.setTraceIdInHeader(headers, context); - return headers; + private final LoggerUtil logger = new LoggerUtil(NotificationServiceClient.class); + private ObjectMapper mapper = new ObjectMapper(); + + private Map<String, String> getHeader(RequestContext context) { + Map<String, String> headers = new HashMap<>(); + headers.put("Accept", "application/json"); + headers.put("Content-type", "application/json"); + ProjectUtil.setTraceIdInHeader(headers, context); + return headers; + } + + private String getJsonString(Request req) throws JsonProcessingException { + String json = mapper.writeValueAsString(req); + json = new String(json.getBytes(), StandardCharsets.UTF_8); + return json; + } + + /** + * Call Sync V2 send notification API to send notification feed + * + * @param reqObj + * @param context + * @return + */ + public Response sendSyncV2Notification(Request reqObj, RequestContext context) { + logger.debug(context, "NotificationServiceClient:sendSyncV2Notification method called : "); + + String serviceUrl = getServiceApiUrl(JsonKey.NOTIFICATION_SERVICE_V2_SEND_URL); + logger.debug( + context, + "NotificationServiceClient:sendSyncV2Notification :: calling notification service URL :" + + serviceUrl); + try { + Response response = callCreateOrDeleteNotificationService(reqObj, context, serviceUrl); + return response; + } catch (Exception ex) { + logger.error( + context, "FeedServiceImpl:sendSyncV2Notification Exception occurred while mapping.", ex); + ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); } - private String getJsonString(Request req) throws JsonProcessingException { - String json = mapper.writeValueAsString(req); - return json; + return null; + } + + /** + * Call v1 Update notification service api to update feeds + * + * @param reqObj + * @param context + * @return + */ + public Response updateV1Notification(Request reqObj, RequestContext context) { + logger.debug(context, "NotificationServiceClient:updateV1Notification method called : "); + + String serviceUrl = getServiceApiUrl(JsonKey.NOTIFICATION_SERVICE_V1_UPDATE_URL); + logger.debug( + context, + "NotificationServiceClient:updateV1Notification :: calling notification service URL :" + + serviceUrl); + try { + String json = getJsonString(reqObj); + String responseStr = HttpClientUtil.patch(serviceUrl, json, getHeader(context), context); + Response response = mapper.readValue(responseStr, Response.class); + return response; + } catch (Exception ex) { + logger.error( + context, "FeedServiceImpl:updateV1Notification Exception occurred while mapping.", ex); + ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); } - /** - * Call Sync V2 send notification API to send notification feed - * @param reqObj - * @param context - * @return - */ - public Response sendSyncV2Notification(Request reqObj, RequestContext context) { - logger.debug(context, "NotificationServiceClient:sendSyncV2Notification method called : "); - - String serviceUrl = getServiceApiUrl(JsonKey.NOTIFICATION_SERVICE_V2_SEND_URL); - logger.debug( - context, - "NotificationServiceClient:sendSyncV2Notification :: calling notification service URL :" + serviceUrl); - try { - Response response = callCreateOrDeleteNotificationService(reqObj, context, serviceUrl); - return response; - } catch (Exception ex) { - logger.error(context, "FeedServiceImpl:sendSyncV2Notification Exception occurred while mapping.", ex); - ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); - } - - return null; + return null; + } + + /** + * Call V1 read notification to read notifications + * + * @param reqObj + * @param context + * @return + */ + public Response readV1Notification(Request reqObj, RequestContext context) { + logger.debug(context, "NotificationServiceClient:readV1Notification method called : "); + String serviceUrl = getServiceApiUrl(JsonKey.NOTIFICATION_SERVICE_V1_READ_URL); + logger.debug( + context, + "NotificationServiceClient:readV1Notification :: calling notification service URL :" + + serviceUrl); + try { + String responseStr = + HttpClientUtil.get( + serviceUrl + "/" + reqObj.getRequest().get(JsonKey.USER_ID), + getHeader(context), + context); + Response response = mapper.readValue(responseStr, Response.class); + return response; + } catch (Exception ex) { + logger.error( + context, "FeedServiceImpl:readV1Notification Exception occurred while mapping.", ex); + ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); } - /** - * Call v1 Update notification service api to update feeds - * @param reqObj - * @param context - * @return - */ - public Response updateV1Notification(Request reqObj, RequestContext context) { - logger.debug(context, "NotificationServiceClient:updateV1Notification method called : "); - - String serviceUrl = getServiceApiUrl(JsonKey.NOTIFICATION_SERVICE_V1_UPDATE_URL); - logger.debug( - context, - "NotificationServiceClient:updateV1Notification :: calling notification service URL :" + serviceUrl); - try { - String json = getJsonString(reqObj); - String responseStr = HttpClientUtil.patch(serviceUrl, json, getHeader(context), context); - Response response = mapper.readValue(responseStr, Response.class); - return response; - } catch (Exception ex) { - logger.error(context, "FeedServiceImpl:updateV1Notification Exception occurred while mapping.", ex); - ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); - } - - return null; - - } - - /** - * Call V1 read notification to read notifications - * @param reqObj - * @param context - * @return - */ - public Response readV1Notification(Request reqObj, RequestContext context) { - logger.debug(context, "NotificationServiceClient:readV1Notification method called : "); - String serviceUrl = getServiceApiUrl(JsonKey.NOTIFICATION_SERVICE_V1_READ_URL); - logger.debug( - context, - "NotificationServiceClient:readV1Notification :: calling notification service URL :" + serviceUrl); - try { - String responseStr = HttpClientUtil.get(serviceUrl+ "/" +reqObj.getRequest().get(JsonKey.USER_ID), getHeader(context), context); - Response response = mapper.readValue(responseStr, Response.class); - return response; - } catch (Exception ex) { - logger.error(context, "FeedServiceImpl:readV1Notification Exception occurred while mapping.", ex); - ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); - } - - return null; - } - - /** - * Call v1 delete notification service api - * @param reqObj - * @param context - * @return - */ - public Response deleteV1Notification(Request reqObj, RequestContext context) { - logger.debug(context, "NotificationServiceClient:deleteV1Notification method called : "); - - String serviceUrl = getServiceApiUrl(JsonKey.NOTIFICATION_SERVICE_V1_DELETE_URL); - logger.debug( - context, - "NotificationServiceClient:deleteV1Notification :: calling notification service URL :" + serviceUrl); - try { - Response response = callCreateOrDeleteNotificationService(reqObj, context, serviceUrl); - return response; - } catch (Exception ex) { - logger.error(context, "FeedServiceImpl:deleteV1Notification Exception occurred while mapping.", ex); - ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); - } - - return null; - } - - private String getServiceApiUrl(String serviceUrlKey){ - String NOTIFICATION_SERVICE_BASE_URL = PropertiesCache.getInstance().getProperty(JsonKey.NOTIFICATION_SERVICE_BASE_URL); - String NOTIFICATION_SERVICE_URL = PropertiesCache.getInstance().getProperty(serviceUrlKey); - return NOTIFICATION_SERVICE_BASE_URL+NOTIFICATION_SERVICE_URL; - } - private Response callCreateOrDeleteNotificationService(Request reqObj, RequestContext context, String serviceUrl) throws JsonProcessingException { - String json = getJsonString(reqObj); - String responseStr = HttpClientUtil.post(serviceUrl, json, getHeader(context), context); - return mapper.readValue(responseStr, Response.class); + return null; + } + + /** + * Call v1 delete notification service api + * + * @param reqObj + * @param context + * @return + */ + public Response deleteV1Notification(Request reqObj, RequestContext context) { + logger.debug(context, "NotificationServiceClient:deleteV1Notification method called : "); + + String serviceUrl = getServiceApiUrl(JsonKey.NOTIFICATION_SERVICE_V1_DELETE_URL); + logger.debug( + context, + "NotificationServiceClient:deleteV1Notification :: calling notification service URL :" + + serviceUrl); + try { + Response response = callCreateOrDeleteNotificationService(reqObj, context, serviceUrl); + return response; + } catch (Exception ex) { + logger.error( + context, "FeedServiceImpl:deleteV1Notification Exception occurred while mapping.", ex); + ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); } + return null; + } + + private String getServiceApiUrl(String serviceUrlKey) { + String NOTIFICATION_SERVICE_BASE_URL = + PropertiesCache.getInstance().getProperty(JsonKey.NOTIFICATION_SERVICE_BASE_URL); + String NOTIFICATION_SERVICE_URL = PropertiesCache.getInstance().getProperty(serviceUrlKey); + return NOTIFICATION_SERVICE_BASE_URL + NOTIFICATION_SERVICE_URL; + } + + private Response callCreateOrDeleteNotificationService( + Request reqObj, RequestContext context, String serviceUrl) throws JsonProcessingException { + String json = getJsonString(reqObj); + String responseStr = HttpClientUtil.post(serviceUrl, json, getHeader(context), context); + return mapper.readValue(responseStr, Response.class); + } } -- GitLab