Search in sources :

Example 51 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition 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 52 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition 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 53 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition 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 54 with Condition

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

the class APIMgtAdminServiceImpl method addBlockCondition.

@Override
public String addBlockCondition(BlockConditions blockConditions) throws APIManagementException {
    try {
        if (StringUtils.isEmpty(blockConditions.getUuid())) {
            blockConditions.setUuid(UUID.randomUUID().toString());
        }
        policyDAO.addBlockConditions(blockConditions);
        apiGateway.addBlockCondition(blockConditions);
        return blockConditions.getUuid();
    } catch (APIMgtDAOException e) {
        String errorMessage = "Couldn't add block condition with condition type: " + blockConditions.getConditionType() + ", condition value: " + blockConditions.getConditionValue();
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, ExceptionCodes.APIMGT_DAO_EXCEPTION);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException)

Example 55 with Condition

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

the class CommonThrottleMappingUtilTestCase method fromJWTConditionDtoToJWTConditionModelTest.

@Test(description = "Convert JWT Condition DTO to JWTCondition Model object")
public void fromJWTConditionDtoToJWTConditionModelTest() throws Exception {
    ThrottleConditionDTO throttleConditionDTO = new ThrottleConditionDTO();
    throttleConditionDTO.setType(PolicyConstants.JWT_CLAIMS_CONDITION_TYPE);
    JWTClaimsConditionDTO jwtClaimsConditionDTO = new JWTClaimsConditionDTO();
    jwtClaimsConditionDTO.setAttribute("testAttribute");
    jwtClaimsConditionDTO.setClaimUrl("http://wso2.org/claims");
    throttleConditionDTO.setJwtClaimsCondition(jwtClaimsConditionDTO);
    JWTClaimsCondition condition = (JWTClaimsCondition) CommonThrottleMappingUtil.fromDTOToCondition(throttleConditionDTO);
    Assert.assertNotNull(condition);
    assertEquals(condition.getAttribute(), "testAttribute");
    assertEquals(condition.getClaimUrl(), "http://wso2.org/claims");
}
Also used : JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) Test(org.testng.annotations.Test)

Aggregations

BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)18 Test (org.testng.annotations.Test)16 PreparedStatement (java.sql.PreparedStatement)12 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)11 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)10 ResultSet (java.sql.ResultSet)9 IPCondition (org.wso2.carbon.apimgt.core.models.policy.IPCondition)9 HeaderCondition (org.wso2.carbon.apimgt.core.models.policy.HeaderCondition)8 JWTClaimsCondition (org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition)8 QueryParameterCondition (org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition)8 ArrayList (java.util.ArrayList)7 Condition (org.wso2.carbon.apimgt.core.models.policy.Condition)7 Pipeline (org.wso2.carbon.apimgt.core.models.policy.Pipeline)7 Connection (java.sql.Connection)6 SQLException (java.sql.SQLException)6 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)6 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)6 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO)5 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)4 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)4