Search in sources :

Example 11 with HeaderCondition

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

the class ThrottlePolicyTemplateBuilder method getPolicyConditionJson.

/**
 * Produces final condition inside a pipeline
 *
 * @param conditions set of conditions
 * @return conditions as a JSON
 */
private static JSONObject getPolicyConditionJson(Set<Condition> conditions) {
    JSONObject tempCondition = new JSONObject();
    for (Condition condition : conditions) {
        org.wso2.carbon.apimgt.api.model.policy.Condition mappedCondition = PolicyMappingUtil.mapCondition(condition);
        JSONObject conditionJson;
        if (tempCondition.containsKey(mappedCondition.getType().toLowerCase(Locale.ENGLISH))) {
            conditionJson = (JSONObject) tempCondition.get(mappedCondition.getType().toLowerCase(Locale.ENGLISH));
        } else {
            conditionJson = new JSONObject();
        }
        tempCondition.put(mappedCondition.getType().toLowerCase(Locale.ENGLISH), conditionJson);
        if (PolicyConstants.IP_SPECIFIC_TYPE.equals(mappedCondition.getType())) {
            IPCondition ipCondition = (IPCondition) mappedCondition;
            if (IPCondition.isIPv6Address(ipCondition.getSpecificIP())) {
                conditionJson.put("specificIp", String.valueOf(APIUtil.ipToBigInteger(ipCondition.getSpecificIP())));
            } else {
                conditionJson.put("specificIp", ipCondition.ipToLong(ipCondition.getSpecificIP()));
            }
        } else if (PolicyConstants.IP_RANGE_TYPE.equals(mappedCondition.getType())) {
            IPCondition ipRangeCondition = (IPCondition) mappedCondition;
            if (IPCondition.isIPv6Address(ipRangeCondition.getStartingIP()) && IPCondition.isIPv6Address(ipRangeCondition.getEndingIP())) {
                conditionJson.put("startingIp", String.valueOf(APIUtil.ipToBigInteger(ipRangeCondition.getStartingIP())));
                conditionJson.put("endingIp", String.valueOf(APIUtil.ipToBigInteger(ipRangeCondition.getEndingIP())));
            } else {
                conditionJson.put("startingIp", ipRangeCondition.ipToLong(ipRangeCondition.getStartingIP()));
                conditionJson.put("endingIp", ipRangeCondition.ipToLong(ipRangeCondition.getEndingIP()));
            }
        } else if (mappedCondition instanceof QueryParameterCondition) {
            QueryParameterCondition queryParameterCondition = (QueryParameterCondition) mappedCondition;
            JSONObject values;
            if (conditionJson.containsKey("values")) {
                values = (JSONObject) conditionJson.get("values");
            } else {
                values = new JSONObject();
                conditionJson.put("values", values);
            }
            values.put(queryParameterCondition.getParameter(), queryParameterCondition.getValue());
        } else if (mappedCondition instanceof HeaderCondition) {
            HeaderCondition headerCondition = (HeaderCondition) mappedCondition;
            JSONObject values;
            if (conditionJson.containsKey("values")) {
                values = (JSONObject) conditionJson.get("values");
            } else {
                values = new JSONObject();
                conditionJson.put("values", values);
            }
            values.put(headerCondition.getHeaderName(), headerCondition.getValue());
        } else if (mappedCondition instanceof JWTClaimsCondition) {
            JWTClaimsCondition jwtClaimsCondition = (JWTClaimsCondition) mappedCondition;
            JSONObject values;
            if (conditionJson.containsKey("values")) {
                values = (JSONObject) conditionJson.get("values");
            } else {
                values = new JSONObject();
                conditionJson.put("values", values);
            }
            values.put(jwtClaimsCondition.getClaimUrl(), jwtClaimsCondition.getAttribute());
        }
        conditionJson.put("invert", mappedCondition.isInvertCondition());
    }
    return tempCondition;
}
Also used : IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) Condition(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) JSONObject(org.json.simple.JSONObject) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)

Example 12 with HeaderCondition

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

the class PolicyMappingUtil method mapCondition.

/**
 * Map a org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition to a
 * org.wso2.carbon.apimgt.api.model.policy.Condition
 *
 * @param conditionDTO org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition
 * @return org.wso2.carbon.apimgt.api.model.policy.Condition object
 */
public static Condition mapCondition(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition conditionDTO) {
    switch(conditionDTO.getConditionType()) {
        case PolicyConstants.IP_RANGE_TYPE:
            IPCondition ipRangeCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
            ipRangeCondition.setInvertCondition(conditionDTO.isInverted());
            ipRangeCondition.setStartingIP(conditionDTO.getName());
            ipRangeCondition.setEndingIP(conditionDTO.getValue());
            return ipRangeCondition;
        case PolicyConstants.IP_SPECIFIC_TYPE:
            IPCondition ipSpecificCondition = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
            ipSpecificCondition.setInvertCondition(conditionDTO.isInverted());
            ipSpecificCondition.setSpecificIP(conditionDTO.getValue());
            return ipSpecificCondition;
        case PolicyConstants.HEADER_TYPE:
            HeaderCondition headerCondition = new HeaderCondition();
            headerCondition.setInvertCondition(conditionDTO.isInverted());
            headerCondition.setHeader(conditionDTO.getName());
            headerCondition.setValue(conditionDTO.getValue());
            return headerCondition;
        case PolicyConstants.JWT_CLAIMS_TYPE:
            JWTClaimsCondition jwtClaimsCondition = new JWTClaimsCondition();
            jwtClaimsCondition.setInvertCondition(conditionDTO.isInverted());
            jwtClaimsCondition.setClaimUrl(conditionDTO.getName());
            jwtClaimsCondition.setAttribute(conditionDTO.getValue());
            return jwtClaimsCondition;
        case PolicyConstants.QUERY_PARAMETER_TYPE:
            QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
            queryParameterCondition.setInvertCondition(conditionDTO.isInverted());
            queryParameterCondition.setParameter(conditionDTO.getName());
            queryParameterCondition.setValue(conditionDTO.getValue());
            return queryParameterCondition;
        default:
            return null;
    }
}
Also used : IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)

Example 13 with HeaderCondition

use of org.wso2.carbon.apimgt.core.models.policy.HeaderCondition 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.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(PolicyConstants.REQUEST_COUNT_TYPE);
    quotaPolicy2.setLimit(requestCountLimit);
    Pipeline pipeline2 = new Pipeline();
    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) IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) Condition(org.wso2.carbon.apimgt.core.models.policy.Condition) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) 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 14 with HeaderCondition

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

the class ApiMgtDAO method addPipeline.

/**
 * Add throttling policy pipeline to database
 *
 * @param pipeline condition pipeline
 * @param policyID id of the policy to add pipeline
 * @param conn     database connection. This should be provided inorder to rollback transaction
 * @throws SQLException
 */
private void addPipeline(Pipeline pipeline, int policyID, Connection conn) throws SQLException {
    PreparedStatement conditionStatement = null;
    ResultSet rs = null;
    try {
        String sqlAddQuery = SQLConstants.ThrottleSQLConstants.INSERT_CONDITION_GROUP_SQL;
        List<Condition> conditionList = pipeline.getConditions();
        // Add data to the AM_CONDITION table
        String dbProductName = conn.getMetaData().getDatabaseProductName();
        conditionStatement = conn.prepareStatement(sqlAddQuery, new String[] { DBUtils.getConvertedAutoGeneratedColumnName(dbProductName, "CONDITION_GROUP_ID") });
        conditionStatement.setInt(1, policyID);
        conditionStatement.setString(2, pipeline.getQuotaPolicy().getType());
        if (PolicyConstants.REQUEST_COUNT_TYPE.equals(pipeline.getQuotaPolicy().getType())) {
            conditionStatement.setLong(3, ((RequestCountLimit) pipeline.getQuotaPolicy().getLimit()).getRequestCount());
            conditionStatement.setString(4, null);
        } else if (PolicyConstants.BANDWIDTH_TYPE.equals(pipeline.getQuotaPolicy().getType())) {
            BandwidthLimit limit = (BandwidthLimit) pipeline.getQuotaPolicy().getLimit();
            conditionStatement.setLong(3, limit.getDataAmount());
            conditionStatement.setString(4, limit.getDataUnit());
        }
        conditionStatement.setLong(5, pipeline.getQuotaPolicy().getLimit().getUnitTime());
        conditionStatement.setString(6, pipeline.getQuotaPolicy().getLimit().getTimeUnit());
        conditionStatement.setString(7, pipeline.getDescription());
        conditionStatement.executeUpdate();
        rs = conditionStatement.getGeneratedKeys();
        // Add Throttling parameters which have multiple entries
        if (rs != null && rs.next()) {
            // Get the inserted
            int pipelineId = rs.getInt(1);
            // CONDITION_GROUP_ID (auto
            // incremented value)
            pipeline.setId(pipelineId);
            for (Condition condition : conditionList) {
                if (condition == null) {
                    continue;
                }
                String type = condition.getType();
                if (PolicyConstants.IP_RANGE_TYPE.equals(type) || PolicyConstants.IP_SPECIFIC_TYPE.equals(type)) {
                    IPCondition ipCondition = (IPCondition) condition;
                    addIPCondition(ipCondition, pipelineId, conn);
                }
                if (PolicyConstants.HEADER_TYPE.equals(type)) {
                    addHeaderCondition((HeaderCondition) condition, pipelineId, conn);
                } else if (PolicyConstants.QUERY_PARAMETER_TYPE.equals(type)) {
                    addQueryParameterCondition((QueryParameterCondition) condition, pipelineId, conn);
                } else if (PolicyConstants.JWT_CLAIMS_TYPE.equals(type)) {
                    addJWTClaimsCondition((JWTClaimsCondition) condition, pipelineId, conn);
                }
            }
        }
    } finally {
        APIMgtDBUtil.closeAllConnections(conditionStatement, null, rs);
    }
}
Also used : QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition) Condition(org.wso2.carbon.apimgt.api.model.policy.Condition) IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Example 15 with HeaderCondition

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

the class APIMgtDAOTest method testUpdateAPIPolicy.

@Test
public void testUpdateAPIPolicy() throws APIManagementException {
    String policyName = "TestUpdateApiPolicy";
    APIPolicy policy = (APIPolicy) getPolicyAPILevelPerUser(policyName);
    apiMgtDAO.addAPIPolicy(policy);
    policy = apiMgtDAO.getAPIPolicy(policyName, -1234);
    policy.setDescription("New Description");
    ArrayList<Pipeline> pipelines = new ArrayList<Pipeline>();
    Pipeline p = new Pipeline();
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    quotaPolicy.setType("requestCount");
    RequestCountLimit requestCountLimit = new RequestCountLimit();
    requestCountLimit.setTimeUnit("min");
    requestCountLimit.setUnitTime(50);
    requestCountLimit.setRequestCount(1000);
    quotaPolicy.setLimit(requestCountLimit);
    ArrayList<Condition> conditions = new ArrayList<Condition>();
    DateCondition dateCondition = new DateCondition();
    dateCondition.setSpecificDate("2016-03-03");
    conditions.add(dateCondition);
    HeaderCondition headerCondition1 = new HeaderCondition();
    headerCondition1.setHeader("User-Agent");
    headerCondition1.setValue("Chrome");
    conditions.add(headerCondition1);
    HeaderCondition headerCondition2 = new HeaderCondition();
    headerCondition2.setHeader("Accept-Ranges");
    headerCondition2.setValue("bytes");
    conditions.add(headerCondition2);
    QueryParameterCondition queryParameterCondition1 = new QueryParameterCondition();
    queryParameterCondition1.setParameter("test1");
    queryParameterCondition1.setValue("testValue1");
    conditions.add(queryParameterCondition1);
    QueryParameterCondition queryParameterCondition2 = new QueryParameterCondition();
    queryParameterCondition2.setParameter("x");
    queryParameterCondition2.setValue("abc");
    conditions.add(queryParameterCondition2);
    JWTClaimsCondition jwtClaimsCondition1 = new JWTClaimsCondition();
    jwtClaimsCondition1.setClaimUrl("test_url");
    jwtClaimsCondition1.setAttribute("test_attribute");
    conditions.add(jwtClaimsCondition1);
    p.setQuotaPolicy(quotaPolicy);
    p.setConditions(conditions);
    pipelines.add(p);
    policy.setPipelines(pipelines);
    apiMgtDAO.updateAPIPolicy(policy);
    APIPolicy apiPolicy = apiMgtDAO.getAPIPolicy(policyName, -1234);
    assertNotNull(apiPolicy);
    List<Pipeline> pipelineList = apiPolicy.getPipelines();
    assertNotNull(pipelineList);
    assertEquals(pipelineList.size(), pipelines.size());
}
Also used : DateRangeCondition(org.wso2.carbon.apimgt.api.model.policy.DateRangeCondition) IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition) DateCondition(org.wso2.carbon.apimgt.api.model.policy.DateCondition) HTTPVerbCondition(org.wso2.carbon.apimgt.api.model.policy.HTTPVerbCondition) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) Condition(org.wso2.carbon.apimgt.api.model.policy.Condition) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) ArrayList(java.util.ArrayList) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition) Pipeline(org.wso2.carbon.apimgt.api.model.policy.Pipeline) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) DateCondition(org.wso2.carbon.apimgt.api.model.policy.DateCondition) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

HeaderCondition (org.wso2.carbon.apimgt.api.model.policy.HeaderCondition)10 IPCondition (org.wso2.carbon.apimgt.api.model.policy.IPCondition)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)6 Condition (org.wso2.carbon.apimgt.api.model.policy.Condition)5 HeaderCondition (org.wso2.carbon.apimgt.core.models.policy.HeaderCondition)5 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 UnsupportedThrottleConditionTypeException (org.wso2.carbon.apimgt.api.UnsupportedThrottleConditionTypeException)2 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)2 BandwidthLimit (org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)2 DateCondition (org.wso2.carbon.apimgt.api.model.policy.DateCondition)2 DateRangeCondition (org.wso2.carbon.apimgt.api.model.policy.DateRangeCondition)2 HTTPVerbCondition (org.wso2.carbon.apimgt.api.model.policy.HTTPVerbCondition)2 Pipeline (org.wso2.carbon.apimgt.api.model.policy.Pipeline)2 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)2 RequestCountLimit (org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit)2 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)2 Condition (org.wso2.carbon.apimgt.core.models.policy.Condition)2