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

Enhance Block Condition Filtering by Adding Support for Exact Value Matching #12560

Merged
merged 3 commits into from
Oct 5, 2024
Merged
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 @@ -14043,11 +14043,12 @@ public List<BlockConditionsDTO> getBlockConditions(String tenantDomain) throws A
}

/**
* Retrieves block conditions based on the specified condition type and condition value.
* Retrieves block conditions based on the specified condition type and condition value. If the condition value is
* wrapped in double quotes (""), an exact match is performed; otherwise, a partial match is applied.
*
* @param conditionType type of the condition
* @param conditionValue condition value
* @param tenantDomain tenant domain
* @param conditionType type of the condition
* @param conditionValue condition value
* @param tenantDomain tenant domain
* @return list of block conditions
* @throws APIManagementException
*/
Expand All @@ -14058,15 +14059,27 @@ public List<BlockConditionsDTO> getBlockConditionsByConditionTypeAndValue(String
ResultSet resultSet = null;
List<BlockConditionsDTO> blockConditionsDTOList = new ArrayList<>();
try {
String query = SQLConstants.ThrottleSQLConstants.GET_BLOCK_CONDITIONS_BY_TYPE_AND_VALUE_SQL;
String query;
boolean isExactMatch = conditionValue != null && conditionValue.startsWith("\"") && conditionValue.endsWith(
"\"");
if (isExactMatch) {
query = ThrottleSQLConstants.GET_BLOCK_CONDITIONS_BY_TYPE_AND_EXACT_VALUE_SQL;
conditionValue = conditionValue.substring(1, conditionValue.length() - 1);
} else {
query = SQLConstants.ThrottleSQLConstants.GET_BLOCK_CONDITIONS_BY_TYPE_AND_VALUE_SQL;
}
connection = APIMgtDBUtil.getConnection();
selectPreparedStatement = connection.prepareStatement(query);
String conditionTypeUpper = conditionType != null ? conditionType.toUpperCase() : null;
selectPreparedStatement.setString(1, conditionTypeUpper);
selectPreparedStatement.setString(2, conditionTypeUpper);
selectPreparedStatement.setString(3, conditionValue);
selectPreparedStatement.setString(4, conditionValue);
selectPreparedStatement.setString(5, tenantDomain);
if (isExactMatch) {
selectPreparedStatement.setString(4, tenantDomain);
} else {
selectPreparedStatement.setString(4, conditionValue);
selectPreparedStatement.setString(5, tenantDomain);
}
resultSet = selectPreparedStatement.executeQuery();
while (resultSet.next()) {
BlockConditionsDTO blockConditionsDTO = populateBlockConditionsDataWithRS(resultSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3395,6 +3395,9 @@ public static class ThrottleSQLConstants{
public static final String GET_BLOCK_CONDITIONS_BY_TYPE_AND_VALUE_SQL =
"SELECT CONDITION_ID, TYPE, BLOCK_CONDITION, ENABLED, DOMAIN, UUID FROM AM_BLOCK_CONDITIONS WHERE "
+ "(TYPE = ? OR ? IS NULL) AND (BLOCK_CONDITION LIKE CONCAT('%', ?, '%') OR ? IS NULL) AND DOMAIN = ?";
public static final String GET_BLOCK_CONDITIONS_BY_TYPE_AND_EXACT_VALUE_SQL =
"SELECT CONDITION_ID, TYPE, BLOCK_CONDITION, ENABLED, DOMAIN, UUID FROM AM_BLOCK_CONDITIONS WHERE "
+ "(TYPE = ? OR ? IS NULL) AND (BLOCK_CONDITION = ?) AND DOMAIN = ?";

public static final String TIER_HAS_SUBSCRIPTION = " select count(sub.TIER_ID) as c from AM_SUBSCRIPTION sub, AM_API api "
+ " where sub.TIER_ID = ? and api.API_PROVIDER like ? and sub.API_ID = api.API_ID ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1193,8 +1193,12 @@ public void testAddUpdateDeleteBlockCondition() throws Exception {
BlockConditionsDTO userUUID = apiMgtDAO.addBlockConditions(userBlockcondition);
assertNotNull(apiMgtDAO.getBlockConditionByUUID(apiUUID.getUUID()));
assertNotNull(userUUID);
assertNotNull(apiMgtDAO.getBlockConditionsByConditionTypeAndValue(APIConstants.BLOCKING_CONDITIONS_API,
"/testAddUpdateDeleteBlockCondition", "carbon.super"));
assertEquals(1, apiMgtDAO.getBlockConditionsByConditionTypeAndValue(APIConstants.BLOCKING_CONDITIONS_API,
"/testAddUpdateDeleteBlock", "carbon.super").size());
assertEquals(1, apiMgtDAO.getBlockConditionsByConditionTypeAndValue(APIConstants.BLOCKING_CONDITIONS_API,
"\"/testAddUpdateDeleteBlockCondition\"", "carbon.super").size());
assertEquals(0, apiMgtDAO.getBlockConditionsByConditionTypeAndValue(APIConstants.BLOCKING_CONDITIONS_API,
"\"/testAddUpdateDeleteBlock\"", "carbon.super").size());
assertNotNull(apiMgtDAO
.updateBlockConditionState(apiMgtDAO.getBlockConditionByUUID(userUUID.getUUID()).getConditionId(),
"FALSE"));
Expand Down
Loading