Search in sources :

Example 16 with PolicyNotFoundException

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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Aggregations

APIProvider (org.wso2.carbon.apimgt.api.APIProvider)16 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)15 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)7 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)7 GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)7 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)7 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 PolicyNotFoundException (org.wso2.carbon.apimgt.api.PolicyNotFoundException)4 Policy (org.wso2.carbon.apimgt.api.model.policy.Policy)4 ParseException (org.json.simple.parser.ParseException)3