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());
}
}
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);
}
}
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;
}
Aggregations