Search in sources :

Example 6 with BlockingConditionDTO

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

the class BlacklistApiServiceImplTest method blacklistConditionIdPutTest.

@Test
public void blacklistConditionIdPutTest() throws APIManagementException, NotFoundException {
    printTestMethodName();
    BlacklistApiServiceImpl blacklistApiService = new BlacklistApiServiceImpl();
    String uuid = UUID.randomUUID().toString();
    BlockingConditionDTO dto = new BlockingConditionDTO();
    dto.setConditionId(UUID.randomUUID().toString());
    dto.setStatus(true);
    dto.setConditionType(BLOCKING_CONDITIONS_IP);
    dto.setConditionValue("12.32.45.3");
    APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
    BlockConditions conditions = BlockingConditionMappingUtil.fromBlockingConditionDTOToBlockCondition(dto);
    Mockito.doReturn(true).doThrow(new IllegalArgumentException()).when(adminService).updateBlockConditionStateByUUID(uuid, true);
    Mockito.doReturn(conditions).doThrow(new IllegalArgumentException()).when(adminService).getBlockConditionByUUID(uuid);
    Response response = blacklistApiService.blacklistConditionIdPut(uuid, dto, null, null, getRequest());
    Assert.assertEquals(response.getStatus(), 200);
}
Also used : Response(javax.ws.rs.core.Response) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO) BlacklistApiServiceImpl(org.wso2.carbon.apimgt.rest.api.admin.impl.BlacklistApiServiceImpl) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with BlockingConditionDTO

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

the class MappingUtil method fromBlockConditionListToListDTO.

/**
 * Converts a List of Block Condition in to REST API LIST DTO Object.
 *
 * @param blockConditionList A List of Block Conditions
 * @return REST API List DTO object derived from Block Condition list
 */
public static BlockingConditionListDTO fromBlockConditionListToListDTO(List<BlockConditions> blockConditionList) {
    BlockingConditionListDTO listDTO = new BlockingConditionListDTO();
    List<BlockingConditionDTO> blockingConditionDTOList = new ArrayList<>();
    if (blockConditionList != null) {
        for (BlockConditions blockCondition : blockConditionList) {
            BlockingConditionDTO dto = fromBlockingConditionToDTO(blockCondition);
            blockingConditionDTOList.add(dto);
        }
    }
    listDTO.setCount(blockingConditionDTOList.size());
    listDTO.setList(blockingConditionDTOList);
    return listDTO;
}
Also used : BlockingConditionListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) ArrayList(java.util.ArrayList) BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionDTO)

Example 8 with BlockingConditionDTO

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

the class MappingUtil 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(BlockConditions blockCondition) {
    if (blockCondition.getUuid() == null) {
        return null;
    }
    BlockingConditionDTO dto = new BlockingConditionDTO();
    dto.setUuid(blockCondition.getUuid());
    dto.setConditionType(blockCondition.getConditionType());
    dto.setEnabled(blockCondition.isEnabled());
    if (blockCondition.getConditionType().equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE)) {
        dto.setStartingIP(APIUtils.ipToLong(blockCondition.getStartingIP()));
        dto.setEndingIP(APIUtils.ipToLong(blockCondition.getEndingIP()));
    }
    String conditionValue = blockCondition.getConditionValue();
    if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP.equals(blockCondition.getConditionType())) {
        dto.setFixedIp(APIUtils.ipToLong(conditionValue));
    }
    dto.setConditionValue(conditionValue);
    return dto;
}
Also used : BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionDTO)

Example 9 with BlockingConditionDTO

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

the class ThrottlingApiServiceImpl method throttlingDenyPoliciesPost.

/**
 * Add a Block Condition
 *
 * @param body        DTO of new block condition to be created
 * @param contentType Content-Type header
 * @return Created block condition along with the location of it with Location header
 */
@Override
public Response throttlingDenyPoliciesPost(String contentType, BlockingConditionDTO body, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        // Add the block condition. It will throw BlockConditionAlreadyExistsException if the condition already
        // exists in the system
        String uuid = null;
        if (ConditionTypeEnum.API.equals(body.getConditionType()) || ConditionTypeEnum.APPLICATION.equals(body.getConditionType()) || ConditionTypeEnum.USER.equals(body.getConditionType())) {
            uuid = apiProvider.addBlockCondition(body.getConditionType().toString(), (String) body.getConditionValue(), body.isConditionStatus());
        } else if (ConditionTypeEnum.IP.equals(body.getConditionType()) || ConditionTypeEnum.IPRANGE.equals(body.getConditionType())) {
            if (body.getConditionValue() instanceof Map) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.putAll((Map) body.getConditionValue());
                if (ConditionTypeEnum.IP.equals(body.getConditionType())) {
                    RestApiAdminUtils.validateIPAddress(jsonObject.get("fixedIp").toString());
                }
                if (ConditionTypeEnum.IPRANGE.equals(body.getConditionType())) {
                    RestApiAdminUtils.validateIPAddress(jsonObject.get("startingIp").toString());
                    RestApiAdminUtils.validateIPAddress(jsonObject.get("endingIp").toString());
                }
                uuid = apiProvider.addBlockCondition(body.getConditionType().toString(), jsonObject.toJSONString(), body.isConditionStatus());
            }
        }
        // retrieve the new blocking condition and send back as the response
        BlockConditionsDTO newBlockingCondition = apiProvider.getBlockConditionByUUID(uuid);
        BlockingConditionDTO dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(newBlockingCondition);
        return Response.created(new URI(RestApiConstants.RESOURCE_PATH_THROTTLING_BLOCK_CONDITIONS + "/" + uuid)).entity(dto).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceAlreadyExists(e)) {
            RestApiUtil.handleResourceAlreadyExistsError("A black list item with type: " + body.getConditionType() + ", value: " + body.getConditionValue() + " already exists", e, log);
        } else {
            String errorMessage = "Error while adding Blocking Condition. Condition type: " + body.getConditionType() + ", " + "value: " + body.getConditionValue() + ". " + e.getMessage();
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    } catch (URISyntaxException | ParseException e) {
        String errorMessage = "Error while retrieving Blocking Condition resource location: Condition type: " + body.getConditionType() + ", " + "value: " + body.getConditionValue() + ". " + e.getMessage();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) URISyntaxException(java.net.URISyntaxException) ParseException(org.json.simple.parser.ParseException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) Map(java.util.Map) URI(java.net.URI)

Example 10 with BlockingConditionDTO

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

the class BlockingConditionMappingUtil method fromBlockConditionListToListDTO.

/**
 * Converts a List of Block Condition in to REST API LIST DTO Object
 *
 * @param blockConditionList A List of Block Conditions
 * @return REST API List DTO object derived from Block Condition list
 */
public static BlockingConditionListDTO fromBlockConditionListToListDTO(List<BlockConditionsDTO> blockConditionList) throws ParseException {
    BlockingConditionListDTO listDTO = new BlockingConditionListDTO();
    List<BlockingConditionDTO> blockingConditionDTOList = new ArrayList<>();
    if (blockConditionList != null) {
        for (BlockConditionsDTO blockCondition : blockConditionList) {
            BlockingConditionDTO dto = fromBlockingConditionToDTO(blockCondition);
            blockingConditionDTOList.add(dto);
        }
    }
    listDTO.setCount(blockingConditionDTOList.size());
    listDTO.setList(blockingConditionDTOList);
    return listDTO;
}
Also used : BlockingConditionListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionListDTO) BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) ArrayList(java.util.ArrayList) BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO)

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