Search in sources :

Example 26 with Condition

use of org.wso2.carbon.user.core.model.Condition in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromDTOToHeaderCondition.

/**
 * Converts a Header Condition DTO object into a model object
 *
 * @param dto Header Condition DTO object
 * @return Header Condition model object derived from Header Condition DTO
 */
public static HeaderCondition fromDTOToHeaderCondition(ThrottleConditionDTO dto) {
    HeaderCondition headerCondition = new HeaderCondition();
    headerCondition = updateFieldsFromDTOToCondition(dto, headerCondition);
    headerCondition.setHeader(dto.getHeaderCondition().getHeaderName());
    headerCondition.setValue(dto.getHeaderCondition().getHeaderValue());
    return headerCondition;
}
Also used : HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition)

Example 27 with Condition

use of org.wso2.carbon.user.core.model.Condition in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromHeaderConditionToDTO.

/**
 * Converts a Header Condition model object into a DTO
 *
 * @param headerCondition Header Condition model object
 * @return DTO object that was derived from Header Condition model object
 */
public static ThrottleConditionDTO fromHeaderConditionToDTO(HeaderCondition headerCondition) {
    ThrottleConditionDTO throttleConditionDTO = new ThrottleConditionDTO();
    throttleConditionDTO.setType(PolicyConstants.HEADER_CONDITION_TYPE);
    throttleConditionDTO.setHeaderCondition(new HeaderConditionDTO());
    throttleConditionDTO = updateFieldsFromConditionToDTO(headerCondition, throttleConditionDTO);
    throttleConditionDTO.getHeaderCondition().setHeaderName(headerCondition.getHeaderName());
    throttleConditionDTO.getHeaderCondition().setHeaderValue(headerCondition.getValue());
    return throttleConditionDTO;
}
Also used : ThrottleConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ThrottleConditionDTO) HeaderConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.HeaderConditionDTO)

Example 28 with Condition

use of org.wso2.carbon.user.core.model.Condition in project carbon-apimgt by wso2.

the class BlacklistApiServiceImpl method blacklistConditionIdDelete.

/**
 * Delete blacklist condition using ID
 *
 * @param conditionId       condition ID of the block condition
 * @param ifMatch           IF-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return Response Object
 * @throws NotFoundException Iif an error occurred when particular resource does not exits in the system.
 */
@Override
public Response blacklistConditionIdDelete(String conditionId, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Blacklist Condition DELETE request with id: " + conditionId);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        apiMgtAdminService.deleteBlockConditionByUuid(conditionId);
        return Response.ok().build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while deleting blacklist condition with UUID " + conditionId;
        org.wso2.carbon.apimgt.rest.api.common.dto.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) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 29 with Condition

use of org.wso2.carbon.user.core.model.Condition in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultPipelines.

/**
 * create default pipeline for api policy
 *
 * @return list of Pipeline objects is returned
 */
public static List<Pipeline> createDefaultPipelines() {
    // Pipeline 1
    IPCondition ipCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
    ipCondition.setStartingIP("192.168.12.3");
    ipCondition.setEndingIP("192.168.88.19");
    IPCondition ipConditionSpecific = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
    ipConditionSpecific.setSpecificIP("123.42.14.56");
    // adding above conditions to condition list of pipeline 1
    // contains conditions for each pipeline
    List<Condition> conditionsList = new ArrayList<>();
    conditionsList.add(ipCondition);
    conditionsList.add(ipConditionSpecific);
    // set quota policy with bandwidth limit
    BandwidthLimit bandwidthLimit = new BandwidthLimit(TIME_UNIT_MONTH, 1, 1000, PolicyConstants.MB);
    QuotaPolicy quotaPolicy1 = new QuotaPolicy();
    quotaPolicy1.setType(PolicyConstants.BANDWIDTH_TYPE);
    quotaPolicy1.setLimit(bandwidthLimit);
    Pipeline pipeline1 = new Pipeline();
    pipeline1.setId(0);
    pipeline1.setConditions(conditionsList);
    pipeline1.setQuotaPolicy(quotaPolicy1);
    // End of pipeline 1 -> Beginning of pipeline 2
    HeaderCondition headerCondition = new HeaderCondition();
    headerCondition.setHeader("Browser");
    headerCondition.setValue("Chrome");
    JWTClaimsCondition jwtClaimsCondition = new JWTClaimsCondition();
    jwtClaimsCondition.setClaimUrl("/path/path2");
    jwtClaimsCondition.setAttribute("attributed");
    QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
    queryParameterCondition.setParameter("Location");
    queryParameterCondition.setValue("Colombo");
    // adding conditions to condition list of pipeline2
    conditionsList = new ArrayList<>();
    conditionsList.add(headerCondition);
    conditionsList.add(jwtClaimsCondition);
    conditionsList.add(queryParameterCondition);
    // pipeline 2 with request count as quota policy
    RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 1, 1000);
    QuotaPolicy quotaPolicy2 = new QuotaPolicy();
    quotaPolicy2.setType(REQUEST_COUNT_TYPE);
    quotaPolicy2.setLimit(requestCountLimit);
    Pipeline pipeline2 = new Pipeline();
    pipeline2.setId(1);
    pipeline2.setConditions(conditionsList);
    pipeline2.setQuotaPolicy(quotaPolicy2);
    // adding pipelines
    // contains all the default pipelines
    List<Pipeline> pipelineList = new ArrayList<>();
    pipelineList.add(pipeline1);
    pipelineList.add(pipeline2);
    return pipelineList;
}
Also used : JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) Condition(org.wso2.carbon.apimgt.core.models.policy.Condition) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) ArrayList(java.util.ArrayList) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline) IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Example 30 with Condition

use of org.wso2.carbon.user.core.model.Condition in project carbon-apimgt by wso2.

the class PolicyDAOImplIT method testAddGetUpdateDeleteBlockConditions.

@Test(description = "Add, Get, Delete block condition")
public void testAddGetUpdateDeleteBlockConditions() throws Exception {
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    BlockConditions blockConditionsIP = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP);
    BlockConditions blockConditionsIpRange = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE);
    BlockConditions blockConditionsApi = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_API);
    BlockConditions blockConditionsApp = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_APPLICATION);
    BlockConditions blockConditionsUser = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_USER);
    String uuidIp = policyDAO.addBlockConditions(blockConditionsIP);
    String uuidIpRange = policyDAO.addBlockConditions(blockConditionsIpRange);
    String uuidApi = policyDAO.addBlockConditions(blockConditionsApi);
    String uuidApp = policyDAO.addBlockConditions(blockConditionsApp);
    String uuidUser = policyDAO.addBlockConditions(blockConditionsUser);
    BlockConditions blockConditionsAddedIP = policyDAO.getBlockConditionByUUID(uuidIp);
    BlockConditions blockConditionsAddedIpRange = policyDAO.getBlockConditionByUUID(uuidIpRange);
    BlockConditions blockConditionsAddedApi = policyDAO.getBlockConditionByUUID(uuidApi);
    BlockConditions blockConditionsAddedApp = policyDAO.getBlockConditionByUUID(uuidApp);
    BlockConditions blockConditionsAddedUser = policyDAO.getBlockConditionByUUID(uuidUser);
    Assert.assertEquals(blockConditionsIP.getConditionValue(), blockConditionsAddedIP.getConditionValue());
    Assert.assertEquals(blockConditionsApi.getConditionValue(), blockConditionsAddedApi.getConditionValue());
    Assert.assertEquals(blockConditionsApp.getConditionValue(), blockConditionsAddedApp.getConditionValue());
    Assert.assertEquals(blockConditionsUser.getConditionValue(), blockConditionsAddedUser.getConditionValue());
    Assert.assertEquals(blockConditionsIpRange.getStartingIP(), blockConditionsAddedIpRange.getStartingIP());
    Assert.assertTrue(policyDAO.updateBlockConditionStateByUUID(uuidIp, true));
    Assert.assertTrue(policyDAO.getBlockConditionByUUID(uuidIp).isEnabled());
    Assert.assertTrue(policyDAO.getBlockConditions().size() == 5);
    Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidIp));
    Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidIpRange));
    Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidApi));
    Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidApp));
    Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidUser));
}
Also used : BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) Test(org.testng.annotations.Test)

Aggregations

ArrayList (java.util.ArrayList)43 HashMap (java.util.HashMap)40 Test (org.testng.annotations.Test)34 Test (org.junit.Test)32 PreparedStatement (java.sql.PreparedStatement)29 List (java.util.List)28 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)26 ConditionDto (org.wso2.carbon.apimgt.impl.dto.ConditionDto)26 MessageContext (org.apache.synapse.MessageContext)25 ResultSet (java.sql.ResultSet)24 Map (java.util.Map)24 SQLException (java.sql.SQLException)22 Connection (java.sql.Connection)21 BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)18 ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)18 HeaderCondition (org.wso2.carbon.apimgt.api.model.policy.HeaderCondition)15 JWTClaimsCondition (org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition)15 QueryParameterCondition (org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)15 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)15 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)15