Search in sources :

Example 1 with SubscriptionThrottlePolicyDTO

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

the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionPolicyArrayToListDTO.

/**
 * Converts an array of Subscription Policy objects into a List DTO
 *
 * @param subscriptionPolicies Array of Subscription Policies
 * @return A List DTO of converted Subscription Policies
 * @throws SubscriptionThrottlePolicyException - If error occurs
 */
public static SubscriptionThrottlePolicyListDTO fromSubscriptionPolicyArrayToListDTO(List<SubscriptionPolicy> subscriptionPolicies) throws SubscriptionThrottlePolicyException {
    SubscriptionThrottlePolicyListDTO listDTO = new SubscriptionThrottlePolicyListDTO();
    List<SubscriptionThrottlePolicyDTO> subscriptionPolicyDTOList = new ArrayList<>();
    if (subscriptionPolicies != null) {
        for (SubscriptionPolicy policy : subscriptionPolicies) {
            SubscriptionThrottlePolicyDTO dto = fromSubscriptionThrottlePolicyToDTO(policy);
            subscriptionPolicyDTOList.add(dto);
        }
    }
    listDTO.setCount(subscriptionPolicyDTOList.size());
    listDTO.setList(subscriptionPolicyDTOList);
    return listDTO;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO) ArrayList(java.util.ArrayList) SubscriptionThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyListDTO)

Example 2 with SubscriptionThrottlePolicyDTO

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

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

the class PoliciesApiServiceImpl method policiesThrottlingSubscriptionPost.

/**
 * Add subscription level policy
 *
 * @param body              DTO object including the Policy meta information
 * @param request           msf4j request object
 * @return Response object
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingSubscriptionPost(SubscriptionThrottlePolicyDTO body, Request request) throws NotFoundException {
    APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.subscription;
    if (log.isDebugEnabled()) {
        log.info("Received Subscription Policy POST request " + body + " with tierLevel = " + tierLevel);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        SubscriptionPolicy subscriptionPolicy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(body);
        String policyId = apiMgtAdminService.addSubscriptionPolicy(subscriptionPolicy);
        return Response.status(Response.Status.CREATED).entity(SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(apiMgtAdminService.getSubscriptionPolicyByUuid(policyId))).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while adding Subscription Policy. policy name: " + body.getPolicyName();
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 4 with SubscriptionThrottlePolicyDTO

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

the class ThrottlingApiServiceImpl method throttlingPoliciesSubscriptionPolicyIdGet.

/**
 * Get a specific Subscription Policy by its uuid
 *
 * @param policyId        uuid of the policy
 * @return Matched Subscription Throttle Policy by the given name
 */
@Override
public Response throttlingPoliciesSubscriptionPolicyIdGet(String policyId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.getLoggedInUsername();
        // This will give PolicyNotFoundException if there's no policy exists with UUID
        SubscriptionPolicy subscriptionPolicy = apiProvider.getSubscriptionPolicyByUUID(policyId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, subscriptionPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, log);
        }
        SubscriptionThrottlePolicyDTO policyDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(subscriptionPolicy);
        // setting policy permissions
        setPolicyPermissionsToDTO(policyDTO);
        return Response.ok().entity(policyDTO).build();
    } catch (APIManagementException | ParseException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, e, log);
        } else {
            String errorMessage = "Error while retrieving Subscription level policy: " + policyId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) ParseException(org.json.simple.parser.ParseException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 5 with SubscriptionThrottlePolicyDTO

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

the class ThrottlingApiServiceImpl method throttlingPoliciesSubscriptionPolicyIdPut.

/**
 * Updates a given Subscription level policy specified by uuid
 *
 * @param policyId          u
 * @param body              DTO of policy to be updated
 * @param contentType       Content-Type header
 * @return Updated policy
 */
@Override
public Response throttlingPoliciesSubscriptionPolicyIdPut(String policyId, String contentType, SubscriptionThrottlePolicyDTO body, MessageContext messageContext) throws APIManagementException {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.getLoggedInUsername();
        // will give PolicyNotFoundException if there's no policy exists with UUID
        SubscriptionPolicy existingPolicy = apiProvider.getSubscriptionPolicyByUUID(policyId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, log);
        }
        // overridden properties
        body.setPolicyId(policyId);
        body.setPolicyName(existingPolicy.getPolicyName());
        // validate if permission info exists and halt the execution in case of an error
        validatePolicyPermissions(body);
        // update the policy
        SubscriptionPolicy subscriptionPolicy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(body);
        apiProvider.updatePolicy(subscriptionPolicy);
        // update policy permissions
        updatePolicyPermissions(body);
        // retrieve the new policy and send back as the response
        SubscriptionPolicy newSubscriptionPolicy = apiProvider.getSubscriptionPolicy(username, body.getPolicyName());
        SubscriptionThrottlePolicyDTO policyDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyToDTO(newSubscriptionPolicy);
        // setting policy permissions
        setPolicyPermissionsToDTO(policyDTO);
        return Response.ok().entity(policyDTO).build();
    } catch (APIManagementException | ParseException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_SUBSCRIPTION_POLICY, policyId, e, log);
        } else {
            String errorMessage = "Error while updating Subscription level policy: " + body.getPolicyName();
            throw new APIManagementException(errorMessage, e);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) ParseException(org.json.simple.parser.ParseException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)10 SubscriptionThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO)6 ArrayList (java.util.ArrayList)5 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)5 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)5 JSONArray (org.json.simple.JSONArray)4 JSONObject (org.json.simple.JSONObject)4 ParseException (org.json.simple.parser.ParseException)4 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)3 CustomAttributeDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.CustomAttributeDTO)3 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 Response (javax.ws.rs.core.Response)2 JSONParser (org.json.simple.parser.JSONParser)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Test (org.testng.annotations.Test)2 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)2 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)2