Search in sources :

Example 21 with UnsupportedThrottleLimitTypeException

use of org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromDTOToLimit.

/**
 * Converts a Throttle Limit DTO object into a Limit object
 *
 * @param dto Throttle Limit DTO object
 * @return Limit object derived from DTO
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static Limit fromDTOToLimit(ThrottleLimitDTO dto) throws UnsupportedThrottleLimitTypeException {
    if (PolicyConstants.BANDWIDTH_LIMIT_TYPE.equals(dto.getType())) {
        // check if all required params are available
        if (dto.getBandwidthLimit() == null || dto.getBandwidthLimit().getDataAmount() == null || dto.getBandwidthLimit().getDataUnit() == null) {
            // can't continue, throw
            String errorMsg = "One or more required params are missing for the ThrottleLimit type: " + dto.getType();
            log.error(errorMsg);
            throw new UnsupportedThrottleLimitTypeException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
        }
        return fromDTOToBandwidthLimit(dto);
    } else if (PolicyConstants.REQUEST_COUNT_LIMIT_TYPE.equals(dto.getType())) {
        // check if all required params are available
        if (dto.getRequestCountLimit() == null || dto.getRequestCountLimit().getRequestCount() == null) {
            // can't continue, throw
            String errorMsg = "One or more required params are missing for the ThrottleLimit type: " + dto.getType();
            log.error(errorMsg);
            throw new UnsupportedThrottleLimitTypeException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
        }
        return fromDTOToRequestCountLimit(dto);
    } else {
        String msg = "Throttle limit type " + dto.getType() + " is not supported";
        log.error(msg);
        throw new UnsupportedThrottleLimitTypeException(msg, ExceptionCodes.UNSUPPORTED_THROTTLE_LIMIT_TYPE);
    }
}
Also used : UnsupportedThrottleLimitTypeException(org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException)

Example 22 with UnsupportedThrottleLimitTypeException

use of org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromDTOToQuotaPolicy.

/**
 * Converts a Throttle Limit DTO object into a Quota Policy object
 *
 * @param dto Throttle limit DTO object
 * @return Derived Quota policy object from DTO
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static QuotaPolicy fromDTOToQuotaPolicy(ThrottleLimitDTO dto) throws UnsupportedThrottleLimitTypeException {
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    quotaPolicy.setLimit(fromDTOToLimit(dto));
    quotaPolicy.setType(mapQuotaPolicyTypeFromDTOToModel(dto.getType()));
    return quotaPolicy;
}
Also used : QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)

Example 23 with UnsupportedThrottleLimitTypeException

use of org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method fromConditionalGroupDTOToPipeline.

/**
 * Converts a single Conditional Group DTO into a Pipeline object
 *
 * @param dto Conditional Group DTO
 * @return Derived Pipeline object from Conditional Group DTO
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 * @throws UnsupportedThrottleConditionTypeException - If error occurs
 */
public static Pipeline fromConditionalGroupDTOToPipeline(ConditionalGroupDTO dto) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    Pipeline pipeline = new Pipeline();
    pipeline.setDescription(dto.getDescription());
    pipeline.setEnabled(true);
    pipeline.setQuotaPolicy(fromDTOToQuotaPolicy(dto.getLimit()));
    List<Condition> conditions = fromDTOListToConditionList(dto.getConditions());
    pipeline.setConditions(conditions);
    return pipeline;
}
Also used : JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) Condition(org.wso2.carbon.apimgt.core.models.policy.Condition) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Example 24 with UnsupportedThrottleLimitTypeException

use of org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException in project carbon-apimgt by wso2.

the class CustomPolicyMappingUtil method fromCustomPolicyDTOToModel.

/**
 * Converts a single Custom Policy DTO object into model object.
 *
 * @param dto Custom Policy DTO object
 * @return Model object derived from DTO
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static CustomPolicy fromCustomPolicyDTOToModel(CustomRuleDTO dto) throws UnsupportedThrottleLimitTypeException {
    CustomPolicy customPolicy = new CustomPolicy(dto.getPolicyName());
    customPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, customPolicy);
    customPolicy.setKeyTemplate(dto.getKeyTemplate());
    customPolicy.setSiddhiQuery(dto.getSiddhiQuery());
    return customPolicy;
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)

Example 25 with UnsupportedThrottleLimitTypeException

use of org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException in project carbon-apimgt by wso2.

the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionThrottlePolicyDTOToModel.

/**
 * Converts a single Subscription Policy DTO into a model object
 *
 * @param dto Subscription policy DTO object
 * @return Converted Subscription policy model object
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
@SuppressWarnings("unchecked")
public static SubscriptionPolicy fromSubscriptionThrottlePolicyDTOToModel(SubscriptionThrottlePolicyDTO dto) throws APIManagementException {
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(dto.getPolicyName());
    subscriptionPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, subscriptionPolicy);
    subscriptionPolicy.setBillingPlan(dto.getBillingPlan());
    subscriptionPolicy.setRateLimitTimeUnit(dto.getRateLimitTimeUnit());
    subscriptionPolicy.setRateLimitCount(dto.getRateLimitCount());
    subscriptionPolicy.setStopOnQuotaReach(dto.getStopOnQuotaReach());
    List<CustomAttributeDTO> customAttributes = dto.getCustomAttributes();
    if (customAttributes != null && customAttributes.size() > 0) {
        JSONArray customAttrJsonArray = new JSONArray();
        for (CustomAttributeDTO customAttributeDTO : customAttributes) {
            JSONObject attrJsonObj = new JSONObject();
            attrJsonObj.put(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_NAME, customAttributeDTO.getName());
            attrJsonObj.put(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_VALUE, customAttributeDTO.getValue());
            customAttrJsonArray.add(attrJsonObj);
        }
        try {
            subscriptionPolicy.setCustomAttributes(customAttrJsonArray.toJSONString().getBytes(System.getProperty(ENCODING_SYSTEM_PROPERTY, ENCODING_UTF_8)));
        } catch (UnsupportedEncodingException e) {
            String errorMsg = "Error while setting custom attributes for Subscription Policy: " + dto.getPolicyName();
            log.error(errorMsg, e);
            throw new APIManagementException(errorMsg, e, ExceptionCodes.INTERNAL_ERROR);
        }
    }
    if (dto.getDefaultLimit() != null) {
        subscriptionPolicy.setDefaultQuotaPolicy(CommonThrottleMappingUtil.fromDTOToQuotaPolicy(dto.getDefaultLimit()));
    }
    return subscriptionPolicy;
}
Also used : JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) JSONArray(org.json.simple.JSONArray) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CustomAttributeDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomAttributeDTO)

Aggregations

ArrayList (java.util.ArrayList)10 JSONArray (org.json.simple.JSONArray)4 JSONObject (org.json.simple.JSONObject)4 AdvancedThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO)3 UnsupportedThrottleLimitTypeException (org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException)3 JSONParser (org.json.simple.parser.JSONParser)2 UnsupportedThrottleLimitTypeException (org.wso2.carbon.apimgt.api.UnsupportedThrottleLimitTypeException)2 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)2 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)2 GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)2 Pipeline (org.wso2.carbon.apimgt.api.model.policy.Pipeline)2 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)2 BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)2 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)2 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)2 CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)2 Pipeline (org.wso2.carbon.apimgt.core.models.policy.Pipeline)2 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)2 ApplicationThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.ApplicationThrottlePolicyDTO)2 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO)2