use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleDTO 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.rest.api.admin.v1.dto.CustomRuleDTO 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.rest.api.admin.v1.dto.CustomRuleDTO in project carbon-apimgt by wso2.
the class CustomPolicyMappingUtilTest method fromCustomPolicyDTOToModelTest.
@Test(description = "Convert DTO to Model")
public void fromCustomPolicyDTOToModelTest() throws Exception {
CustomRuleDTO dto = new CustomRuleDTO();
dto.setPolicyName(name);
CustomPolicy policy = CustomPolicyMappingUtil.fromCustomPolicyDTOToModel(dto);
Assert.assertNotNull(policy);
Assert.assertEquals(policy.getPolicyName(), name);
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleDTO in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingPoliciesCustomPost.
/**
* Add an Global 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 throttlingPoliciesCustomPost(String contentType, CustomRuleDTO body, MessageContext messageContext) throws APIManagementException {
RestApiAdminUtils.validateCustomRuleRequiredProperties(body, (String) messageContext.get(Message.HTTP_REQUEST_METHOD));
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// only super tenant is allowed to access global policies/custom rules
checkTenantDomainForCustomRules();
GlobalPolicy globalPolicy = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyDTOToModel(body);
// Check if there's a policy exists before adding the new policy
try {
Policy policyIfExists = apiProvider.getGlobalPolicy(globalPolicy.getPolicyName());
if (policyIfExists != null) {
RestApiUtil.handleResourceAlreadyExistsError("Custom rule with name " + globalPolicy.getPolicyName() + " already exists", log);
}
} catch (PolicyNotFoundException ignore) {
}
// Add the policy
apiProvider.addPolicy(globalPolicy);
// retrieve the new policy and send back as the response
GlobalPolicy newGlobalPolicy = apiProvider.getGlobalPolicy(body.getPolicyName());
CustomRuleDTO policyDTO = GlobalThrottlePolicyMappingUtil.fromGlobalThrottlePolicyToDTO(newGlobalPolicy);
return Response.created(new URI(RestApiConstants.RESOURCE_PATH_THROTTLING_POLICIES_GLOBAL + "/" + policyDTO.getPolicyId())).entity(policyDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while adding a custom rule: " + body.getPolicyName();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving Global Throttle policy location : " + body.getPolicyName();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleDTO in project carbon-apimgt by wso2.
the class GlobalThrottlePolicyMappingUtil method fromGlobalThrottlePolicyDTOToModel.
/**
* Converts a single Global policy DTO object into model object
*
* @param dto Global policy DTO object
* @return Model object derived from DTO
* @throws UnsupportedThrottleLimitTypeException
*/
public static GlobalPolicy fromGlobalThrottlePolicyDTOToModel(CustomRuleDTO dto) throws UnsupportedThrottleLimitTypeException {
// update mandatory fields such as tenantDomain etc.
dto = CommonThrottleMappingUtil.updateDefaultMandatoryFieldsOfThrottleDTO(dto);
GlobalPolicy globalPolicy = new GlobalPolicy(dto.getPolicyName());
globalPolicy = CommonThrottleMappingUtil.updateFieldsFromDTOToPolicy(dto, globalPolicy);
globalPolicy.setKeyTemplate(dto.getKeyTemplate());
globalPolicy.setSiddhiQuery(dto.getSiddhiQuery());
return globalPolicy;
}
Aggregations