use of org.wso2.carbon.apimgt.api.PolicyNotFoundException in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingPoliciesAdvancedPolicyIdPut.
/**
* Updates a given Advanced level policy specified by uuid
*
* @param policyId uuid of the policy
* @param body DTO of policy to be updated
* @param contentType Content-Type header
* @return Updated policy
*/
@Override
public Response throttlingPoliciesAdvancedPolicyIdPut(String policyId, String contentType, AdvancedThrottlePolicyDTO body, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String username = RestApiCommonUtil.getLoggedInUsername();
// will give PolicyNotFoundException if there's no policy exists with UUID
APIPolicy existingPolicy = apiProvider.getAPIPolicyByUUID(policyId);
if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_ADVANCED_POLICY, policyId, log);
}
// overridden parameters
body.setPolicyId(policyId);
body.setPolicyName(existingPolicy.getPolicyName());
// update the policy
APIPolicy apiPolicy = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(body);
apiProvider.updatePolicy(apiPolicy);
// retrieve the new policy and send back as the response
APIPolicy newApiPolicy = apiProvider.getAPIPolicyByUUID(policyId);
AdvancedThrottlePolicyDTO policyDTO = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(newApiPolicy);
return Response.ok().entity(policyDTO).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_ADVANCED_POLICY, policyId, e, log);
} else {
String errorMessage = "Error while updating Advanced level policy: " + body.getPolicyName();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
Aggregations