Search in sources :

Example 51 with Condition

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

the class ApiMgtDAO method setQueryParameterConditions.

/**
 * Add Query parameter 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 setQueryParameterConditions(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_QUERY_PARAMETER_CONDITIONS_SQL);
        conditionsStatement.setInt(1, pipelineId);
        resultSet = conditionsStatement.executeQuery();
        while (resultSet.next()) {
            QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
            queryParameterCondition.setParameter(resultSet.getString(ThrottlePolicyConstants.COLUMN_PARAMETER_NAME));
            queryParameterCondition.setValue(resultSet.getString(ThrottlePolicyConstants.COLUMN_PARAMETER_VALUE));
            queryParameterCondition.setInvertCondition(resultSet.getBoolean(ThrottlePolicyConstants.COLUMN_IS_PARAM_MAPPING));
            conditions.add(queryParameterCondition);
        }
    } catch (SQLException e) {
        handleException("Failed to get query parameter conditions for pipelineId: " + pipelineId, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(conditionsStatement, connection, resultSet);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)

Example 52 with Condition

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

the class ApiMgtDAO method getPipelines.

/**
 * Retrieves list of pipelines for the policy with policy Id: <code>policyId</code>
 *
 * @param policyId policy id of the pipelines
 * @return list of pipelines
 * @throws APIManagementException
 */
private ArrayList<Pipeline> getPipelines(int policyId) throws APIManagementException {
    Connection connection = null;
    PreparedStatement pipelinesStatement = null;
    ResultSet resultSet = null;
    ArrayList<Pipeline> pipelines = new ArrayList<Pipeline>();
    try {
        connection = APIMgtDBUtil.getConnection();
        pipelinesStatement = connection.prepareStatement(SQLConstants.ThrottleSQLConstants.GET_PIPELINES_SQL);
        int unitTime = 0;
        int quota = 0;
        int pipelineId = -1;
        String timeUnit = null;
        String quotaUnit = null;
        String description;
        pipelinesStatement.setInt(1, policyId);
        resultSet = pipelinesStatement.executeQuery();
        while (resultSet.next()) {
            Pipeline pipeline = new Pipeline();
            ArrayList<Condition> conditions = null;
            QuotaPolicy quotaPolicy = new QuotaPolicy();
            quotaPolicy.setType(resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
            timeUnit = resultSet.getString(ThrottlePolicyConstants.COLUMN_TIME_UNIT);
            quotaUnit = resultSet.getString(ThrottlePolicyConstants.COLUMN_QUOTA_UNIT);
            unitTime = resultSet.getInt(ThrottlePolicyConstants.COLUMN_UNIT_TIME);
            quota = resultSet.getInt(ThrottlePolicyConstants.COLUMN_QUOTA);
            pipelineId = resultSet.getInt(ThrottlePolicyConstants.COLUMN_CONDITION_ID);
            description = resultSet.getString(ThrottlePolicyConstants.COLUMN_DESCRIPTION);
            if (PolicyConstants.REQUEST_COUNT_TYPE.equals(quotaPolicy.getType())) {
                RequestCountLimit requestCountLimit = new RequestCountLimit();
                requestCountLimit.setUnitTime(unitTime);
                requestCountLimit.setTimeUnit(timeUnit);
                requestCountLimit.setRequestCount(quota);
                quotaPolicy.setLimit(requestCountLimit);
            } else if (PolicyConstants.BANDWIDTH_TYPE.equals(quotaPolicy.getType())) {
                BandwidthLimit bandwidthLimit = new BandwidthLimit();
                bandwidthLimit.setUnitTime(unitTime);
                bandwidthLimit.setTimeUnit(timeUnit);
                bandwidthLimit.setDataUnit(quotaUnit);
                bandwidthLimit.setDataAmount(quota);
                quotaPolicy.setLimit(bandwidthLimit);
            }
            conditions = getConditions(pipelineId);
            pipeline.setConditions(conditions);
            pipeline.setQuotaPolicy(quotaPolicy);
            pipeline.setId(pipelineId);
            pipeline.setDescription(description);
            pipelines.add(pipeline);
        }
    } catch (SQLException e) {
        handleException("Failed to get pipelines for policyId: " + policyId, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(pipelinesStatement, connection, resultSet);
    }
    return pipelines;
}
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) RequestCountLimit(org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Pipeline(org.wso2.carbon.apimgt.api.model.policy.Pipeline) ResultSet(java.sql.ResultSet) QuotaPolicy(org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy) BandwidthLimit(org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)

Example 53 with Condition

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

the class ApiMgtDAO method setHeaderConditions.

/**
 * Add Header 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 setHeaderConditions(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_HEADER_CONDITIONS_SQL);
        conditionsStatement.setInt(1, pipelineId);
        resultSet = conditionsStatement.executeQuery();
        while (resultSet.next()) {
            HeaderCondition headerCondition = new HeaderCondition();
            headerCondition.setHeader(resultSet.getString(ThrottlePolicyConstants.COLUMN_HEADER_FIELD_NAME));
            headerCondition.setValue(resultSet.getString(ThrottlePolicyConstants.COLUMN_HEADER_FIELD_VALUE));
            headerCondition.setInvertCondition(resultSet.getBoolean(ThrottlePolicyConstants.COLUMN_IS_HEADER_FIELD_MAPPING));
            conditions.add(headerCondition);
        }
    } catch (SQLException e) {
        handleException("Failed to get header conditions for pipelineId: " + pipelineId, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(conditionsStatement, connection, resultSet);
    }
}
Also used : HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 54 with Condition

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

the class ApiMgtDAO method getBlockCondition.

/**
 * Get details of a block condition by Id
 *
 * @param conditionId id of the condition
 * @return Block conditoin represented by the UUID
 * @throws APIManagementException
 */
public BlockConditionsDTO getBlockCondition(int conditionId) throws APIManagementException {
    Connection connection = null;
    PreparedStatement selectPreparedStatement = null;
    ResultSet resultSet = null;
    BlockConditionsDTO blockCondition = null;
    try {
        String query = SQLConstants.ThrottleSQLConstants.GET_BLOCK_CONDITION_SQL;
        connection = APIMgtDBUtil.getConnection();
        connection.setAutoCommit(true);
        selectPreparedStatement = connection.prepareStatement(query);
        selectPreparedStatement.setInt(1, conditionId);
        resultSet = selectPreparedStatement.executeQuery();
        if (resultSet.next()) {
            blockCondition = new BlockConditionsDTO();
            blockCondition.setEnabled(resultSet.getBoolean("ENABLED"));
            blockCondition.setConditionType(resultSet.getString("TYPE"));
            blockCondition.setConditionValue(resultSet.getString("BLOCK_CONDITION"));
            blockCondition.setConditionId(conditionId);
            blockCondition.setTenantDomain(resultSet.getString("DOMAIN"));
            blockCondition.setUUID(resultSet.getString("UUID"));
        }
    } catch (SQLException e) {
        if (connection != null) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                handleException("Failed to rollback getting Block condition with id " + conditionId, ex);
            }
        }
        handleException("Failed to get Block condition with id " + conditionId, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(selectPreparedStatement, connection, resultSet);
    }
    return blockCondition;
}
Also used : BlockConditionsDTO(org.wso2.carbon.apimgt.api.model.BlockConditionsDTO) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 55 with Condition

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

the class AbstractAPIManagerTestCase method testGetTiersForTenant.

@Test
public void testGetTiersForTenant() throws APIManagementException {
    Mockito.when(privilegedCarbonContext.getTenantId()).thenReturn(-1234, -1, 1);
    Map<String, Tier> tierMap1 = new HashMap<String, Tier>();
    Map<String, Tier> tierMap2 = new HashMap<String, Tier>();
    Map<String, Tier> tierMap3 = new HashMap<String, Tier>();
    Tier tier1 = new Tier("tier1");
    Tier tier2 = new Tier("tier2");
    Tier tier3 = new Tier("tier3");
    tierMap1.put("Gold", tier1);
    tierMap2.put("Gold", tier1);
    tierMap2.put("Silver", tier2);
    tierMap3.put("Gold", tier1);
    tierMap3.put("Silver", tier2);
    tierMap3.put("Platinum", tier3);
    PowerMockito.mockStatic(APIUtil.class);
    PowerMockito.when(APIUtil.getTiers()).thenReturn(tierMap1);
    PowerMockito.when(APIUtil.getTiers(Mockito.anyInt())).thenReturn(tierMap2);
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(null, null, null, null);
    PowerMockito.when(APIUtil.getTiersFromPolicies(Mockito.anyString(), Mockito.anyInt())).thenReturn(tierMap1);
    Assert.assertEquals(abstractAPIManager.getTiers(SAMPLE_TENANT_DOMAIN_1).size(), 1);
    // verify next branch of if
    Assert.assertEquals(abstractAPIManager.getTiers(SAMPLE_TENANT_DOMAIN_1).size(), 1);
    // condition
    PowerMockito.when(APIUtil.getTiersFromPolicies(Mockito.anyString(), Mockito.anyInt())).thenReturn(tierMap2);
    Assert.assertEquals(abstractAPIManager.getTiers(SAMPLE_TENANT_DOMAIN_1).size(), 2);
    PowerMockito.when(APIUtil.getTiersFromPolicies(Mockito.anyString(), Mockito.anyInt())).thenReturn(tierMap3);
    Assert.assertEquals(abstractAPIManager.getTiers(SAMPLE_TENANT_DOMAIN_1).size(), 3);
}
Also used : Tier(org.wso2.carbon.apimgt.api.model.Tier) HashMap(java.util.HashMap) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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