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..6da7b9c57 --- /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 NetCore : " + 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("netcore_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() {} } 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); }