use of org.wso2.carbon.apimgt.core.models.policy.IPCondition in project carbon-apimgt by wso2.
the class PolicyDAOImpl method setIPCondition.
/**
* Retrieve IP condition from pipeline
*
* @param pipelineId id of the pipeline to get ip condition
* @param conditions condition list to add each ip condition
* @param connection connection to db
* @throws SQLException If error occurred while getting ip condition form db
*/
private void setIPCondition(int pipelineId, ArrayList<Condition> conditions, Connection connection) throws SQLException {
final String sqlQuery = "SELECT " + "STARTING_IP, " + "ENDING_IP, " + "SPECIFIC_IP,WITHIN_IP_RANGE " + "FROM " + "" + "AM_IP_CONDITION " + "WHERE " + "CONDITION_GROUP_ID = ? ";
String startingIP;
String endingIP;
String specificIP;
boolean invert;
try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
preparedStatement.setInt(1, pipelineId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
startingIP = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_STARTING_IP);
endingIP = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_ENDING_IP);
specificIP = resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_SPECIFIC_IP);
invert = resultSet.getBoolean(APIMgtConstants.ThrottlePolicyConstants.COLUMN_WITHIN_IP_RANGE);
if (specificIP != null && !"".equals(specificIP)) {
IPCondition ipCondition = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
ipCondition.setSpecificIP(specificIP);
ipCondition.setInvertCondition(invert);
conditions.add(ipCondition);
} else if (startingIP != null && !"".equals(startingIP)) {
/*
Assumes availability of starting ip means ip range is enforced.
Therefore availability of ending ip is not checked.
*/
IPCondition ipRangeCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
ipRangeCondition.setStartingIP(startingIP);
ipRangeCondition.setEndingIP(endingIP);
ipRangeCondition.setInvertCondition(invert);
conditions.add(ipRangeCondition);
}
}
}
}
}
use of org.wso2.carbon.apimgt.core.models.policy.IPCondition in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil method fromDTOToIPCondition.
/**
* Converts a IP Condition DTO object into a model object
*
* @param dto IP Condition DTO object
* @return IP Condition model object derived from DTO
*/
public static IPCondition fromDTOToIPCondition(ThrottleConditionDTO dto) throws UnsupportedThrottleConditionTypeException {
String ipConditionType = mapIPConditionTypeFromDTOToModel(dto.getIpCondition().getIpConditionType());
IPCondition ipCondition = new IPCondition(ipConditionType);
ipCondition = updateFieldsFromDTOToCondition(dto, ipCondition);
ipCondition.setSpecificIP(dto.getIpCondition().getSpecificIP());
ipCondition.setStartingIP(dto.getIpCondition().getStartingIP());
ipCondition.setEndingIP(dto.getIpCondition().getEndingIP());
return ipCondition;
}
use of org.wso2.carbon.apimgt.core.models.policy.IPCondition 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;
}
use of org.wso2.carbon.apimgt.core.models.policy.IPCondition in project carbon-apimgt by wso2.
the class BlockingConditionMappingUtilTestCase method fromBlockingConditionDTOToBlockConditionTest.
@Test(description = "From Blocking Condition DTO to Model")
public void fromBlockingConditionDTOToBlockConditionTest() throws Exception {
BlockingConditionDTO dto = new BlockingConditionDTO();
dto.setConditionId(UUID.randomUUID().toString());
dto.setConditionType(BLOCKING_CONDITION_IP_RANGE);
IPConditionDTO ipCondition = new IPConditionDTO();
ipCondition.setStartingIP("12.32.45.3");
ipCondition.setEndingIP("12.32.45.31");
dto.setIpCondition(ipCondition);
dto.setStatus(true);
BlockConditions conditions = BlockingConditionMappingUtil.fromBlockingConditionDTOToBlockCondition(dto);
Assert.assertNotNull(conditions);
Assert.assertEquals(BLOCKING_CONDITION_IP_RANGE, conditions.getConditionType());
Assert.assertEquals(conditions.getStartingIP(), dto.getIpCondition().getStartingIP());
Assert.assertEquals(conditions.getEndingIP(), dto.getIpCondition().getEndingIP());
}
use of org.wso2.carbon.apimgt.core.models.policy.IPCondition in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtilTestCase method fromIPRangeConditionDtoToIPConditionModelTest.
@Test(description = "Convert IP range IPCondition DTO to IPCondition Model object")
public void fromIPRangeConditionDtoToIPConditionModelTest() throws Exception {
ThrottleConditionDTO throttleConditionDTO = new ThrottleConditionDTO();
throttleConditionDTO.setType(PolicyConstants.IP_CONDITION_TYPE);
IPConditionDTO ipConditionDTO = new IPConditionDTO();
ipConditionDTO.setIpConditionType(IP_RANGE_TYPE);
ipConditionDTO.setStartingIP("10.100.0.158");
ipConditionDTO.setEndingIP("10.100.0.178");
throttleConditionDTO.setIpCondition(ipConditionDTO);
IPCondition condition = (IPCondition) CommonThrottleMappingUtil.fromDTOToCondition(throttleConditionDTO);
Assert.assertNotNull(condition);
assertEquals(condition.getStartingIP(), "10.100.0.158");
assertEquals(condition.getEndingIP(), "10.100.0.178");
assertEquals(condition.getType(), IP_RANGE_TYPE);
}
Aggregations