use of org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition 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.JWTClaimsCondition 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.JWTClaimsCondition 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;
}
use of org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil method fromJWTClaimsConditionToDTO.
/**
* Converts a JWT Claims Condition model object into a DTO
*
* @param condition JWT Claims Condition model object
* @return DTO object that was derived from JWT Claims Condition model object
*/
public static ThrottleConditionDTO fromJWTClaimsConditionToDTO(JWTClaimsCondition condition) {
ThrottleConditionDTO throttleConditionDTO = new ThrottleConditionDTO();
throttleConditionDTO.setType(PolicyConstants.JWT_CLAIMS_CONDITION_TYPE);
throttleConditionDTO.setJwtClaimsCondition(new JWTClaimsConditionDTO());
throttleConditionDTO = updateFieldsFromConditionToDTO(condition, throttleConditionDTO);
throttleConditionDTO.getJwtClaimsCondition().setClaimUrl(condition.getClaimUrl());
throttleConditionDTO.getJwtClaimsCondition().setAttribute(condition.getAttribute());
return throttleConditionDTO;
}
use of org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil method fromDTOToJWTClaimsCondition.
/**
* Converts a JWT Claims Condition DTO object into a model object
*
* @param dto JWT Claims Condition DTO object
* @return JWT Claims Condition model object derived from JWT Claims Condition DTO
*/
public static JWTClaimsCondition fromDTOToJWTClaimsCondition(ThrottleConditionDTO dto) {
JWTClaimsCondition jwtClaimsCondition = new JWTClaimsCondition();
jwtClaimsCondition = updateFieldsFromDTOToCondition(dto, jwtClaimsCondition);
jwtClaimsCondition.setAttribute(dto.getJwtClaimsCondition().getAttribute());
jwtClaimsCondition.setClaimUrl(dto.getJwtClaimsCondition().getClaimUrl());
return jwtClaimsCondition;
}
Aggregations