Search in sources :

Example 26 with BlockConditionsDTO

use of org.wso2.carbon.apimgt.api.model.BlockConditionsDTO in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingDenyPolicyConditionIdGet.

/**
 * Get a specific Block condition by its id
 *
 * @param conditionId     Id of the block condition
 * @return Matched block condition for the given Id
 */
@Override
public Response throttlingDenyPolicyConditionIdGet(String conditionId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.getLoggedInUsername();
        // This will give BlockConditionNotFoundException if there's no block condition exists with UUID
        BlockConditionsDTO blockCondition = apiProvider.getBlockConditionByUUID(conditionId);
        if (!RestApiAdminUtils.isBlockConditionAccessibleToUser(username, blockCondition)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_BLOCK_CONDITION, conditionId, log);
        }
        BlockingConditionDTO dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(blockCondition);
        return Response.ok().entity(dto).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_BLOCK_CONDITION, conditionId, e, log);
        } else {
            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;
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ParseException(org.json.simple.parser.ParseException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 27 with BlockConditionsDTO

use of org.wso2.carbon.apimgt.api.model.BlockConditionsDTO in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingDenyPoliciesGet.

/**
 * Retrieves all Block Conditions
 *
 * @param accept          Accept header value
 * @return All matched block conditions to the given request
 */
@Override
public Response throttlingDenyPoliciesGet(String accept, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        List<BlockConditionsDTO> 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);
    }
    return null;
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ParseException(org.json.simple.parser.ParseException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 28 with BlockConditionsDTO

use of org.wso2.carbon.apimgt.api.model.BlockConditionsDTO in project carbon-apimgt by wso2.

the class BlockingConditionMappingUtil method fromBlockingConditionToDTO.

/**
 * Converts a single Block Condition model object into REST API DTO object
 *
 * @param blockCondition Block condition model object
 * @return Block condition DTO object derived from block condition model object
 */
public static BlockingConditionDTO fromBlockingConditionToDTO(BlockConditionsDTO blockCondition) throws ParseException {
    BlockingConditionDTO dto = new BlockingConditionDTO();
    dto.setConditionId(blockCondition.getUUID());
    dto.setConditionType(BlockingConditionDTO.ConditionTypeEnum.fromValue(blockCondition.getConditionType()));
    if (APIConstants.BLOCKING_CONDITIONS_API.equals(blockCondition.getConditionType()) || APIConstants.BLOCKING_CONDITIONS_APPLICATION.equals(blockCondition.getConditionType()) || APIConstants.BLOCKING_CONDITIONS_USER.equals(blockCondition.getConditionType())) {
        dto.setConditionValue(blockCondition.getConditionValue());
    } else if (APIConstants.BLOCKING_CONDITIONS_IP.equals(blockCondition.getConditionType()) || APIConstants.BLOCK_CONDITION_IP_RANGE.equalsIgnoreCase(blockCondition.getConditionType())) {
        Object parse = new JSONParser().parse(blockCondition.getConditionValue());
        dto.setConditionValue(parse);
    }
    dto.setConditionStatus(blockCondition.isEnabled());
    return dto;
}
Also used : BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO) JSONParser(org.json.simple.parser.JSONParser)

Aggregations

BlockConditionsDTO (org.wso2.carbon.apimgt.api.model.BlockConditionsDTO)24 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)5 ParseException (org.json.simple.parser.ParseException)4 ArrayList (java.util.ArrayList)3 JSONObject (org.json.simple.JSONObject)2 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Date (java.util.Date)1