use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleDTO in project carbon-apimgt by wso2.
the class PoliciesApiServiceImplTest method policiesThrottlingCustomPostTest.
@Test
public void policiesThrottlingCustomPostTest() throws APIManagementException, NotFoundException {
printTestMethodName();
PoliciesApiServiceImpl policiesApiService = new PoliciesApiServiceImpl();
CustomRuleDTO dto = new CustomRuleDTO();
String uuid = UUID.randomUUID().toString();
dto.setId(uuid);
CustomPolicy policy = CustomPolicyMappingUtil.fromCustomPolicyDTOToModel(dto);
APIMgtAdminServiceImpl adminService = Mockito.mock(APIMgtAdminServiceImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.mockStatic(CustomPolicyMappingUtil.class);
PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(adminService);
Mockito.doReturn(uuid).doThrow(new IllegalArgumentException()).when(adminService).addCustomRule(policy);
Mockito.doReturn(policy).doThrow(new IllegalArgumentException()).when(adminService).getCustomRuleByUUID(uuid);
PowerMockito.when(CustomPolicyMappingUtil.fromCustomPolicyToDTO(policy)).thenReturn(dto);
Response response = policiesApiService.policiesThrottlingCustomPost(dto, getRequest());
Assert.assertEquals(201, response.getStatus());
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleDTO 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.rest.api.admin.v1.dto.CustomRuleDTO 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.rest.api.admin.v1.dto.CustomRuleDTO 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.rest.api.admin.v1.dto.CustomRuleDTO 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();
}
}
Aggregations