use of org.wso2.carbon.apimgt.api.dto.ConditionDTO in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method testGetThrottledInConditionWithJWTConditionInvert.
@Test
public void testGetThrottledInConditionWithJWTConditionInvert() {
ThrottleProperties throttleProperties = new ThrottleProperties();
throttleProperties.setEnableJwtConditions(true);
String jwt = "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3c28yLm9yZy9wcm9kdWN0cy9hbSIsImV4cCI6MTM0NTE4MzQ5MjE4MSwiaHR0cDov" + "L3dzbzIub3JnL2NsYWltcy9hYmMiOiJjZGUiLCJodHRwOi8vd3NvMi5vcmcvY2xhaW1zL2JjZCI6Inh5eiJ9.9zGU062DJ5mQ5hne" + "41h4IRpLbaY_b5thRxb3feebOcA";
AuthenticationContext authenticationContext = new AuthenticationContext();
authenticationContext.setCallerToken(jwt);
ServiceReferenceHolder.getInstance().setThrottleProperties(throttleProperties);
MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
Map<String, List<ConditionDto>> conditionMap = new HashMap<>();
conditionMap.put("condition1", Arrays.asList(new ConditionDto[] { getJWTCondition(true) }));
conditionMap.put("default", Arrays.asList(new ConditionDto[] { getJWTCondition(true) }));
String condition = throttleConditionEvaluator.getThrottledInCondition(messageContext, authenticationContext, conditionMap);
Assert.assertEquals(condition, "default");
}
use of org.wso2.carbon.apimgt.api.dto.ConditionDTO in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluator method isThrottledWithinCondition.
private boolean isThrottledWithinCondition(MessageContext axis2MessageContext, AuthenticationContext authContext, List<ConditionDto> conditionDtoList) {
ThrottleProperties throttleProperties = ServiceReferenceHolder.getInstance().getThrottleProperties();
boolean status = true;
for (ConditionDto condition : conditionDtoList) {
status = true;
if (condition.getIpCondition() != null) {
if (!isMatchingIP(axis2MessageContext, condition.getIpCondition())) {
status = false;
}
} else if (condition.getIpRangeCondition() != null) {
if (!isWithinIP(axis2MessageContext, condition.getIpRangeCondition())) {
status = false;
}
}
if (condition.getHeaderConditions() != null && throttleProperties.isEnableHeaderConditions() && !condition.getHeaderConditions().getValues().isEmpty()) {
if (!isHeaderPresent(axis2MessageContext, condition.getHeaderConditions())) {
status = false;
}
}
if (condition.getJwtClaimConditions() != null && throttleProperties.isEnableJwtConditions() && !condition.getJwtClaimConditions().getValues().isEmpty()) {
if (!isJWTClaimPresent(authContext, condition.getJwtClaimConditions())) {
status = false;
}
}
if (condition.getQueryParameterConditions() != null && throttleProperties.isEnableQueryParamConditions() && !condition.getQueryParameterConditions().getValues().isEmpty()) {
if (!isQueryParamPresent(axis2MessageContext, condition.getQueryParameterConditions())) {
status = false;
}
}
if (status) {
break;
}
}
return status;
}
use of org.wso2.carbon.apimgt.api.dto.ConditionDTO in project carbon-apimgt by wso2.
the class SubscriptionValidationDataUtil method fromApiPolicyToApiPolicyListDTO.
public static ApiPolicyListDTO fromApiPolicyToApiPolicyListDTO(List<APIPolicy> model) {
ApiPolicyListDTO apiPolicyListDTO = new ApiPolicyListDTO();
if (model != null) {
for (APIPolicy apiPolicyModel : model) {
ApiPolicyDTO policyDTO = new ApiPolicyDTO();
policyDTO.setName(apiPolicyModel.getName());
policyDTO.setQuotaType(apiPolicyModel.getQuotaType());
policyDTO.setTenantId(apiPolicyModel.getTenantId());
policyDTO.setTenantDomain(apiPolicyModel.getTenantDomain());
policyDTO.setApplicableLevel(apiPolicyModel.getApplicableLevel());
policyDTO.setDefaultLimit(getThrottleLimitDTO(apiPolicyModel));
apiPolicyListDTO.getList().add(policyDTO);
List<APIPolicyConditionGroup> retrievedGroups = apiPolicyModel.getConditionGroups();
List<ApiPolicyConditionGroupDTO> condGroups = new ArrayList<ApiPolicyConditionGroupDTO>();
for (APIPolicyConditionGroup retGroup : retrievedGroups) {
ApiPolicyConditionGroupDTO group = new ApiPolicyConditionGroupDTO();
group.setConditionGroupId(retGroup.getConditionGroupId());
group.setQuotaType(retGroup.getQuotaType());
group.setDefaultLimit(getThrottleLimitDTO(retGroup));
group.setPolicyId(retGroup.getPolicyId());
List<org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO> condition = new ArrayList<org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO>();
List<ConditionDTO> retrievedConditions = retGroup.getConditionDTOS();
for (ConditionDTO retrievedCondition : retrievedConditions) {
org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO conditionDTO = new org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO();
conditionDTO.setConditionType(retrievedCondition.getConditionType());
conditionDTO.setIsInverted(retrievedCondition.isInverted());
conditionDTO.setName(retrievedCondition.getConditionName());
conditionDTO.setValue(retrievedCondition.getConditionValue());
condition.add(conditionDTO);
}
group.setCondition(condition);
condGroups.add(group);
}
policyDTO.setConditionGroups(condGroups);
}
apiPolicyListDTO.setCount(model.size());
} else {
apiPolicyListDTO.setCount(0);
}
return apiPolicyListDTO;
}
use of org.wso2.carbon.apimgt.api.dto.ConditionDTO 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.dto.ConditionDTO in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method testApplicabilityOfMatchingIPSpecificCondition.
@Test
public void testApplicabilityOfMatchingIPSpecificCondition() {
ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
conditionGroupDTO.setConditionGroupId("IPSpecificConditionGroup");
ConditionDTO matchingCondition = new ConditionDTO();
matchingCondition.setConditionType("IPSpecific");
matchingCondition.setConditionValue("127.0.0.1");
ConditionDTO[] conditionDTOS = { matchingCondition };
conditionGroupDTO.setConditions(conditionDTOS);
ConditionGroupDTO[] conditionGroupDTOS = { conditionGroupDTO };
List<ConditionGroupDTO> matchingConditionGroups = throttleConditionEvaluator.getApplicableConditions(TestUtils.getMessageContext(apiContext, apiVersion), new AuthenticationContext(), conditionGroupDTOS);
Assert.assertEquals(matchingConditionGroups.get(0).getConditionGroupId(), "IPSpecificConditionGroup");
}
Aggregations