use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingCustomRuleIdPut.
/**
* 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 ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return Updated policy
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingCustomRuleIdPut(String ruleId, CustomRuleDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
if (log.isDebugEnabled()) {
log.debug("Received Custom Policy PUT request " + body + " with rule ID = " + ruleId);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
CustomPolicy customPolicy = CustomPolicyMappingUtil.fromCustomPolicyDTOToModel(body);
customPolicy.setUuid(ruleId);
apiMgtAdminService.updateCustomRule(customPolicy);
return Response.status(Response.Status.OK).entity(CustomPolicyMappingUtil.fromCustomPolicyToDTO(apiMgtAdminService.getCustomRuleByUUID(ruleId))).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while updating Custom Policy. policy ID: " + ruleId;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingCustomRuleIdGet.
/**
* Get a specific custom rule by its id.
*
* @param ruleId uuid of the policy
* @param ifNoneMatch If-None-Match header value
* @param ifModifiedSince If-Modified-Since header value
* @param request msf4j request object
* @return Matched Global Throttle Policy by the given name
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingCustomRuleIdGet(String ruleId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
if (log.isDebugEnabled()) {
log.debug("Received Custom Policy GET request with rule ID = " + ruleId);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
CustomPolicy customPolicy = apiMgtAdminService.getCustomRuleByUUID(ruleId);
CustomRuleDTO dto = CustomPolicyMappingUtil.fromCustomPolicyToDTO(customPolicy);
return Response.status(Response.Status.OK).entity(dto).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while getting custom policy. policy uuid: " + ruleId;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class CustomPolicyMappingUtil method fromCustomPolicyDTOToModel.
/**
* Converts a single Custom Policy DTO object into model object.
*
* @param dto Custom Policy DTO object
* @return Model object derived from DTO
* @throws UnsupportedThrottleLimitTypeException - If error occurs
*/
public static CustomPolicy fromCustomPolicyDTOToModel(CustomRuleDTO dto) throws UnsupportedThrottleLimitTypeException {
CustomPolicy customPolicy = new CustomPolicy(dto.getPolicyName());
customPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, customPolicy);
customPolicy.setKeyTemplate(dto.getKeyTemplate());
customPolicy.setSiddhiQuery(dto.getSiddhiQuery());
return customPolicy;
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultCustomPolicy.
/**
* Create a custom policy.
*
* @return CustomPolicy object
*/
protected static CustomPolicy createDefaultCustomPolicy() {
CustomPolicy customPolicy = new CustomPolicy(SAMPLE_CUSTOM_RULE);
customPolicy.setKeyTemplate("$userId");
String siddhiQuery = "FROM RequestStream SELECT userId, ( userId == 'admin@carbon.super' ) AS isEligible , " + "str:concat('admin@carbon.super','') as throttleKey INSERT INTO EligibilityStream;" + "FROM EligibilityStream[isEligible==true]#throttler:timeBatch(1 min) SELECT throttleKey, " + "(count(userId) >= 5 as isThrottled, expiryTimeStamp group by throttleKey INSERT ALL EVENTS into " + "ResultStream;";
customPolicy.setSiddhiQuery(siddhiQuery);
customPolicy.setDescription("Sample custom policy");
return customPolicy;
}
use of org.wso2.carbon.apimgt.core.models.policy.CustomPolicy in project carbon-apimgt by wso2.
the class PolicyDAOImplIT method testGetCustomPolicies.
@Test
public void testGetCustomPolicies() throws Exception {
PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
int size = policyDAO.getCustomPolicies().size();
CustomPolicy customPolicy = SampleTestObjectCreator.createDefaultCustomPolicy();
policyDAO.addCustomPolicy(customPolicy);
Assert.assertTrue(policyDAO.getCustomPolicies().size() == size + 1);
}
Aggregations