Search in sources :

Example 1 with AdvancedThrottlePolicyListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyListDTO in project carbon-apimgt by wso2.

the class AdvancedThrottlePolicyMappingUtil method fromAPIPolicyArrayToListDTO.

/**
 * Converts an array of Advanced Policy objects into a List DTO
 *
 * @param policies Array of Advanced Policies
 * @return A List DTO of converted Advanced Policies
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 * @throws UnsupportedThrottleConditionTypeException - If error occurs
 */
public static AdvancedThrottlePolicyListDTO fromAPIPolicyArrayToListDTO(List<APIPolicy> policies) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    AdvancedThrottlePolicyListDTO listDTO = new AdvancedThrottlePolicyListDTO();
    List<AdvancedThrottlePolicyDTO> advancedPolicyDTOs = new ArrayList<>();
    if (policies != null) {
        for (APIPolicy policy : policies) {
            advancedPolicyDTOs.add(fromAdvancedPolicyToDTO(policy));
        }
    }
    listDTO.setList(advancedPolicyDTOs);
    listDTO.setCount(advancedPolicyDTOs.size());
    return listDTO;
}
Also used : AdvancedThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyListDTO) ArrayList(java.util.ArrayList) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 2 with AdvancedThrottlePolicyListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyListDTO in project carbon-apimgt by wso2.

the class AdvancedThrottlePolicyMappingUtilTestCase method fromAPIPolicyArrayToListDTOTest.

@Test(description = "Convert Policy list to Policy List DTO object")
public void fromAPIPolicyArrayToListDTOTest() throws Exception {
    APIPolicy policy1 = SampleTestObjectCreator.createAPIPolicyWithRequestLimit("Gold1");
    APIPolicy policy2 = SampleTestObjectCreator.createAPIPolicyWithRequestLimit("Gold2");
    List<APIPolicy> policyList = new ArrayList<>();
    policyList.add(policy1);
    policyList.add(policy2);
    AdvancedThrottlePolicyListDTO listDTO = AdvancedThrottlePolicyMappingUtil.fromAPIPolicyArrayToListDTO(policyList);
    Assert.assertEquals((Integer) policyList.size(), listDTO.getCount());
    Assert.assertEquals(policy1.getDisplayName(), listDTO.getList().get(0).getDisplayName());
    Assert.assertEquals(policy1.getDescription(), listDTO.getList().get(0).getDescription());
    Assert.assertEquals(policy1.getDefaultQuotaPolicy().getType(), "requestCount");
    Assert.assertEquals(policy1.getDefaultQuotaPolicy().getLimit().getTimeUnit(), listDTO.getList().get(0).getDefaultLimit().getTimeUnit());
    Assert.assertEquals((Integer) policy1.getDefaultQuotaPolicy().getLimit().getUnitTime(), listDTO.getList().get(0).getDefaultLimit().getUnitTime());
    Assert.assertEquals((Integer) ((RequestCountLimit) policy1.getDefaultQuotaPolicy().getLimit()).getRequestCount(), listDTO.getList().get(0).getDefaultLimit().getRequestCountLimit().getRequestCount());
    Assert.assertEquals(policy2.getDisplayName(), listDTO.getList().get(1).getDisplayName());
    Assert.assertEquals(policy2.getDescription(), listDTO.getList().get(1).getDescription());
    Assert.assertEquals(policy2.getDefaultQuotaPolicy().getType(), "requestCount");
    Assert.assertEquals(policy2.getDefaultQuotaPolicy().getLimit().getTimeUnit(), listDTO.getList().get(1).getDefaultLimit().getTimeUnit());
    Assert.assertEquals((Integer) policy2.getDefaultQuotaPolicy().getLimit().getUnitTime(), listDTO.getList().get(1).getDefaultLimit().getUnitTime());
    Assert.assertEquals((Integer) ((RequestCountLimit) policy2.getDefaultQuotaPolicy().getLimit()).getRequestCount(), listDTO.getList().get(1).getDefaultLimit().getRequestCountLimit().getRequestCount());
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) ArrayList(java.util.ArrayList) AdvancedThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyListDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Test(org.testng.annotations.Test)

Example 3 with AdvancedThrottlePolicyListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyListDTO in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingAdvancedGet.

/**
 * Get policies
 *
 * @param ifNoneMatch       If-None-Match header value
 * @param ifModifiedSince   If-Modified-Since header value
 * @param request           msf4j request object
 * @return Response object
 * @throws NotFoundException if an error occurred when particular resource does not exits in the system.
 */
@Override
public Response policiesThrottlingAdvancedGet(String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Advance Throttle Policy GET request");
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        List<APIPolicy> policies = apiMgtAdminService.getApiPolicies();
        AdvancedThrottlePolicyListDTO advancedThrottlePolicyListDTO = AdvancedThrottlePolicyMappingUtil.fromAPIPolicyArrayToListDTO(policies);
        return Response.ok().entity(advancedThrottlePolicyListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while retrieving Advance 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) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 4 with AdvancedThrottlePolicyListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyListDTO in project carbon-apimgt by wso2.

the class ThrottlingApiServiceImpl method throttlingPoliciesAdvancedGet.

/**
 * Retrieves all Advanced level policies
 *
 * @param accept          Accept header value
 * @return All matched Advanced Throttle policies to the given request
 */
@Override
public Response throttlingPoliciesAdvancedGet(String accept, MessageContext messageContext) {
    try {
        APIAdmin apiAdmin = new APIAdminImpl();
        String userName = RestApiCommonUtil.getLoggedInUsername();
        int tenantId = APIUtil.getTenantId(userName);
        Policy[] apiPolicies = apiAdmin.getPolicies(tenantId, PolicyConstants.POLICY_LEVEL_API);
        List<APIPolicy> policies = new ArrayList<>();
        for (Policy policy : apiPolicies) {
            policies.add((APIPolicy) policy);
        }
        AdvancedThrottlePolicyListDTO listDTO = AdvancedThrottlePolicyMappingUtil.fromAPIPolicyArrayToListDTO(policies.toArray(new APIPolicy[policies.size()]));
        return Response.ok().entity(listDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving Advanced 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) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) ArrayList(java.util.ArrayList) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy)

Example 5 with AdvancedThrottlePolicyListDTO

use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyListDTO in project carbon-apimgt by wso2.

the class AdvancedThrottlePolicyMappingUtil method fromAPIPolicyArrayToListDTO.

/**
 * Converts an array of Advanced Policy objects into a List DTO
 *
 * @param apiPolicies Array of Advanced Policies
 * @return A List DTO of converted Advanced Policies
 * @throws UnsupportedThrottleLimitTypeException
 * @throws UnsupportedThrottleConditionTypeException
 */
public static AdvancedThrottlePolicyListDTO fromAPIPolicyArrayToListDTO(APIPolicy[] apiPolicies) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    AdvancedThrottlePolicyListDTO listDTO = new AdvancedThrottlePolicyListDTO();
    List<AdvancedThrottlePolicyInfoDTO> advancedPolicyDTOs = new ArrayList<>();
    if (apiPolicies != null) {
        for (APIPolicy apiPolicy : apiPolicies) {
            advancedPolicyDTOs.add(fromAdvancedPolicyToInfoDTO(apiPolicy));
        }
    }
    listDTO.setList(advancedPolicyDTOs);
    listDTO.setCount(advancedPolicyDTOs.size());
    return listDTO;
}
Also used : AdvancedThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyListDTO) ArrayList(java.util.ArrayList) AdvancedThrottlePolicyInfoDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyInfoDTO) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy)

Aggregations

ArrayList (java.util.ArrayList)4 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)3 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)2 AdvancedThrottlePolicyListDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyListDTO)2 Test (org.testng.annotations.Test)1 APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)1 GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)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 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)1 APIAdminImpl (org.wso2.carbon.apimgt.impl.APIAdminImpl)1 AdvancedThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO)1 AdvancedThrottlePolicyInfoDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyInfoDTO)1 AdvancedThrottlePolicyListDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyListDTO)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1