use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method getIPCondition.
private ConditionDto getIPCondition(boolean invert) {
ConditionDto conditionDto = new ConditionDto();
conditionDto.setIpCondition(new ConditionDto.IPCondition(APIUtil.ipToBigInteger("127.0.0.1"), invert));
return conditionDto;
}
use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method testApplicabilityOfNotMatchingHeaderCondition.
@Test
public void testApplicabilityOfNotMatchingHeaderCondition() {
ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
conditionGroupDTO.setConditionGroupId("HeaderConditionGroup");
ConditionDTO notMatchingCondition = new ConditionDTO();
notMatchingCondition.setConditionType("Header");
notMatchingCondition.setConditionName("host");
notMatchingCondition.setConditionValue("org.ibm.com");
ConditionDTO[] conditionDTOS = { notMatchingCondition };
conditionGroupDTO.setConditions(conditionDTOS);
ConditionGroupDTO[] conditionGroupDTOS = { conditionGroupDTO };
AuthenticationContext authenticationContext = new AuthenticationContext();
authenticationContext.setCallerToken(JWTToken);
MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
Map map = new TreeMap();
map.put("host", "org.wso2.com");
((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, map);
List<ConditionGroupDTO> matchingConditionGroups = throttleConditionEvaluator.getApplicableConditions(messageContext, authenticationContext, conditionGroupDTOS);
Assert.assertNull(matchingConditionGroups.get(0));
}
use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method testGetThrottledInConditionWithComplexConditionNegative.
@Test
public void testGetThrottledInConditionWithComplexConditionNegative() {
ThrottleProperties throttleProperties = new ThrottleProperties();
throttleProperties.setEnableHeaderConditions(true);
ServiceReferenceHolder.getInstance().setThrottleProperties(throttleProperties);
MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty("REST_URL_POSTFIX", "/temperature?abc=cde&bcd=xyz");
Map<String, List<ConditionDto>> conditionMap = new HashMap<>();
conditionMap.put("condition1", Arrays.asList(new ConditionDto[] { getComplexCondition1() }));
conditionMap.put("default", Arrays.asList(new ConditionDto[] { getComplexCondition1(), getComplexCondition2() }));
String condition = throttleConditionEvaluator.getThrottledInCondition(messageContext, null, conditionMap);
Assert.assertEquals(null, condition);
}
use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.
the class APIUtil method extractConditionDto.
public static List<ConditionDto> extractConditionDto(String base64EncodedString) throws ParseException {
List<ConditionDto> conditionDtoList = new ArrayList<>();
String base64Decoded = new String(Base64.decodeBase64(base64EncodedString));
JSONArray conditionJsonArray = (JSONArray) new JSONParser().parse(base64Decoded);
for (Object conditionJson : conditionJsonArray) {
ConditionDto conditionDto = new ConditionDto();
JSONObject conditionJsonObject = (JSONObject) conditionJson;
if (conditionJsonObject.containsKey(PolicyConstants.IP_SPECIFIC_TYPE.toLowerCase())) {
JSONObject ipSpecificCondition = (JSONObject) conditionJsonObject.get(PolicyConstants.IP_SPECIFIC_TYPE.toLowerCase());
ConditionDto.IPCondition ipCondition = new Gson().fromJson(ipSpecificCondition.toJSONString(), ConditionDto.IPCondition.class);
conditionDto.setIpCondition(ipCondition);
} else if (conditionJsonObject.containsKey(PolicyConstants.IP_RANGE_TYPE.toLowerCase())) {
JSONObject ipRangeCondition = (JSONObject) conditionJsonObject.get(PolicyConstants.IP_RANGE_TYPE.toLowerCase());
ConditionDto.IPCondition ipCondition = new Gson().fromJson(ipRangeCondition.toJSONString(), ConditionDto.IPCondition.class);
conditionDto.setIpRangeCondition(ipCondition);
}
if (conditionJsonObject.containsKey(PolicyConstants.JWT_CLAIMS_TYPE.toLowerCase())) {
JSONObject jwtClaimConditions = (JSONObject) conditionJsonObject.get(PolicyConstants.JWT_CLAIMS_TYPE.toLowerCase());
ConditionDto.JWTClaimConditions jwtClaimCondition = new Gson().fromJson(jwtClaimConditions.toJSONString(), ConditionDto.JWTClaimConditions.class);
conditionDto.setJwtClaimConditions(jwtClaimCondition);
}
if (conditionJsonObject.containsKey(PolicyConstants.HEADER_TYPE.toLowerCase())) {
JSONObject headerConditionJson = (JSONObject) conditionJsonObject.get(PolicyConstants.HEADER_TYPE.toLowerCase());
ConditionDto.HeaderConditions headerConditions = new Gson().fromJson(headerConditionJson.toJSONString(), ConditionDto.HeaderConditions.class);
conditionDto.setHeaderConditions(headerConditions);
}
if (conditionJsonObject.containsKey(PolicyConstants.QUERY_PARAMETER_TYPE.toLowerCase())) {
JSONObject queryParamConditionJson = (JSONObject) conditionJsonObject.get(PolicyConstants.QUERY_PARAMETER_TYPE.toLowerCase());
ConditionDto.QueryParamConditions queryParamCondition = new Gson().fromJson(queryParamConditionJson.toJSONString(), ConditionDto.QueryParamConditions.class);
conditionDto.setQueryParameterConditions(queryParamCondition);
}
conditionDtoList.add(conditionDto);
}
conditionDtoList.sort(new Comparator<ConditionDto>() {
@Override
public int compare(ConditionDto o1, ConditionDto o2) {
if (o1.getIpCondition() != null && o2.getIpCondition() == null) {
return -1;
} else if (o1.getIpCondition() == null && o2.getIpCondition() != null) {
return 1;
} else {
if (o1.getIpRangeCondition() != null && o2.getIpRangeCondition() == null) {
return -1;
} else if (o1.getIpRangeCondition() == null && o2.getIpRangeCondition() != null) {
return 1;
} else {
if (o1.getHeaderConditions() != null && o2.getHeaderConditions() == null) {
return -1;
} else if (o1.getHeaderConditions() == null && o2.getHeaderConditions() != null) {
return 1;
} else {
if (o1.getQueryParameterConditions() != null && o2.getQueryParameterConditions() == null) {
return -1;
} else if (o1.getQueryParameterConditions() == null && o2.getQueryParameterConditions() != null) {
return 1;
} else {
if (o1.getJwtClaimConditions() != null && o2.getJwtClaimConditions() == null) {
return -1;
} else if (o1.getJwtClaimConditions() == null && o2.getJwtClaimConditions() != null) {
return 1;
}
}
}
}
}
return 0;
}
});
return conditionDtoList;
}
use of org.wso2.carbon.apimgt.impl.dto.ConditionDto in project carbon-apimgt by wso2.
the class APIUtilTest method testGetConditionDtoListWithHavingQueryParamConditionOnly.
@Test
public void testGetConditionDtoListWithHavingQueryParamConditionOnly() throws ParseException {
String base64EncodedString = "W3sicXVlcnlwYXJhbWV0ZXJ0eXBlIjp7ImludmVydCI6ZmFsc2UsInZhbHVlcyI6eyJhYmMiOiJkZWYifX19XQo=";
List<ConditionDto> conditionDtoList = APIUtil.extractConditionDto(base64EncodedString);
Assert.assertEquals(conditionDtoList.size(), 1);
ConditionDto conditionDto = conditionDtoList.get(0);
Assert.assertNotNull(conditionDto.getQueryParameterConditions());
Assert.assertEquals(conditionDto.getQueryParameterConditions().getValues().size(), 1);
}
Aggregations