Search in sources :

Example 6 with CustomAttributeDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomAttributeDTO in project carbon-apimgt by wso2.

the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionThrottlePolicyToDTO.

/**
 * Converts a single Subscription Policy model into REST API DTO
 *
 * @param subscriptionPolicy Subscription Policy model object
 * @return Converted Subscription policy REST API DTO object
 * @throws UnsupportedThrottleLimitTypeException
 * @throws ParseException
 */
public static SubscriptionThrottlePolicyDTO fromSubscriptionThrottlePolicyToDTO(SubscriptionPolicy subscriptionPolicy) throws UnsupportedThrottleLimitTypeException, ParseException {
    SubscriptionThrottlePolicyDTO policyDTO = new SubscriptionThrottlePolicyDTO();
    policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(subscriptionPolicy, policyDTO);
    policyDTO.setBillingPlan(subscriptionPolicy.getBillingPlan());
    policyDTO.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
    policyDTO.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
    policyDTO.setStopOnQuotaReach(subscriptionPolicy.isStopOnQuotaReach());
    policyDTO.setGraphQLMaxComplexity(subscriptionPolicy.getGraphQLMaxComplexity());
    policyDTO.setGraphQLMaxDepth(subscriptionPolicy.getGraphQLMaxDepth());
    policyDTO.setSubscriberCount(subscriptionPolicy.getSubscriberCount());
    byte[] customAttributes = subscriptionPolicy.getCustomAttributes();
    if (customAttributes != null) {
        List<CustomAttributeDTO> customAttributeDTOs = new ArrayList<>();
        JSONParser parser = new JSONParser();
        JSONArray attributeArray = (JSONArray) parser.parse(new String(subscriptionPolicy.getCustomAttributes()));
        for (Object attributeObj : attributeArray) {
            JSONObject attribute = (JSONObject) attributeObj;
            CustomAttributeDTO customAttributeDTO = CommonThrottleMappingUtil.getCustomAttribute(attribute.get(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_NAME).toString(), attribute.get(RestApiConstants.THROTTLING_CUSTOM_ATTRIBUTE_VALUE).toString());
            customAttributeDTOs.add(customAttributeDTO);
        }
        policyDTO.setCustomAttributes(customAttributeDTOs);
    }
    if (subscriptionPolicy.getDefaultQuotaPolicy() != null) {
        policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(subscriptionPolicy.getDefaultQuotaPolicy()));
    }
    policyDTO.setType(SUBSCRIPTION_THROTTLE_POLICY_TYPE);
    if (APIConstants.COMMERCIAL_TIER_PLAN.equals(subscriptionPolicy.getBillingPlan())) {
        MonetizationInfoDTO monetizationInfoDTO = new MonetizationInfoDTO();
        if (APIConstants.Monetization.FIXED_RATE.equalsIgnoreCase(subscriptionPolicy.getMonetizationPlan())) {
            monetizationInfoDTO.setMonetizationPlan(MonetizationInfoDTO.MonetizationPlanEnum.FIXEDRATE);
        } else {
            monetizationInfoDTO.setMonetizationPlan(MonetizationInfoDTO.MonetizationPlanEnum.DYNAMICRATE);
        }
        monetizationInfoDTO.setProperties(subscriptionPolicy.getMonetizationPlanProperties());
        policyDTO.setMonetization(monetizationInfoDTO);
    }
    return policyDTO;
}
Also used : MonetizationInfoDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MonetizationInfoDTO) JSONObject(org.json.simple.JSONObject) SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.SubscriptionThrottlePolicyDTO) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) CustomAttributeDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomAttributeDTO)

Example 7 with CustomAttributeDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomAttributeDTO in project carbon-apimgt by wso2.

the class CommonThrottleMappingUtil method getCustomAttribute.

/**
 * Create a custom attribute using the given name and value
 *
 * @param name  Name of the attribute
 * @param value Value of the attribute
 * @return Custom Attribute object containing the given name and value
 */
public static CustomAttributeDTO getCustomAttribute(String name, String value) {
    CustomAttributeDTO customAttributeDTO = new CustomAttributeDTO();
    customAttributeDTO.setName(name);
    customAttributeDTO.setValue(value);
    return customAttributeDTO;
}
Also used : CustomAttributeDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomAttributeDTO)

Aggregations

JSONArray (org.json.simple.JSONArray)4 JSONObject (org.json.simple.JSONObject)4 CustomAttributeDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.CustomAttributeDTO)4 ArrayList (java.util.ArrayList)3 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)3 CustomAttributeDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomAttributeDTO)3 JSONParser (org.json.simple.parser.JSONParser)2 SubscriptionThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ParseException (org.json.simple.parser.ParseException)1 Test (org.testng.annotations.Test)1 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)1 RequestCountLimitDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.RequestCountLimitDTO)1 ThrottleLimitDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.ThrottleLimitDTO)1 SubscriptionThrottlePolicyException (org.wso2.carbon.apimgt.rest.api.admin.exceptions.SubscriptionThrottlePolicyException)1 UnsupportedThrottleLimitTypeException (org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException)1