Java Code Examples for org.wso2.carbon.apimgt.impl.utils.APIUtil#ipToBigInteger()

The following examples show how to use org.wso2.carbon.apimgt.impl.utils.APIUtil#ipToBigInteger() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThrottleConditionEvaluator.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
private boolean isWithinIP(MessageContext messageContext, ConditionDto.IPCondition ipCondition) {

        String currentIpString = GatewayUtils.getIp(messageContext);
        boolean status;
        if (StringUtils.isNotEmpty(currentIpString)) {
            BigInteger currentIp = APIUtil.ipToBigInteger(currentIpString);
            status = ipCondition.getStartingIp().compareTo(currentIp) <= 0
                    && ipCondition.getEndingIp().compareTo(currentIp) >= 0;
        } else {
            return false;
        }
        if (ipCondition.isInvert()) {
            return !status;
        } else {
            return status;
        }
    }
 
Example 2
Source File: ThrottleConditionEvaluator.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private boolean isWithinIP(MessageContext messageContext, ConditionDTO condition) {
    // For an IP Range Condition, starting IP is set as a the name, ending IP as the value.
    BigInteger startIp = APIUtil.ipToBigInteger(condition.getConditionName());
    BigInteger endIp = APIUtil.ipToBigInteger(condition.getConditionValue());

    String currentIpString = GatewayUtils.getIp(messageContext);
    if (!currentIpString.isEmpty()) {
        BigInteger currentIp = APIUtil.ipToBigInteger(currentIpString);

        return startIp.compareTo(currentIp) <= 0 && endIp.compareTo(currentIp) >= 0;
    }
    return false;
}
 
Example 3
Source File: ThrottleConditionEvaluator.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private boolean isMatchingIP(MessageContext messageContext, ConditionDto.IPCondition ipCondition) {

        String currentIpString = GatewayUtils.getIp(messageContext);
        BigInteger longValueOfIp = APIUtil.ipToBigInteger(currentIpString);

        if (ipCondition.isInvert()) {
            return !longValueOfIp.equals(ipCondition.getSpecificIp());
        }
        return longValueOfIp.equals(ipCondition.getSpecificIp());
    }
 
Example 4
Source File: ThrottleDataHolder.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private boolean isIpLevelBlocked(String apiTenantDomain, String ip) {

        Set<IPRange> ipRanges = blockedIpConditionsMap.get(apiTenantDomain);
        if (ipRanges != null && ipRanges.size() > 0) {
            log.debug("Tenant " + apiTenantDomain + " contains block conditions");
            for (IPRange ipRange : ipRanges) {
                if (APIConstants.BLOCKING_CONDITIONS_IP.equals(ipRange.getType())) {
                    if (ip.equals(ipRange.getFixedIp())) {
                        if (!ipRange.isInvert()) {
                            log.debug("Block IP selected for Blocked");
                            return true;
                        }
                    } else {
                        if (ipRange.isInvert()) {
                            log.debug("Block IP selected for Blocked");
                            return true;
                        }
                    }
                } else if (APIConstants.BLOCK_CONDITION_IP_RANGE.equals(ipRange.getType())) {
                    BigInteger ipBigIntegerValue = APIUtil.ipToBigInteger(ip);

                    if (((ipBigIntegerValue.compareTo(ipRange.getStartingIpBigIntValue()) > 0) &&
                            (ipBigIntegerValue.compareTo(ipRange.getEndingIpBigIntValue()) < 0))) {
                        if (!ipRange.isInvert()) {
                            log.debug("Block IPRange selected for Blocked");
                            return true;
                        }
                    } else {
                        if (ipRange.isInvert()) {
                            log.debug("Block IPRange selected for Blocked");
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
 
Example 5
Source File: ThrottleConditionEvaluatorTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
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;
    }
 
Example 6
Source File: ThrottleConditionEvaluatorTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
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;
    }