Search in sources :

Example 26 with APICategory

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

the class RegistryPersistenceUtil 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.getEnvironments()));
        artifact.setAttribute(APIConstants.API_OVERVIEW_TRANSPORTS, apiProduct.getTransports());
        artifact.setAttribute(APIConstants.API_OVERVIEW_CORS_CONFIGURATION, 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");
        artifact.setAttribute(APIConstants.API_GATEWAY_VENDOR, apiProduct.getGatewayVendor());
        // 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());
            }
        }
        // set version timestamp
        artifact.addAttribute(APIConstants.API_OVERVIEW_VERSION_TIMESTAMP, apiProduct.getVersionTimestamp());
    } 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 27 with APICategory

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

the class RegistryPersistenceUtil method getAPICategoriesFromAPIGovernanceArtifact.

/**
 * This method returns the categories attached to the API
 *
 * @param artifact API artifact
 * @param tenantID tenant ID of API Provider
 * @return List<APICategory> list of categories
 */
private static List<APICategory> getAPICategoriesFromAPIGovernanceArtifact(GovernanceArtifact artifact, int tenantID) throws GovernanceException, APIManagementException {
    String[] categoriesOfAPI = artifact.getAttributes(APIConstants.API_CATEGORIES_CATEGORY_NAME);
    List<APICategory> categoryList = new ArrayList<>();
    if (ArrayUtils.isNotEmpty(categoriesOfAPI)) {
        for (String categoryName : categoriesOfAPI) {
            APICategory category = new APICategory();
            category.setName(categoryName);
            categoryList.add(category);
        }
    }
    return categoryList;
}
Also used : ArrayList(java.util.ArrayList) APICategory(org.wso2.carbon.apimgt.api.model.APICategory)

Example 28 with APICategory

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

the class ApiCategoriesApiServiceImpl method apiCategoriesGet.

@Override
public Response apiCategoriesGet(MessageContext messageContext) {
    try {
        APIAdmin apiAdmin = new APIAdminImpl();
        String organization = RestApiUtil.getOrganization(messageContext);
        List<APICategory> categoryList = apiAdmin.getAPICategoriesOfOrganization(organization);
        APICategoryListDTO categoryListDTO = APICategoryMappingUtil.fromCategoryListToCategoryListDTO(categoryList);
        return Response.ok().entity(categoryListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving API categories";
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) APICategory(org.wso2.carbon.apimgt.api.model.APICategory) APICategoryListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.APICategoryListDTO)

Example 29 with APICategory

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

the class ApiCategoriesApiServiceImpl method apiCategoriesApiCategoryIdPut.

@Override
public Response apiCategoriesApiCategoryIdPut(String apiCategoryId, APICategoryDTO body, MessageContext messageContext) {
    try {
        APIAdmin apiAdmin = new APIAdminImpl();
        String organization = RestApiUtil.getOrganization(messageContext);
        int tenantID = APIUtil.getInternalOrganizationId(organization);
        APICategory apiCategoryToUpdate = APICategoryMappingUtil.fromCategoryDTOToCategory(body);
        APICategory apiCategoryOriginal = apiAdmin.getAPICategoryByID(apiCategoryId);
        if (apiCategoryOriginal == null) {
            String errorMsg = "No API category with the given category ID exists: " + apiCategoryId;
            throw new APIManagementException(errorMsg);
        }
        // Override several properties as they are not allowed to be updated
        apiCategoryToUpdate.setName(apiCategoryOriginal.getName());
        apiCategoryToUpdate.setId(apiCategoryOriginal.getId());
        apiCategoryToUpdate.setTenantID(apiCategoryOriginal.getTenantID());
        apiCategoryToUpdate.setOrganization(organization);
        // We allow to update API Category name given that the new category name is not taken yet
        String oldName = apiCategoryOriginal.getName();
        String updatedName = apiCategoryToUpdate.getName();
        if (!oldName.equals(updatedName) && apiAdmin.isCategoryNameExists(updatedName, apiCategoryId, organization)) {
            String errorMsg = "An API category already exists by the new API category name :" + updatedName;
            throw new APIManagementException(errorMsg);
        }
        apiAdmin.updateCategory(apiCategoryToUpdate);
        APICategory updatedAPICategory = apiAdmin.getAPICategoryByID(apiCategoryId);
        APICategoryDTO updatedAPICategoryDTO = APICategoryMappingUtil.fromCategoryToCategoryDTO(updatedAPICategory);
        return Response.ok().entity(updatedAPICategoryDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while updating API Category '" + body.getName() + "' - " + e.getMessage();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) APICategoryDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.APICategoryDTO) APICategory(org.wso2.carbon.apimgt.api.model.APICategory)

Example 30 with APICategory

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

the class APIProductMapper method mapAPICategoriesToList.

default List<APICategory> mapAPICategoriesToList(Set<String> apiCategories) {
    List<APICategory> categoryList = new ArrayList<APICategory>();
    if (apiCategories != null) {
        for (String category : apiCategories) {
            APICategory apiCategory = new APICategory();
            apiCategory.setName(category);
            categoryList.add(apiCategory);
        }
    }
    return categoryList;
}
Also used : ArrayList(java.util.ArrayList) APICategory(org.wso2.carbon.apimgt.api.model.APICategory)

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