use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class SearchResultMappingUtil method fromAPIProductToAPIResultDTO.
/**
* Get API result representation for content search.
*
* @param apiProduct APIProduct
* @return APIProductSearchResultDTO
*/
public static APIProductSearchResultDTO fromAPIProductToAPIResultDTO(APIProduct apiProduct) {
APIProductSearchResultDTO apiProductResultDTO = new APIProductSearchResultDTO();
apiProductResultDTO.setId(apiProduct.getUuid());
APIProductIdentifier apiproductId = apiProduct.getId();
apiProductResultDTO.setName(apiproductId.getName());
apiProductResultDTO.setVersion(apiproductId.getVersion());
apiProductResultDTO.setProvider(APIUtil.replaceEmailDomainBack(apiproductId.getProviderName()));
String context = apiProduct.getContextTemplate();
if (context.endsWith("/" + RestApiConstants.API_VERSION_PARAM)) {
context = context.replace("/" + RestApiConstants.API_VERSION_PARAM, "");
}
apiProductResultDTO.setContext(context);
apiProductResultDTO.setType(SearchResultDTO.TypeEnum.APIPRODUCT);
apiProductResultDTO.setDescription(apiProduct.getDescription());
apiProductResultDTO.setStatus(apiProduct.getState());
apiProductResultDTO.setThumbnailUri(apiProduct.getThumbnailUrl());
return apiProductResultDTO;
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class PublisherCommonUtils method updateApiProduct.
/**
* Update an API Product.
*
* @param originalAPIProduct Existing API Product
* @param apiProductDtoToUpdate New API Product DTO to update
* @param apiProvider API Provider
* @param username Username
* @throws APIManagementException If an error occurs while retrieving and updating an existing API Product
* @throws FaultGatewaysException If an error occurs while updating an existing API Product
*/
public static APIProduct updateApiProduct(APIProduct originalAPIProduct, APIProductDTO apiProductDtoToUpdate, APIProvider apiProvider, String username, String orgId) throws APIManagementException, FaultGatewaysException {
List<String> apiSecurity = apiProductDtoToUpdate.getSecurityScheme();
// validation for tiers
List<String> tiersFromDTO = apiProductDtoToUpdate.getPolicies();
if (apiSecurity.contains(APIConstants.DEFAULT_API_SECURITY_OAUTH2) || apiSecurity.contains(APIConstants.API_SECURITY_API_KEY)) {
if (tiersFromDTO == null || tiersFromDTO.isEmpty()) {
throw new APIManagementException("No tier defined for the API Product", ExceptionCodes.TIER_CANNOT_BE_NULL);
}
}
// check whether the added API Products's tiers are all valid
Set<Tier> definedTiers = apiProvider.getTiers();
List<String> invalidTiers = PublisherCommonUtils.getInvalidTierNames(definedTiers, tiersFromDTO);
if (!invalidTiers.isEmpty()) {
throw new APIManagementException("Specified tier(s) " + Arrays.toString(invalidTiers.toArray()) + " are invalid", ExceptionCodes.TIER_NAME_INVALID);
}
if (apiProductDtoToUpdate.getAdditionalProperties() != null) {
String errorMessage = PublisherCommonUtils.validateAdditionalProperties(apiProductDtoToUpdate.getAdditionalProperties());
if (!errorMessage.isEmpty()) {
throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.INVALID_ADDITIONAL_PROPERTIES, originalAPIProduct.getId().getName(), originalAPIProduct.getId().getVersion()));
}
}
APIProduct product = APIMappingUtil.fromDTOtoAPIProduct(apiProductDtoToUpdate, username);
product.setState(originalAPIProduct.getState());
// We do not allow to modify provider,name,version and uuid. Set the origial value
APIProductIdentifier productIdentifier = originalAPIProduct.getId();
product.setID(productIdentifier);
product.setUuid(originalAPIProduct.getUuid());
product.setOrganization(orgId);
Map<API, List<APIProductResource>> apiToProductResourceMapping = apiProvider.updateAPIProduct(product);
apiProvider.updateAPIProductSwagger(originalAPIProduct.getUuid(), apiToProductResourceMapping, product, orgId);
// preserve monetization status in the update flow
apiProvider.configureMonetizationInAPIProductArtifact(product);
return apiProvider.getAPIProduct(productIdentifier);
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class APIMappingUtil method fromAPIListToDTO.
/**
* Converts a List object of APIs into a DTO
*
* @param apiList List of APIs
* @return APIListDTO object containing APIDTOs
* @throws APIManagementException
*/
public static APIListDTO fromAPIListToDTO(List<Object> apiList, String organization) throws APIManagementException {
APIListDTO apiListDTO = new APIListDTO();
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
Set<String> deniedTiers = apiConsumer.getDeniedTiers(organization);
Map<String, Tier> tierMap = APIUtil.getTiers(organization);
List<APIInfoDTO> apiInfoDTOs = apiListDTO.getList();
if (apiList != null) {
for (Object api : apiList) {
APIInfoDTO apiInfoDTO = null;
if (api instanceof API) {
API api1 = (API) api;
apiInfoDTO = fromAPIToInfoDTO((API) api);
setThrottlePoliciesAndMonetization(api1, apiInfoDTO, deniedTiers, tierMap);
} else if (api instanceof APIProduct) {
APIProduct api1 = (APIProduct) api;
apiInfoDTO = fromAPIToInfoDTO((API) api);
setThrottlePoliciesAndMonetization(api1, apiInfoDTO, deniedTiers, tierMap);
}
apiInfoDTOs.add(apiInfoDTO);
}
}
apiListDTO.setCount(apiInfoDTOs.size());
return apiListDTO;
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class APIMappingUtil method setThrottlePoliciesAndMonetization.
public static void setThrottlePoliciesAndMonetization(APIProduct apiProduct, APIInfoDTO apiInfoDTO, Set<String> deniedTiers, Map<String, Tier> tierMap) {
Set<Tier> throttlingPolicies = new HashSet<Tier>();
List<String> throttlingPolicyNames = new ArrayList<>();
Set<Tier> apiTiers = apiProduct.getAvailableTiers();
for (Tier currentTier : apiTiers) {
if (!deniedTiers.contains(currentTier.getName())) {
throttlingPolicies.add(currentTier);
throttlingPolicyNames.add(currentTier.getName());
}
}
int free = 0, commercial = 0;
for (Tier tier : throttlingPolicies) {
tier = tierMap.get(tier.getName());
if (RestApiConstants.FREE.equalsIgnoreCase(tier.getTierPlan())) {
free = free + 1;
} else if (RestApiConstants.COMMERCIAL.equalsIgnoreCase(tier.getTierPlan())) {
commercial = commercial + 1;
}
}
if (free > 0 && commercial == 0) {
apiInfoDTO.setMonetizationLabel(RestApiConstants.FREE);
} else if (free == 0 && commercial > 0) {
apiInfoDTO.setMonetizationLabel(RestApiConstants.PAID);
} else if (free > 0 && commercial > 0) {
apiInfoDTO.setMonetizationLabel(RestApiConstants.FREEMIUM);
}
apiInfoDTO.setThrottlingPolicies(throttlingPolicyNames);
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class APIMappingUtil method fromAPIToInfoDTO.
/**
* Creates a minimal DTO representation of an API Product object
*
* @param apiProduct API Product object
* @return a minimal representation DTO
* @throws APIManagementException
*/
static APIInfoDTO fromAPIToInfoDTO(APIProduct apiProduct, String organization) throws APIManagementException {
APIInfoDTO apiInfoDTO = new APIInfoDTO();
apiInfoDTO.setDescription(apiProduct.getDescription());
apiInfoDTO.setContext(apiProduct.getContext());
apiInfoDTO.setId(apiProduct.getUuid());
APIProductIdentifier apiId = apiProduct.getId();
apiInfoDTO.setName(apiId.getName());
apiInfoDTO.setVersion(apiId.getVersion());
apiInfoDTO.setProvider(apiId.getProviderName());
apiInfoDTO.setLifeCycleStatus(apiProduct.getState());
apiInfoDTO.setType(APIType.API_PRODUCT.toString());
apiInfoDTO.setAvgRating(String.valueOf(apiProduct.getRating()));
String providerName = apiProduct.getId().getProviderName();
apiInfoDTO.setProvider(APIUtil.replaceEmailDomainBack(providerName));
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
Set<String> deniedTiers = apiConsumer.getDeniedTiers(organization);
Map<String, Tier> tierMap = APIUtil.getTiers(organization);
setThrottlePoliciesAndMonetization(apiProduct, apiInfoDTO, deniedTiers, tierMap);
APIBusinessInformationDTO apiBusinessInformationDTO = new APIBusinessInformationDTO();
apiBusinessInformationDTO.setBusinessOwner(apiProduct.getBusinessOwner());
apiBusinessInformationDTO.setBusinessOwnerEmail(apiProduct.getBusinessOwnerEmail());
apiBusinessInformationDTO.setTechnicalOwner(apiProduct.getTechnicalOwner());
apiBusinessInformationDTO.setTechnicalOwnerEmail(apiProduct.getTechnicalOwnerEmail());
apiInfoDTO.setBusinessInformation(apiBusinessInformationDTO);
if (!StringUtils.isBlank(apiProduct.getThumbnailUrl())) {
apiInfoDTO.setThumbnailUri(apiProduct.getThumbnailUrl());
}
// Since same APIInfoDTO is used for listing APIProducts in StoreUI set default AdvertisedInfo to the DTO
AdvertiseInfoDTO advertiseInfoDTO = new AdvertiseInfoDTO();
advertiseInfoDTO.setAdvertised(false);
apiInfoDTO.setAdvertiseInfo(advertiseInfoDTO);
String apiTenant = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(apiProduct.getId().getProviderName()));
String subscriptionAvailability = apiProduct.getSubscriptionAvailability();
String subscriptionAllowedTenants = apiProduct.getSubscriptionAvailableTenants();
apiInfoDTO.setIsSubscriptionAvailable(isSubscriptionAvailable(apiTenant, subscriptionAvailability, subscriptionAllowedTenants));
return apiInfoDTO;
}
Aggregations