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;
}
Aggregations