Search in sources :

Example 1 with APIProductBusinessInformationDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductBusinessInformationDTO in project carbon-apimgt by wso2.

the class APIMappingUtil method fromAPIProducttoDTO.

public static APIProductDTO fromAPIProducttoDTO(APIProduct product) throws APIManagementException {
    APIProductDTO productDto = new APIProductDTO();
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    productDto.setName(product.getId().getName());
    productDto.setProvider(APIUtil.replaceEmailDomainBack(product.getId().getProviderName()));
    productDto.setId(product.getUuid());
    productDto.setContext(product.getContext());
    productDto.setDescription(product.getDescription());
    productDto.setApiType(APIProductDTO.ApiTypeEnum.fromValue(APIConstants.AuditLogConstants.API_PRODUCT));
    productDto.setAuthorizationHeader(product.getAuthorizationHeader());
    productDto.setGatewayVendor(product.getGatewayVendor());
    Set<String> apiTags = product.getTags();
    List<String> tagsToReturn = new ArrayList<>(apiTags);
    productDto.setTags(tagsToReturn);
    productDto.setEnableSchemaValidation(product.isEnabledSchemaValidation());
    productDto.setIsRevision(product.isRevision());
    productDto.setRevisionedApiProductId(product.getRevisionedApiProductId());
    productDto.setRevisionId(product.getRevisionId());
    if (APIConstants.ENABLED.equals(product.getResponseCache())) {
        productDto.setResponseCachingEnabled(Boolean.TRUE);
    } else {
        productDto.setResponseCachingEnabled(Boolean.FALSE);
    }
    productDto.setCacheTimeout(product.getCacheTimeout());
    APIProductBusinessInformationDTO businessInformation = new APIProductBusinessInformationDTO();
    businessInformation.setBusinessOwner(product.getBusinessOwner());
    businessInformation.setBusinessOwnerEmail(product.getBusinessOwnerEmail());
    businessInformation.setTechnicalOwner(product.getTechnicalOwner());
    businessInformation.setTechnicalOwnerEmail(product.getTechnicalOwnerEmail());
    productDto.setBusinessInformation(businessInformation);
    APICorsConfigurationDTO apiCorsConfigurationDTO = new APICorsConfigurationDTO();
    CORSConfiguration corsConfiguration = product.getCorsConfiguration();
    if (corsConfiguration == null) {
        corsConfiguration = APIUtil.getDefaultCorsConfiguration();
    }
    apiCorsConfigurationDTO.setAccessControlAllowOrigins(corsConfiguration.getAccessControlAllowOrigins());
    apiCorsConfigurationDTO.setAccessControlAllowHeaders(corsConfiguration.getAccessControlAllowHeaders());
    apiCorsConfigurationDTO.setAccessControlAllowMethods(corsConfiguration.getAccessControlAllowMethods());
    apiCorsConfigurationDTO.setCorsConfigurationEnabled(corsConfiguration.isCorsConfigurationEnabled());
    apiCorsConfigurationDTO.setAccessControlAllowCredentials(corsConfiguration.isAccessControlAllowCredentials());
    productDto.setCorsConfiguration(apiCorsConfigurationDTO);
    productDto.setState(StateEnum.valueOf(product.getState()));
    productDto.setWorkflowStatus(product.getWorkflowStatus());
    // Aggregate API resources to each relevant API.
    Map<String, ProductAPIDTO> aggregatedAPIs = new HashMap<String, ProductAPIDTO>();
    List<APIProductResource> resources = product.getProductResources();
    for (APIProductResource apiProductResource : resources) {
        String uuid = apiProductResource.getApiId();
        if (aggregatedAPIs.containsKey(uuid)) {
            ProductAPIDTO productAPI = aggregatedAPIs.get(uuid);
            URITemplate template = apiProductResource.getUriTemplate();
            List<APIOperationsDTO> operations = productAPI.getOperations();
            APIOperationsDTO operation = getOperationFromURITemplate(template);
            operations.add(operation);
        } else {
            ProductAPIDTO productAPI = new ProductAPIDTO();
            productAPI.setApiId(uuid);
            productAPI.setName(apiProductResource.getApiName());
            productAPI.setVersion(apiProductResource.getApiIdentifier().getVersion());
            List<APIOperationsDTO> operations = new ArrayList<APIOperationsDTO>();
            URITemplate template = apiProductResource.getUriTemplate();
            APIOperationsDTO operation = getOperationFromURITemplate(template);
            operations.add(operation);
            productAPI.setOperations(operations);
            aggregatedAPIs.put(uuid, productAPI);
        }
    }
    productDto.setApis(new ArrayList<>(aggregatedAPIs.values()));
    String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(product.getId().getProviderName()));
    String apiSwaggerDefinition = apiProvider.getOpenAPIDefinition(product.getId(), tenantDomain);
    List<ScopeDTO> scopeDTOS = getScopesFromSwagger(apiSwaggerDefinition);
    productDto.setScopes(getAPIScopesFromScopeDTOs(scopeDTOS));
    String subscriptionAvailability = product.getSubscriptionAvailability();
    if (subscriptionAvailability != null) {
        productDto.setSubscriptionAvailability(mapSubscriptionAvailabilityFromAPIProducttoDTO(subscriptionAvailability));
    }
    if (product.getSubscriptionAvailableTenants() != null) {
        productDto.setSubscriptionAvailableTenants(Arrays.asList(product.getSubscriptionAvailableTenants().split(",")));
    }
    Set<org.wso2.carbon.apimgt.api.model.Tier> apiTiers = product.getAvailableTiers();
    List<String> tiersToReturn = new ArrayList<>();
    for (org.wso2.carbon.apimgt.api.model.Tier tier : apiTiers) {
        tiersToReturn.add(tier.getName());
    }
    productDto.setPolicies(tiersToReturn);
    productDto.setApiThrottlingPolicy(product.getProductLevelPolicy());
    if (product.getVisibility() != null) {
        productDto.setVisibility(mapVisibilityFromAPIProducttoDTO(product.getVisibility()));
    }
    if (product.getVisibleRoles() != null) {
        productDto.setVisibleRoles(Arrays.asList(product.getVisibleRoles().split(",")));
    }
    if (product.getVisibleTenants() != null) {
        productDto.setVisibleTenants(Arrays.asList(product.getVisibleTenants().split(",")));
    }
    productDto.setAccessControl(APIConstants.API_RESTRICTED_VISIBILITY.equals(product.getAccessControl()) ? APIProductDTO.AccessControlEnum.RESTRICTED : APIProductDTO.AccessControlEnum.NONE);
    if (product.getAccessControlRoles() != null) {
        productDto.setAccessControlRoles(Arrays.asList(product.getAccessControlRoles().split(",")));
    }
    if (StringUtils.isEmpty(product.getTransports())) {
        List<String> transports = new ArrayList<>();
        transports.add(APIConstants.HTTPS_PROTOCOL);
        productDto.setTransport(transports);
    } else {
        productDto.setTransport(Arrays.asList(product.getTransports().split(",")));
    }
    if (product.getAdditionalProperties() != null) {
        JSONObject additionalProperties = product.getAdditionalProperties();
        List<APIInfoAdditionalPropertiesDTO> additionalPropertiesList = new ArrayList<>();
        Map<String, APIInfoAdditionalPropertiesMapDTO> additionalPropertiesMap = new HashMap<>();
        for (Object propertyKey : additionalProperties.keySet()) {
            APIInfoAdditionalPropertiesDTO additionalPropertiesDTO = new APIInfoAdditionalPropertiesDTO();
            APIInfoAdditionalPropertiesMapDTO apiInfoAdditionalPropertiesMapDTO = new APIInfoAdditionalPropertiesMapDTO();
            String key = (String) propertyKey;
            int index = key.lastIndexOf(APIConstants.API_RELATED_CUSTOM_PROPERTIES_SURFIX);
            additionalPropertiesDTO.setValue((String) additionalProperties.get(key));
            apiInfoAdditionalPropertiesMapDTO.setValue((String) additionalProperties.get(key));
            if (index > 0) {
                additionalPropertiesDTO.setName(key.substring(0, index));
                apiInfoAdditionalPropertiesMapDTO.setName(key.substring(0, index));
                additionalPropertiesDTO.setDisplay(true);
            } else {
                additionalPropertiesDTO.setName(key);
                apiInfoAdditionalPropertiesMapDTO.setName(key);
                additionalPropertiesDTO.setDisplay(false);
            }
            apiInfoAdditionalPropertiesMapDTO.setDisplay(false);
            additionalPropertiesMap.put(key, apiInfoAdditionalPropertiesMapDTO);
            additionalPropertiesList.add(additionalPropertiesDTO);
        }
        productDto.setAdditionalPropertiesMap(additionalPropertiesMap);
        productDto.setAdditionalProperties(additionalPropertiesList);
    }
    if (product.getApiSecurity() != null) {
        productDto.setSecurityScheme(Arrays.asList(product.getApiSecurity().split(",")));
    }
    List<APICategory> apiCategories = product.getApiCategories();
    List<String> categoryNameList = new ArrayList<>();
    if (apiCategories != null && !apiCategories.isEmpty()) {
        for (APICategory category : apiCategories) {
            categoryNameList.add(category.getName());
        }
    }
    productDto.setCategories(categoryNameList);
    if (null != product.getLastUpdated()) {
        Date lastUpdateDate = product.getLastUpdated();
        Timestamp timeStamp = new Timestamp(lastUpdateDate.getTime());
        productDto.setLastUpdatedTime(String.valueOf(timeStamp));
    }
    if (null != product.getCreatedTime()) {
        Date createdTime = product.getCreatedTime();
        Timestamp timeStamp = new Timestamp(createdTime.getTime());
        productDto.setCreatedTime(String.valueOf(timeStamp));
    }
    return productDto;
}
Also used : APIInfoAdditionalPropertiesMapDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIInfoAdditionalPropertiesMapDTO) HashMap(java.util.HashMap) ScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO) APIScopeDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO) APIProductDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO) ArrayList(java.util.ArrayList) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) Timestamp(java.sql.Timestamp) APIProductBusinessInformationDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductBusinessInformationDTO) ProductAPIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ProductAPIDTO) APIInfoAdditionalPropertiesDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIInfoAdditionalPropertiesDTO) Tier(org.wso2.carbon.apimgt.api.model.Tier) Tier(org.wso2.carbon.apimgt.api.model.Tier) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) APICorsConfigurationDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APICorsConfigurationDTO) Date(java.util.Date) CORSConfiguration(org.wso2.carbon.apimgt.api.model.CORSConfiguration) JSONObject(org.json.simple.JSONObject) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIOperationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO) JSONObject(org.json.simple.JSONObject) APICategory(org.wso2.carbon.apimgt.api.model.APICategory)

Aggregations

Timestamp (java.sql.Timestamp)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 JSONObject (org.json.simple.JSONObject)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 APICategory (org.wso2.carbon.apimgt.api.model.APICategory)1 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)1 CORSConfiguration (org.wso2.carbon.apimgt.api.model.CORSConfiguration)1 Tier (org.wso2.carbon.apimgt.api.model.Tier)1 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)1 APICorsConfigurationDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APICorsConfigurationDTO)1 APIInfoAdditionalPropertiesDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIInfoAdditionalPropertiesDTO)1 APIInfoAdditionalPropertiesMapDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIInfoAdditionalPropertiesMapDTO)1 APIOperationsDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO)1 APIProductBusinessInformationDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductBusinessInformationDTO)1 APIProductDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO)1 APIScopeDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO)1 ProductAPIDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ProductAPIDTO)1 ScopeDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO)1