use of org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleListDTO in project carbon-apimgt by wso2.
the class CustomPolicyMappingUtil method fromCustomPolicyArrayToListDTO.
/**
* Converts an array of Custom Policy model objects into REST API DTO objects.
*
* @param customPolicies An array of custom policy model objects
* @return A List DTO of Custom Policy DTOs derived from the array of model objects
* @throws UnsupportedThrottleLimitTypeException - If error occurs
*/
public static CustomRuleListDTO fromCustomPolicyArrayToListDTO(List<CustomPolicy> customPolicies) throws UnsupportedThrottleLimitTypeException {
CustomRuleListDTO listDTO = new CustomRuleListDTO();
List<CustomRuleDTO> customPolicyDTOList = new ArrayList<>();
if (customPolicies != null) {
for (CustomPolicy policy : customPolicies) {
CustomRuleDTO dto = fromCustomPolicyToDTO(policy);
customPolicyDTOList.add(dto);
}
}
listDTO.setCount(customPolicyDTOList.size());
listDTO.setList(customPolicyDTOList);
return listDTO;
}
use of org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleListDTO in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingCustomGet.
/**
* Retrieves all custom policies.
*
* @param ifNoneMatch If-None-Match header value
* @param ifModifiedSince If-Modified-Since header value
* @param request msf4j request object
* @return All matched Global Throttle policies to the given request
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingCustomGet(String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
if (log.isDebugEnabled()) {
log.debug("Received Custom Policy GET request.");
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
List<CustomPolicy> policies = apiMgtAdminService.getCustomRules();
CustomRuleListDTO customRuleListDTO = CustomPolicyMappingUtil.fromCustomPolicyArrayToListDTO(policies);
return Response.ok().entity(customRuleListDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while retrieving custom policies";
org.wso2.carbon.apimgt.rest.api.common.dto.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.dto.CustomRuleListDTO in project carbon-apimgt by wso2.
the class CustomPolicyMappingUtilTest method fromCustomPolicyArrayToListDTOTest.
@Test(description = "Convert List<CustomPolicy> to CustomRuleListDTO")
public void fromCustomPolicyArrayToListDTOTest() throws Exception {
CustomPolicy customPolicy1 = new CustomPolicy("namw1");
customPolicy1.setDescription("custom policy desc1");
customPolicy1.setKeyTemplate("keytemplate1");
customPolicy1.setSiddhiQuery("sample query1");
CustomPolicy customPolicy2 = new CustomPolicy("namw2");
customPolicy2.setDescription("custom policy desc2");
customPolicy2.setKeyTemplate("keytemplate2");
customPolicy2.setSiddhiQuery("sample query2");
List<CustomPolicy> customPolicyList = new ArrayList<>();
customPolicyList.add(customPolicy1);
customPolicyList.add(customPolicy2);
CustomRuleListDTO listDTO = CustomPolicyMappingUtil.fromCustomPolicyArrayToListDTO(customPolicyList);
Assert.assertEquals((Integer) customPolicyList.size(), listDTO.getCount());
Assert.assertEquals(customPolicy1.getPolicyName(), listDTO.getList().get(0).getPolicyName());
Assert.assertEquals(customPolicy1.getDisplayName(), listDTO.getList().get(0).getDisplayName());
Assert.assertEquals(customPolicy1.getKeyTemplate(), listDTO.getList().get(0).getKeyTemplate());
Assert.assertEquals(customPolicy1.getSiddhiQuery(), listDTO.getList().get(0).getSiddhiQuery());
Assert.assertEquals(customPolicy1.getDescription(), listDTO.getList().get(0).getDescription());
Assert.assertEquals(customPolicy2.getPolicyName(), listDTO.getList().get(1).getPolicyName());
Assert.assertEquals(customPolicy2.getDisplayName(), listDTO.getList().get(1).getDisplayName());
Assert.assertEquals(customPolicy2.getKeyTemplate(), listDTO.getList().get(1).getKeyTemplate());
Assert.assertEquals(customPolicy2.getSiddhiQuery(), listDTO.getList().get(1).getSiddhiQuery());
Assert.assertEquals(customPolicy2.getDescription(), listDTO.getList().get(1).getDescription());
}
use of org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleListDTO in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingPoliciesCustomGet.
/**
* Retrieves all Global level policies
*
* @param accept Accept header value
* @return All matched Global Throttle policies to the given request
*/
@Override
public Response throttlingPoliciesCustomGet(String accept, MessageContext messageContext) {
try {
APIAdmin apiAdmin = new APIAdminImpl();
String userName = RestApiCommonUtil.getLoggedInUsername();
int tenantId = APIUtil.getTenantId(userName);
// only super tenant is allowed to access global policies/custom rules
checkTenantDomainForCustomRules();
Policy[] globalPolicies = apiAdmin.getPolicies(tenantId, PolicyConstants.POLICY_LEVEL_GLOBAL);
List<GlobalPolicy> policies = new ArrayList<>();
for (Policy policy : globalPolicies) {
policies.add((GlobalPolicy) policy);
}
CustomRuleListDTO listDTO = GlobalThrottlePolicyMappingUtil.fromGlobalPolicyArrayToListDTO(policies.toArray(new GlobalPolicy[policies.size()]));
return Response.ok().entity(listDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving Global level policies";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleListDTO in project carbon-apimgt by wso2.
the class GlobalThrottlePolicyMappingUtil method fromGlobalPolicyArrayToListDTO.
/**
* Converts an array of Global policy model objects into REST API DTO objects
*
* @param GlobalPolicies An array of Global Policy model objects
* @return A List DTO of Global Policy DTOs derived from the array of model objects
* @throws UnsupportedThrottleLimitTypeException
*/
public static CustomRuleListDTO fromGlobalPolicyArrayToListDTO(GlobalPolicy[] GlobalPolicies) throws UnsupportedThrottleLimitTypeException {
CustomRuleListDTO listDTO = new CustomRuleListDTO();
List<CustomRuleDTO> globalPolicyDTOList = new ArrayList<>();
if (GlobalPolicies != null) {
for (GlobalPolicy policy : GlobalPolicies) {
CustomRuleDTO dto = fromGlobalThrottlePolicyToDTO(policy);
globalPolicyDTOList.add(dto);
}
}
listDTO.setCount(globalPolicyDTOList.size());
listDTO.setList(globalPolicyDTOList);
return listDTO;
}
Aggregations