Search in sources :

Example 16 with SubscriptionThrottlePolicyDTO

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

the class ThrottlingApiServiceImpl method updatePolicyPermissions.

/**
 * Update APIM with the subscription throttle policy permission
 *
 * @param body subscription throttle policy
 * @throws APIManagementException when there are validation errors or error while updating the permissions
 */
private void updatePolicyPermissions(SubscriptionThrottlePolicyDTO body) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    SubscriptionThrottlePolicyPermissionDTO policyPermissions = body.getPermissions();
    if (policyPermissions != null) {
        if (policyPermissions.getRoles().size() > 0) {
            String roles = StringUtils.join(policyPermissions.getRoles(), ",");
            String permissionType;
            if (policyPermissions.getPermissionType() == SubscriptionThrottlePolicyPermissionDTO.PermissionTypeEnum.ALLOW) {
                permissionType = APIConstants.TIER_PERMISSION_ALLOW;
            } else {
                permissionType = APIConstants.TIER_PERMISSION_DENY;
            }
            apiProvider.updateThrottleTierPermissions(body.getPolicyName(), permissionType, roles);
        } else {
            throw new APIManagementException(ExceptionCodes.ROLES_CANNOT_BE_EMPTY);
        }
    } else {
        apiProvider.deleteTierPermissions(body.getPolicyName());
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 17 with SubscriptionThrottlePolicyDTO

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

the class ThrottlingApiServiceImpl method setPolicyPermissionsToDTO.

/**
 * Set subscription throttle policy permission info into the DTO
 *
 * @param policyDTO subscription throttle policy DTO
 * @throws APIManagementException error while setting/retrieve the permissions to the DTO
 */
private void setPolicyPermissionsToDTO(SubscriptionThrottlePolicyDTO policyDTO) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    TierPermissionDTO addedPolicyPermission = (TierPermissionDTO) apiProvider.getThrottleTierPermission(policyDTO.getPolicyName());
    if (addedPolicyPermission != null) {
        SubscriptionThrottlePolicyPermissionDTO addedPolicyPermissionDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyPermissionToDTO(addedPolicyPermission);
        policyDTO.setPermissions(addedPolicyPermissionDTO);
    }
}
Also used : TierPermissionDTO(org.wso2.carbon.apimgt.impl.dto.TierPermissionDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 18 with SubscriptionThrottlePolicyDTO

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

the class ThrottlingApiServiceImpl method throttlingPoliciesSubscriptionPost.

/**
 * Add a Subscription Level Throttle Policy
 *
 * @param body        DTO of new policy to be created
 * @param contentType Content-Type header
 * @return Created policy along with the location of it with Location header
 */
@Override
public Response throttlingPoliciesSubscriptionPost(String contentType, SubscriptionThrottlePolicyDTO body, MessageContext messageContext) throws APIManagementException {
    RestApiAdminUtils.validateThrottlePolicyNameProperty(body.getPolicyName());
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.getLoggedInUsername();
        SubscriptionPolicy subscriptionPolicy = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionThrottlePolicyDTOToModel(body);
        // Check if there's a policy exists before adding the new policy
        try {
            Policy policyIfExists = apiProvider.getSubscriptionPolicy(username, subscriptionPolicy.getPolicyName());
            if (policyIfExists != null) {
                RestApiUtil.handleResourceAlreadyExistsError("Subscription Policy with name " + subscriptionPolicy.getPolicyName() + " already exists", log);
            }
        } catch (PolicyNotFoundException ignore) {
        }
        // validate if permission info exists and halt the execution in case of an error
        validatePolicyPermissions(body);
        // Add the policy
        apiProvider.addPolicy(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.created(new URI(RestApiConstants.RESOURCE_PATH_THROTTLING_POLICIES_SUBSCRIPTION + "/" + policyDTO.getPolicyId())).entity(policyDTO).build();
    } catch (ParseException e) {
        String errorMessage = "Error while adding a Subscription level policy: " + body.getPolicyName();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving Subscription Throttle policy location : " + body.getPolicyName();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) PolicyNotFoundException(org.wso2.carbon.apimgt.api.PolicyNotFoundException) ParseException(org.json.simple.parser.ParseException) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI)

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