Search in sources :

Example 26 with APIPolicy

use of org.wso2.carbon.apimgt.api.model.policy.APIPolicy in project carbon-apimgt by wso2.

the class AdvancedThrottlePolicyMappingUtil method fromAdvancedPolicyToInfoDTO.

/**
 * Converts a single Advanced Policy model into REST API DTO
 *
 * @param apiPolicy 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 fromAdvancedPolicyToInfoDTO(APIPolicy apiPolicy) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
    AdvancedThrottlePolicyDTO policyDTO = new AdvancedThrottlePolicyDTO();
    policyDTO = CommonThrottleMappingUtil.updateFieldsFromToPolicyToDTO(apiPolicy, policyDTO);
    if (apiPolicy.getDefaultQuotaPolicy() != null) {
        policyDTO.setDefaultLimit(CommonThrottleMappingUtil.fromQuotaPolicyToDTO(apiPolicy.getDefaultQuotaPolicy()));
    }
    return policyDTO;
}
Also used : AdvancedThrottlePolicyDTO(org.wso2.carbon.apimgt.rest.api.admin.dto.AdvancedThrottlePolicyDTO)

Example 27 with APIPolicy

use of org.wso2.carbon.apimgt.api.model.policy.APIPolicy in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingAdvancedIdGet.

/**
 * Get policy by id
 * @param Id          Uuid of the Advanced policy.
 * @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 policiesThrottlingAdvancedIdGet(String Id, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    if (log.isDebugEnabled()) {
        log.info("Received Advanced Policy Get request. Policy uuid: " + Id);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        APIPolicy policy = apiMgtAdminService.getApiPolicyByUuid(Id);
        return Response.status(Response.Status.OK).entity(AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(policy)).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while getting Advanced Policy. policy uuid: " + Id;
        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 28 with APIPolicy

use of org.wso2.carbon.apimgt.api.model.policy.APIPolicy in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesThrottlingAdvancedPost.

/**
 * Create Policy
 *
 * @param body              DTO object including the Policy meta information
 * @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 policiesThrottlingAdvancedPost(AdvancedThrottlePolicyDTO body, Request request) throws NotFoundException {
    APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.api;
    if (log.isDebugEnabled()) {
        log.info("Received Advance Policy POST request " + body + " with tierLevel = " + tierLevel);
    }
    if (log.isDebugEnabled()) {
        log.info("Received Advance Policy PUT request " + body + " with tierLevel = " + tierLevel);
    }
    try {
        APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
        APIPolicy apiPolicy = AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyDTOToPolicy(body);
        String policyId = apiMgtAdminService.addApiPolicy(apiPolicy);
        return Response.status(Response.Status.CREATED).entity(AdvancedThrottlePolicyMappingUtil.fromAdvancedPolicyToDTO(apiMgtAdminService.getApiPolicyByUuid(policyId))).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error occurred while adding Advanced Throttle Policy, policy name: " + body.getPolicyName();
        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 29 with APIPolicy

use of org.wso2.carbon.apimgt.api.model.policy.APIPolicy in project carbon-apimgt by wso2.

the class APIImportExportTestCase method createApi.

private static API.APIBuilder createApi(String provider, String apiId, String name, String version, String description, Map<String, Endpoint> endpointTypeToIdMap) throws APIManagementException {
    Set<String> transport = new HashSet<>();
    transport.add("http");
    Set<Policy> policies = new HashSet<>();
    policies.add(new SubscriptionPolicy("Silver"));
    policies.add(new SubscriptionPolicy("Bronze"));
    Set<String> tags = new HashSet<>();
    tags.add("food");
    tags.add("beverage");
    BusinessInformation businessInformation = new BusinessInformation();
    businessInformation.setBusinessOwner("John Doe");
    businessInformation.setBusinessOwnerEmail("john.doe@annonymous.com");
    businessInformation.setTechnicalOwner("Jane Doe");
    businessInformation.setBusinessOwnerEmail("jane.doe@annonymous.com");
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    corsConfiguration.setEnabled(true);
    corsConfiguration.setAllowMethods(Arrays.asList("GET", "POST", "DELETE"));
    corsConfiguration.setAllowHeaders(Arrays.asList("Authorization", "X-Custom"));
    corsConfiguration.setAllowCredentials(true);
    corsConfiguration.setAllowOrigins(Collections.singletonList("*"));
    API.APIBuilder apiBuilder = new API.APIBuilder(provider, name, version).id(apiId).context(UUID.randomUUID().toString()).description(description).lifeCycleStatus("CREATED").apiDefinition(api1Definition).wsdlUri("http://www.webservicex.net/globalweather.asmx?op=GetWeather?wsdl").isResponseCachingEnabled(true).cacheTimeout(120).isDefaultVersion(true).apiPolicy(new APIPolicy("Gold")).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.RESTRICTED).visibleRoles(new HashSet<>(Arrays.asList("customer", "manager", "employee"))).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy("Adam Doe").lastUpdatedTime(LocalDateTime.now()).endpoint(endpointTypeToIdMap);
    apiBuilder.uriTemplates(Collections.emptyMap());
    return apiBuilder;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) API(org.wso2.carbon.apimgt.core.models.API) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) HashSet(java.util.HashSet)

Example 30 with APIPolicy

use of org.wso2.carbon.apimgt.api.model.policy.APIPolicy in project carbon-apimgt by wso2.

the class MappingUtil method toAPI.

/**
 * This method converts the API model object from the DTO object.
 *
 * @param apidto APIDTO object with API data
 * @return APIBuilder object
 */
public static API.APIBuilder toAPI(APIDTO apidto) throws JsonProcessingException {
    BusinessInformation businessInformation = new BusinessInformation();
    API_businessInformationDTO apiBusinessInformationDTO = apidto.getBusinessInformation();
    if (apiBusinessInformationDTO != null) {
        businessInformation.setBusinessOwner(apiBusinessInformationDTO.getBusinessOwner());
        businessInformation.setBusinessOwnerEmail(apiBusinessInformationDTO.getBusinessOwnerEmail());
        businessInformation.setTechnicalOwner(apiBusinessInformationDTO.getTechnicalOwner());
        businessInformation.setTechnicalOwnerEmail(apiBusinessInformationDTO.getTechnicalOwnerEmail());
    }
    API_corsConfigurationDTO apiCorsConfigurationDTO = apidto.getCorsConfiguration();
    CorsConfiguration corsConfiguration = new CorsConfiguration();
    if (apiCorsConfigurationDTO != null) {
        corsConfiguration.setAllowCredentials(apiCorsConfigurationDTO.getAccessControlAllowCredentials());
        corsConfiguration.setAllowHeaders(apiCorsConfigurationDTO.getAccessControlAllowHeaders());
        corsConfiguration.setAllowMethods(apiCorsConfigurationDTO.getAccessControlAllowMethods());
        corsConfiguration.setAllowOrigins(apiCorsConfigurationDTO.getAccessControlAllowOrigins());
        corsConfiguration.setEnabled(apiCorsConfigurationDTO.getCorsConfigurationEnabled());
    }
    List<API_operationsDTO> operationList = apidto.getOperations();
    Map<String, UriTemplate> uriTemplateList = new HashMap<>();
    for (API_operationsDTO operationsDTO : operationList) {
        UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder();
        uriTemplateBuilder.uriTemplate(operationsDTO.getUritemplate());
        uriTemplateBuilder.authType(operationsDTO.getAuthType());
        uriTemplateBuilder.httpVerb(operationsDTO.getHttpVerb());
        uriTemplateBuilder.policy(new APIPolicy(operationsDTO.getPolicy()));
        uriTemplateBuilder.scopes(operationsDTO.getScopes());
        if (operationsDTO.getEndpoint() != null && !operationsDTO.getEndpoint().isEmpty()) {
            uriTemplateBuilder.endpoint(fromEndpointListToMap(operationsDTO.getEndpoint()));
        }
        if (operationsDTO.getId() != null) {
            uriTemplateBuilder.templateId(operationsDTO.getId());
        } else {
            uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(operationsDTO.getUritemplate(), operationsDTO.getHttpVerb()));
        }
        uriTemplateList.put(uriTemplateBuilder.getTemplateId(), uriTemplateBuilder.build());
    }
    Set<Policy> subscriptionPolicies = new HashSet<>();
    apidto.getPolicies().forEach(v -> subscriptionPolicies.add(new SubscriptionPolicy(v)));
    API.APIBuilder apiBuilder = new API.APIBuilder(apidto.getProvider(), apidto.getName(), apidto.getVersion()).id(apidto.getId()).context(apidto.getContext()).description(apidto.getDescription()).lifeCycleStatus(apidto.getLifeCycleStatus()).endpoint(fromEndpointListToMap(apidto.getEndpoint())).visibleRoles(new HashSet<>(apidto.getVisibleRoles())).policies(subscriptionPolicies).apiPermission(apidto.getPermission()).tags(new HashSet<>(apidto.getTags())).labels(new ArrayList<>(apidto.getLabels())).transport(new HashSet<>(apidto.getTransport())).isResponseCachingEnabled(Boolean.valueOf(apidto.getResponseCaching())).businessInformation(businessInformation).uriTemplates(uriTemplateList).corsConfiguration(corsConfiguration).wsdlUri(apidto.getWsdlUri()).scopes(apidto.getScopes()).securityScheme(mapSecuritySchemeListToInt(apidto.getSecurityScheme()));
    if (apidto.getIsDefaultVersion() != null) {
        apiBuilder.isDefaultVersion(apidto.getIsDefaultVersion());
    }
    if (apidto.getVisibility() != null) {
        apiBuilder.visibility(API.Visibility.valueOf(apidto.getVisibility().toString()));
    }
    if (apidto.getCacheTimeout() != null) {
        apiBuilder.cacheTimeout(apidto.getCacheTimeout());
    }
    if (apidto.getApiPolicy() != null) {
        Policy policy = new APIPolicy(apidto.getApiPolicy());
        apiBuilder.apiPolicy(policy);
    }
    if (apidto.getHasOwnGateway() != null) {
        apiBuilder.hasOwnGateway(apidto.getHasOwnGateway());
    }
    if (apidto.getThreatProtectionPolicies() != null) {
        API_threatProtectionPoliciesDTO threatProtectionPoliciesDTO = apidto.getThreatProtectionPolicies();
        List<API_threatProtectionPolicies_listDTO> threatProtectionPolicies_listDTO = threatProtectionPoliciesDTO.getList();
        Set<String> policyIdSet = new HashSet<>();
        for (API_threatProtectionPolicies_listDTO listDTO : threatProtectionPolicies_listDTO) {
            policyIdSet.add(listDTO.getPolicyId());
        }
        apiBuilder.threatProtectionPolicies(policyIdSet);
    }
    return apiBuilder;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ThreatProtectionPolicy(org.wso2.carbon.apimgt.core.models.policy.ThreatProtectionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) HashMap(java.util.HashMap) API_threatProtectionPolicies_listDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_threatProtectionPolicies_listDTO) API_operationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_operationsDTO) API_threatProtectionPoliciesDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_threatProtectionPoliciesDTO) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) API_businessInformationDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_businessInformationDTO) API_corsConfigurationDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.API_corsConfigurationDTO) HashSet(java.util.HashSet) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) API(org.wso2.carbon.apimgt.core.models.API) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Aggregations

APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)87 Test (org.testng.annotations.Test)44 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)40 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)39 ArrayList (java.util.ArrayList)38 API (org.wso2.carbon.apimgt.core.models.API)33 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)30 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)27 HashMap (java.util.HashMap)25 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)22 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)22 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)22 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)20 HashSet (java.util.HashSet)19 Test (org.junit.Test)19 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)19 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)17 PreparedStatement (java.sql.PreparedStatement)15