Search in sources :

Example 11 with PolicyNotFoundException

use of org.wso2.carbon.apimgt.api.PolicyNotFoundException in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingPoliciesCustomPost.

/**
 * Add an Global 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 throttlingPoliciesCustomPost(String contentType, CustomRuleDTO body, MessageContext messageContext) throws APIManagementException {
    RestApiAdminUtils.validateCustomRuleRequiredProperties(body, (String) messageContext.get(Message.HTTP_REQUEST_METHOD));
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        // only super tenant is allowed to access global policies/custom rules
        checkTenantDomainForCustomRules();
        GlobalPolicy globalPolicy = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyDTOToModel(body);
        // Check if there's a policy exists before adding the new policy
        try {
            Policy policyIfExists = apiProvider.getGlobalPolicy(globalPolicy.getPolicyName());
            if (policyIfExists != null) {
                RestApiUtil.handleResourceAlreadyExistsError("Custom rule with name " + globalPolicy.getPolicyName() + " already exists", log);
            }
        } catch (PolicyNotFoundException ignore) {
        }
        // Add the policy
        apiProvider.addPolicy(globalPolicy);
        // retrieve the new policy and send back as the response
        GlobalPolicy newGlobalPolicy = apiProvider.getGlobalPolicy(body.getPolicyName());
        CustomRuleDTO policyDTO = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyToDTO(newGlobalPolicy);
        return Response.created(new URI(RestApiConstants.RESOURCE_PATH_THROTTLING_POLICIES_GLOBAL + "/" + policyDTO.getPolicyId())).entity(policyDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding a custom rule: " + body.getPolicyName();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving Global 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) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) PolicyNotFoundException(org.wso2.carbon.apimgt.api.PolicyNotFoundException) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI)

Example 12 with PolicyNotFoundException

use of org.wso2.carbon.apimgt.api.PolicyNotFoundException 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)

Example 13 with PolicyNotFoundException

use of org.wso2.carbon.apimgt.api.PolicyNotFoundException in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingPoliciesAdvancedPolicyIdGet.

/**
 * Get a specific Advanced Level Policy
 *
 * @param policyId        uuid of the policy
 * @return Required policy specified by name
 */
@Override
public Response throttlingPoliciesAdvancedPolicyIdGet(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
        APIPolicy apiPolicy = apiProvider.getAPIPolicyByUUID(policyId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, apiPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_ADVANCED_POLICY, policyId, log);
        }
        AdvancedThrottlePolicyDTO policyDTO = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(apiPolicy);
        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 retrieving Advanced level policy : " + policyId;
            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)

Example 14 with PolicyNotFoundException

use of org.wso2.carbon.apimgt.api.PolicyNotFoundException in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingPoliciesApplicationPost.

/**
 * Add an Application Level Throttle Policy
 *
 * @param body        DTO of the Application Policy to add
 * @param contentType Content-Type header
 * @return Newly created Application Throttle Policy with the location with the Location header
 */
@Override
public Response throttlingPoliciesApplicationPost(String contentType, ApplicationThrottlePolicyDTO body, MessageContext messageContext) throws APIManagementException {
    RestApiAdminUtils.validateThrottlePolicyNameProperty(body.getPolicyName());
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.getLoggedInUsername();
        ApplicationPolicy appPolicy = ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyDTOToModel(body);
        // Check if there's a policy exists before adding the new policy
        try {
            Policy policyIfExists = apiProvider.getApplicationPolicy(username, appPolicy.getPolicyName());
            if (policyIfExists != null) {
                RestApiUtil.handleResourceAlreadyExistsError("Application Policy with name " + appPolicy.getPolicyName() + " already exists", log);
            }
        } catch (PolicyNotFoundException ignore) {
        }
        // Add the policy
        apiProvider.addPolicy(appPolicy);
        // retrieve the new policy and send back as the response
        ApplicationPolicy newAppPolicy = apiProvider.getApplicationPolicy(username, body.getPolicyName());
        ApplicationThrottlePolicyDTO policyDTO = ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyToDTO(newAppPolicy);
        return Response.created(new URI(RestApiConstants.RESOURCE_PATH_THROTTLING_POLICIES_APPLICATION + "/" + policyDTO.getPolicyId())).entity(policyDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding an Application level policy: " + body.getPolicyName();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving Application 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) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) PolicyNotFoundException(org.wso2.carbon.apimgt.api.PolicyNotFoundException) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI)

Example 15 with PolicyNotFoundException

use of org.wso2.carbon.apimgt.api.PolicyNotFoundException in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingPoliciesAdvancedPost.

/**
 * Add an Advanced 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 throttlingPoliciesAdvancedPost(String contentType, AdvancedThrottlePolicyDTO body, MessageContext messageContext) throws APIManagementException {
    RestApiAdminUtils.validateThrottlePolicyNameProperty(body.getPolicyName());
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String userName = RestApiCommonUtil.getLoggedInUsername();
        APIPolicy apiPolicy = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(body);
        // Check if there's a policy exists before adding the new policy
        try {
            Policy policyIfExists = apiProvider.getAPIPolicy(userName, apiPolicy.getPolicyName());
            if (policyIfExists != null) {
                RestApiUtil.handleResourceAlreadyExistsError("Advanced Policy with name " + apiPolicy.getPolicyName() + " already exists", log);
            }
        } catch (PolicyNotFoundException ignore) {
        }
        // Add the policy
        apiProvider.addPolicy(apiPolicy);
        // retrieve the new policy and send back as the response
        APIPolicy newApiPolicy = apiProvider.getAPIPolicy(userName, body.getPolicyName());
        AdvancedThrottlePolicyDTO policyDTO = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(newApiPolicy);
        return Response.created(new URI(RestApiConstants.RESOURCE_PATH_THROTTLING_POLICIES_ADVANCED + "/" + policyDTO.getPolicyId())).entity(policyDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding an Advanced level policy: " + body.getPolicyName();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving Advanced 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) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) PolicyNotFoundException(org.wso2.carbon.apimgt.api.PolicyNotFoundException) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI)

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