Search in sources :

Example 16 with BlockingConditionDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO in project carbon-apimgt by wso2.

the class BlacklistApiServiceImpl method blacklistPost.

/**
 * Add a blacklist condition.
 *
 * @param body        DTO object including the blacklist condition data
 * @param request     msf4j request object
 * @return Response object
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response blacklistPost(BlockingConditionDTO body, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received BlockCondition POST request with body: " + body);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        // Add the block condition. It will throw BlockConditionAlreadyExistsException if the condition already
        // exists in the system
        BlockConditions blockConditions = BlockingConditionMappingUtil.fromBlockingConditionDTOToBlockCondition(body);
        String uuid = apiMgtAdminService.addBlockCondition(blockConditions);
        // retrieve the new blocking condition and send back as the response
        BlockConditions newBlockingCondition = apiMgtAdminService.getBlockConditionByUUID(uuid);
        BlockingConditionDTO dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(newBlockingCondition);
        return Response.status(Response.Status.CREATED).entity(dto).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while adding blocking condition with UUID " + body.getConditionId();
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 17 with BlockingConditionDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO in project carbon-apimgt by wso2.

the class MappingUtilTestCase method fromBlockingConditionToDTOIPRangeTest.

@Test
public void fromBlockingConditionToDTOIPRangeTest() {
    BlockConditions blockConditionIPRANGE = SampleTestObjectCreator.createUniqueBlockConditions(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE);
    BlockingConditionDTO blockingConditionDTo = MappingUtil.fromBlockingConditionToDTO(blockConditionIPRANGE);
    Assert.assertEquals(blockConditionIPRANGE.getUuid(), blockingConditionDTo.getUuid());
    Assert.assertEquals(blockConditionIPRANGE.getConditionType(), blockingConditionDTo.getConditionType());
    Assert.assertEquals(blockConditionIPRANGE.getConditionValue(), blockingConditionDTo.getConditionValue());
    Assert.assertEquals(Boolean.valueOf(blockConditionIPRANGE.isEnabled()), Boolean.valueOf(blockingConditionDTo.getEnabled()));
    Assert.assertEquals(Long.valueOf(APIUtils.ipToLong(blockConditionIPRANGE.getEndingIP())), Long.valueOf(blockingConditionDTo.getEndingIP()));
    Assert.assertEquals(Long.valueOf(APIUtils.ipToLong(blockConditionIPRANGE.getStartingIP())), Long.valueOf(blockingConditionDTo.getStartingIP()));
    // Test for null handling
    blockConditionIPRANGE.setUuid(null);
    Assert.assertNull(MappingUtil.fromBlockingConditionToDTO(blockConditionIPRANGE));
}
Also used : BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionDTO) Test(org.testng.annotations.Test)

Example 18 with BlockingConditionDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingDenyPolicyConditionIdPatch.

/**
 * Updates an existing condition status of a blocking condition
 *
 * @param conditionId       Id of the block condition
 * @param body              content to update
 * @param contentType       Content-Type header
 * @return 200 response if successful
 */
@Override
public Response throttlingDenyPolicyConditionIdPatch(String conditionId, String contentType, BlockingConditionStatusDTO body, 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 existingCondition = apiProvider.getBlockConditionByUUID(conditionId);
        if (!RestApiAdminUtils.isBlockConditionAccessibleToUser(username, existingCondition)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_BLOCK_CONDITION, conditionId, log);
        }
        // update the status
        apiProvider.updateBlockConditionByUUID(conditionId, String.valueOf(body.isConditionStatus()));
        // retrieve the new blocking condition and send back as the response
        BlockConditionsDTO newBlockingCondition = apiProvider.getBlockConditionByUUID(conditionId);
        BlockingConditionDTO dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(newBlockingCondition);
        return Response.ok().entity(dto).build();
    } catch (APIManagementException | ParseException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_BLOCK_CONDITION, conditionId, e, log);
        } else {
            String errorMessage = "Error while updating Block Condition Status. Id : " + conditionId;
            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 19 with BlockingConditionDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO 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 20 with BlockingConditionDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO 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

BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)13 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO)7 Test (org.testng.annotations.Test)5 BlockConditionsDTO (org.wso2.carbon.apimgt.api.model.BlockConditionsDTO)4 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionDTO)4 ArrayList (java.util.ArrayList)3 ParseException (org.json.simple.parser.ParseException)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)3 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)2 BlacklistApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.BlacklistApiServiceImpl)2 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1