Java Code Examples for org.wso2.carbon.apimgt.api.APIProvider#getGlobalPolicyByUUID()

The following examples show how to use org.wso2.carbon.apimgt.api.APIProvider#getGlobalPolicyByUUID() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThrottlingApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Get a specific custom rule by its name
 *
 * @param ruleId          uuid of the policy
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @return Matched Global Throttle Policy by the given name
 */
@Override
public Response throttlingPoliciesCustomRuleIdGet(String ruleId, String ifNoneMatch, String ifModifiedSince,
                                                  MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
        String username = RestApiUtil.getLoggedInUsername();

        //only super tenant is allowed to access global policies/custom rules
        checkTenantDomainForCustomRules();

        //This will give PolicyNotFoundException if there's no policy exists with UUID
        GlobalPolicy globalPolicy = apiProvider.getGlobalPolicyByUUID(ruleId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, globalPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, log);
        }
        CustomRuleDTO policyDTO = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyToDTO(globalPolicy);
        return Response.ok().entity(policyDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, e, log);
        } else {
            String errorMessage = "Error while retrieving Custom Rule: " + ruleId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
 
Example 2
Source File: ThrottlingApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Delete a Global level policy/custom rule specified by uuid
 *
 * @param ruleId            uuid of the policy
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @return 200 OK response if successfully deleted the policy
 */
@Override
public Response throttlingPoliciesCustomRuleIdDelete(String ruleId, String ifMatch, String ifUnmodifiedSince,
                                                     MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();

        //only super tenant is allowed to access global policies/custom rules
        checkTenantDomainForCustomRules();

        String username = RestApiUtil.getLoggedInUsername();

        //This will give PolicyNotFoundException if there's no policy exists with UUID
        GlobalPolicy existingPolicy = apiProvider.getGlobalPolicyByUUID(ruleId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, log);
        }
        apiProvider.deletePolicy(username, PolicyConstants.POLICY_LEVEL_GLOBAL, existingPolicy.getPolicyName());
        return Response.ok().build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, e, log);
        } else {
            String errorMessage = "Error while deleting custom rule : " + ruleId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
 
Example 3
Source File: ThrottlingApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Get a specific custom rule by its name
 *
 * @param ruleId      uuid of the policy
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @return Matched Global Throttle Policy by the given name
 */
@Override
public Response throttlingPoliciesCustomRuleIdGet(String ruleId, String ifNoneMatch,
        String ifModifiedSince) {
    try {
        APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
        String username = RestApiUtil.getLoggedInUsername();

        //only super tenant is allowed to access global policies/custom rules
        checkTenantDomainForCustomRules();

        //This will give PolicyNotFoundException if there's no policy exists with UUID
        GlobalPolicy globalPolicy = apiProvider.getGlobalPolicyByUUID(ruleId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, globalPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, log);
        }
        CustomRuleDTO policyDTO = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyToDTO(globalPolicy);
        return Response.ok().entity(policyDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, e, log);
        } else {
            String errorMessage = "Error while retrieving Custom Rule: " + ruleId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
 
Example 4
Source File: ThrottlingApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Delete a Global level policy/custom rule specified by uuid
 *
 * @param ruleId        uuid of the policy
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @return 200 OK response if successfully deleted the policy
 */
@Override
public Response throttlingPoliciesCustomRuleIdDelete(String ruleId, String ifMatch,
        String ifUnmodifiedSince) {
    try {
        APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();

        //only super tenant is allowed to access global policies/custom rules
        checkTenantDomainForCustomRules();

        String username = RestApiUtil.getLoggedInUsername();

        //This will give PolicyNotFoundException if there's no policy exists with UUID
        GlobalPolicy existingPolicy = apiProvider.getGlobalPolicyByUUID(ruleId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, log);
        }
        apiProvider.deletePolicy(username, PolicyConstants.POLICY_LEVEL_GLOBAL, existingPolicy.getPolicyName());
        return Response.ok().build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, e, log);
        } else {
            String errorMessage = "Error while deleting custom rule : " + ruleId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
 
Example 5
Source File: ThrottlingApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Updates a given Global level policy/custom rule specified by uuid
 *
 * @param ruleId            uuid of the policy
 * @param body              DTO of policy to be updated
 * @param contentType       Content-Type header
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @return Updated policy
 */
@Override
public Response throttlingPoliciesCustomRuleIdPut(String ruleId, CustomRuleDTO body, String contentType,
                                                  String ifMatch, String ifUnmodifiedSince,
                                                  MessageContext messageContext) throws APIManagementException {

    RestApiAdminUtils
            .validateCustomRuleRequiredProperties(body, (String) messageContext.get(Message.HTTP_REQUEST_METHOD));

    try {
        APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
        String username = RestApiUtil.getLoggedInUsername();

        //only super tenant is allowed to access global policies/custom rules
        checkTenantDomainForCustomRules();

        //will give PolicyNotFoundException if there's no policy exists with UUID
        GlobalPolicy existingPolicy = apiProvider.getGlobalPolicyByUUID(ruleId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, log);
        }

        //overridden properties
        body.setPolicyId(ruleId);
        body.setPolicyName(existingPolicy.getPolicyName());

        //update the policy
        GlobalPolicy globalPolicy = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyDTOToModel(body);
        apiProvider.updatePolicy(globalPolicy);

        //retrieve the new policy and send back as the response
        GlobalPolicy newGlobalPolicy = apiProvider.getGlobalPolicyByUUID(ruleId);
        CustomRuleDTO policyDTO = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyToDTO(newGlobalPolicy);
        return Response.ok().entity(policyDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, e, log);
        } else {
            String errorMessage = "Error while updating custom rule: " + body.getPolicyName();
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
 
Example 6
Source File: ThrottlingApiServiceImpl.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Updates a given Global level policy/custom rule specified by uuid
 *
 * @param ruleId        uuid of the policy
 * @param body              DTO of policy to be updated
 * @param contentType       Content-Type header
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @return Updated policy
 */
@Override
public Response throttlingPoliciesCustomRuleIdPut(String ruleId, CustomRuleDTO body, String contentType,
        String ifMatch, String ifUnmodifiedSince) {
    try {
        APIProvider apiProvider = RestApiUtil.getLoggedInUserProvider();
        String username = RestApiUtil.getLoggedInUsername();
        
        //only super tenant is allowed to access global policies/custom rules
        checkTenantDomainForCustomRules();

        //will give PolicyNotFoundException if there's no policy exists with UUID
        GlobalPolicy existingPolicy = apiProvider.getGlobalPolicyByUUID(ruleId);
        if (!RestApiAdminUtils.isPolicyAccessibleToUser(username, existingPolicy)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, log);
        }

        //overridden properties
        body.setPolicyId(ruleId);
        body.setPolicyName(existingPolicy.getPolicyName());

        //update the policy
        GlobalPolicy globalPolicy = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyDTOToModel(
                body);
        apiProvider.updatePolicy(globalPolicy);

        //retrieve the new policy and send back as the response
        GlobalPolicy newGlobalPolicy = apiProvider.getGlobalPolicyByUUID(ruleId);
        CustomRuleDTO policyDTO = GlobalThrottlePolicyMappingUtil
                .fromGlobalThrottlePolicyToDTO(newGlobalPolicy);
        return Response.ok().entity(policyDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_CUSTOM_RULE, ruleId, e, log);
        } else {
            String errorMessage = "Error while updating custom rule: " + body.getPolicyName();
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}