diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java index 7d497f72f01a..bd7768f3b3b7 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java @@ -690,6 +690,17 @@ APIStateChangeResponse changeLifeCycleStatus(String orgId, ApiTypeWrapper apiTyp */ List getBlockConditions() throws APIManagementException; + /** + * Get a lightweight version of list of block Conditions. + * + * @param conditionType type of the condition + * @param conditionValue condition value + * @return list of block conditions + * @throws APIManagementException + */ + List getLightweightBlockConditions(String conditionType, String conditionValue) + throws APIManagementException; + /** * * @return Retrieve a block Condition diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java index 6fff88ff8713..4d3a76297a6b 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/ExceptionCodes.java @@ -371,6 +371,12 @@ public enum ExceptionCodes implements ErrorHandler { SCOPE_VALIDATION_FAILED(900986, "Scope validation failed", 412, "Scope validation failed"), SHARED_SCOPE_DISPLAY_NAME_NOT_SPECIFIED(900987, "Shared Scope display name not specified", 400, "Shared Scope display name not specified"), + BLOCK_CONDITION_RETRIEVE_PARAMS_EXCEPTION(900254, "Block conditions retrieval error", 400, + "Provided query parameters are not valid"), + BLOCK_CONDITION_RETRIEVE_FAILED(900255, "Failed to get Block conditions", 500, + "Failed to retrieve Block conditions from the database"), + INVALID_BLOCK_CONDITION_VALUES(900256, "Error while retrieving Block Conditions", 500, + "Invalid format for condition values"), SCOPE_ALREADY_ASSIGNED(900988, "Scope already assigned locally by another API", 400, "Scope already assigned locally by another API"), diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java index 394c44553927..a7d71b9bebbe 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java @@ -1968,6 +1968,8 @@ public enum RegistryResourceTypesForUI { public static final String BLOCK_CONDITION_ENDING_IP = "endingIp"; public static final String BLOCK_CONDITION_INVERT = "invert"; public static final String BLOCK_CONDITION_IP_TYPE = "type"; + public static final String BLOCK_CONDITION_TYPE = "conditionType"; + public static final String BLOCK_CONDITION_VALUE = "conditionValue"; public static final String REVOKED_TOKEN_KEY = "revokedToken"; public static final String REVOKED_TOKEN_EXPIRY_TIME = "expiryTime"; public static final String EVENT_TYPE = "eventType"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index cc66aae84b77..81c0f3dba670 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -3679,6 +3679,12 @@ public BlockConditionsDTO getBlockConditionByUUID(String uuid) throws APIManagem return blockCondition; } + @Override + public List getLightweightBlockConditions(String conditionType, String conditionValue) + throws APIManagementException { + return apiMgtDAO.getBlockConditionsByConditionTypeAndValue(conditionType, conditionValue, tenantDomain); + } + @Override public boolean updateBlockCondition(int conditionId, String state) throws APIManagementException { boolean updateState = apiMgtDAO.updateBlockConditionState(conditionId, state); 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 c2be5d3c7def..4e150d897563 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 @@ -13736,13 +13736,7 @@ public List getBlockConditions(String tenantDomain) throws A selectPreparedStatement.setString(1, tenantDomain); resultSet = selectPreparedStatement.executeQuery(); while (resultSet.next()) { - BlockConditionsDTO blockConditionsDTO = new BlockConditionsDTO(); - blockConditionsDTO.setEnabled(resultSet.getBoolean("ENABLED")); - blockConditionsDTO.setConditionType(resultSet.getString("TYPE")); - blockConditionsDTO.setConditionValue(resultSet.getString("BLOCK_CONDITION")); - blockConditionsDTO.setConditionId(resultSet.getInt("CONDITION_ID")); - blockConditionsDTO.setUUID(resultSet.getString("UUID")); - blockConditionsDTO.setTenantDomain(resultSet.getString("DOMAIN")); + BlockConditionsDTO blockConditionsDTO = populateBlockConditionsDataWithRS(resultSet); blockConditionsDTOList.add(blockConditionsDTO); } } catch (SQLException e) { @@ -13750,10 +13744,52 @@ public List getBlockConditions(String tenantDomain) throws A try { connection.rollback(); } catch (SQLException ex) { - handleException("Failed to rollback getting Block conditions ", ex); + throw new APIManagementException("Failed to rollback getting Block conditions.", + ExceptionCodes.BLOCK_CONDITION_RETRIEVE_FAILED); } } - handleException("Failed to get Block conditions", e); + throw new APIManagementException("Failed to retrieve all block conditions for the tenant " + tenantDomain, + ExceptionCodes.BLOCK_CONDITION_RETRIEVE_FAILED); + } finally { + APIMgtDBUtil.closeAllConnections(selectPreparedStatement, connection, resultSet); + } + return blockConditionsDTOList; + } + + /** + * Retrieves block conditions based on the specified condition type and condition value. + * + * @param conditionType type of the condition + * @param conditionValue condition value + * @param tenantDomain tenant domain + * @return list of block conditions + * @throws APIManagementException + */ + public List getBlockConditionsByConditionTypeAndValue(String conditionType, + String conditionValue, String tenantDomain) throws APIManagementException { + Connection connection = null; + PreparedStatement selectPreparedStatement = null; + ResultSet resultSet = null; + List blockConditionsDTOList = new ArrayList<>(); + try { + String 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); + resultSet = selectPreparedStatement.executeQuery(); + while (resultSet.next()) { + BlockConditionsDTO blockConditionsDTO = populateBlockConditionsDataWithRS(resultSet); + blockConditionsDTOList.add(blockConditionsDTO); + } + } catch (SQLException e) { + throw new APIManagementException( + "Failed to get Block conditions by condition type: " + conditionType + " and condition value: " + + conditionValue, ExceptionCodes.BLOCK_CONDITION_RETRIEVE_FAILED); } finally { APIMgtDBUtil.closeAllConnections(selectPreparedStatement, connection, resultSet); } @@ -21694,4 +21730,16 @@ public void addRevokedConsumerKey(String consumerKey, long revocationTime, Strin + e.getMessage(), e); } } + + private BlockConditionsDTO populateBlockConditionsDataWithRS(ResultSet resultSet) throws SQLException { + + BlockConditionsDTO blockConditionsDTO = new BlockConditionsDTO(); + blockConditionsDTO.setEnabled(resultSet.getBoolean("ENABLED")); + blockConditionsDTO.setConditionType(resultSet.getString("TYPE")); + blockConditionsDTO.setConditionValue(resultSet.getString("BLOCK_CONDITION")); + blockConditionsDTO.setConditionId(resultSet.getInt("CONDITION_ID")); + blockConditionsDTO.setUUID(resultSet.getString("UUID")); + blockConditionsDTO.setTenantDomain(resultSet.getString("DOMAIN")); + return blockConditionsDTO; + } } 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 198019391647..499b8ab5b074 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 @@ -3321,6 +3321,9 @@ public static class ThrottleSQLConstants{ public static final String GET_SUBSCRIPTION_BLOCK_CONDITION_BY_VALUE_AND_DOMAIN_SQL = "SELECT CONDITION_ID,TYPE,BLOCK_CONDITION,ENABLED,DOMAIN,UUID FROM AM_BLOCK_CONDITIONS WHERE " + "BLOCK_CONDITION = ? AND DOMAIN = ? "; + 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 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/APIProviderImplTest.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/APIProviderImplTest.java index 2eb068ab6de8..234c96fb8e69 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/APIProviderImplTest.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/APIProviderImplTest.java @@ -442,6 +442,15 @@ public void testGetBlockConditionByUUID() throws APIManagementException { } } + @Test + public void testGetBlockConditionsByConditionTypeAndValue() throws APIManagementException { + APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO); + List list = new ArrayList<>(); + Mockito.when(apimgtDAO.getBlockConditionsByConditionTypeAndValue(Mockito.anyString(), Mockito.anyString(), + Mockito.anyString())).thenReturn(list); + assertNotNull(apiProvider.getLightweightBlockConditions("conditionType", "conditionValue")); + } + @Test public void testUpdateBlockCondition() throws APIManagementException { APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO); 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 bcb45a5291d3..c26a3f863fdf 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,6 +1193,8 @@ 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")); assertNotNull(apiMgtDAO .updateBlockConditionState(apiMgtDAO.getBlockConditionByUUID(userUUID.getUUID()).getConditionId(), "FALSE")); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/admin/v1/ThrottlingApi.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/admin/v1/ThrottlingApi.java index 04e8143ae392..729880d1b0a4 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/admin/v1/ThrottlingApi.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/admin/v1/ThrottlingApi.java @@ -104,8 +104,8 @@ public Response importThrottlingPolicy( @Multipart(value = "file") InputStream f @ApiResponses(value = { @ApiResponse(code = 200, message = "OK. Deny Policies returned ", response = BlockingConditionListDTO.class), @ApiResponse(code = 406, message = "Not Acceptable. The requested media type is not supported.", response = ErrorDTO.class) }) - public Response throttlingDenyPoliciesGet( @ApiParam(value = "Media types acceptable for the response. Default is application/json. " , defaultValue="application/json")@HeaderParam("Accept") String accept) throws APIManagementException{ - return delegate.throttlingDenyPoliciesGet(accept, securityContext); + public Response throttlingDenyPoliciesGet( @ApiParam(value = "Media types acceptable for the response. Default is application/json. " , defaultValue="application/json")@HeaderParam("Accept") String accept, @ApiParam(value = "**Search condition**. You can search in attributes by using **\"conditionType:\"** modifier and **\"conditionValue:\"** modifier. Eg. The entry \"conditionType:API\" will result in a match with blocking conditions only if the conditionType is \"API\". Similarly, \"conditionValue:test/1.0.0\" will result in a match with blocking conditions only if the conditionValue is \"test/1.0.0\". When you use \"conditionType:API & conditionValue:test/1.0.0\" as a combination, it will result in a match with blocking conditions only if both the conditionType is \"API\" and the conditionValue is \"test/1.0.0\". If query attribute is provided, this returns the blocking conditions that match the specified attributes. Please note that you need to use encoded URL (URL encoding) if you are using a client which does not support URL encoding (such as curl) ") @QueryParam("query") String query) throws APIManagementException{ + return delegate.throttlingDenyPoliciesGet(accept, query, securityContext); } @POST diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/admin/v1/ThrottlingApiService.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/admin/v1/ThrottlingApiService.java index 050c58905e5b..dd7a897fa967 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/admin/v1/ThrottlingApiService.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/admin/v1/ThrottlingApiService.java @@ -36,7 +36,7 @@ public interface ThrottlingApiService { public Response exportThrottlingPolicy(String policyId, String name, String type, MessageContext messageContext) throws APIManagementException; public Response importThrottlingPolicy(InputStream fileInputStream, Attachment fileDetail, Boolean overwrite, MessageContext messageContext) throws APIManagementException; - public Response throttlingDenyPoliciesGet(String accept, MessageContext messageContext) throws APIManagementException; + public Response throttlingDenyPoliciesGet(String accept, String query, MessageContext messageContext) throws APIManagementException; public Response throttlingDenyPoliciesPost(String contentType, BlockingConditionDTO blockingConditionDTO, MessageContext messageContext) throws APIManagementException; public Response throttlingDenyPolicyConditionIdDelete(String conditionId, MessageContext messageContext) throws APIManagementException; public Response throttlingDenyPolicyConditionIdGet(String conditionId, MessageContext messageContext) throws APIManagementException; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ThrottlingApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ThrottlingApiServiceImpl.java index dcf32d315567..d09135e0b2ec 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ThrottlingApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ThrottlingApiServiceImpl.java @@ -1397,18 +1397,26 @@ private Response resolveUpdateThrottlingPolicy(String policyType, boolean overwr * @return All matched block conditions to the given request */ @Override - public Response throttlingDenyPoliciesGet(String accept, MessageContext messageContext) { - try { - APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); - List blockConditions = apiProvider.getBlockConditions(); - BlockingConditionListDTO listDTO = - BlockingConditionMappingUtil.fromBlockConditionListToListDTO(blockConditions); - return Response.ok().entity(listDTO).build(); - } catch (APIManagementException | ParseException e) { - String errorMessage = "Error while retrieving Block Conditions"; - RestApiUtil.handleInternalServerError(errorMessage, e, log); + public Response throttlingDenyPoliciesGet(String accept, String query, MessageContext messageContext) + throws APIManagementException { + APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider(); + List blockConditions = new ArrayList<>(); + // If conditionType and conditionValue are provided, retrieve the block conditions list for the given values. + if (StringUtils.isNotEmpty(query)) { + Map parametersMap = BlockingConditionMappingUtil.getQueryParams(query); + if (parametersMap != null && !parametersMap.isEmpty()) { + blockConditions = apiProvider.getLightweightBlockConditions( + parametersMap.get(APIConstants.BLOCK_CONDITION_TYPE), + parametersMap.get(APIConstants.BLOCK_CONDITION_VALUE)); + } else { + throw new APIManagementException(ExceptionCodes.BLOCK_CONDITION_RETRIEVE_PARAMS_EXCEPTION); + } + } else { + blockConditions = apiProvider.getBlockConditions(); } - return null; + BlockingConditionListDTO listDTO = BlockingConditionMappingUtil.fromBlockConditionListToListDTO( + blockConditions); + return Response.ok().entity(listDTO).build(); } /** @@ -1465,7 +1473,7 @@ public Response throttlingDenyPoliciesPost(String contentType, BlockingCondition + body.getConditionType() + ", " + "value: " + body.getConditionValue() + ". " + e.getMessage(); RestApiUtil.handleInternalServerError(errorMessage, e, log); } - } catch (URISyntaxException | ParseException e) { + } catch (URISyntaxException e) { String errorMessage = "Error while retrieving Blocking Condition resource location: Condition type: " + body.getConditionType() + ", " + "value: " + body.getConditionValue() + ". " + e.getMessage(); RestApiUtil.handleInternalServerError(errorMessage, e, log); @@ -1499,9 +1507,6 @@ public Response throttlingDenyPolicyConditionIdGet(String conditionId, MessageCo String errorMessage = "Error while retrieving Block Condition. Id : " + conditionId; RestApiUtil.handleInternalServerError(errorMessage, e, log); } - } catch (ParseException e) { - String errorMessage = "Error while retrieving Blocking Conditions"; - RestApiUtil.handleInternalServerError(errorMessage, e, log); } return null; } @@ -1568,7 +1573,7 @@ public Response throttlingDenyPolicyConditionIdPatch(String conditionId, String APIUtil.logAuditMessage(APIConstants.AuditLogConstants.DENY_POLICIES, new Gson().toJson(dto), APIConstants.AuditLogConstants.UPDATED, RestApiCommonUtil.getLoggedInUsername()); return Response.ok().entity(dto).build(); - } catch (APIManagementException | ParseException e) { + } catch (APIManagementException e) { if (RestApiUtil.isDueToResourceNotFound(e)) { RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_BLOCK_CONDITION, conditionId, e, log); } else { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/throttling/BlockingConditionMappingUtil.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/throttling/BlockingConditionMappingUtil.java index 5858429ce69a..495987080052 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/throttling/BlockingConditionMappingUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/throttling/BlockingConditionMappingUtil.java @@ -17,18 +17,24 @@ package org.wso2.carbon.apimgt.rest.api.admin.v1.utils.mappings.throttling; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; +import org.wso2.carbon.apimgt.api.APIManagementException; +import org.wso2.carbon.apimgt.api.ExceptionCodes; import org.wso2.carbon.apimgt.api.model.BlockConditionsDTO; import org.wso2.carbon.apimgt.impl.APIConstants; import org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO; import org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionListDTO; import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * This class is responsible for mapping Block Condition model and its sub components into REST API DTOs and vice-versa @@ -44,7 +50,7 @@ public class BlockingConditionMappingUtil { * @return REST API List DTO object derived from Block Condition list */ public static BlockingConditionListDTO fromBlockConditionListToListDTO( - List blockConditionList) throws ParseException { + List blockConditionList) throws APIManagementException { BlockingConditionListDTO listDTO = new BlockingConditionListDTO(); List blockingConditionDTOList = new ArrayList<>(); if (blockConditionList != null) { @@ -70,7 +76,7 @@ public static BlockingConditionListDTO fromBlockConditionListToListDTO( * @return Block condition DTO object derived from block condition model object */ public static BlockingConditionDTO fromBlockingConditionToDTO( - BlockConditionsDTO blockCondition) throws ParseException { + BlockConditionsDTO blockCondition) throws APIManagementException { BlockingConditionDTO dto = new BlockingConditionDTO(); dto.setConditionId(blockCondition.getUUID()); @@ -97,16 +103,41 @@ public static BlockingConditionDTO fromBlockingConditionToDTO( } else { // This is a true parsing exception. Hence, it will be thrown without handling. log.error("Error parsing IP blocking condition value", e); - throw new ParseException(ParseException.ERROR_UNEXPECTED_EXCEPTION, e); + throw new APIManagementException(ExceptionCodes.INVALID_BLOCK_CONDITION_VALUES); } } else { // This is a true parsing exception. Hence, it will be thrown without handling. log.error("Error parsing IP blocking condition value. The value is null.", e); - throw new ParseException(ParseException.ERROR_UNEXPECTED_EXCEPTION, e); + throw new APIManagementException(ExceptionCodes.INVALID_BLOCK_CONDITION_VALUES); } } } dto.setConditionStatus(blockCondition.isEnabled()); return dto; } + + /** + * Get query parameter values for conditionType and conditionValue from the query string. + * + * @param query Request query + * @return map of conditionType and conditionValue values + */ + public static Map getQueryParams(String query) { + if (query == null || StringUtils.isBlank(query)) { + return Collections.emptyMap(); + } + Map parameters = new HashMap<>(); + String[] pairs = query.split("&"); + for (String pair : pairs) { + String[] keyValue = pair.split(":"); + if (keyValue.length == 2) { + String key = keyValue[0]; + String value = keyValue[1]; + if (key.equals(APIConstants.BLOCK_CONDITION_TYPE) || key.equals(APIConstants.BLOCK_CONDITION_VALUE)) { + parameters.put(key, value); + } + } + } + return parameters; + } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/resources/admin-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/resources/admin-api.yaml index 9aa4db9ea6a8..8dff2ebf0c29 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/resources/admin-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/resources/admin-api.yaml @@ -1330,6 +1330,18 @@ paths: Retrieves all existing deny policies. parameters: - $ref: '#/components/parameters/Accept' + - name: query + in: query + description: | + **Search condition**. + You can search in attributes by using **"conditionType:"** modifier and **"conditionValue:"** modifier. + Eg. + The entry "conditionType:API" will result in a match with blocking conditions only if the conditionType is "API". Similarly, "conditionValue:test/1.0.0" will result in a match with blocking conditions only if the conditionValue is "test/1.0.0". + When you use "conditionType:API & conditionValue:test/1.0.0" as a combination, it will result in a match with blocking conditions only if both the conditionType is "API" and the conditionValue is "test/1.0.0". + If query attribute is provided, this returns the blocking conditions that match the specified attributes. + Please note that you need to use encoded URL (URL encoding) if you are using a client which does not support URL encoding (such as curl) + schema: + type: string responses: 200: description: |