Search in sources :

Example 6 with IPCondition

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;
}
Also used : HashMap(java.util.HashMap) ConditionDto(org.wso2.carbon.apimgt.impl.dto.ConditionDto) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 7 with IPCondition

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;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) Gson(com.google.gson.Gson) JSONObject(org.json.simple.JSONObject) ConditionDto(org.wso2.carbon.apimgt.impl.dto.ConditionDto) JSONParser(org.json.simple.parser.JSONParser) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject)

Example 8 with IPCondition

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;
}
Also used : HashMap(java.util.HashMap) ConditionDto(org.wso2.carbon.apimgt.impl.dto.ConditionDto) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 9 with IPCondition

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;
}
Also used : IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) Condition(org.wso2.carbon.apimgt.api.model.policy.Condition) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) ArrayList(java.util.ArrayList) UnsupportedThrottleConditionTypeException(org.wso2.carbon.apimgt.api.UnsupportedThrottleConditionTypeException) ThrottleConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ThrottleConditionDTO)

Example 10 with IPCondition

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;
}
Also used : IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition)

Aggregations

IPCondition (org.wso2.carbon.apimgt.api.model.policy.IPCondition)9 HeaderCondition (org.wso2.carbon.apimgt.api.model.policy.HeaderCondition)8 JWTClaimsCondition (org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition)8 QueryParameterCondition (org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)8 ArrayList (java.util.ArrayList)7 IPCondition (org.wso2.carbon.apimgt.core.models.policy.IPCondition)7 Condition (org.wso2.carbon.apimgt.api.model.policy.Condition)5 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 Test (org.testng.annotations.Test)3 Pipeline (org.wso2.carbon.apimgt.core.models.policy.Pipeline)3 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)3 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)3 ConditionDto (org.wso2.carbon.apimgt.impl.dto.ConditionDto)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 JSONObject (org.json.simple.JSONObject)2 UnsupportedThrottleConditionTypeException (org.wso2.carbon.apimgt.api.UnsupportedThrottleConditionTypeException)2 BandwidthLimit (org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)2