Search in sources :

Example 36 with Pipeline

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

the class ApiMgtDAO method getAPIPolicies.

/**
 * Get API level policies. Result only contains basic details of the policy,
 * it doesn't contain pipeline information.
 *
 * @param tenantID policies are selected using tenantID
 * @return APIPolicy ArrayList
 * @throws APIManagementException
 */
public APIPolicy[] getAPIPolicies(int tenantID) throws APIManagementException {
    List<APIPolicy> policies = new ArrayList<APIPolicy>();
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sqlQuery = SQLConstants.ThrottleSQLConstants.GET_API_POLICIES;
    if (forceCaseInsensitiveComparisons) {
        sqlQuery = SQLConstants.ThrottleSQLConstants.GET_API_POLICIES;
    }
    try {
        conn = APIMgtDBUtil.getConnection();
        ps = conn.prepareStatement(sqlQuery);
        ps.setInt(1, tenantID);
        rs = ps.executeQuery();
        while (rs.next()) {
            APIPolicy apiPolicy = new APIPolicy(rs.getString(ThrottlePolicyConstants.COLUMN_NAME));
            setCommonPolicyDetails(apiPolicy, rs);
            apiPolicy.setUserLevel(rs.getString(ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
            policies.add(apiPolicy);
        }
    } catch (SQLException e) {
        handleException("Error while executing SQL", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, rs);
    }
    return policies.toArray(new APIPolicy[policies.size()]);
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy)

Example 37 with Pipeline

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

the class ApiMgtDAO method setJWTClaimConditions.

/**
 * Add JWT claim conditions of pipeline with pipeline Id: <code>pipelineId</code> to a
 * provided {@link Condition} array
 *
 * @param pipelineId Id of the pipeline
 * @param conditions condition array to populate
 * @throws APIManagementException
 */
private void setJWTClaimConditions(int pipelineId, ArrayList<Condition> conditions) throws APIManagementException {
    Connection connection = null;
    PreparedStatement conditionsStatement = null;
    ResultSet resultSet = null;
    try {
        connection = APIMgtDBUtil.getConnection();
        conditionsStatement = connection.prepareStatement(SQLConstants.ThrottleSQLConstants.GET_JWT_CLAIM_CONDITIONS_SQL);
        conditionsStatement.setInt(1, pipelineId);
        resultSet = conditionsStatement.executeQuery();
        while (resultSet.next()) {
            JWTClaimsCondition jwtClaimsCondition = new JWTClaimsCondition();
            jwtClaimsCondition.setClaimUrl(resultSet.getString(ThrottlePolicyConstants.COLUMN_CLAIM_URI));
            jwtClaimsCondition.setAttribute(resultSet.getString(ThrottlePolicyConstants.COLUMN_CLAIM_ATTRIBUTE));
            jwtClaimsCondition.setInvertCondition(resultSet.getBoolean(ThrottlePolicyConstants.COLUMN_IS_CLAIM_MAPPING));
            conditions.add(jwtClaimsCondition);
        }
    } catch (SQLException e) {
        handleException("Failed to get jwt claim conditions for pipelineId: " + pipelineId, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(conditionsStatement, connection, resultSet);
    }
}
Also used : JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 38 with Pipeline

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

the class ApiMgtDAO method createConditionGroupDTO.

/**
 * Converts an {@code Pipeline} object into a {@code ConditionGroupDTO}.{@code ConditionGroupDTO} class tries to
 * contain the same information held by  {@code Pipeline}, but in a much lightweight fashion.
 *
 * @param conditionGroup Id of the condition group ({@code Pipeline}) to be converted
 * @return An object of {@code ConditionGroupDTO} type.
 * @throws APIManagementException
 */
public ConditionGroupDTO createConditionGroupDTO(int conditionGroup) throws APIManagementException {
    List<Condition> conditions = getConditions(conditionGroup);
    ArrayList<ConditionDTO> conditionDTOs = new ArrayList<ConditionDTO>(conditions.size());
    for (Condition condition : conditions) {
        ConditionDTO conditionDTO = new ConditionDTO();
        conditionDTO.setConditionType(condition.getType());
        conditionDTO.isInverted(condition.isInvertCondition());
        if (PolicyConstants.IP_RANGE_TYPE.equals(condition.getType())) {
            IPCondition ipRangeCondition = (IPCondition) condition;
            conditionDTO.setConditionName(ipRangeCondition.getStartingIP());
            conditionDTO.setConditionValue(ipRangeCondition.getEndingIP());
        } else if (PolicyConstants.IP_SPECIFIC_TYPE.equals(condition.getType())) {
            IPCondition ipCondition = (IPCondition) condition;
            conditionDTO.setConditionName(PolicyConstants.IP_SPECIFIC_TYPE);
            conditionDTO.setConditionValue(ipCondition.getSpecificIP());
        } else if (PolicyConstants.HEADER_TYPE.equals(condition.getType())) {
            HeaderCondition headerCondition = (HeaderCondition) condition;
            conditionDTO.setConditionName(headerCondition.getHeaderName());
            conditionDTO.setConditionValue(headerCondition.getValue());
        } else if (PolicyConstants.JWT_CLAIMS_TYPE.equals(condition.getType())) {
            JWTClaimsCondition jwtClaimsCondition = (JWTClaimsCondition) condition;
            conditionDTO.setConditionName(jwtClaimsCondition.getClaimUrl());
            conditionDTO.setConditionValue(jwtClaimsCondition.getAttribute());
        } else if (PolicyConstants.QUERY_PARAMETER_TYPE.equals(condition.getType())) {
            QueryParameterCondition parameterCondition = (QueryParameterCondition) condition;
            conditionDTO.setConditionName(parameterCondition.getParameter());
            conditionDTO.setConditionValue(parameterCondition.getValue());
        }
        conditionDTOs.add(conditionDTO);
    }
    ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
    conditionGroupDTO.setConditions(conditionDTOs.toArray(new ConditionDTO[] {}));
    return conditionGroupDTO;
}
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) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) ConditionDTO(org.wso2.carbon.apimgt.api.dto.ConditionDTO) ArrayList(java.util.ArrayList) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)

Example 39 with Pipeline

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

the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForAppLevel.

/**
 * Generate application level policy
 *
 * @param policy policy with level 'app'. 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 getThrottlePolicyForAppLevel(ApplicationPolicy policy) throws APITemplateException {
    StringWriter writer = new StringWriter();
    if (log.isDebugEnabled()) {
        log.debug("Generating policy for app 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(getTemplatePathForApplication());
        VelocityContext context = new VelocityContext();
        setConstantContext(context);
        context.put("policy", policy);
        context.put("quotaPolicy", policy.getDefaultLimit());
        template.merge(context, writer);
        if (log.isDebugEnabled()) {
            log.debug("Policy : " + writer.toString());
        }
    } 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 40 with Pipeline

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

the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForSubscriptionLevel.

/**
 * Generate policy for subscription level
 *
 * @param policy policy with level 'sub'. 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 getThrottlePolicyForSubscriptionLevel(SubscriptionPolicy policy) throws APITemplateException {
    StringWriter writer = new StringWriter();
    if (log.isDebugEnabled()) {
        log.debug("Generating policy for subscription Level :" + policy.toString());
    }
    try {
        VelocityEngine velocityengine = new VelocityEngine();
        velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
        APIUtil.initializeVelocityContext(velocityengine);
        velocityengine.init();
        Template template;
        if (PolicyConstants.EVENT_COUNT_TYPE.equals(policy.getDefaultLimit().getQuotaType())) {
            template = velocityengine.getTemplate(getTemplatePathForAsyncSubscription());
        } else {
            template = velocityengine.getTemplate(getTemplatePathForSubscription());
        }
        VelocityContext context = new VelocityContext();
        setConstantContext(context);
        context.put("policy", policy);
        context.put("quotaPolicy", policy.getDefaultLimit());
        template.merge(context, writer);
        if (log.isDebugEnabled()) {
            log.debug("Policy : " + writer.toString());
        }
    } 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)

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