Search in sources :

Example 6 with QueryParameterCondition

use of org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromDTOToQueryParameterCondition.

/**
 * Converts a Query Parameter Condition DTO object into a model object
 *
 * @param dto             Query Parameter Condition DTO object
 * @param invertCondition Invert condition relevant to the DTO
 * @return Query Parameter Condition model object derived from Query Parameter Condition DTO
 */
public static QueryParameterCondition fromDTOToQueryParameterCondition(QueryParameterConditionDTO dto, boolean invertCondition) {
    QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
    queryParameterCondition.setConditionEnabled(Boolean.TRUE.toString());
    queryParameterCondition.setInvertCondition(invertCondition);
    queryParameterCondition.setParameter(dto.getParameterName());
    queryParameterCondition.setValue(dto.getParameterValue());
    return queryParameterCondition;
}
Also used : QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)

Example 7 with QueryParameterCondition

use of org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition 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 8 with QueryParameterCondition

use of org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromConditionToDTO.

/**
 * Converts a Throttle Condition model object into a DTO
 *
 * @param condition Throttle condition model object
 * @return Derived DTO object from the model object
 * @throws UnsupportedThrottleConditionTypeException
 */
public static // .................
ThrottleConditionDTO fromConditionToDTO(// .................
Condition condition) throws UnsupportedThrottleConditionTypeException {
    ThrottleConditionDTO throttleConditionDTO = new ThrottleConditionDTO();
    throttleConditionDTO.setInvertCondition(condition.isInvertCondition());
    if (condition instanceof IPCondition) {
        throttleConditionDTO.setType(ThrottleConditionDTO.TypeEnum.IPCONDITION);
        throttleConditionDTO.setIpCondition(fromIPConditionToDTO((IPCondition) condition));
    } else if (condition instanceof HeaderCondition) {
        throttleConditionDTO.setType(ThrottleConditionDTO.TypeEnum.HEADERCONDITION);
        throttleConditionDTO.setHeaderCondition(fromHeaderConditionToDTO((HeaderCondition) condition));
    } else if (condition instanceof QueryParameterCondition) {
        throttleConditionDTO.setType(ThrottleConditionDTO.TypeEnum.QUERYPARAMETERCONDITION);
        throttleConditionDTO.setQueryParameterCondition(fromQueryParameterConditionToDTO((QueryParameterCondition) condition));
    } else if (condition instanceof JWTClaimsCondition) {
        throttleConditionDTO.setType(ThrottleConditionDTO.TypeEnum.JWTCLAIMSCONDITION);
        throttleConditionDTO.setJwtClaimsCondition(fromJWTClaimsConditionToDTO((JWTClaimsCondition) condition));
    } else {
        String msg = "Throttle Condition type " + condition.getClass().getName() + " is not supported";
        throw new UnsupportedThrottleConditionTypeException(msg);
    }
    return throttleConditionDTO;
}
Also used : IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) UnsupportedThrottleConditionTypeException(org.wso2.carbon.apimgt.api.UnsupportedThrottleConditionTypeException) ThrottleConditionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ThrottleConditionDTO) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)

Example 9 with QueryParameterCondition

use of org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition in project carbon-apimgt by wso2.

the class ThrottlePolicyTemplateBuilder method getPolicyConditionJson.

/**
 * Produces final condition inside a pipeline
 *
 * @param conditions set of conditions
 * @return conditions as a JSON
 */
private static JSONObject getPolicyConditionJson(Set<Condition> conditions) {
    JSONObject tempCondition = new JSONObject();
    for (Condition condition : conditions) {
        org.wso2.carbon.apimgt.api.model.policy.Condition mappedCondition = PolicyMappingUtil.mapCondition(condition);
        JSONObject conditionJson;
        if (tempCondition.containsKey(mappedCondition.getType().toLowerCase(Locale.ENGLISH))) {
            conditionJson = (JSONObject) tempCondition.get(mappedCondition.getType().toLowerCase(Locale.ENGLISH));
        } else {
            conditionJson = new JSONObject();
        }
        tempCondition.put(mappedCondition.getType().toLowerCase(Locale.ENGLISH), conditionJson);
        if (PolicyConstants.IP_SPECIFIC_TYPE.equals(mappedCondition.getType())) {
            IPCondition ipCondition = (IPCondition) mappedCondition;
            if (IPCondition.isIPv6Address(ipCondition.getSpecificIP())) {
                conditionJson.put("specificIp", String.valueOf(APIUtil.ipToBigInteger(ipCondition.getSpecificIP())));
            } else {
                conditionJson.put("specificIp", ipCondition.ipToLong(ipCondition.getSpecificIP()));
            }
        } else if (PolicyConstants.IP_RANGE_TYPE.equals(mappedCondition.getType())) {
            IPCondition ipRangeCondition = (IPCondition) mappedCondition;
            if (IPCondition.isIPv6Address(ipRangeCondition.getStartingIP()) && IPCondition.isIPv6Address(ipRangeCondition.getEndingIP())) {
                conditionJson.put("startingIp", String.valueOf(APIUtil.ipToBigInteger(ipRangeCondition.getStartingIP())));
                conditionJson.put("endingIp", String.valueOf(APIUtil.ipToBigInteger(ipRangeCondition.getEndingIP())));
            } else {
                conditionJson.put("startingIp", ipRangeCondition.ipToLong(ipRangeCondition.getStartingIP()));
                conditionJson.put("endingIp", ipRangeCondition.ipToLong(ipRangeCondition.getEndingIP()));
            }
        } else if (mappedCondition instanceof QueryParameterCondition) {
            QueryParameterCondition queryParameterCondition = (QueryParameterCondition) mappedCondition;
            JSONObject values;
            if (conditionJson.containsKey("values")) {
                values = (JSONObject) conditionJson.get("values");
            } else {
                values = new JSONObject();
                conditionJson.put("values", values);
            }
            values.put(queryParameterCondition.getParameter(), queryParameterCondition.getValue());
        } else if (mappedCondition instanceof HeaderCondition) {
            HeaderCondition headerCondition = (HeaderCondition) mappedCondition;
            JSONObject values;
            if (conditionJson.containsKey("values")) {
                values = (JSONObject) conditionJson.get("values");
            } else {
                values = new JSONObject();
                conditionJson.put("values", values);
            }
            values.put(headerCondition.getHeaderName(), headerCondition.getValue());
        } else if (mappedCondition instanceof JWTClaimsCondition) {
            JWTClaimsCondition jwtClaimsCondition = (JWTClaimsCondition) mappedCondition;
            JSONObject values;
            if (conditionJson.containsKey("values")) {
                values = (JSONObject) conditionJson.get("values");
            } else {
                values = new JSONObject();
                conditionJson.put("values", values);
            }
            values.put(jwtClaimsCondition.getClaimUrl(), jwtClaimsCondition.getAttribute());
        }
        conditionJson.put("invert", mappedCondition.isInvertCondition());
    }
    return tempCondition;
}
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.throttle.policy.deployer.dto.Condition) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) IPCondition(org.wso2.carbon.apimgt.api.model.policy.IPCondition) JWTClaimsCondition(org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition) JSONObject(org.json.simple.JSONObject) HeaderCondition(org.wso2.carbon.apimgt.api.model.policy.HeaderCondition) QueryParameterCondition(org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)

Example 10 with QueryParameterCondition

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

Aggregations

QueryParameterCondition (org.wso2.carbon.apimgt.api.model.policy.QueryParameterCondition)10 HeaderCondition (org.wso2.carbon.apimgt.api.model.policy.HeaderCondition)8 IPCondition (org.wso2.carbon.apimgt.api.model.policy.IPCondition)8 JWTClaimsCondition (org.wso2.carbon.apimgt.api.model.policy.JWTClaimsCondition)8 ArrayList (java.util.ArrayList)6 Condition (org.wso2.carbon.apimgt.api.model.policy.Condition)5 QueryParameterCondition (org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition)5 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 UnsupportedThrottleConditionTypeException (org.wso2.carbon.apimgt.api.UnsupportedThrottleConditionTypeException)2 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)2 BandwidthLimit (org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit)2 DateCondition (org.wso2.carbon.apimgt.api.model.policy.DateCondition)2 DateRangeCondition (org.wso2.carbon.apimgt.api.model.policy.DateRangeCondition)2 HTTPVerbCondition (org.wso2.carbon.apimgt.api.model.policy.HTTPVerbCondition)2 Pipeline (org.wso2.carbon.apimgt.api.model.policy.Pipeline)2 QuotaPolicy (org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy)2 RequestCountLimit (org.wso2.carbon.apimgt.api.model.policy.RequestCountLimit)2 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)2 Condition (org.wso2.carbon.apimgt.core.models.policy.Condition)2