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;
}
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;
}
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;
}
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);
}
}
}
}
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;
}
Aggregations