From da1d1feb71e827c27cb4905e14e5c36b2b370d21 Mon Sep 17 00:00:00 2001 From: karthik-tarento Date: Tue, 18 Jun 2024 12:29:55 +0530 Subject: [PATCH 1/3] Added Amnex SMS Gateway for GCP --- .../sms/provider/ISmsProvider.java | 1 + .../providerimpl/GcpGatewaySmsProvider.java | 132 ++++++++++++++++++ .../GcpGatewaySmsProviderFactory.java | 16 +++ .../notification/utils/SMSFactory.java | 4 + .../main/java/org/sunbird/keys/JsonKey.java | 1 + .../org/sunbird/util/SMSTemplateProvider.java | 2 + 6 files changed, 156 insertions(+) create mode 100644 core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/GcpGatewaySmsProvider.java create mode 100644 core/notification-utils/src/main/java/org/sunbird/notification/sms/providerimpl/GcpGatewaySmsProviderFactory.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 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 0a7d8e1d6e..338cbd5bba 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 @@ -651,5 +651,6 @@ public final class JsonKey { public static final String CONTEXT_TOKEN = "contextToken"; public static final String OTP_VALIDATION_SECRET_KEY = "otpValidationSecretKey"; public static final String OTP_EXPIRATION_TIME_TOKEN = "otpExpirationTimeToken"; + public static final String GCP = "GCP"; 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 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); } From 172b62046e126121f50b58fc3ebd793dc7d52eb6 Mon Sep 17 00:00:00 2001 From: karthik-tarento Date: Wed, 31 Jul 2024 16:40:18 +0530 Subject: [PATCH 2/3] Added logs when sending email --- .../org/sunbird/actor/notification/EmailServiceActor.java | 4 ++++ 1 file changed, 4 insertions(+) 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) { From 887a00551a9eb2d47bc048917035b5e73b60632f Mon Sep 17 00:00:00 2001 From: Sreerag K S <58926794+sreeragksgh@users.noreply.github.com> Date: Tue, 9 Jul 2024 13:03:41 +0530 Subject: [PATCH 3/3] Fix for removing white space at the end of user's name while creation/updation (#89) --- .../org/sunbird/actor/user/validator/UserRequestValidator.java | 2 +- .../service/user/impl/ExtendedUserProfileServiceImpl.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/service/src/main/java/org/sunbird/actor/user/validator/UserRequestValidator.java b/service/src/main/java/org/sunbird/actor/user/validator/UserRequestValidator.java index b71ed37ad3..5946e9c4a5 100644 --- a/service/src/main/java/org/sunbird/actor/user/validator/UserRequestValidator.java +++ b/service/src/main/java/org/sunbird/actor/user/validator/UserRequestValidator.java @@ -92,7 +92,7 @@ private void formatFirstName(Request userRequest) { .append(" "); } } - userRequest.getRequest().put(JsonKey.FIRST_NAME,modifiedFirstName.toString()); + userRequest.getRequest().put(JsonKey.FIRST_NAME,modifiedFirstName.toString().trim()); } /** diff --git a/service/src/main/java/org/sunbird/service/user/impl/ExtendedUserProfileServiceImpl.java b/service/src/main/java/org/sunbird/service/user/impl/ExtendedUserProfileServiceImpl.java index 35e26a21e0..40604c63f7 100644 --- a/service/src/main/java/org/sunbird/service/user/impl/ExtendedUserProfileServiceImpl.java +++ b/service/src/main/java/org/sunbird/service/user/impl/ExtendedUserProfileServiceImpl.java @@ -57,6 +57,6 @@ private String formatFirstName(String firstName) { .append(" "); } } - return modifiedFirstName.toString(); + return modifiedFirstName.toString().trim(); } } \ No newline at end of file