Search in sources :

Example 16 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 17 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 18 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 19 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)

Example 20 with PolicyDTO

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

the class OperationPolicyMappingUtil method fromOperationPolicyListToDTO.

public static APIOperationPoliciesDTO fromOperationPolicyListToDTO(List<OperationPolicy> operationPolicyList) {
    APIOperationPoliciesDTO dto = new APIOperationPoliciesDTO();
    List<OperationPolicyDTO> request = new ArrayList<>();
    List<OperationPolicyDTO> response = new ArrayList<>();
    List<OperationPolicyDTO> fault = new ArrayList<>();
    for (OperationPolicy op : operationPolicyList) {
        OperationPolicyDTO policyDTO = fromOperationPolicyToDTO(op);
        if (APIConstants.OPERATION_SEQUENCE_TYPE_REQUEST.equals(op.getDirection())) {
            request.add(policyDTO);
        } else if (APIConstants.OPERATION_SEQUENCE_TYPE_RESPONSE.equals(op.getDirection())) {
            response.add(policyDTO);
        } else if (APIConstants.OPERATION_SEQUENCE_TYPE_FAULT.equals(op.getDirection())) {
            fault.add(policyDTO);
        }
    }
    dto.setRequest(request);
    dto.setResponse(response);
    dto.setFault(fault);
    return dto;
}
Also used : APIOperationPoliciesDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationPoliciesDTO) OperationPolicyDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OperationPolicyDTO) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) ArrayList(java.util.ArrayList)

Aggregations

APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)11 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)6 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)6 GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)6 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)6 ArrayList (java.util.ArrayList)5 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 ParseException (org.json.simple.parser.ParseException)4 PolicyNotFoundException (org.wso2.carbon.apimgt.api.PolicyNotFoundException)4 Policy (org.wso2.carbon.apimgt.api.model.policy.Policy)4 AdvancedThrottlePolicyDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO)3 JSONArray (org.json.simple.JSONArray)2 JSONObject (org.json.simple.JSONObject)2 JSONParser (org.json.simple.parser.JSONParser)2 PolicyDTO (org.wso2.carbon.apimgt.rest.api.core.dto.PolicyDTO)2 Test (org.testng.annotations.Test)1 ConditionDTO (org.wso2.carbon.apimgt.api.dto.ConditionDTO)1 OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)1