Search in sources :

Example 26 with PolicyDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO 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 27 with PolicyDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO 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)

Example 28 with PolicyDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO 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)

Example 29 with PolicyDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO in project carbon-apimgt by wso2.

the class AdvancedThrottlePolicyMappingUtil method fromAdvancedPolicyToDTO.

/**
 * Converts a single Advanced Policy model into REST API DTO
 *
 * @param apiPolicy Advanced Policy model object
 * @return Converted Advanced policy REST API DTO object
 * @throws UnsupportedThrottleLimitTypeException
 * @throws UnsupportedThrottleConditionTypeException
 */
public static AdvancedThrottlePolicyDTO fromAdvancedPolicyToDTO(APIPolicy apiPolicy) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    AdvancedThrottlePolicyDTO policyDTO = new AdvancedThrottlePolicyDTO();
    policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(apiPolicy, policyDTO);
    List<ConditionalGroupDTO> groupDTOs = CommonThrottleMappingUtil.fromPipelineListToConditionalGroupDTOList(apiPolicy.getPipelines());
    policyDTO.setConditionalGroups(groupDTOs);
    policyDTO.setType(ADVANCED_THROTTLING_POLICY_TYPE);
    if (apiPolicy.getDefaultQuotaPolicy() != null) {
        policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(apiPolicy.getDefaultQuotaPolicy()));
    }
    return policyDTO;
}
Also used : ConditionalGroupDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ConditionalGroupDTO) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyDTO)

Aggregations

APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)11 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)6 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)6 GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)6 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)6 ArrayList (java.util.ArrayList)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 ParseException (org.json.simple.parser.ParseException)4 PolicyNotFoundException (org.wso2.carbon.apimgt.api.PolicyNotFoundException)4 Policy (org.wso2.carbon.apimgt.api.model.policy.Policy)4 AdvancedThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO)3 JSONArray (org.json.simple.JSONArray)2 JSONObject (org.json.simple.JSONObject)2 JSONParser (org.json.simple.parser.JSONParser)2 PolicyDTO (org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO)2 Test (org.testng.annotations.Test)1 ConditionDTO (org.wso2.carbon.apimgt.api.dto.ConditionDTO)1 OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)1