use of org.wso2.carbon.apimgt.api.model.policy.IPCondition in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil method fromConditionToDTO.
/**
* Converts a Throttle Condition model object into a DTO
*
* @param condition Throttle condition model object
* @return Derived DTO object from the model object
* @throws UnsupportedThrottleConditionTypeException
*/
public static // .................
ThrottleConditionDTO fromConditionToDTO(// .................
Condition condition) throws UnsupportedThrottleConditionTypeException {
ThrottleConditionDTO throttleConditionDTO = new ThrottleConditionDTO();
throttleConditionDTO.setInvertCondition(condition.isInvertCondition());
if (condition instanceof IPCondition) {
throttleConditionDTO.setType(ThrottleConditionDTO.TypeEnum.IPCONDITION);
throttleConditionDTO.setIpCondition(fromIPConditionToDTO((IPCondition) condition));
} else if (condition instanceof HeaderCondition) {
throttleConditionDTO.setType(ThrottleConditionDTO.TypeEnum.HEADERCONDITION);
throttleConditionDTO.setHeaderCondition(fromHeaderConditionToDTO((HeaderCondition) condition));
} else if (condition instanceof QueryParameterCondition) {
throttleConditionDTO.setType(ThrottleConditionDTO.TypeEnum.QUERYPARAMETERCONDITION);
throttleConditionDTO.setQueryParameterCondition(fromQueryParameterConditionToDTO((QueryParameterCondition) condition));
} else if (condition instanceof JWTClaimsCondition) {
throttleConditionDTO.setType(ThrottleConditionDTO.TypeEnum.JWTCLAIMSCONDITION);
throttleConditionDTO.setJwtClaimsCondition(fromJWTClaimsConditionToDTO((JWTClaimsCondition) condition));
} else {
String msg = "Throttle Condition type " + condition.getClass().getName() + " is not supported";
throw new UnsupportedThrottleConditionTypeException(msg);
}
return throttleConditionDTO;
}
use of org.wso2.carbon.apimgt.api.model.policy.IPCondition in project carbon-apimgt by wso2.
the class ThrottlePolicyTemplateBuilder method getPolicyConditionJson.
/**
* Produces final condition inside a pipeline
*
* @param conditions set of conditions
* @return conditions as a JSON
*/
private static JSONObject getPolicyConditionJson(Set<Condition> conditions) {
JSONObject tempCondition = new JSONObject();
for (Condition condition : conditions) {
org.wso2.carbon.apimgt.api.model.policy.Condition mappedCondition = PolicyMappingUtil.mapCondition(condition);
JSONObject conditionJson;
if (tempCondition.containsKey(mappedCondition.getType().toLowerCase(Locale.ENGLISH))) {
conditionJson = (JSONObject) tempCondition.get(mappedCondition.getType().toLowerCase(Locale.ENGLISH));
} else {
conditionJson = new JSONObject();
}
tempCondition.put(mappedCondition.getType().toLowerCase(Locale.ENGLISH), conditionJson);
if (PolicyConstants.IP_SPECIFIC_TYPE.equals(mappedCondition.getType())) {
IPCondition ipCondition = (IPCondition) mappedCondition;
if (IPCondition.isIPv6Address(ipCondition.getSpecificIP())) {
conditionJson.put("specificIp", String.valueOf(APIUtil.ipToBigInteger(ipCondition.getSpecificIP())));
} else {
conditionJson.put("specificIp", ipCondition.ipToLong(ipCondition.getSpecificIP()));
}
} else if (PolicyConstants.IP_RANGE_TYPE.equals(mappedCondition.getType())) {
IPCondition ipRangeCondition = (IPCondition) mappedCondition;
if (IPCondition.isIPv6Address(ipRangeCondition.getStartingIP()) && IPCondition.isIPv6Address(ipRangeCondition.getEndingIP())) {
conditionJson.put("startingIp", String.valueOf(APIUtil.ipToBigInteger(ipRangeCondition.getStartingIP())));
conditionJson.put("endingIp", String.valueOf(APIUtil.ipToBigInteger(ipRangeCondition.getEndingIP())));
} else {
conditionJson.put("startingIp", ipRangeCondition.ipToLong(ipRangeCondition.getStartingIP()));
conditionJson.put("endingIp", ipRangeCondition.ipToLong(ipRangeCondition.getEndingIP()));
}
} else if (mappedCondition instanceof QueryParameterCondition) {
QueryParameterCondition queryParameterCondition = (QueryParameterCondition) mappedCondition;
JSONObject values;
if (conditionJson.containsKey("values")) {
values = (JSONObject) conditionJson.get("values");
} else {
values = new JSONObject();
conditionJson.put("values", values);
}
values.put(queryParameterCondition.getParameter(), queryParameterCondition.getValue());
} else if (mappedCondition instanceof HeaderCondition) {
HeaderCondition headerCondition = (HeaderCondition) mappedCondition;
JSONObject values;
if (conditionJson.containsKey("values")) {
values = (JSONObject) conditionJson.get("values");
} else {
values = new JSONObject();
conditionJson.put("values", values);
}
values.put(headerCondition.getHeaderName(), headerCondition.getValue());
} else if (mappedCondition instanceof JWTClaimsCondition) {
JWTClaimsCondition jwtClaimsCondition = (JWTClaimsCondition) mappedCondition;
JSONObject values;
if (conditionJson.containsKey("values")) {
values = (JSONObject) conditionJson.get("values");
} else {
values = new JSONObject();
conditionJson.put("values", values);
}
values.put(jwtClaimsCondition.getClaimUrl(), jwtClaimsCondition.getAttribute());
}
conditionJson.put("invert", mappedCondition.isInvertCondition());
}
return tempCondition;
}
use of org.wso2.carbon.apimgt.api.model.policy.IPCondition in project carbon-apimgt by wso2.
the class PolicyMappingUtil method mapCondition.
/**
* Map a org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition to a
* org.wso2.carbon.apimgt.api.model.policy.Condition
*
* @param conditionDTO org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition
* @return org.wso2.carbon.apimgt.api.model.policy.Condition object
*/
public static Condition mapCondition(org.wso2.carbon.apimgt.throttle.policy.deployer.dto.Condition conditionDTO) {
switch(conditionDTO.getConditionType()) {
case PolicyConstants.IP_RANGE_TYPE:
IPCondition ipRangeCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
ipRangeCondition.setInvertCondition(conditionDTO.isInverted());
ipRangeCondition.setStartingIP(conditionDTO.getName());
ipRangeCondition.setEndingIP(conditionDTO.getValue());
return ipRangeCondition;
case PolicyConstants.IP_SPECIFIC_TYPE:
IPCondition ipSpecificCondition = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
ipSpecificCondition.setInvertCondition(conditionDTO.isInverted());
ipSpecificCondition.setSpecificIP(conditionDTO.getValue());
return ipSpecificCondition;
case PolicyConstants.HEADER_TYPE:
HeaderCondition headerCondition = new HeaderCondition();
headerCondition.setInvertCondition(conditionDTO.isInverted());
headerCondition.setHeader(conditionDTO.getName());
headerCondition.setValue(conditionDTO.getValue());
return headerCondition;
case PolicyConstants.JWT_CLAIMS_TYPE:
JWTClaimsCondition jwtClaimsCondition = new JWTClaimsCondition();
jwtClaimsCondition.setInvertCondition(conditionDTO.isInverted());
jwtClaimsCondition.setClaimUrl(conditionDTO.getName());
jwtClaimsCondition.setAttribute(conditionDTO.getValue());
return jwtClaimsCondition;
case PolicyConstants.QUERY_PARAMETER_TYPE:
QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
queryParameterCondition.setInvertCondition(conditionDTO.isInverted());
queryParameterCondition.setParameter(conditionDTO.getName());
queryParameterCondition.setValue(conditionDTO.getValue());
return queryParameterCondition;
default:
return null;
}
}
use of org.wso2.carbon.apimgt.api.model.policy.IPCondition in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createNewIPRangePipeline.
/**
* Creates a new {@link Pipeline} instance
*
* @return created Pipeline instance
*/
public static Pipeline createNewIPRangePipeline() {
IPCondition ipCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
ipCondition.setStartingIP("10.100.0.105");
ipCondition.setEndingIP("10.100.0.115");
Pipeline pipeline = new Pipeline();
RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 1, 1000);
QuotaPolicy quotaPolicy = new QuotaPolicy();
quotaPolicy.setType(REQUEST_COUNT_TYPE);
quotaPolicy.setLimit(requestCountLimit);
pipeline.setQuotaPolicy(quotaPolicy);
pipeline.setConditions(Arrays.asList(ipCondition));
return pipeline;
}
use of org.wso2.carbon.apimgt.api.model.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.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(PolicyConstants.REQUEST_COUNT_TYPE);
quotaPolicy2.setLimit(requestCountLimit);
Pipeline pipeline2 = new Pipeline();
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;
}
Aggregations