Search in sources :

Example 6 with PolicyDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesGet.

@Override
public Response policiesGet(String accept, Request request) throws NotFoundException {
    PolicyListDTO policyListDTO = new PolicyListDTO();
    try {
        APIMgtAdminService adminService = RestApiUtil.getAPIMgtAdminService();
        List<PolicyDTO> policyDTOList = MappingUtil.convertToPolicyDtoList(adminService.getAllPolicies());
        policyListDTO.setList(policyDTOList);
        policyListDTO.setCount(policyDTOList.size());
        return Response.ok().entity(policyListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving Policies";
        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) PolicyDTO(org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.PolicyListDTO) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 7 with PolicyDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO in project carbon-apimgt by wso2.

the class MappingUtil method convertToPolicyDtoList.

/**
 * Convert policy validation data list to policy dto list
 *
 * @param allPolicies all policies
 * @return PolicyDTO list
 */
public static List<PolicyDTO> convertToPolicyDtoList(Set<PolicyValidationData> allPolicies) {
    List<PolicyDTO> policyDTOList = new ArrayList<>();
    allPolicies.forEach(v -> {
        PolicyDTO policyDTO = new PolicyDTO();
        policyDTO.setId(v.getId());
        policyDTO.setName(v.getName());
        policyDTO.setStopOnQuotaReach(v.isStopOnQuotaReach());
        policyDTOList.add(policyDTO);
    });
    return policyDTOList;
}
Also used : PolicyDTO(org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO) ThreatProtectionPolicyDTO(org.wso2.carbon.apimgt.rest.api.core.dto.ThreatProtectionPolicyDTO) ArrayList(java.util.ArrayList)

Example 8 with PolicyDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO in project carbon-apimgt by wso2.

the class AdvancedThrottlePolicyMappingUtil method fromAdvancedPolicyToDTO.

/**
 * Converts a single Advanced Policy model into REST API DTO
 *
 * @param policy Advanced Policy model object
 * @return Converted Advanced policy REST API DTO object
 * @throws UnsupportedThrottleLimitTypeException - If error occurs
 * @throws UnsupportedThrottleConditionTypeException - If error occurs
 */
public static AdvancedThrottlePolicyDTO fromAdvancedPolicyToDTO(APIPolicy policy) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    AdvancedThrottlePolicyDTO policyDTO = new AdvancedThrottlePolicyDTO();
    policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(policy, policyDTO);
    List<ConditionalGroupDTO> groupDTOs = CommonThrottleMappingUtil.fromPipelineListToConditionalGroupDTOList(policy.getPipelines());
    policyDTO.setConditionalGroups(groupDTOs);
    if (policy.getDefaultQuotaPolicy() != null) {
        policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(policy.getDefaultQuotaPolicy()));
    }
    return policyDTO;
}
Also used : ConditionalGroupDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.ConditionalGroupDTO) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO)

Example 9 with PolicyDTO

use of org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO in project carbon-apimgt by wso2.

the class AdvancedThrottlePolicyMappingUtilTestCase method fromAdvancedPolicyToInfoDTOTest.

@Test(description = "Convert Policy DTO to Policy object")
public void fromAdvancedPolicyToInfoDTOTest() throws Exception {
    APIPolicy apiPolicy = new APIPolicy(APIMgtConstants.DEFAULT_API_POLICY);
    String uuid = UUID.randomUUID().toString();
    String displayName = "SampleAPIPolicy";
    String description = "Sample Description";
    apiPolicy.setUuid(uuid);
    apiPolicy.setDisplayName(displayName);
    apiPolicy.setDescription(description);
    QuotaPolicy quotaPolicy = new QuotaPolicy();
    quotaPolicy.setType("requestCount");
    RequestCountLimit requestCountLimit = new RequestCountLimit("s", 60, 10);
    quotaPolicy.setLimit(requestCountLimit);
    apiPolicy.setDefaultQuotaPolicy(quotaPolicy);
    AdvancedThrottlePolicyDTO policyDTO = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToInfoDTO(apiPolicy);
    Assert.assertNotNull(policyDTO);
    Assert.assertEquals(policyDTO.getDisplayName(), displayName);
    Assert.assertEquals(policyDTO.getDescription(), description);
    Assert.assertEquals(policyDTO.getDefaultLimit().getType(), "RequestCountLimit");
    Assert.assertEquals(policyDTO.getDefaultLimit().getTimeUnit(), apiPolicy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
    Assert.assertEquals(policyDTO.getDefaultLimit().getUnitTime(), (Integer) apiPolicy.getDefaultQuotaPolicy().getLimit().getUnitTime());
    Assert.assertEquals(policyDTO.getDefaultLimit().getRequestCountLimit().getRequestCount(), (Integer) ((RequestCountLimit) apiPolicy.getDefaultQuotaPolicy().getLimit()).getRequestCount());
}
Also used : RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) Test(org.testng.annotations.Test)

Aggregations

AdvancedThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO)3 ArrayList (java.util.ArrayList)2 PolicyDTO (org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO)2 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1 ParseException (org.json.simple.parser.ParseException)1 Test (org.testng.annotations.Test)1 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)1 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)1 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)1 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)1 ApplicationThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.ApplicationThrottlePolicyDTO)1 ConditionalGroupDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.ConditionalGroupDTO)1 CustomAttributeDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.CustomAttributeDTO)1 CustomRuleDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.CustomRuleDTO)1 SubscriptionThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.SubscriptionThrottlePolicyDTO)1 ThreatProtectionPolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.ThreatProtectionPolicyDTO)1