Skip to content

Commit

Permalink
Merge pull request #12560 from ashanhr/improvement-3084
Browse files Browse the repository at this point in the history
Enhance Block Condition Filtering by Adding Support for Exact Value Matching
  • Loading branch information
RakhithaRR authored Oct 5, 2024
2 parents 579b3b7 + b0f3d87 commit 81b93b0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
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 @@ -3394,6 +3394,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

0 comments on commit 81b93b0

Please sign in to comment.