Search in sources :

Example 1 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 policy Subscription Policy model object
 * @return Converted Subscription policy REST API DTO object
 * @throws SubscriptionThrottlePolicyException - If error occurs
 */
public static SubscriptionThrottlePolicyDTO fromSubscriptionThrottlePolicyToDTO(SubscriptionPolicy policy) throws SubscriptionThrottlePolicyException {
    try {
        SubscriptionThrottlePolicyDTO policyDTO = new SubscriptionThrottlePolicyDTO();
        policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(policy, policyDTO);
        SubscriptionPolicy subscriptionPolicy = policy;
        policyDTO.setBillingPlan(subscriptionPolicy.getBillingPlan());
        policyDTO.setRateLimitCount(subscriptionPolicy.getRateLimitCount());
        policyDTO.setRateLimitTimeUnit(subscriptionPolicy.getRateLimitTimeUnit());
        policyDTO.setStopOnQuotaReach(subscriptionPolicy.isStopOnQuotaReach());
        byte[] customAttributes = subscriptionPolicy.getCustomAttributes();
        if (customAttributes != null && customAttributes.length > 0) {
            List<CustomAttributeDTO> customAttributeDTOs = new ArrayList<>();
            JSONParser parser = new JSONParser();
            JSONArray attributeArray = (JSONArray) parser.parse(new String(customAttributes, StandardCharsets.UTF_8));
            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 (policy.getDefaultQuotaPolicy() != null) {
            policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(policy.getDefaultQuotaPolicy()));
        }
        return policyDTO;
    } catch (ParseException | UnsupportedThrottleLimitTypeException e) {
        throw new SubscriptionThrottlePolicyException(e.getMessage(), e);
    }
}
Also used : SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) SubscriptionThrottlePolicyException(org.wso2.carbon.apimgt.rest.api.admin.exceptions.SubscriptionThrottlePolicyException) UnsupportedThrottleLimitTypeException(org.wso2.carbon.apimgt.rest.api.admin.exceptions.UnsupportedThrottleLimitTypeException) JSONObject(org.json.simple.JSONObject) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) CustomAttributeDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomAttributeDTO)

Example 2 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.dto.CustomAttributeDTO)

Example 3 with CustomAttributeDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomAttributeDTO 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)

Example 4 with CustomAttributeDTO

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

the class SubscriptionThrottlePolicyMappingUtilTest method fromSubscriptionThrottlePolicyDTOToModelTest.

@Test(description = "Convert Subscription Throttle Policy DTO to Model")
public void fromSubscriptionThrottlePolicyDTOToModelTest() throws Exception {
    SubscriptionThrottlePolicyDTO dto = new SubscriptionThrottlePolicyDTO();
    dto.setRateLimitTimeUnit("m");
    dto.setRateLimitCount(1);
    dto.setStopOnQuotaReach(true);
    ThrottleLimitDTO throttleLimitDTO = new ThrottleLimitDTO();
    throttleLimitDTO.setType("RequestCountLimit");
    throttleLimitDTO.setTimeUnit("s");
    throttleLimitDTO.setUnitTime(1);
    RequestCountLimitDTO requestCountLimitDTO = new RequestCountLimitDTO();
    requestCountLimitDTO.setRequestCount(2);
    throttleLimitDTO.setRequestCountLimit(requestCountLimitDTO);
    dto.setDefaultLimit(throttleLimitDTO);
    List<CustomAttributeDTO> customAttributeDTOs = new ArrayList<>();
    CustomAttributeDTO customAttributeDTO1 = new CustomAttributeDTO();
    customAttributeDTO1.setName("ABC");
    customAttributeDTO1.setValue("ABCVALUE");
    CustomAttributeDTO customAttributeDTO2 = new CustomAttributeDTO();
    customAttributeDTO2.setName("CDE");
    customAttributeDTO2.setValue("CDEVALUE");
    customAttributeDTOs.add(customAttributeDTO1);
    customAttributeDTOs.add(customAttributeDTO2);
    dto.setCustomAttributes(customAttributeDTOs);
    SubscriptionPolicy policy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(dto);
    Assert.assertNotNull(policy);
    Assert.assertEquals(policy.getRateLimitCount(), 1);
    Assert.assertEquals(policy.getRateLimitTimeUnit(), "m");
    Assert.assertEquals(policy.isStopOnQuotaReach(), true);
    Assert.assertEquals(policy.getDefaultQuotaPolicy().getType(), "requestCount");
    Assert.assertEquals(policy.getDefaultQuotaPolicy().getLimit().getTimeUnit(), dto.getDefaultLimit().getTimeUnit());
    Assert.assertEquals((Integer) policy.getDefaultQuotaPolicy().getLimit().getUnitTime(), dto.getDefaultLimit().getUnitTime());
    Assert.assertEquals((Integer) ((RequestCountLimit) policy.getDefaultQuotaPolicy().getLimit()).getRequestCount(), dto.getDefaultLimit().getRequestCountLimit().getRequestCount());
    Assert.assertTrue(new String(policy.getCustomAttributes()).contains("ABC"));
    Assert.assertTrue(new String(policy.getCustomAttributes()).contains("ABCVALUE"));
    Assert.assertTrue(new String(policy.getCustomAttributes()).contains("CDE"));
    Assert.assertTrue(new String(policy.getCustomAttributes()).contains("CDEVALUE"));
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) RequestCountLimitDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.RequestCountLimitDTO) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO) ArrayList(java.util.ArrayList) ThrottleLimitDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ThrottleLimitDTO) CustomAttributeDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomAttributeDTO) Test(org.testng.annotations.Test)

Example 5 with CustomAttributeDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomAttributeDTO 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
 */
@SuppressWarnings("unchecked")
public static SubscriptionPolicy fromSubscriptionThrottlePolicyDTOToModel(SubscriptionThrottlePolicyDTO dto) throws UnsupportedThrottleLimitTypeException {
    // update mandatory fields such as tenantDomain etc.
    dto = CommonThrottleMappingUtil.updateDefaultMandatoryFieldsOfThrottleDTO(dto);
    SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(dto.getPolicyName());
    subscriptionPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, subscriptionPolicy);
    subscriptionPolicy.setBillingPlan(dto.getBillingPlan());
    subscriptionPolicy.setRateLimitTimeUnit(dto.getRateLimitTimeUnit());
    subscriptionPolicy.setRateLimitCount(dto.getRateLimitCount());
    subscriptionPolicy.setSubscriberCount(dto.getSubscriberCount());
    subscriptionPolicy.setStopOnQuotaReach(dto.isStopOnQuotaReach());
    if (dto.getGraphQLMaxComplexity() != null) {
        subscriptionPolicy.setGraphQLMaxComplexity(dto.getGraphQLMaxComplexity());
    }
    if (dto.getGraphQLMaxDepth() != null) {
        subscriptionPolicy.setGraphQLMaxDepth(dto.getGraphQLMaxDepth());
    }
    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);
        }
        subscriptionPolicy.setCustomAttributes(customAttrJsonArray.toJSONString().getBytes());
    } else {
        // if no custom attributes are set, assign an empty byte array
        subscriptionPolicy.setCustomAttributes(new JSONArray().toJSONString().getBytes());
    }
    if (dto.getMonetization() != null && dto.getMonetization().getMonetizationPlan() != null && StringUtils.isNotBlank(dto.getMonetization().getMonetizationPlan().name())) {
        String tierMonetizationPlan = dto.getMonetization().getMonetizationPlan().toString();
        subscriptionPolicy.setMonetizationPlan(tierMonetizationPlan);
        if (dto.getMonetization().getProperties() != null) {
            Map<String, String> tierMonetizationProperties = new HashMap<>();
            Map<String, String> props = dto.getMonetization().getProperties();
            for (Map.Entry<String, String> entry : props.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                tierMonetizationProperties.put(key, value);
            }
            subscriptionPolicy.setMonetizationPlanProperties(tierMonetizationProperties);
        }
    }
    if (dto.getDefaultLimit() != null) {
        subscriptionPolicy.setDefaultQuotaPolicy(CommonThrottleMappingUtil.fromDTOToQuotaPolicy(dto.getDefaultLimit()));
    }
    return subscriptionPolicy;
}
Also used : JSONObject(org.json.simple.JSONObject) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) HashMap(java.util.HashMap) JSONArray(org.json.simple.JSONArray) HashMap(java.util.HashMap) Map(java.util.Map) 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