Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/cbrelease-4.8.15' into cbrelease…
Browse files Browse the repository at this point in the history
…-4.8.16
  • Loading branch information
karthik-tarento committed Aug 9, 2024
2 parents d92255a + 1189e1c commit 410d965
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Map<String, String>> smsTemplateConfig = SmsTemplateUtil.getSmsTemplateConfigMap();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<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, 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<NameValuePair> 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;
}

}
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 NetCoreGatewaySmsProviderFactory implements ISmsProviderFactory {
private NetCoreGatewaySmsProvider ncSmsProvider = null;

@Override
public ISmsProvider create() {
if (ncSmsProvider == null) {
ncSmsProvider = new NetCoreGatewaySmsProvider();
}
return ncSmsProvider;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 410d965

Please sign in to comment.