diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java index c56dca0eca31..c2691f23e52d 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java @@ -14043,11 +14043,12 @@ public List 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 */ @@ -14058,15 +14059,27 @@ public List getBlockConditionsByConditionTypeAndValue(String ResultSet resultSet = null; List 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); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java index b8544be164eb..7c4bdf5039cb 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java @@ -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 "; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/dao/test/APIMgtDAOTest.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/dao/test/APIMgtDAOTest.java index 0e8b2f19ef52..83f197f30e61 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/dao/test/APIMgtDAOTest.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/dao/test/APIMgtDAOTest.java @@ -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"));