Search in sources :

Example 16 with Condition

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.

the class PolicyDAOImpl method setHeaderConditions.

/**
 * Add Header conditions of pipeline with pipeline Id: <code>pipelineId</code> to a
 * provided {@link Condition} array
 *
 * @param connection Connection of the database
 * @param pipelineId Id of the pipeline
 * @param conditions condition array to populate
 * @throws SQLException
 */
private void setHeaderConditions(int pipelineId, ArrayList<Condition> conditions, Connection connection) throws SQLException {
    final String query = "SELECT " + "HEADER_FIELD_NAME, " + "HEADER_FIELD_VALUE , IS_HEADER_FIELD_MAPPING " + " FROM " + "AM_HEADER_FIELD_CONDITION " + "WHERE " + "CONDITION_GROUP_ID =?";
    try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setInt(1, pipelineId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                HeaderCondition headerCondition = new HeaderCondition();
                headerCondition.setHeader(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_HEADER_FIELD_NAME));
                headerCondition.setValue(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_HEADER_FIELD_VALUE));
                headerCondition.setInvertCondition(resultSet.getBoolean(APIMgtConstants.ThrottlePolicyConstants.COLUMN_IS_HEADER_FIELD_MAPPING));
                conditions.add(headerCondition);
            }
        }
    }
}
Also used : HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 17 with Condition

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImpl method updateBlockConditionStateByUUID.

@Override
public boolean updateBlockConditionStateByUUID(String uuid, Boolean state) throws APIManagementException {
    try {
        if (policyDAO.updateBlockConditionStateByUUID(uuid, state)) {
            BlockConditions blockConditions = getBlockConditionByUUID(uuid);
            apiGateway.updateBlockCondition(blockConditions);
            return true;
        } else {
            return false;
        }
    } catch (APIMgtDAOException e) {
        String errorMessage = "Couldn't update block condition with UUID: " + uuid + ", state: " + state;
        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) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions)

Example 18 with Condition

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.

the class APIThrottlePolicyTemplateBuilder method getThrottlePolicyTemplateForPipelines.

/**
 * Generate policy for api level throttling
 *
 * @return throttle policies for api level
 * @throws APITemplateException throws if generation failure occur
 */
public Map<String, String> getThrottlePolicyTemplateForPipelines() throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Generating Siddhi App for apiLevel :" + apiPolicy.toString());
    }
    // get velocity template for API policy pipeline and generate the template
    Map<String, String> policyArray = new HashMap<String, String>();
    StringWriter writer;
    VelocityContext context;
    VelocityEngine velocityengine = initVelocityEngine();
    Template template = velocityengine.getTemplate(getTemplatePathForAPI());
    // Generate template for pipeline conditions if pipelines not null
    if (apiPolicy.getPipelines() != null) {
        for (Pipeline pipeline : apiPolicy.getPipelines()) {
            // set values for velocity context
            context = new VelocityContext();
            setConstantContext(context);
            context.put(PIPELINE_ITEM, pipeline);
            context.put(POLICY, apiPolicy);
            context.put(QUOTA_POLICY, pipeline.getQuotaPolicy());
            context.put(PIPELINE, CONDITION + UNDERSCORE + pipeline.getId());
            String conditionString = getPolicyCondition(pipeline.getConditions());
            context.put(CONDITION, AND + conditionString);
            writer = new StringWriter();
            template.merge(context, writer);
            if (log.isDebugEnabled()) {
                log.debug("Generated Siddhi App : " + writer.toString());
            }
            String policyName = PolicyConstants.POLICY_LEVEL_RESOURCE + UNDERSCORE + apiPolicy.getPolicyName() + UNDERSCORE + CONDITION + UNDERSCORE + pipeline.getId();
            policyArray.put(policyName, writer.toString());
        }
    }
    return policyArray;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Example 19 with Condition

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.

the class APIThrottlePolicyTemplateBuilder method getPolicyCondition.

/**
 * Produces final condition inside a pipeline.
 *
 * @param conditions list of throttle conditions
 * @return formatted condition list
 */
private static String getPolicyCondition(List<Condition> conditions) {
    String conditionString = null;
    int i = 0;
    for (Condition condition : conditions) {
        if (i == 0) {
            conditionString = condition.getCondition();
        } else {
            conditionString = conditionString + AND + condition.getCondition();
        }
        i++;
    }
    return conditionString;
}
Also used : Condition(org.wso2.carbon.apimgt.core.models.policy.Condition)

Example 20 with Condition

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition in project carbon-apimgt by wso2.

the class APIThrottlePolicyTemplateBuilder method getPolicyConditionForDefault.

/**
 * Produces final condition inside a pipeline for default policy with null string
 *
 * @param conditions list of default throttle conditions
 * @return formatted condition list
 */
private static String getPolicyConditionForDefault(List<Condition> conditions) {
    String conditionString = null;
    int i = 0;
    for (Condition condition : conditions) {
        String conditionStringComplete = condition.getCondition();
        if (i == 0) {
            conditionString = conditionStringComplete;
        } else {
            conditionString = conditionString + AND + conditionStringComplete;
        }
        i++;
    }
    return conditionString;
}
Also used : Condition(org.wso2.carbon.apimgt.core.models.policy.Condition)

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