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 46d3403a23..a19b3b6ea0 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 @@ -10,6 +10,7 @@ public interface ISmsProvider { String MSG_91_PROVIDER = JsonKey.MSG_91; String NIC_PROVIDER = JsonKey.NIC; + String GCP_PROVIDER = JsonKey.GCP; 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/GcpGatewaySmsProvider.java b/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/GcpGatewaySmsProvider.java new file mode 100644 index 0000000000..3e3ff417da --- /dev/null +++ b/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/GcpGatewaySmsProvider.java @@ -0,0 +1,132 @@ +package org.sunbird.notification.sms.providerimpl; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang3.StringUtils; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +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; + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class GcpGatewaySmsProvider implements ISmsProvider { + private static final LoggerUtil logger = new LoggerUtil(GcpGatewaySmsProvider.class); + + private static String baseUrl = null; + private static String senderId = null; + private static String authKey = null; + private static String campaignName = null; + + static { + boolean response = init(); + logger.info("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, GCP_PROVIDER); + if (StringUtils.isBlank(dltTemplateId)) { + logger.error(context, "dlt template id is empty for sms : " + smsText, new Exception("TemplateId is not configured properly.")); + return retVal; + } + Map messageMap = new HashMap(); + messageMap.put("msgdata", smsText); + messageMap.put("Template_ID", dltTemplateId); + messageMap.put("coding", "1"); + messageMap.put("flash_message", 1); + messageMap.put("scheduleTime", ""); + Map request = new HashMap(); + request.put("message", messageMap); + request.put("campaign_name", campaignName); + request.put("auth_key", authKey); + request.put("receivers", mobileNumber); + request.put("sender", senderId); + request.put("route", "TR"); + long startTime = System.currentTimeMillis(); + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + HttpPost post = new HttpPost(baseUrl); + post.setHeader("Content-Type", "application/json"); + post.setEntity(new StringEntity((new ObjectMapper()).writeValueAsString(request))); + try (CloseableHttpResponse response = httpClient.execute(post)) { + int responseCode = response.getStatusLine().getStatusCode(); + logger.info(String.format("Sent SMS. ResponseCode: %s, Response body: %s, TimeTaken %s seconds", + responseCode, response.getEntity(), (System.currentTimeMillis() - startTime) / 1000)); + if (responseCode == 200) { + retVal = true; + } + } catch (Exception e) { + logger.error("Failed to send SMS to mobile: " + mobileNumber + ", Exception: ", e); + } + } catch (Exception e) { + logger.error("Failed to create httpClient. Exception: ", e); + } + } catch (Exception ex) { + logger.error(context, "Exception occurred while sending sms.", ex); + } + return retVal; + } + + /** this method will do the SMS properties initialization. */ + public static boolean init() { + baseUrl = System.getenv("gcp_sms_gateway_provider_base_url"); + if (JsonUtil.isStringNullOREmpty(baseUrl)) { + baseUrl = PropertiesCache.getInstance().getProperty("gcp_sms_gateway_provider_base_url"); + } + authKey = System.getenv("gcp_sms_gateway_provider_authkey"); + if (JsonUtil.isStringNullOREmpty(authKey)) { + authKey = PropertiesCache.getInstance().getProperty("gcp_sms_gateway_provider_authkey"); + } + senderId = System.getenv("gcp_sms_gateway_provider_senderid"); + if (JsonUtil.isStringNullOREmpty(senderId)) { + senderId = PropertiesCache.getInstance().getProperty("gcp_sms_gateway_provider_senderid"); + } + campaignName = System.getenv("gcp_sms_gateway_provider_campaign_name"); + if (JsonUtil.isStringNullOREmpty(campaignName)) { + campaignName = PropertiesCache.getInstance().getProperty("gcp_sms_gateway_provider_campaign_name"); + } + return validateSettings(); + } + + private static boolean validateSettings() { + if (!JsonUtil.isStringNullOREmpty(baseUrl) + && !JsonUtil.isStringNullOREmpty(authKey) + && !JsonUtil.isStringNullOREmpty(senderId) + && !JsonUtil.isStringNullOREmpty(campaignName)) { + return true; + } + return false; + } +} diff --git a/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/GcpGatewaySmsProviderFactory.java b/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/GcpGatewaySmsProviderFactory.java new file mode 100644 index 0000000000..905d6c40f8 --- /dev/null +++ b/core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/GcpGatewaySmsProviderFactory.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 GcpGatewaySmsProviderFactory implements ISmsProviderFactory { + private GcpGatewaySmsProvider gcpSmsProvider = null; + + @Override + public ISmsProvider create() { + if (gcpSmsProvider == null) { + gcpSmsProvider = new GcpGatewaySmsProvider(); + } + return gcpSmsProvider; + } +} 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 77e171081d..ad021e9c99 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 @@ -3,6 +3,7 @@ import org.sunbird.keys.JsonKey; import org.sunbird.notification.sms.provider.ISmsProvider; import org.sunbird.notification.sms.provider.ISmsProviderFactory; +import org.sunbird.notification.sms.providerimpl.GcpGatewaySmsProviderFactory; import org.sunbird.notification.sms.providerimpl.Msg91SmsProviderFactory; import org.sunbird.notification.sms.providerimpl.NICGatewaySmsProviderFactory; import org.sunbird.util.ProjectUtil; @@ -30,6 +31,9 @@ public static ISmsProvider getInstance() { } else if (JsonKey.NIC.equalsIgnoreCase(SMS_PROVIDER)) { ISmsProviderFactory factory = new NICGatewaySmsProviderFactory(); return factory.create(); + } else if (JsonKey.GCP.equalsIgnoreCase(SMS_PROVIDER)) { + ISmsProviderFactory factory = new GcpGatewaySmsProviderFactory(); + 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 9354add70d..d588b725f9 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 @@ -654,6 +654,6 @@ public final class JsonKey { public static final String FIRST_NAME_LOWER_CASE = "firstname"; public static final String PHONE_CAPS = "Phone"; public static final String EMAIL_CAPS = "Email"; - + public static final String GCP = "GCP"; private JsonKey() {} } diff --git a/service/src/main/java/org/sunbird/actor/notification/EmailServiceActor.java b/service/src/main/java/org/sunbird/actor/notification/EmailServiceActor.java index 6f7b1b92ae..fd2961ee21 100644 --- a/service/src/main/java/org/sunbird/actor/notification/EmailServiceActor.java +++ b/service/src/main/java/org/sunbird/actor/notification/EmailServiceActor.java @@ -10,6 +10,7 @@ import org.apache.velocity.app.Velocity; import org.sunbird.actor.core.BaseActor; import org.sunbird.keys.JsonKey; +import org.sunbird.logging.LoggerUtil; import org.sunbird.mail.SendEmail; import org.sunbird.mail.SendgridConnection; import org.sunbird.operations.ActorOperations; @@ -21,6 +22,7 @@ public class EmailServiceActor extends BaseActor { + public final LoggerUtil logger = new LoggerUtil(EmailServiceActor.class); private final NotificationService notificationService = new NotificationService(); private final SendgridConnection connection = new SendgridConnection(); private final String resetInterval = @@ -98,6 +100,7 @@ private void sendMail( List emails, String template, RequestContext requestContext) { + long startTime = System.currentTimeMillis(); try { SendEmail sendEmail = new SendEmail(); Velocity.init(); @@ -126,6 +129,7 @@ private void sendMail( "EmailServiceActor:sendMail: Exception occurred with message = " + e.getMessage(), e); } + logger.info("Email Sent. Time taken (in ms): " + (System.currentTimeMillis() - startTime)); } private void resetConnection(RequestContext context) { diff --git a/service/src/main/java/org/sunbird/util/SMSTemplateProvider.java b/service/src/main/java/org/sunbird/util/SMSTemplateProvider.java index 77c3d0ee3e..14c9687f9b 100644 --- a/service/src/main/java/org/sunbird/util/SMSTemplateProvider.java +++ b/service/src/main/java/org/sunbird/util/SMSTemplateProvider.java @@ -26,6 +26,8 @@ private static String getTemplate(String templateId, RequestContext context) { String defaultTemplate = templateId; if (StringUtils.isNotBlank(templateId) && JsonKey.NIC.equalsIgnoreCase(SMS_PROVIDER)) { defaultTemplate = templateId + "_nic"; + } else if (StringUtils.isNotBlank(templateId) && JsonKey.GCP.equalsIgnoreCase(SMS_PROVIDER)) { + defaultTemplate = templateId + "_gcp"; } return emailTemplateDao.getTemplate(defaultTemplate, context); }