Search in sources :

Example 11 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
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static BlockingConditionListDTO fromBlockConditionListToListDTO(List<BlockConditions> blockConditionList) throws UnsupportedThrottleLimitTypeException {
    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.admin.dto.BlockingConditionListDTO) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) ArrayList(java.util.ArrayList) BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO)

Example 12 with BlockingConditionDTO

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

the class BlockingConditionMappingUtil method fromBlockingConditionDTOToBlockCondition.

/**
 * Convert BlockingConditionDTO to BlockCondition.
 *
 * @param blockingConditionDTO blockindConditionDTO to be converted
 * @return BlockCondition Object
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static BlockConditions fromBlockingConditionDTOToBlockCondition(BlockingConditionDTO blockingConditionDTO) throws UnsupportedThrottleLimitTypeException {
    BlockConditions blockConditions = new BlockConditions();
    blockConditions.setUuid(blockingConditionDTO.getConditionId());
    blockConditions.setConditionType(blockingConditionDTO.getConditionType());
    blockConditions.setConditionValue(blockingConditionDTO.getConditionValue());
    blockConditions.setEnabled(blockingConditionDTO.getStatus());
    if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE.equals(blockConditions.getConditionType())) {
        blockConditions.setStartingIP(blockingConditionDTO.getIpCondition().getStartingIP());
        blockConditions.setEndingIP(blockingConditionDTO.getIpCondition().getEndingIP());
    }
    return blockConditions;
}
Also used : BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions)

Example 13 with BlockingConditionDTO

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

the class BlacklistApiServiceImplTest method blacklistPostTest.

@Test
public void blacklistPostTest() throws APIManagementException, NotFoundException {
    printTestMethodName();
    BlacklistApiServiceImpl blacklistApiService = new BlacklistApiServiceImpl();
    String uuid = UUID.randomUUID().toString();
    BlockingConditionDTO dto = new BlockingConditionDTO();
    dto.setConditionId(uuid);
    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.mockStatic(BlockingConditionMappingUtil.class);
    PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
    BlockConditions conditions = BlockingConditionMappingUtil.fromBlockingConditionDTOToBlockCondition(dto);
    Mockito.doReturn(uuid).doThrow(new IllegalArgumentException()).when(adminService).addBlockCondition(conditions);
    Mockito.doReturn(conditions).doThrow(new IllegalArgumentException()).when(adminService).getBlockConditionByUUID(uuid);
    PowerMockito.when(BlockingConditionMappingUtil.fromBlockingConditionDTOToBlockCondition(dto)).thenReturn(conditions);
    Response response = blacklistApiService.blacklistPost(dto, getRequest());
    Assert.assertEquals(201, response.getStatus());
}
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 14 with BlockingConditionDTO

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

the class BlacklistApiServiceImpl method blacklistConditionIdPut.

/**
 * Update blacklist/block condition statues
 *
 * @param conditionId       uuid of the blacklist condition
 * @param body              body of the blacklist status to be updated
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @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 blacklistConditionIdPut(String conditionId, BlockingConditionDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received BlockCondition GET request with id: " + conditionId);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        // This will give BlockConditionNotFoundException if there's no block condition exists with UUID
        Boolean status = apiMgtAdminService.updateBlockConditionStateByUUID(conditionId, body.getStatus());
        BlockingConditionDTO dto = null;
        if (status) {
            BlockConditions blockCondition = apiMgtAdminService.getBlockConditionByUUID(conditionId);
            dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(blockCondition);
        }
        return Response.ok().entity(dto).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while getting blacklist condition with UUID " + conditionId;
        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 15 with BlockingConditionDTO

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

the class BlacklistApiServiceImpl method blacklistConditionIdGet.

/**
 * Get blacklist condition by ID.
 *
 * @param conditionId     ID of the blacklist condition to be retrieved
 * @param ifNoneMatch     IF-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @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 blacklistConditionIdGet(String conditionId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received BlockCondition GET request with id: " + conditionId);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        // This will give BlockConditionNotFoundException if there's no block condition exists with UUID
        BlockConditions blockCondition = apiMgtAdminService.getBlockConditionByUUID(conditionId);
        BlockingConditionDTO dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(blockCondition);
        return Response.ok().entity(dto).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while getting blacklist condition with UUID " + conditionId;
        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)

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