Skip to content

Commit

Permalink
Merge pull request #3 from KB-iGOT/cbrelease-4.8.15
Browse files Browse the repository at this point in the history
Cbrelease 4.8.15
  • Loading branch information
karthik-tarento authored Aug 7, 2024
2 parents 26b8df3 + 1529d9f commit d92255a
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Map<String, String>> smsTemplateConfig = SmsTemplateUtil.getSmsTemplateConfigMap();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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<String, Object> messageMap = new HashMap<String, Object>();
messageMap.put("msgdata", smsText);
messageMap.put("Template_ID", dltTemplateId);
messageMap.put("coding", "1");
messageMap.put("flash_message", 1);
messageMap.put("scheduleTime", "");
Map<String, Object> request = new HashMap<String, Object>();
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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 =
Expand Down Expand Up @@ -98,6 +100,7 @@ private void sendMail(
List<String> emails,
String template,
RequestContext requestContext) {
long startTime = System.currentTimeMillis();
try {
SendEmail sendEmail = new SendEmail();
Velocity.init();
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit d92255a

Please sign in to comment.