From 125b0de93c627cf67b03b2d53d493de753ea045e Mon Sep 17 00:00:00 2001 From: karthik-tarento Date: Fri, 9 Aug 2024 15:14:20 +0530 Subject: [PATCH 1/3] Added netcore sms handling --- .../sms/provider/ISmsProvider.java | 1 + .../NetCoreGatewaySmsProvider.java | 138 ++++++++++++++++++ .../NetCoreGatewaySmsProviderFactory.java | 16 ++ .../notification/utils/SMSFactory.java | 4 + .../main/java/org/sunbird/keys/JsonKey.java | 1 + 5 files changed, 160 insertions(+) create mode 100644 core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProvider.java create mode 100644 core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProviderFactory.java diff --git a/core/notification-utils/src/main/java/org/sunbird/notification/sms/provider/ISmsProvider.java b/core/notification-utils/src/main/java/org/sunbird/notification/sms/provider/ISmsProvider.java index a19b3b6ea..959b0280b 100644 --- a/core/notification-utils/src/main/java/org/sunbird/notification/sms/provider/ISmsProvider.java +++ b/core/notification-utils/src/main/java/org/sunbird/notification/sms/provider/ISmsProvider.java @@ -11,6 +11,7 @@ public interface ISmsProvider { String MSG_91_PROVIDER = JsonKey.MSG_91; String NIC_PROVIDER = JsonKey.NIC; String GCP_PROVIDER = JsonKey.GCP; + String NETCORE_PROVIDER = JsonKey.NETCORE; default String getTemplateId(String sms, String provider) { Map> smsTemplateConfig = SmsTemplateUtil.getSmsTemplateConfigMap(); diff --git a/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProvider.java b/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProvider.java new file mode 100644 index 000000000..7231b64ce --- /dev/null +++ b/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProvider.java @@ -0,0 +1,138 @@ +package org.sunbird.notification.sms.providerimpl; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.StringUtils; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; +import org.sunbird.logging.LoggerUtil; +import org.sunbird.notification.sms.provider.ISmsProvider; +import org.sunbird.notification.utils.JsonUtil; +import org.sunbird.notification.utils.PropertiesCache; +import org.sunbird.request.RequestContext; + +public class NetCoreGatewaySmsProvider implements ISmsProvider { + + private static final LoggerUtil logger = new LoggerUtil(NetCoreGatewaySmsProvider.class); + + private static String baseUrl = null; + private static String feedId = null; + private static String userName = null; + private static String password = null; + + static { + boolean response = init(); + logger.info("NetCoreGatewaySmsProvider:: SMS configuration values are set using GCP : " + response); + } + + @Override + public boolean send(String phoneNumber, String smsText, RequestContext context) { + return sendSms(phoneNumber, smsText, context); + } + + @Override + public boolean send(String phoneNumber, String countryCode, String smsText, RequestContext context) { + return sendSms(phoneNumber, smsText, context); + } + + @Override + public boolean send(List phoneNumbers, String smsText, RequestContext context) { + phoneNumbers + .stream() + .forEach( + phone -> { + sendSms(phone, smsText, context); + }); + return true; + } + + public boolean sendSms(String mobileNumber, String smsText, RequestContext context) { + boolean retVal = false; + try { + // add dlt template + String dltTemplateId = getTemplateId(smsText, NETCORE_PROVIDER); + if (StringUtils.isBlank(dltTemplateId)) { + logger.error(context, "NetCoreGatewaySmsProvider:: dlt template id is empty for sms : " + smsText, + new Exception("TemplateId is not configured properly.")); + return retVal; + } + + long startTime = System.currentTimeMillis(); + + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + HttpPost post = new HttpPost(baseUrl); + post.setHeader("Content-Type", "application/x-www-form-urlencoded"); + + List params = new ArrayList<>(); + params.add(new BasicNameValuePair("feedid", feedId)); + params.add(new BasicNameValuePair("username", userName)); + params.add(new BasicNameValuePair("password", password)); + params.add(new BasicNameValuePair("to", mobileNumber)); + params.add(new BasicNameValuePair("text", smsText)); + params.add(new BasicNameValuePair("templateid", dltTemplateId)); + params.add(new BasicNameValuePair("short", "0")); + params.add(new BasicNameValuePair("async", "0")); + + post.setEntity(new UrlEncodedFormEntity(params)); + + try (CloseableHttpResponse response = httpClient.execute(post)) { + int responseCode = response.getStatusLine().getStatusCode(); + String responseStr = EntityUtils.toString(response.getEntity()); + logger.info(String.format("SMS Sent. ResponseCode: %s, Response Body: %s, TimeTaken: %s", + responseCode, responseStr, (System.currentTimeMillis() - startTime))); + if (responseCode == 200) { + retVal = true; + } + } catch (Exception e) { + logger.error(String.format("Failed to send SMS to mobile: %s, TimeTaken: %s, Exception: %s", + mobileNumber, (System.currentTimeMillis() - startTime), e.getMessage()), e); + } + } catch (Exception e) { + logger.error(String.format("Failed to create httpClient. TimeTaken: %s, Exception: %s", + (System.currentTimeMillis() - startTime), e.getMessage()), e); + } + } catch (Exception ex) { + logger.error(context, "NetCoreGatewaySmsProvider:: Exception occurred while sending sms.", ex); + } + return retVal; + } + + /** this method will do the SMS properties initialization. */ + public static boolean init() { + baseUrl = System.getenv("netcore_sms_gateway_provider_base_url"); + if (JsonUtil.isStringNullOREmpty(baseUrl)) { + baseUrl = PropertiesCache.getInstance().getProperty("gcp_sms_gateway_provider_base_url"); + } + feedId = System.getenv("netcore_sms_gateway_provider_feedid"); + if (JsonUtil.isStringNullOREmpty(feedId)) { + feedId = PropertiesCache.getInstance().getProperty("netcore_sms_gateway_provider_feedid"); + } + userName = System.getenv("netcore_sms_gateway_provider_username"); + if (JsonUtil.isStringNullOREmpty(userName)) { + userName = PropertiesCache.getInstance().getProperty("netcore_sms_gateway_provider_username"); + } + password = System.getenv("netcore_sms_gateway_provider_password"); + if (JsonUtil.isStringNullOREmpty(password)) { + password = PropertiesCache.getInstance().getProperty("netcore_sms_gateway_provider_password"); + } + return validateSettings(); + } + + private static boolean validateSettings() { + if (!JsonUtil.isStringNullOREmpty(baseUrl) + && !JsonUtil.isStringNullOREmpty(feedId) + && !JsonUtil.isStringNullOREmpty(userName) + && !JsonUtil.isStringNullOREmpty(password)) { + return true; + } + return false; + } + +} diff --git a/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProviderFactory.java b/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProviderFactory.java new file mode 100644 index 000000000..907269bd1 --- /dev/null +++ b/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProviderFactory.java @@ -0,0 +1,16 @@ +package org.sunbird.notification.sms.providerimpl; + +import org.sunbird.notification.sms.provider.ISmsProvider; +import org.sunbird.notification.sms.provider.ISmsProviderFactory; + +public class NetCoreGatewaySmsProviderFactory implements ISmsProviderFactory { + private NetCoreGatewaySmsProvider ncSmsProvider = null; + + @Override + public ISmsProvider create() { + if (ncSmsProvider == null) { + ncSmsProvider = new NetCoreGatewaySmsProvider(); + } + return ncSmsProvider; + } +} diff --git a/core/notification-utils/src/main/java/org/sunbird/notification/utils/SMSFactory.java b/core/notification-utils/src/main/java/org/sunbird/notification/utils/SMSFactory.java index ad021e9c9..f87c2639f 100644 --- a/core/notification-utils/src/main/java/org/sunbird/notification/utils/SMSFactory.java +++ b/core/notification-utils/src/main/java/org/sunbird/notification/utils/SMSFactory.java @@ -6,6 +6,7 @@ import org.sunbird.notification.sms.providerimpl.GcpGatewaySmsProviderFactory; import org.sunbird.notification.sms.providerimpl.Msg91SmsProviderFactory; import org.sunbird.notification.sms.providerimpl.NICGatewaySmsProviderFactory; +import org.sunbird.notification.sms.providerimpl.NetCoreGatewaySmsProviderFactory; import org.sunbird.util.ProjectUtil; /** @@ -34,6 +35,9 @@ public static ISmsProvider getInstance() { } else if (JsonKey.GCP.equalsIgnoreCase(SMS_PROVIDER)) { ISmsProviderFactory factory = new GcpGatewaySmsProviderFactory(); return factory.create(); + } else if (JsonKey.NETCORE.equalsIgnoreCase(SMS_PROVIDER)) { + ISmsProviderFactory factory = new NetCoreGatewaySmsProviderFactory(); + return factory.create(); } else { ISmsProviderFactory factory = new Msg91SmsProviderFactory(); return factory.create(); diff --git a/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java b/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java index d588b725f..65405db41 100644 --- a/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java +++ b/core/platform-common/src/main/java/org/sunbird/keys/JsonKey.java @@ -655,5 +655,6 @@ public final class JsonKey { public static final String PHONE_CAPS = "Phone"; public static final String EMAIL_CAPS = "Email"; public static final String GCP = "GCP"; + public static final String NETCORE = "NetCore"; private JsonKey() {} } From ede09ed94b0cd742bfdd8afadd59bc2f57992e78 Mon Sep 17 00:00:00 2001 From: karthik-tarento Date: Fri, 9 Aug 2024 15:31:20 +0530 Subject: [PATCH 2/3] Added template handling for netcore --- .../sms/providerimpl/NetCoreGatewaySmsProvider.java | 2 +- service/src/main/java/org/sunbird/util/SMSTemplateProvider.java | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProvider.java b/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProvider.java index 7231b64ce..2a5d47d93 100644 --- a/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProvider.java +++ b/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProvider.java @@ -108,7 +108,7 @@ public boolean sendSms(String mobileNumber, String smsText, RequestContext conte public static boolean init() { baseUrl = System.getenv("netcore_sms_gateway_provider_base_url"); if (JsonUtil.isStringNullOREmpty(baseUrl)) { - baseUrl = PropertiesCache.getInstance().getProperty("gcp_sms_gateway_provider_base_url"); + baseUrl = PropertiesCache.getInstance().getProperty("netcore_sms_gateway_provider_base_url"); } feedId = System.getenv("netcore_sms_gateway_provider_feedid"); if (JsonUtil.isStringNullOREmpty(feedId)) { diff --git a/service/src/main/java/org/sunbird/util/SMSTemplateProvider.java b/service/src/main/java/org/sunbird/util/SMSTemplateProvider.java index 14c9687f9..d28c44a93 100644 --- a/service/src/main/java/org/sunbird/util/SMSTemplateProvider.java +++ b/service/src/main/java/org/sunbird/util/SMSTemplateProvider.java @@ -28,6 +28,8 @@ private static String getTemplate(String templateId, RequestContext context) { defaultTemplate = templateId + "_nic"; } else if (StringUtils.isNotBlank(templateId) && JsonKey.GCP.equalsIgnoreCase(SMS_PROVIDER)) { defaultTemplate = templateId + "_gcp"; + } else if (StringUtils.isNotBlank(templateId) && JsonKey.NETCORE.equalsIgnoreCase(SMS_PROVIDER)) { + defaultTemplate = templateId + "_netcore"; } return emailTemplateDao.getTemplate(defaultTemplate, context); } From 66b448f85a957d625586d6239c09c57043053e20 Mon Sep 17 00:00:00 2001 From: karthik-tarento Date: Fri, 9 Aug 2024 16:06:18 +0530 Subject: [PATCH 3/3] Updated logs --- .../sms/providerimpl/NetCoreGatewaySmsProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProvider.java b/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProvider.java index 2a5d47d93..6da7b9c57 100644 --- a/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProvider.java +++ b/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/NetCoreGatewaySmsProvider.java @@ -29,7 +29,7 @@ public class NetCoreGatewaySmsProvider implements ISmsProvider { static { boolean response = init(); - logger.info("NetCoreGatewaySmsProvider:: SMS configuration values are set using GCP : " + response); + logger.info("NetCoreGatewaySmsProvider:: SMS configuration values are set using NetCore : " + response); } @Override