Search in sources :

Example 41 with APICategory

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

the class APIUtil method createAPIProductArtifactContent.

/**
 * Create Governance artifact from given attributes
 *
 * @param artifact   initial governance artifact
 * @param apiProduct APIProduct object with the attributes value
 * @return GenericArtifact
 * @throws APIManagementException if failed to create API Product
 */
public static GenericArtifact createAPIProductArtifactContent(GenericArtifact artifact, APIProduct apiProduct) throws APIManagementException {
    try {
        // todo : review and add missing fields
        artifact.setAttribute(APIConstants.API_OVERVIEW_NAME, apiProduct.getId().getName());
        artifact.setAttribute(APIConstants.API_OVERVIEW_VERSION, apiProduct.getId().getVersion());
        artifact.setAttribute(APIConstants.API_OVERVIEW_PROVIDER, apiProduct.getId().getProviderName());
        artifact.setAttribute(APIConstants.API_OVERVIEW_CONTEXT, apiProduct.getContext());
        artifact.setAttribute(APIConstants.API_OVERVIEW_DESCRIPTION, apiProduct.getDescription());
        artifact.setAttribute(APIConstants.API_OVERVIEW_TYPE, APIConstants.AuditLogConstants.API_PRODUCT);
        artifact.setAttribute(APIConstants.API_OVERVIEW_STATUS, apiProduct.getState());
        artifact.setAttribute(APIConstants.API_OVERVIEW_VISIBILITY, apiProduct.getVisibility());
        artifact.setAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES, apiProduct.getVisibleRoles());
        artifact.setAttribute(APIConstants.API_OVERVIEW_VISIBLE_TENANTS, apiProduct.getVisibleTenants());
        artifact.setAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER, apiProduct.getBusinessOwner());
        artifact.setAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER_EMAIL, apiProduct.getBusinessOwnerEmail());
        artifact.setAttribute(APIConstants.API_OVERVIEW_TEC_OWNER, apiProduct.getTechnicalOwner());
        artifact.setAttribute(APIConstants.API_OVERVIEW_TEC_OWNER_EMAIL, apiProduct.getTechnicalOwnerEmail());
        artifact.setAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABILITY, apiProduct.getSubscriptionAvailability());
        artifact.setAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABLE_TENANTS, apiProduct.getSubscriptionAvailableTenants());
        artifact.setAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL, apiProduct.getThumbnailUrl());
        artifact.setAttribute(APIConstants.API_OVERVIEW_CACHE_TIMEOUT, Integer.toString(apiProduct.getCacheTimeout()));
        StringBuilder policyBuilder = new StringBuilder();
        for (Tier tier : apiProduct.getAvailableTiers()) {
            policyBuilder.append(tier.getName());
            policyBuilder.append("||");
        }
        String policies = policyBuilder.toString();
        if (!"".equals(policies)) {
            policies = policies.substring(0, policies.length() - 2);
            artifact.setAttribute(APIConstants.API_OVERVIEW_TIER, policies);
        } else {
            artifact.setAttribute(APIConstants.API_OVERVIEW_TIER, policies);
        }
        artifact.setAttribute(APIConstants.API_OVERVIEW_ENVIRONMENTS, writeEnvironmentsToArtifact(apiProduct));
        artifact.setAttribute(APIConstants.API_OVERVIEW_TRANSPORTS, apiProduct.getTransports());
        artifact.setAttribute(APIConstants.API_OVERVIEW_CORS_CONFIGURATION, APIUtil.getCorsConfigurationJsonFromDto(apiProduct.getCorsConfiguration()));
        artifact.setAttribute(APIConstants.API_OVERVIEW_AUTHORIZATION_HEADER, apiProduct.getAuthorizationHeader());
        artifact.setAttribute(APIConstants.API_OVERVIEW_API_SECURITY, apiProduct.getApiSecurity());
        // Validate if the API has an unsupported context before setting it in the artifact
        String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
        if (APIConstants.SUPER_TENANT_DOMAIN.equals(tenantDomain)) {
            String invalidContext = File.separator + APIConstants.VERSION_PLACEHOLDER;
            if (invalidContext.equals(apiProduct.getContextTemplate())) {
                throw new APIManagementException("API : " + apiProduct.getId() + " has an unsupported context : " + apiProduct.getContextTemplate());
            }
        } else {
            String invalidContext = APIConstants.TENANT_PREFIX + tenantDomain + File.separator + APIConstants.VERSION_PLACEHOLDER;
            if (invalidContext.equals(apiProduct.getContextTemplate())) {
                throw new APIManagementException("API : " + apiProduct.getId() + " has an unsupported context : " + apiProduct.getContextTemplate());
            }
        }
        artifact.setAttribute(APIConstants.API_OVERVIEW_ENABLE_JSON_SCHEMA, Boolean.toString(apiProduct.isEnabledSchemaValidation()));
        artifact.setAttribute(APIConstants.API_OVERVIEW_ENABLE_STORE, Boolean.toString(apiProduct.isEnableStore()));
        artifact.setAttribute(APIConstants.API_OVERVIEW_RESPONSE_CACHING, apiProduct.getResponseCache());
        // This is to support the pluggable version strategy.
        artifact.setAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE, apiProduct.getContextTemplate());
        artifact.setAttribute(APIConstants.API_OVERVIEW_VERSION_TYPE, "context");
        // set monetization status (i.e - enabled or disabled)
        artifact.setAttribute(APIConstants.Monetization.API_MONETIZATION_STATUS, Boolean.toString(apiProduct.getMonetizationStatus()));
        // set additional monetization data
        if (apiProduct.getMonetizationProperties() != null) {
            artifact.setAttribute(APIConstants.Monetization.API_MONETIZATION_PROPERTIES, apiProduct.getMonetizationProperties().toJSONString());
        }
        // attaching api categories to the API
        List<APICategory> attachedApiCategories = apiProduct.getApiCategories();
        artifact.removeAttribute(APIConstants.API_CATEGORIES_CATEGORY_NAME);
        if (attachedApiCategories != null) {
            for (APICategory category : attachedApiCategories) {
                artifact.addAttribute(APIConstants.API_CATEGORIES_CATEGORY_NAME, category.getName());
            }
        }
    } catch (GovernanceException e) {
        String msg = "Failed to create API for : " + apiProduct.getId().getName();
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    }
    return artifact;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Tier(org.wso2.carbon.apimgt.api.model.Tier) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) APICategory(org.wso2.carbon.apimgt.api.model.APICategory)

Example 42 with APICategory

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

the class APICategoryMappingUtil method fromCategoryListToCategoryListDTO.

/**
 * Convert list of API Categories to CategoryListDTO
 *
 * @param categories List of api categories
 * @return CategoryListDTO list containing api category data
 */
public static APICategoryListDTO fromCategoryListToCategoryListDTO(List<APICategory> categories) {
    APICategoryListDTO categoryListDTO = new APICategoryListDTO();
    categoryListDTO.setCount(categories.size());
    categoryListDTO.setList(fromCategoryListToCategoryDTOList(categories));
    return categoryListDTO;
}
Also used : APICategoryListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.APICategoryListDTO)

Aggregations

APICategory (org.wso2.carbon.apimgt.api.model.APICategory)38 ArrayList (java.util.ArrayList)23 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)14 Tier (org.wso2.carbon.apimgt.api.model.Tier)13 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)8 JSONObject (org.json.simple.JSONObject)7 Test (org.junit.Test)6 API (org.wso2.carbon.apimgt.api.model.API)6 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 JSONParser (org.json.simple.parser.JSONParser)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)4 Scope (org.wso2.carbon.apimgt.api.model.Scope)4 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)4 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)3 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)3