use of org.wso2.carbon.apimgt.api.model.policy.IPCondition in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method getComplexCondition2.
private ConditionDto getComplexCondition2() {
ConditionDto conditionDto = new ConditionDto();
ConditionDto.IPCondition ipCondition = new ConditionDto.IPCondition(APIUtil.ipToBigInteger("127.0.0.1"), false);
conditionDto.setIpCondition(ipCondition);
ConditionDto.QueryParamConditions queryParamConditions = new ConditionDto.QueryParamConditions();
Map map = new HashMap();
map.put("abc", "cde");
map.put("bcd", "xyz");
queryParamConditions.setValues(map);
conditionDto.setQueryParameterConditions(queryParamConditions);
return conditionDto;
}
use of org.wso2.carbon.apimgt.api.model.policy.IPCondition 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.api.model.policy.IPCondition in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method getComplexCondition1.
private ConditionDto getComplexCondition1() {
ConditionDto conditionDto = new ConditionDto();
ConditionDto.IPCondition ipCondition = new ConditionDto.IPCondition(APIUtil.ipToBigInteger("127.0.0.1"), false);
conditionDto.setIpCondition(ipCondition);
ConditionDto.HeaderConditions headerConditions = new ConditionDto.HeaderConditions();
Map map = new HashMap();
map.put("abc", "cde");
map.put("bcd", "xyz");
headerConditions.setValues(map);
conditionDto.setHeaderConditions(headerConditions);
return conditionDto;
}
use of org.wso2.carbon.apimgt.api.model.policy.IPCondition in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil method fromDTOListToConditionList.
/**
* Converts a list of Throttle Condition DTOs into a list of Condition model objects
*
* @param throttleConditionDTOs list of Throttle Condition DTOs
* @return Derived list of Condition model objects from Throttle Condition DTOs
* @throws UnsupportedThrottleConditionTypeException
*/
public static List<Condition> fromDTOListToConditionList(List<ThrottleConditionDTO> throttleConditionDTOs) throws UnsupportedThrottleConditionTypeException {
List<Condition> conditions = new ArrayList<>();
String errorMessage;
if (throttleConditionDTOs != null) {
for (ThrottleConditionDTO dto : throttleConditionDTOs) {
ThrottleConditionDTO.TypeEnum conditionType = dto.getType();
if (conditionType != null) {
switch(conditionType) {
case HEADERCONDITION:
{
if (dto.getHeaderCondition() != null) {
conditions.add(fromDTOToHeaderCondition(dto.getHeaderCondition(), dto.isInvertCondition()));
} else {
errorMessage = RestApiAdminUtils.constructMissingThrottleObjectErrorMessage(ThrottleConditionDTO.TypeEnum.HEADERCONDITION) + dto.toString();
throw new UnsupportedThrottleConditionTypeException(errorMessage);
}
break;
}
case IPCONDITION:
{
if (dto.getIpCondition() != null) {
conditions.add(fromDTOToIPCondition(dto.getIpCondition(), dto.isInvertCondition()));
} else {
errorMessage = RestApiAdminUtils.constructMissingThrottleObjectErrorMessage(ThrottleConditionDTO.TypeEnum.IPCONDITION) + dto.toString();
throw new UnsupportedThrottleConditionTypeException(errorMessage);
}
break;
}
case QUERYPARAMETERCONDITION:
{
if (dto.getQueryParameterCondition() != null) {
conditions.add(fromDTOToQueryParameterCondition(dto.getQueryParameterCondition(), dto.isInvertCondition()));
} else {
errorMessage = RestApiAdminUtils.constructMissingThrottleObjectErrorMessage(ThrottleConditionDTO.TypeEnum.QUERYPARAMETERCONDITION) + dto.toString();
throw new UnsupportedThrottleConditionTypeException(errorMessage);
}
break;
}
case JWTCLAIMSCONDITION:
{
if (dto.getJwtClaimsCondition() != null) {
conditions.add(fromDTOToJWTClaimsCondition(dto.getJwtClaimsCondition(), dto.isInvertCondition()));
} else {
errorMessage = RestApiAdminUtils.constructMissingThrottleObjectErrorMessage(ThrottleConditionDTO.TypeEnum.JWTCLAIMSCONDITION) + dto.toString();
throw new UnsupportedThrottleConditionTypeException(errorMessage);
}
break;
}
default:
return null;
}
} else {
errorMessage = "Condition item 'type' property has not been specified\n" + dto.toString();
throw new UnsupportedThrottleConditionTypeException(errorMessage);
}
}
}
return conditions;
}
use of org.wso2.carbon.apimgt.api.model.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
* @param invertCondition Invert condition relevant to the DTO
* @return IP Condition model object derived from DTO
*/
public static IPCondition fromDTOToIPCondition(IPConditionDTO dto, boolean invertCondition) {
String ipConditionType = mapIPConditionTypeFromDTOToModel(dto.getIpConditionType());
IPCondition ipCondition = new IPCondition(ipConditionType);
ipCondition.setConditionEnabled(Boolean.TRUE.toString());
ipCondition.setInvertCondition(invertCondition);
ipCondition.setSpecificIP(dto.getSpecificIP());
ipCondition.setStartingIP(dto.getStartingIP());
ipCondition.setEndingIP(dto.getEndingIP());
return ipCondition;
}
Aggregations