Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HUBDEV-2496] Blacklist improvements - SP/APP/API-wise blacklist #180

Open
wants to merge 1 commit into
base: blacklistmodification_M2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ public boolean mediate(MessageContext messageContext) {
String apiName = (String) messageContext.getProperty("API_NAME");
String apiVersion = (String) messageContext.getProperty("VERSION");
String apiPublisher = (String) messageContext.getProperty("API_PUBLISHER");
String applicationId = (String) messageContext.getProperty("APPLICATION_ID");
String userId = (String) messageContext.getProperty("USER_ID");

String spName = "";
if (userId.contains("@")) {
spName = userId.split("@")[0];
}

String apiID;
APIService apiService = new APIService();
Expand All @@ -45,7 +52,7 @@ public boolean mediate(MessageContext messageContext) {

try {
apiID = apiService.getAPIId(apiPublisher, apiName, apiVersion);
if (apiService.isBlackListedNumber(apiID, formattedPhoneNumber)) {
if (apiService.isBlackListedNumber(spName, applicationId, apiID, formattedPhoneNumber)) {
log.info(msisdn + " is BlackListed number for " + apiName + " API" + apiVersion + " version");
messageContext.setProperty(SynapseConstants.ERROR_CODE, "POL0001:");
messageContext.setProperty(SynapseConstants.ERROR_MESSAGE, "Internal Server Error. Blacklisted " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,54 @@ public String getAPIId(String apiPublisher, String apiName,
return apiId;
}

public boolean checkMsisdnBlacklistStatus(String spName, String appId, String apiId, String msisdn) throws Exception {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
boolean isBlacklisted = false;

try {
StringBuilder queryBuilder = new StringBuilder();
queryBuilder.append("select MSISDN FROM ");
queryBuilder.append(DatabaseTables.BLACKLIST_MSISDN);
queryBuilder.append(" where ");
queryBuilder.append(" (SP_NAME = ? OR SP_NAME = '_ALL_') AND ");
queryBuilder.append(" (APP_ID = ? OR APP_ID = '_ALL_') AND ");
queryBuilder.append(" (API_ID = ? OR API_ID = '_ALL_') AND ");
queryBuilder.append(" (MSISDN = ? OR MSISDN = concat('tel:+', ?))");

connection = DbUtils.getDbConnection(DataSourceNames.WSO2AM_STATS_DB);
preparedStatement = connection.prepareStatement(queryBuilder.toString());
preparedStatement.setString(1, spName);
preparedStatement.setString(2, appId);
preparedStatement.setString(3, apiId);
preparedStatement.setString(4, msisdn);
preparedStatement.setString(5, msisdn);

resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
isBlacklisted = true;

if (log.isDebugEnabled()) {
log.debug("Blacklisted MSISDNs for [SP: " + spName + ", AppId: " + appId + ", ApiId: " + apiId + "]");
do {
log.debug(resultSet.getString("MSISDN"));
} while (resultSet.next());
}
}
} catch (SQLException e) {
log.error("SQL Exception occurred when checking MSISDN blacklist status.", e);
throw e;
} catch (Exception e) {
log.error("Error occurred when checking MSISDN blacklist status.", e);
throw e;
} finally {
DbUtils.closeAllConnections(preparedStatement, connection, resultSet);
}

return isBlacklisted;
}

/**
* Read blacklist numbers.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,9 @@ public String getAPIId(String apiPublisher, String apiName,
return apiId;
}

public boolean isBlackListedNumber(String apiId, String msisdn)
public boolean isBlackListedNumber(String spName, String appId, String apiId, String msisdn)
throws Exception {
try {
List<String> msisdnArrayList = apiDAO.readBlacklistNumbers(apiId);
if (msisdnArrayList.contains(msisdn)
|| msisdnArrayList.contains("tel3A+" + msisdn)) {
return true;
}
} catch (Exception ex) {
log.error("Error while checking whether the msisdn :" + msisdn
+ " is blacklisted", ex);
throw ex;
}

return false;
return apiDAO.checkMsisdnBlacklistStatus(spName, appId, apiId, msisdn);
}

public String getSubscriptionID(String apiId, String applicationId)
Expand Down