Search in sources :

Example 31 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class MappingUtil method fromBlockConditionListToListDTO.

/**
 * Converts a List of Block Condition in to REST API LIST DTO Object.
 *
 * @param blockConditionList A List of Block Conditions
 * @return REST API List DTO object derived from Block Condition list
 */
public static BlockingConditionListDTO fromBlockConditionListToListDTO(List<BlockConditions> blockConditionList) {
    BlockingConditionListDTO listDTO = new BlockingConditionListDTO();
    List<BlockingConditionDTO> blockingConditionDTOList = new ArrayList<>();
    if (blockConditionList != null) {
        for (BlockConditions blockCondition : blockConditionList) {
            BlockingConditionDTO dto = fromBlockingConditionToDTO(blockCondition);
            blockingConditionDTOList.add(dto);
        }
    }
    listDTO.setCount(blockingConditionDTOList.size());
    listDTO.setList(blockingConditionDTOList);
    return listDTO;
}
Also used : BlockingConditionListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) ArrayList(java.util.ArrayList) BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionDTO)

Example 32 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class MappingUtil method fromBlockingConditionToDTO.

/**
 * Converts a single Block Condition model object into REST API DTO object.
 *
 * @param blockCondition Block condition model object
 * @return Block condition DTO object derived from block condition model object
 */
public static BlockingConditionDTO fromBlockingConditionToDTO(BlockConditions blockCondition) {
    if (blockCondition.getUuid() == null) {
        return null;
    }
    BlockingConditionDTO dto = new BlockingConditionDTO();
    dto.setUuid(blockCondition.getUuid());
    dto.setConditionType(blockCondition.getConditionType());
    dto.setEnabled(blockCondition.isEnabled());
    if (blockCondition.getConditionType().equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE)) {
        dto.setStartingIP(APIUtils.ipToLong(blockCondition.getStartingIP()));
        dto.setEndingIP(APIUtils.ipToLong(blockCondition.getEndingIP()));
    }
    String conditionValue = blockCondition.getConditionValue();
    if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP.equals(blockCondition.getConditionType())) {
        dto.setFixedIp(APIUtils.ipToLong(conditionValue));
    }
    dto.setConditionValue(conditionValue);
    return dto;
}
Also used : BlockingConditionDTO(org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionDTO)

Example 33 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class PolicyDAOImpl method isBlockConditionExist.

/**
 * Check if a blocking condition already exists.
 *
 * @param blockConditions BlockConditions object to be added
 * @return true/false depending on the success
 * @throws APIMgtDAOException If failed to check if block condition exist
 */
private boolean isBlockConditionExist(BlockConditions blockConditions) throws APIMgtDAOException {
    boolean status = false;
    if (blockConditions.getConditionType().equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE)) {
        String isExistQuery = "SELECT STARTING_IP, ENDING_IP FROM AM_IP_RANGE_CONDITION WHERE STARTING_IP =? " + "AND ENDING_IP =?";
        try (Connection connection = DAOUtil.getConnection();
            PreparedStatement checkIsExistPreparedStatement = connection.prepareStatement(isExistQuery)) {
            checkIsExistPreparedStatement.setString(1, blockConditions.getStartingIP());
            checkIsExistPreparedStatement.setString(2, blockConditions.getEndingIP());
            try (ResultSet checkIsResultSet = checkIsExistPreparedStatement.executeQuery()) {
                if (checkIsResultSet.next()) {
                    status = true;
                }
            }
        } catch (SQLException e) {
            String msg = DAOUtil.DAO_ERROR_PREFIX + "checking if the IP range blacklist condition exists with starting IP: " + blockConditions.getStartingIP() + ", ending IP: " + blockConditions.getEndingIP();
            throw new APIMgtDAOException(msg, e);
        }
    } else {
        String isExistQuery = "SELECT CONDITION_ID,TYPE,VALUE,ENABLED,UUID FROM AM_BLOCK_CONDITIONS WHERE TYPE =? " + "AND VALUE =?";
        try (Connection connection = DAOUtil.getConnection();
            PreparedStatement checkIsExistPreparedStatement = connection.prepareStatement(isExistQuery)) {
            connection.setAutoCommit(false);
            checkIsExistPreparedStatement.setString(1, blockConditions.getConditionType());
            checkIsExistPreparedStatement.setString(2, blockConditions.getConditionValue());
            try (ResultSet checkIsResultSet = checkIsExistPreparedStatement.executeQuery()) {
                if (checkIsResultSet.next()) {
                    status = true;
                }
            }
        } catch (SQLException e) {
            String msg = DAOUtil.DAO_ERROR_PREFIX + "checking if the Block Condition Exist with condition type: " + blockConditions.getConditionType() + ", condition value: " + blockConditions.getConditionValue();
            throw new APIMgtDAOException(msg, e);
        }
    }
    return status;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 34 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class PolicyDAOImpl 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 SQLException
 */
private void setQueryParameterConditions(int pipelineId, ArrayList<Condition> conditions, Connection connection) throws SQLException {
    final String query = "SELECT " + "PARAMETER_NAME,PARAMETER_VALUE , IS_PARAM_MAPPING " + "FROM " + "AM_QUERY_PARAMETER_CONDITION " + "WHERE " + "CONDITION_GROUP_ID =?";
    try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setInt(1, pipelineId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
                queryParameterCondition.setParameter(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_PARAMETER_NAME));
                queryParameterCondition.setValue(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_PARAMETER_VALUE));
                queryParameterCondition.setInvertCondition(resultSet.getBoolean(APIMgtConstants.ThrottlePolicyConstants.COLUMN_IS_PARAM_MAPPING));
                conditions.add(queryParameterCondition);
            }
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition)

Example 35 with Condition

use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.

the class PolicyDAOImpl 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 SQLException
 */
private ArrayList<Pipeline> getPipelines(String policyId, Connection connection) throws SQLException {
    ArrayList<Pipeline> pipelines = new ArrayList<>();
    final String sqlQuery = "SELECT CONDITION_GROUP_ID,QUOTA_TYPE,QUOTA,QUOTA_UNIT,UNIT_TIME,TIME_UNIT," + "DESCRIPTION FROM AM_CONDITION_GROUP WHERE UUID =?";
    try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        int unitTime;
        int quota;
        int pipelineId;
        String timeUnit;
        String quotaUnit;
        String description;
        preparedStatement.setString(1, policyId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                Pipeline pipeline = new Pipeline();
                ArrayList<Condition> conditions;
                QuotaPolicy quotaPolicy = new QuotaPolicy();
                quotaPolicy.setType(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA_POLICY_TYPE));
                timeUnit = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_TIME_UNIT);
                quotaUnit = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA_UNIT);
                unitTime = resultSet.getInt(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UNIT_TIME);
                quota = resultSet.getInt(APIMgtConstants.ThrottlePolicyConstants.COLUMN_QUOTA);
                pipelineId = resultSet.getInt(APIMgtConstants.ThrottlePolicyConstants.COLUMN_CONDITION_ID);
                description = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_DESCRIPTION);
                if (PolicyConstants.REQUEST_COUNT_TYPE.equals(quotaPolicy.getType())) {
                    RequestCountLimit requestCountLimit = new RequestCountLimit(timeUnit, unitTime, quota);
                    quotaPolicy.setLimit(requestCountLimit);
                } else if (PolicyConstants.BANDWIDTH_TYPE.equals(quotaPolicy.getType())) {
                    BandwidthLimit bandwidthLimit = new BandwidthLimit(timeUnit, unitTime, quota, quotaUnit);
                    quotaPolicy.setLimit(bandwidthLimit);
                }
                conditions = getConditions(pipelineId, connection);
                pipeline.setConditions(conditions);
                pipeline.setQuotaPolicy(quotaPolicy);
                pipeline.setId(pipelineId);
                pipeline.setDescription(description);
                pipelines.add(pipeline);
            }
        }
    }
    return pipelines;
}
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) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Aggregations

BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)18 Test (org.testng.annotations.Test)16 PreparedStatement (java.sql.PreparedStatement)12 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)11 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)10 ResultSet (java.sql.ResultSet)9 IPCondition (org.wso2.carbon.apimgt.core.models.policy.IPCondition)9 HeaderCondition (org.wso2.carbon.apimgt.core.models.policy.HeaderCondition)8 JWTClaimsCondition (org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition)8 QueryParameterCondition (org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition)8 ArrayList (java.util.ArrayList)7 Condition (org.wso2.carbon.apimgt.core.models.policy.Condition)7 Pipeline (org.wso2.carbon.apimgt.core.models.policy.Pipeline)7 Connection (java.sql.Connection)6 SQLException (java.sql.SQLException)6 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)6 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)6 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO)5 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)4 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)4