Unverified Commit ab98243b authored by Reshmi V Nair's avatar Reshmi V Nair Committed by GitHub
Browse files

Merging 4.6 to master (#1040) (#1041)

Showing with 142 additions and 129 deletions
+142 -129
...@@ -14,6 +14,7 @@ import org.apache.http.client.methods.HttpGet; ...@@ -14,6 +14,7 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch; import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectionKeepAliveStrategy; import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
...@@ -99,7 +100,7 @@ public class HttpClientUtil { ...@@ -99,7 +100,7 @@ public class HttpClientUtil {
httpPost.addHeader(entry.getKey(), entry.getValue()); httpPost.addHeader(entry.getKey(), entry.getValue());
} }
} }
StringEntity entity = new StringEntity(params); StringEntity entity = new StringEntity(params,ContentType.APPLICATION_JSON);
httpPost.setEntity(entity); httpPost.setEntity(entity);
response = httpclient.execute(httpPost); response = httpclient.execute(httpPost);
...@@ -154,7 +155,7 @@ public class HttpClientUtil { ...@@ -154,7 +155,7 @@ public class HttpClientUtil {
httpPatch.addHeader(entry.getKey(), entry.getValue()); httpPatch.addHeader(entry.getKey(), entry.getValue());
} }
} }
StringEntity entity = new StringEntity(params); StringEntity entity = new StringEntity(params, ContentType.APPLICATION_JSON);
httpPatch.setEntity(entity); httpPatch.setEntity(entity);
response = httpclient.execute(httpPatch); response = httpclient.execute(httpPatch);
......
...@@ -54,7 +54,6 @@ public class TelemetryGenerator { ...@@ -54,7 +54,6 @@ public class TelemetryGenerator {
} }
Map<String, Object> edata = generateAuditEdata(params); Map<String, Object> edata = generateAuditEdata(params);
edata.put(JsonKey.REQUEST_ID, reqId);
Telemetry telemetry = Telemetry telemetry =
new Telemetry(TelemetryEvents.AUDIT.getName(), actor, eventContext, edata, targetObject); new Telemetry(TelemetryEvents.AUDIT.getName(), actor, eventContext, edata, targetObject);
telemetry.setMid(reqId); telemetry.setMid(reqId);
...@@ -208,7 +207,6 @@ public class TelemetryGenerator { ...@@ -208,7 +207,6 @@ public class TelemetryGenerator {
eventContext.getCdata().add(map); eventContext.getCdata().add(map);
} }
Map<String, Object> edata = generateSearchEdata(params); Map<String, Object> edata = generateSearchEdata(params);
edata.put(JsonKey.REQUEST_ID, reqId);
Telemetry telemetry = Telemetry telemetry =
new Telemetry(TelemetryEvents.SEARCH.getName(), actor, eventContext, edata); new Telemetry(TelemetryEvents.SEARCH.getName(), actor, eventContext, edata);
telemetry.setMid(reqId); telemetry.setMid(reqId);
...@@ -264,7 +262,6 @@ public class TelemetryGenerator { ...@@ -264,7 +262,6 @@ public class TelemetryGenerator {
} }
Map<String, Object> edata = generateLogEdata(params); Map<String, Object> edata = generateLogEdata(params);
edata.put(JsonKey.REQUEST_ID, reqId);
Telemetry telemetry = new Telemetry(TelemetryEvents.LOG.getName(), actor, eventContext, edata); Telemetry telemetry = new Telemetry(TelemetryEvents.LOG.getName(), actor, eventContext, edata);
telemetry.setMid(reqId); telemetry.setMid(reqId);
return getTelemetry(telemetry); return getTelemetry(telemetry);
......
...@@ -2,6 +2,9 @@ package org.sunbird.client; ...@@ -2,6 +2,9 @@ package org.sunbird.client;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.sunbird.exception.ProjectCommonException; import org.sunbird.exception.ProjectCommonException;
import org.sunbird.exception.ResponseCode; import org.sunbird.exception.ResponseCode;
import org.sunbird.http.HttpClientUtil; import org.sunbird.http.HttpClientUtil;
...@@ -13,138 +16,150 @@ import org.sunbird.response.Response; ...@@ -13,138 +16,150 @@ import org.sunbird.response.Response;
import org.sunbird.util.ProjectUtil; import org.sunbird.util.ProjectUtil;
import org.sunbird.util.PropertiesCache; import org.sunbird.util.PropertiesCache;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class NotificationServiceClient { public class NotificationServiceClient {
private final LoggerUtil logger = new LoggerUtil(NotificationServiceClient.class); private final LoggerUtil logger = new LoggerUtil(NotificationServiceClient.class);
private ObjectMapper mapper = new ObjectMapper(); private ObjectMapper mapper = new ObjectMapper();
private Map<String, String> getHeader(RequestContext context) {
private Map<String,String> getHeader(RequestContext context){ Map<String, String> headers = new HashMap<>();
Map<String, String> headers = new HashMap<>(); headers.put("Accept", "application/json");
headers.put("Accept", "application/json"); headers.put("Content-type", "application/json");
headers.put("Content-type", "application/json"); ProjectUtil.setTraceIdInHeader(headers, context);
ProjectUtil.setTraceIdInHeader(headers, context); return headers;
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 { return null;
String json = mapper.writeValueAsString(req); }
json = new String(json.getBytes(), StandardCharsets.UTF_8);
return json; /**
* 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 Sync V2 send notification API to send notification feed }
* @param reqObj
* @param context /**
* @return * Call V1 read notification to read notifications
*/ *
public Response sendSyncV2Notification(Request reqObj, RequestContext context) { * @param reqObj
logger.debug(context, "NotificationServiceClient:sendSyncV2Notification method called : "); * @param context
* @return
String serviceUrl = getServiceApiUrl(JsonKey.NOTIFICATION_SERVICE_V2_SEND_URL); */
logger.debug( public Response readV1Notification(Request reqObj, RequestContext context) {
context, logger.debug(context, "NotificationServiceClient:readV1Notification method called : ");
"NotificationServiceClient:sendSyncV2Notification :: calling notification service URL :" + serviceUrl); String serviceUrl = getServiceApiUrl(JsonKey.NOTIFICATION_SERVICE_V1_READ_URL);
try { logger.debug(
Response response = callCreateOrDeleteNotificationService(reqObj, context, serviceUrl); context,
return response; "NotificationServiceClient:readV1Notification :: calling notification service URL :"
} catch (Exception ex) { + serviceUrl);
logger.error(context, "FeedServiceImpl:sendSyncV2Notification Exception occurred while mapping.", ex); try {
ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); String responseStr =
} HttpClientUtil.get(
serviceUrl + "/" + reqObj.getRequest().get(JsonKey.USER_ID),
return null; 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 Update notification service api to update feeds }
* @param reqObj
* @param context /**
* @return * Call v1 delete notification service api
*/ *
public Response updateV1Notification(Request reqObj, RequestContext context) { * @param reqObj
logger.debug(context, "NotificationServiceClient:updateV1Notification method called : "); * @param context
* @return
String serviceUrl = getServiceApiUrl(JsonKey.NOTIFICATION_SERVICE_V1_UPDATE_URL); */
logger.debug( public Response deleteV1Notification(Request reqObj, RequestContext context) {
context, logger.debug(context, "NotificationServiceClient:deleteV1Notification method called : ");
"NotificationServiceClient:updateV1Notification :: calling notification service URL :" + serviceUrl);
try { String serviceUrl = getServiceApiUrl(JsonKey.NOTIFICATION_SERVICE_V1_DELETE_URL);
String json = getJsonString(reqObj); logger.debug(
String responseStr = HttpClientUtil.patch(serviceUrl, json, getHeader(context), context); context,
Response response = mapper.readValue(responseStr, Response.class); "NotificationServiceClient:deleteV1Notification :: calling notification service URL :"
return response; + serviceUrl);
} catch (Exception ex) { try {
logger.error(context, "FeedServiceImpl:updateV1Notification Exception occurred while mapping.", ex); Response response = callCreateOrDeleteNotificationService(reqObj, context, serviceUrl);
ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR); return response;
} } catch (Exception ex) {
logger.error(
return null; context, "FeedServiceImpl:deleteV1Notification Exception occurred while mapping.", ex);
ProjectCommonException.throwServerErrorException(ResponseCode.SERVER_ERROR);
}
/**
* 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;
}
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);
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment