use of org.wso2.carbon.apimgt.api.model.subscription.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;
}
use of org.wso2.carbon.apimgt.api.model.subscription.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;
}
use of org.wso2.carbon.apimgt.api.model.subscription.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;
}
use of org.wso2.carbon.apimgt.api.model.subscription.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;
}
use of org.wso2.carbon.apimgt.api.model.subscription.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);
}
Aggregations