Search in sources :

Example 1 with SubscriptionThrottlePolicyListDTO

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

the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionPolicyArrayToListDTO.

/**
 * Converts an array of Subscription Policy objects into a List DTO
 *
 * @param subscriptionPolicies Array of Subscription Policies
 * @return A List DTO of converted Subscription Policies
 * @throws SubscriptionThrottlePolicyException - If error occurs
 */
public static SubscriptionThrottlePolicyListDTO fromSubscriptionPolicyArrayToListDTO(List<SubscriptionPolicy> subscriptionPolicies) throws SubscriptionThrottlePolicyException {
    SubscriptionThrottlePolicyListDTO listDTO = new SubscriptionThrottlePolicyListDTO();
    List<SubscriptionThrottlePolicyDTO> subscriptionPolicyDTOList = new ArrayList<>();
    if (subscriptionPolicies != null) {
        for (SubscriptionPolicy policy : subscriptionPolicies) {
            SubscriptionThrottlePolicyDTO dto = fromSubscriptionThrottlePolicyToDTO(policy);
            subscriptionPolicyDTOList.add(dto);
        }
    }
    listDTO.setCount(subscriptionPolicyDTOList.size());
    listDTO.setList(subscriptionPolicyDTOList);
    return listDTO;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO) ArrayList(java.util.ArrayList) SubscriptionThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyListDTO)

Example 2 with SubscriptionThrottlePolicyListDTO

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

the class SubscriptionThrottlePolicyMappingUtilTest method fromSubscriptionPolicyArrayToListDTOTest.

@Test(description = "Convert Subscription Throttle Policy List to List DTO")
public void fromSubscriptionPolicyArrayToListDTOTest() throws Exception {
    List<SubscriptionPolicy> subscriptionPolicies = new ArrayList<>();
    SubscriptionPolicy policy1 = SampleTestObjectCreator.createSubscriptionPolicyWithRequestLimit("Gold1");
    SubscriptionPolicy policy2 = SampleTestObjectCreator.createSubscriptionPolicyWithRequestLimit("Gold2");
    subscriptionPolicies.add(policy1);
    subscriptionPolicies.add(policy2);
    SubscriptionThrottlePolicyListDTO listDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionPolicyArrayToListDTO(subscriptionPolicies);
    assertEquals(listDTO.getCount(), (Integer) subscriptionPolicies.size());
    assertEquals(listDTO.getList().get(0).getPolicyName(), policy1.getPolicyName());
    assertEquals(listDTO.getList().get(0).getDescription(), policy1.getDescription());
    assertEquals(listDTO.getList().get(0).getDefaultLimit().getUnitTime(), (Integer) policy1.getDefaultQuotaPolicy().getLimit().getUnitTime());
    assertEquals(listDTO.getList().get(0).getDefaultLimit().getRequestCountLimit().getRequestCount(), (Integer) ((RequestCountLimit) policy1.getDefaultQuotaPolicy().getLimit()).getRequestCount());
    assertEquals(listDTO.getList().get(1).getPolicyName(), policy2.getPolicyName());
    assertEquals(listDTO.getList().get(1).getDescription(), policy2.getDescription());
    assertEquals(listDTO.getList().get(1).getDefaultLimit().getUnitTime(), (Integer) policy2.getDefaultQuotaPolicy().getLimit().getUnitTime());
    assertEquals(listDTO.getList().get(1).getDefaultLimit().getRequestCountLimit().getRequestCount(), (Integer) ((RequestCountLimit) policy2.getDefaultQuotaPolicy().getLimit()).getRequestCount());
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ArrayList(java.util.ArrayList) SubscriptionThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyListDTO) Test(org.testng.annotations.Test)

Example 3 with SubscriptionThrottlePolicyListDTO

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

the class PoliciesApiServiceImpl method policiesThrottlingSubscriptionGet.

/**
 * Get subscription level 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 policiesThrottlingSubscriptionGet(String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.debug("Received Application Throttle Policy GET request");
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        List<SubscriptionPolicy> policies = apiMgtAdminService.getSubscriptionPolicies();
        SubscriptionThrottlePolicyListDTO subscriptionThrottlePolicyListDTO = SubscriptionThrottlePolicyMappingUtil.fromSubscriptionPolicyArrayToListDTO(policies);
        return Response.ok().entity(subscriptionThrottlePolicyListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while retrieving Application 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) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 4 with SubscriptionThrottlePolicyListDTO

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

the class SubscriptionThrottlePolicyMappingUtil method fromSubscriptionPolicyArrayToListDTO.

/**
 * Converts an array of Subscription Policy objects into a List DTO
 *
 * @param subscriptionPolicies Array of Subscription Policies
 * @return A List DTO of converted Subscription Policies
 * @throws UnsupportedThrottleLimitTypeException
 * @throws ParseException
 */
public static SubscriptionThrottlePolicyListDTO fromSubscriptionPolicyArrayToListDTO(SubscriptionPolicy[] subscriptionPolicies) throws UnsupportedThrottleLimitTypeException, ParseException {
    SubscriptionThrottlePolicyListDTO listDTO = new SubscriptionThrottlePolicyListDTO();
    List<SubscriptionThrottlePolicyDTO> subscriptionPolicyDTOList = new ArrayList<>();
    if (subscriptionPolicies != null) {
        for (SubscriptionPolicy policy : subscriptionPolicies) {
            SubscriptionThrottlePolicyDTO dto = fromSubscriptionThrottlePolicyToDTO(policy);
            subscriptionPolicyDTOList.add(dto);
        }
    }
    listDTO.setCount(subscriptionPolicyDTOList.size());
    listDTO.setList(subscriptionPolicyDTOList);
    return listDTO;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) SubscriptionThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.SubscriptionThrottlePolicyDTO) ArrayList(java.util.ArrayList) SubscriptionThrottlePolicyListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.SubscriptionThrottlePolicyListDTO)

Example 5 with SubscriptionThrottlePolicyListDTO

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

the class ThrottlingApiServiceImpl method throttlingPoliciesSubscriptionGet.

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

Aggregations

ArrayList (java.util.ArrayList)4 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)3 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)2 SubscriptionThrottlePolicyListDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyListDTO)2 ParseException (org.json.simple.parser.ParseException)1 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 GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)1 Policy (org.wso2.carbon.apimgt.api.model.policy.Policy)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 SubscriptionThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO)1 SubscriptionThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.SubscriptionThrottlePolicyDTO)1 SubscriptionThrottlePolicyListDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.SubscriptionThrottlePolicyListDTO)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1