Search in sources :

Example 6 with Pipeline

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

use of org.wso2.carbon.apimgt.api.model.policy.Pipeline 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)

Example 8 with Pipeline

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

the class CommonThrottleMappingUtil method fromPipelineToConditionalGroupDTO.

/**
 * Converts a single Pipeline object into a Conditional Group DTO object
 *
 * @param pipeline Pipeline object
 * @return Derived DTO object from Pipeline object
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 * @throws UnsupportedThrottleConditionTypeException - If error occurs
 */
public static ConditionalGroupDTO fromPipelineToConditionalGroupDTO(Pipeline pipeline) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    ConditionalGroupDTO groupDTO = new ConditionalGroupDTO();
    groupDTO.setDescription(pipeline.getDescription());
    groupDTO.setLimit(fromQuotaPolicyToDTO(pipeline.getQuotaPolicy()));
    List<ThrottleConditionDTO> conditionDTOList = fromConditionListToDTOList(pipeline.getConditions());
    groupDTO.setConditions(conditionDTOList);
    return groupDTO;
}
Also used : ConditionalGroupDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ConditionalGroupDTO) ThrottleConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ThrottleConditionDTO)

Example 9 with Pipeline

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

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

the class ApiMgtDAO method addAPIPolicy.

/**
 * Add a API level throttling policy to database.
 * <p>
 * If valid policy Id (not -1) is present in the <code>policy</code> object,
 * policy will be inserted with that policy Id.
 * Otherwise policy Id will be auto incremented.
 * </p>
 *
 * @param policy policy object defining the throttle policy
 * @throws SQLException
 */
private void addAPIPolicy(APIPolicy policy, Connection conn) throws SQLException {
    ResultSet resultSet = null;
    PreparedStatement policyStatement = null;
    String addQuery = SQLConstants.ThrottleSQLConstants.INSERT_API_POLICY_SQL;
    int policyId;
    try {
        String dbProductName = conn.getMetaData().getDatabaseProductName();
        policyStatement = conn.prepareStatement(addQuery, new String[] { DBUtils.getConvertedAutoGeneratedColumnName(dbProductName, "POLICY_ID") });
        setCommonParametersForPolicy(policyStatement, policy);
        policyStatement.setString(12, policy.getUserLevel());
        policyStatement.executeUpdate();
        // Get the inserted POLICY_ID (auto incremented value)
        resultSet = policyStatement.getGeneratedKeys();
        // Returns only single row
        if (resultSet.next()) {
            /*
                 *  H2 doesn't return generated keys when key is provided (not generated).
                   Therefore policyId should be policy parameter's policyId when it is provided.
                 */
            policyId = resultSet.getInt(1);
            List<Pipeline> pipelines = policy.getPipelines();
            if (pipelines != null) {
                for (Pipeline pipeline : pipelines) {
                    // add each pipeline data to AM_CONDITION_GROUP table
                    addPipeline(pipeline, policyId, conn);
                }
            }
        }
    } finally {
        APIMgtDBUtil.closeAllConnections(policyStatement, null, resultSet);
    }
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Pipeline(org.wso2.carbon.apimgt.api.model.policy.Pipeline)

Aggregations

PreparedStatement (java.sql.PreparedStatement)15 ResultSet (java.sql.ResultSet)15 ArrayList (java.util.ArrayList)13 Pipeline (org.wso2.carbon.apimgt.api.model.policy.Pipeline)12 HeaderCondition (org.wso2.carbon.apimgt.api.model.policy.HeaderCondition)11 JWTClaimsCondition (org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition)11 QueryParameterCondition (org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)11 IPCondition (org.wso2.carbon.apimgt.api.model.policy.IPCondition)10 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)9 Condition (org.wso2.carbon.apimgt.api.model.policy.Condition)9 Pipeline (org.wso2.carbon.apimgt.core.models.policy.Pipeline)9 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)7 Condition (org.wso2.carbon.apimgt.core.models.policy.Condition)7 IPCondition (org.wso2.carbon.apimgt.core.models.policy.IPCondition)7 StringWriter (java.io.StringWriter)6 Template (org.apache.velocity.Template)6 VelocityContext (org.apache.velocity.VelocityContext)6 VelocityEngine (org.apache.velocity.app.VelocityEngine)6 RequestCountLimit (org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit)6