Search in sources :

Example 61 with APIProduct

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;
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIProductSearchResultDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductSearchResultDTO)

Example 62 with APIProduct

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);
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Tier(org.wso2.carbon.apimgt.api.model.Tier) API(org.wso2.carbon.apimgt.api.model.API) List(java.util.List) ArrayList(java.util.ArrayList)

Example 63 with APIProduct

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;
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) Tier(org.wso2.carbon.apimgt.api.model.Tier) APIListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIListDTO) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) API(org.wso2.carbon.apimgt.api.model.API) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) APIInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIInfoDTO)

Example 64 with APIProduct

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);
}
Also used : Tier(org.wso2.carbon.apimgt.api.model.Tier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 65 with APIProduct

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;
}
Also used : APIBusinessInformationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIBusinessInformationDTO) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) AdvertiseInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.AdvertiseInfoDTO) Tier(org.wso2.carbon.apimgt.api.model.Tier) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) APIInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APIInfoDTO)

Aggregations

APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)71 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)52 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)51 API (org.wso2.carbon.apimgt.api.model.API)37 ArrayList (java.util.ArrayList)31 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)22 PublisherAPIProduct (org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct)22 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)21 Tier (org.wso2.carbon.apimgt.api.model.Tier)21 HashMap (java.util.HashMap)19 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)19 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)18 JSONObject (org.json.simple.JSONObject)17 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)17 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)16 HashSet (java.util.HashSet)15 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)15 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)14 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)14 ParseException (org.json.simple.parser.ParseException)12