Search in sources :

Example 1 with IPCondition

use of org.wso2.carbon.apimgt.api.model.policy.IPCondition in project carbon-apimgt by wso2.

the class PolicyDAOImpl method setIPCondition.

/**
 * Retrieve IP condition from pipeline
 *
 * @param pipelineId id of the pipeline to get ip condition
 * @param conditions condition list to add each ip condition
 * @param connection connection to db
 * @throws SQLException If error occurred while getting ip condition form db
 */
private void setIPCondition(int pipelineId, ArrayList<Condition> conditions, Connection connection) throws SQLException {
    final String sqlQuery = "SELECT " + "STARTING_IP, " + "ENDING_IP, " + "SPECIFIC_IP,WITHIN_IP_RANGE " + "FROM " + "" + "AM_IP_CONDITION " + "WHERE " + "CONDITION_GROUP_ID = ? ";
    String startingIP;
    String endingIP;
    String specificIP;
    boolean invert;
    try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setInt(1, pipelineId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                startingIP = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_STARTING_IP);
                endingIP = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_ENDING_IP);
                specificIP = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_SPECIFIC_IP);
                invert = resultSet.getBoolean(APIMgtConstants.ThrottlePolicyConstants.COLUMN_WITHIN_IP_RANGE);
                if (specificIP != null && !"".equals(specificIP)) {
                    IPCondition ipCondition = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
                    ipCondition.setSpecificIP(specificIP);
                    ipCondition.setInvertCondition(invert);
                    conditions.add(ipCondition);
                } else if (startingIP != null && !"".equals(startingIP)) {
                    /*
                     Assumes availability of starting ip means ip range is enforced.
                     Therefore availability of ending ip is not checked.
                    */
                    IPCondition ipRangeCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
                    ipRangeCondition.setStartingIP(startingIP);
                    ipRangeCondition.setEndingIP(endingIP);
                    ipRangeCondition.setInvertCondition(invert);
                    conditions.add(ipRangeCondition);
                }
            }
        }
    }
}
Also used : IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 2 with IPCondition

use of org.wso2.carbon.apimgt.api.model.policy.IPCondition in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromDTOToIPCondition.

/**
 * Converts a IP Condition DTO object into a model object
 *
 * @param dto IP Condition DTO object
 * @return IP Condition model object derived from DTO
 */
public static IPCondition fromDTOToIPCondition(ThrottleConditionDTO dto) throws UnsupportedThrottleConditionTypeException {
    String ipConditionType = mapIPConditionTypeFromDTOToModel(dto.getIpCondition().getIpConditionType());
    IPCondition ipCondition = new IPCondition(ipConditionType);
    ipCondition = updateFieldsFromDTOToCondition(dto, ipCondition);
    ipCondition.setSpecificIP(dto.getIpCondition().getSpecificIP());
    ipCondition.setStartingIP(dto.getIpCondition().getStartingIP());
    ipCondition.setEndingIP(dto.getIpCondition().getEndingIP());
    return ipCondition;
}
Also used : IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition)

Example 3 with IPCondition

use of org.wso2.carbon.apimgt.api.model.policy.IPCondition 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 4 with IPCondition

use of org.wso2.carbon.apimgt.api.model.policy.IPCondition in project carbon-apimgt by wso2.

the class BlockingConditionMappingUtilTestCase method fromBlockingConditionDTOToBlockConditionTest.

@Test(description = "From Blocking Condition DTO to Model")
public void fromBlockingConditionDTOToBlockConditionTest() throws Exception {
    BlockingConditionDTO dto = new BlockingConditionDTO();
    dto.setConditionId(UUID.randomUUID().toString());
    dto.setConditionType(BLOCKING_CONDITION_IP_RANGE);
    IPConditionDTO ipCondition = new IPConditionDTO();
    ipCondition.setStartingIP("12.32.45.3");
    ipCondition.setEndingIP("12.32.45.31");
    dto.setIpCondition(ipCondition);
    dto.setStatus(true);
    BlockConditions conditions = BlockingConditionMappingUtil.fromBlockingConditionDTOToBlockCondition(dto);
    Assert.assertNotNull(conditions);
    Assert.assertEquals(BLOCKING_CONDITION_IP_RANGE, conditions.getConditionType());
    Assert.assertEquals(conditions.getStartingIP(), dto.getIpCondition().getStartingIP());
    Assert.assertEquals(conditions.getEndingIP(), dto.getIpCondition().getEndingIP());
}
Also used : IPConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.IPConditionDTO) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO) Test(org.testng.annotations.Test)

Example 5 with IPCondition

use of org.wso2.carbon.apimgt.api.model.policy.IPCondition in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtilTestCase method fromIPRangeConditionDtoToIPConditionModelTest.

@Test(description = "Convert IP range IPCondition DTO to IPCondition Model object")
public void fromIPRangeConditionDtoToIPConditionModelTest() throws Exception {
    ThrottleConditionDTO throttleConditionDTO = new ThrottleConditionDTO();
    throttleConditionDTO.setType(PolicyConstants.IP_CONDITION_TYPE);
    IPConditionDTO ipConditionDTO = new IPConditionDTO();
    ipConditionDTO.setIpConditionType(IP_RANGE_TYPE);
    ipConditionDTO.setStartingIP("10.100.0.158");
    ipConditionDTO.setEndingIP("10.100.0.178");
    throttleConditionDTO.setIpCondition(ipConditionDTO);
    IPCondition condition = (IPCondition) CommonThrottleMappingUtil.fromDTOToCondition(throttleConditionDTO);
    Assert.assertNotNull(condition);
    assertEquals(condition.getStartingIP(), "10.100.0.158");
    assertEquals(condition.getEndingIP(), "10.100.0.178");
    assertEquals(condition.getType(), IP_RANGE_TYPE);
}
Also used : IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) Test(org.testng.annotations.Test)

Aggregations

IPCondition (org.wso2.carbon.apimgt.api.model.policy.IPCondition)9 HeaderCondition (org.wso2.carbon.apimgt.api.model.policy.HeaderCondition)8 JWTClaimsCondition (org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition)8 QueryParameterCondition (org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)8 ArrayList (java.util.ArrayList)7 IPCondition (org.wso2.carbon.apimgt.core.models.policy.IPCondition)7 Condition (org.wso2.carbon.apimgt.api.model.policy.Condition)5 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 Test (org.testng.annotations.Test)3 Pipeline (org.wso2.carbon.apimgt.core.models.policy.Pipeline)3 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)3 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)3 ConditionDto (org.wso2.carbon.apimgt.impl.dto.ConditionDto)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 JSONObject (org.json.simple.JSONObject)2 UnsupportedThrottleConditionTypeException (org.wso2.carbon.apimgt.api.UnsupportedThrottleConditionTypeException)2 BandwidthLimit (org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)2