Search in sources :

Example 1 with CustomRuleListDTO

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;
}
Also used : CustomRuleDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) ArrayList(java.util.ArrayList) CustomRuleListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleListDTO)

Example 2 with CustomRuleListDTO

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();
    }
}
Also used : APIMgtAdminService(org.wso2.carbon.apimgt.core.api.APIMgtAdminService) CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 3 with CustomRuleListDTO

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());
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) ArrayList(java.util.ArrayList) CustomRuleListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleListDTO) Test(org.testng.annotations.Test)

Example 4 with CustomRuleListDTO

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;
}
Also used : GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) ArrayList(java.util.ArrayList) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl)

Example 5 with CustomRuleListDTO

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;
}
Also used : CustomRuleDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleDTO) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) ArrayList(java.util.ArrayList) CustomRuleListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleListDTO)

Aggregations

ArrayList (java.util.ArrayList)4 CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)3 GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)2 CustomRuleListDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleListDTO)2 Test (org.testng.annotations.Test)1 APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)1 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)1 Policy (org.wso2.carbon.apimgt.api.model.policy.Policy)1 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)1 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 APIAdminImpl (org.wso2.carbon.apimgt.impl.APIAdminImpl)1 CustomRuleDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO)1 CustomRuleDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleDTO)1 CustomRuleListDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleListDTO)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1