Search in sources :

Example 91 with Condition

use of org.wso2.carbon.apimgt.keymgt.model.entity.Condition 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 92 with Condition

use of org.wso2.carbon.apimgt.keymgt.model.entity.Condition in project carbon-apimgt by wso2.

the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForGlobalLevel.

/**
 * Generate policy for global level
 *
 * @param policy policy with level 'global'. Multiple pipelines are not allowed. Can define more than one condition
 *               as set of conditions. all these conditions should be passed as a single pipeline
 * @return the generated execution plan for the policy
 * @throws APITemplateException if failed to generate policy
 */
public String getThrottlePolicyForGlobalLevel(GlobalPolicy policy) throws APITemplateException {
    StringWriter writer = new StringWriter();
    if (log.isDebugEnabled()) {
        log.debug("Generating policy for global level :" + policy.toString());
    }
    try {
        VelocityEngine velocityengine = new VelocityEngine();
        APIUtil.initializeVelocityContext(velocityengine);
        velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
        velocityengine.init();
        Template template = velocityengine.getTemplate(getTemplatePathForGlobal());
        VelocityContext context = new VelocityContext();
        setConstantContext(context);
        context.put("policy", policy);
        if (log.isDebugEnabled()) {
            log.debug("Policy : " + writer.toString());
        }
        template.merge(context, writer);
    } catch (VelocityException e) {
        log.error("Velocity Error", e);
        throw new APITemplateException("Velocity Error", e);
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) VelocityException(org.apache.velocity.exception.VelocityException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) Template(org.apache.velocity.Template)

Example 93 with Condition

use of org.wso2.carbon.apimgt.keymgt.model.entity.Condition in project carbon-apimgt by wso2.

the class ThrottlePolicyTemplateBuilder method getPolicyCondition.

/**
 * Produces final condition inside a pipeline
 *
 * @param conditions set of conditions
 * @return combined condition string
 */
private static String getPolicyCondition(Set<Condition> conditions) {
    StringBuilder conditionString = new StringBuilder();
    int i = 0;
    for (Condition condition : conditions) {
        org.wso2.carbon.apimgt.api.model.policy.Condition mappedCondition = PolicyMappingUtil.mapCondition(condition);
        if (i == 0) {
            conditionString.append(mappedCondition.getCondition());
        } else {
            conditionString.append(" AND ").append(mappedCondition.getCondition());
        }
        i++;
    }
    return conditionString.toString();
}
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)

Example 94 with Condition

use of org.wso2.carbon.apimgt.keymgt.model.entity.Condition in project carbon-apimgt by wso2.

the class ThrottlePolicyTemplateBuilder method getPolicyConditionForDefault.

/**
 * Produces final condition inside a pipeline for default policy with null string
 *
 * @param conditions set of conditions
 * @return default policy condition string
 */
private static String getPolicyConditionForDefault(Set<Condition> conditions) {
    StringBuilder conditionString = new StringBuilder();
    int i = 0;
    for (Condition condition : conditions) {
        org.wso2.carbon.apimgt.api.model.policy.Condition mappedCondition = PolicyMappingUtil.mapCondition(condition);
        String conditionStringComplete = mappedCondition.getCondition();
        if (i == 0) {
            conditionString.append(conditionStringComplete);
        } else {
            conditionString.append(" AND ").append(conditionStringComplete);
        }
        i++;
    }
    return conditionString.toString();
}
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)

Example 95 with Condition

use of org.wso2.carbon.apimgt.keymgt.model.entity.Condition in project carbon-apimgt by wso2.

the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForAPILevelDefault.

/**
 * Generate default policy for API level throttling
 *
 * @param policy Policy with level 'api'. Policy can have multiple pipelines and a default condition which will be
 *               used as else condition
 * @return the generated execution plan for the policy
 * @throws APITemplateException if failed to generate policy
 */
public String getThrottlePolicyForAPILevelDefault(ApiPolicy policy) throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Generating default policy for API Level :" + policy.toString());
    }
    Set<String> conditionsSet = new HashSet<>();
    try {
        VelocityEngine velocityengine = new VelocityEngine();
        APIUtil.initializeVelocityContext(velocityengine);
        velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
        velocityengine.init();
        Template template = velocityengine.getTemplate(getTemplatePathForAPIDefaultPolicy());
        StringWriter writer;
        VelocityContext context;
        List<APIPolicyConditionGroup> conditionGroups = policy.getConditionGroups();
        JSONArray policyConditionJson = new JSONArray();
        if (conditionGroups != null) {
            for (APIPolicyConditionGroup conditionGroup : conditionGroups) {
                if (conditionGroup.getDefaultLimit() == null) {
                    continue;
                }
                policyConditionJson.add(getPolicyConditionJson(conditionGroup.getCondition()));
                String conditionString = getPolicyConditionForDefault(conditionGroup.getCondition());
                if (!StringUtils.isEmpty(conditionString)) {
                    conditionsSet.add(conditionString);
                }
            }
        }
        context = new VelocityContext();
        setConstantContext(context);
        context.put("policy", policy);
        context.put("quotaPolicy", policy.getDefaultLimit());
        context.put("evaluatedConditions", new String(Base64.encodeBase64(policyConditionJson.toJSONString().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8));
        String conditionSetString = getConditionForDefault(conditionsSet);
        if (!StringUtils.isEmpty(conditionSetString)) {
            context.put("condition", " AND " + conditionSetString);
        } else {
            context.put("condition", "");
        }
        writer = new StringWriter();
        template.merge(context, writer);
        if (log.isDebugEnabled()) {
            log.debug("Policy : " + writer.toString());
        }
        return writer.toString();
    } catch (VelocityException e) {
        log.error("Velocity Error", e);
        throw new APITemplateException("Velocity Error", e);
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) APIPolicyConditionGroup(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.APIPolicyConditionGroup) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) JSONArray(org.json.simple.JSONArray) VelocityException(org.apache.velocity.exception.VelocityException) APITemplateException(org.wso2.carbon.apimgt.impl.template.APITemplateException) HashSet(java.util.HashSet) Template(org.apache.velocity.Template)

Aggregations

HashMap (java.util.HashMap)39 Test (org.junit.Test)32 Test (org.testng.annotations.Test)31 ArrayList (java.util.ArrayList)30 List (java.util.List)26 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)26 ConditionDto (org.wso2.carbon.apimgt.impl.dto.ConditionDto)26 MessageContext (org.apache.synapse.MessageContext)25 PreparedStatement (java.sql.PreparedStatement)23 Map (java.util.Map)22 ResultSet (java.sql.ResultSet)20 BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)18 ThrottleProperties (org.wso2.carbon.apimgt.impl.dto.ThrottleProperties)18 Connection (java.sql.Connection)16 SQLException (java.sql.SQLException)16 TreeMap (java.util.TreeMap)16 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