Search in sources :

Example 36 with BlockConditions

use of org.wso2.carbon.apimgt.core.models.BlockConditions 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 37 with BlockConditions

use of org.wso2.carbon.apimgt.core.models.BlockConditions 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)

Example 38 with BlockConditions

use of org.wso2.carbon.apimgt.core.models.BlockConditions 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 39 with BlockConditions

use of org.wso2.carbon.apimgt.core.models.BlockConditions 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 40 with BlockConditions

use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createUniqueBlockConditions.

/**
 * Create unique BlockConditions  objects.
 *
 * @return BlockConditions object
 */
public static BlockConditions createUniqueBlockConditions(String conditionType) {
    BlockConditions blockConditions = new BlockConditions();
    blockConditions.setUuid(UUID.randomUUID().toString());
    blockConditions.setConditionId(new Random().nextInt());
    blockConditions.setConditionType(conditionType);
    if ((APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE).equals(conditionType)) {
        // Generate random IP addresses
        blockConditions.setEndingIP(InetAddresses.fromInteger(new Random().nextInt()).getHostAddress());
        blockConditions.setStartingIP(InetAddresses.fromInteger(new Random().nextInt()).getHostAddress());
    } else if ((APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP).equals(conditionType)) {
        blockConditions.setConditionValue(InetAddresses.fromInteger(new Random().nextInt()).getHostAddress());
    }
    blockConditions.setEnabled(new Random().nextBoolean());
    return blockConditions;
}
Also used : Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions)

Aggregations

BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)32 Test (org.testng.annotations.Test)14 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)12 ArrayList (java.util.ArrayList)8 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)8 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)7 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO)7 Response (javax.ws.rs.core.Response)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)5 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)5 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 BlacklistApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.BlacklistApiServiceImpl)4 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionDTO)4 BlockingConditionListDTO (org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO)4 ResultSet (java.sql.ResultSet)3