Search in sources :

Example 1 with GlobalPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy in project carbon-apimgt by wso2.

the class CustomPolicyMappingUtil method fromCustomPolicyToDTO.

/**
 * Converts a single Custom Policy model object into DTO object.
 *
 * @param globalPolicy Custom Policy model object
 * @return DTO object derived from the Policy model object
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 */
public static CustomRuleDTO fromCustomPolicyToDTO(CustomPolicy globalPolicy) throws UnsupportedThrottleLimitTypeException {
    CustomRuleDTO policyDTO = new CustomRuleDTO();
    policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(globalPolicy, policyDTO);
    policyDTO.setKeyTemplate(globalPolicy.getKeyTemplate());
    policyDTO.setSiddhiQuery(globalPolicy.getSiddhiQuery());
    return policyDTO;
}
Also used : CustomRuleDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO)

Example 2 with GlobalPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingPoliciesCustomRuleIdGet.

/**
 * Get a specific custom rule by its name
 *
 * @param ruleId          uuid of the policy
 * @return Matched Global Throttle Policy by the given name
 */
@Override
public Response throttlingPoliciesCustomRuleIdGet(String ruleId, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 3 with GlobalPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingPoliciesCustomRuleIdPut.

/**
 * 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
 * @return Updated policy
 */
@Override
public Response throttlingPoliciesCustomRuleIdPut(String ruleId, String contentType, CustomRuleDTO body, MessageContext messageContext) throws APIManagementException {
    RestApiAdminUtils.validateCustomRuleRequiredProperties(body, (String) messageContext.get(Message.HTTP_REQUEST_METHOD));
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String username = RestApiCommonUtil.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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 4 with GlobalPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy in project carbon-apimgt by wso2.

the class GlobalThrottlePolicyMappingUtil method fromGlobalThrottlePolicyToDTO.

/**
 * Converts a single Global Policy model object into DTO object
 *
 * @param globalPolicy Global Policy model object
 * @return DTO object derived from the Policy model object
 * @throws UnsupportedThrottleLimitTypeException
 */
public static CustomRuleDTO fromGlobalThrottlePolicyToDTO(GlobalPolicy globalPolicy) throws UnsupportedThrottleLimitTypeException {
    CustomRuleDTO policyDTO = new CustomRuleDTO();
    policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(globalPolicy, policyDTO);
    policyDTO.setKeyTemplate(globalPolicy.getKeyTemplate());
    policyDTO.setSiddhiQuery(globalPolicy.getSiddhiQuery());
    policyDTO.setType(CUSTOM_RULE_THROTTLING_POLICY_TYPE);
    return policyDTO;
}
Also used : CustomRuleDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleDTO)

Example 5 with GlobalPolicy

use of org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy in project carbon-apimgt by wso2.

the class APIMgtDAOTest method testAddAndGetGlobalPolicy.

@Test
public void testAddAndGetGlobalPolicy() throws Exception {
    GlobalPolicy globalPolicy = new GlobalPolicy("testAddAndGetGlobalPolicy");
    globalPolicy.setTenantId(-1234);
    globalPolicy.setKeyTemplate("$user");
    globalPolicy.setSiddhiQuery("Select From 1");
    apiMgtDAO.addGlobalPolicy(globalPolicy);
    GlobalPolicy retrievedGlobalPolicyFromName = apiMgtDAO.getGlobalPolicy("testAddAndGetGlobalPolicy");
    assertTrue(apiMgtDAO.isKeyTemplatesExist(globalPolicy));
    GlobalPolicy retrievedFromUUID = apiMgtDAO.getGlobalPolicyByUUID(retrievedGlobalPolicyFromName.getUUID());
    assertEquals(retrievedGlobalPolicyFromName.getKeyTemplate(), retrievedFromUUID.getKeyTemplate());
    assertTrue(apiMgtDAO.getGlobalPolicies(-1234).length > 0);
    assertTrue(apiMgtDAO.getGlobalPolicyKeyTemplates(-1234).contains("$user"));
    apiMgtDAO.updateGlobalPolicy(globalPolicy);
    retrievedFromUUID.setPolicyName(null);
    apiMgtDAO.updateGlobalPolicy(retrievedFromUUID);
    apiMgtDAO.setPolicyDeploymentStatus(PolicyConstants.POLICY_LEVEL_GLOBAL, globalPolicy.getPolicyName(), -1234, true);
    assertTrue(apiMgtDAO.isPolicyDeployed(PolicyConstants.POLICY_LEVEL_GLOBAL, -1234, globalPolicy.getPolicyName()));
    assertTrue(apiMgtDAO.getPolicyNames(PolicyConstants.POLICY_LEVEL_GLOBAL, "admin").length > 0);
    assertTrue(apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_GLOBAL, -1234, globalPolicy.getPolicyName()));
    apiMgtDAO.removeThrottlePolicy(PolicyConstants.POLICY_LEVEL_GLOBAL, "testAddAndGetGlobalPolicy", -1234);
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)19 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 GlobalPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.GlobalPolicy)9 InputStream (java.io.InputStream)7 GlobalPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 SQLException (java.sql.SQLException)6 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)6 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)6 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)6 ResultSet (java.sql.ResultSet)5 APIPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)4 ApplicationPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent)4 SubscriptionPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent)4