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