Skip to content

Commit eaf6c11

Browse files
Merge pull request #159 from aimansharief/scala-conversion-fix
Issue #SBCOSS-00 : Fix Scala Conversions and Addition of public key base path to config
2 parents d761ca0 + 639b5c2 commit eaf6c11

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

all-actors/src/main/java/org/sunbird/notification/actor/CreateNotificationActor.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.sunbird.notification.actor;
22

33

4+
import com.fasterxml.jackson.core.type.TypeReference;
45
import com.fasterxml.jackson.databind.ObjectMapper;
56
import org.apache.commons.lang3.StringUtils;
67
import org.sunbird.BaseActor;
@@ -19,7 +20,7 @@
1920
import org.sunbird.request.LoggerUtil;
2021
import org.sunbird.telemetry.TelemetryEnvKey;
2122
import org.sunbird.telemetry.util.TelemetryUtil;
22-
import scala.collection.JavaConverters;
23+
import org.sunbird.util.Util;
2324

2425
import java.text.MessageFormat;
2526
import java.util.ArrayList;
@@ -44,14 +45,11 @@ public void onReceive(Request request) throws Throwable {
4445
try {
4546
Response response = new Response();
4647
ObjectMapper mapper = new ObjectMapper();
47-
// Convert Scala collection to Java List to avoid ClassCastException
4848
Object notificationsObject = request.getRequest().get(JsonKey.NOTIFICATIONS);
49-
List<Map<String, Object>> notifications;
50-
if (notificationsObject instanceof scala.collection.Seq) {
51-
notifications = new ArrayList<>(JavaConverters.asJavaCollectionConverter((scala.collection.Seq<Map<String, Object>>) notificationsObject).asJavaCollection());
52-
} else {
53-
notifications = (List<Map<String, Object>>) notificationsObject;
54-
}
49+
List<Map<String, Object>> notifications = Util.convertToList(
50+
notificationsObject,
51+
new TypeReference<List<Map<String, Object>>>() {}
52+
);
5553

5654
String deliveryMode = request.getManagerName();
5755
if (StringUtils.isNotBlank(deliveryMode) && "sync".equalsIgnoreCase(deliveryMode)) {

all-actors/src/main/java/org/sunbird/notification/actor/NotificationActor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import org.sunbird.request.LoggerUtil;
2222
import org.sunbird.util.validator.OtpRequestValidator;
2323

24+
import com.fasterxml.jackson.core.type.TypeReference;
25+
import org.sunbird.util.Util;
26+
2427
import java.text.MessageFormat;
2528
import java.util.ArrayList;
2629
import java.util.List;
@@ -54,7 +57,10 @@ public void notify(Request request) throws BaseException {
5457
logger.info(request.getRequest(),"Call started for notify method");
5558
List<NotificationRequest> notificationRequestList =
5659
NotificationRequestMapper.toList(
57-
(List<Map<String, Object>>) request.getRequest().get(JsonKey.NOTIFICATIONS));
60+
Util.convertToList(
61+
request.getRequest().get(JsonKey.NOTIFICATIONS),
62+
new TypeReference<List<Map<String, Object>>>() {}
63+
));
5864
List<String> ids = new ArrayList<String>();
5965
for (NotificationRequest notificationRequest : notificationRequestList) {
6066
if (CollectionUtils.isNotEmpty(notificationRequest.getIds())) {

all-actors/src/main/java/org/sunbird/util/Util.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.sunbird.util;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import org.apache.commons.collections.MapUtils;
45
import org.sunbird.JsonKey;
56
import org.sunbird.common.exception.BaseException;
@@ -41,6 +42,25 @@ public static Map<String, Object> getTemplate(NotificationV2Request notification
4142
return template;
4243
}
4344

45+
private static com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
46+
private static com.fasterxml.jackson.databind.ObjectMapper javaMapper = new com.fasterxml.jackson.databind.ObjectMapper();
47+
48+
static {
49+
mapper.findAndRegisterModules();
50+
mapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
51+
javaMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
52+
}
53+
54+
public static <T> java.util.List<T> convertToList(Object object, com.fasterxml.jackson.core.type.TypeReference<java.util.List<T>> typeReference) {
55+
try {
56+
JsonNode node = mapper.valueToTree(object);
57+
return javaMapper.convertValue(node, typeReference);
58+
} catch (Exception e) {
59+
throw new org.sunbird.common.exception.BaseException(IResponseMessage.Key.INVALID_REQUESTED_DATA,
60+
"Invalid data format.", ResponseCode.CLIENT_ERROR.getCode());
61+
}
62+
}
63+
4464
public static Response writeDataToKafka(
4565
NotificationRequest notification,
4666
Response response,
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
notification_category_type_config=certificateUpload,member-added
22
version_support_config_enable=True
33
feed_limit=31
4-
sunbird_notification_keyspace=sunbird_notifications
4+
sunbird_notification_keyspace=sunbird_notifications
5+
accesstoken.publickey.basepath=/keys/

0 commit comments

Comments
 (0)