use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class APIProviderImpl method updateAPIProductForStateChange.
/**
* Update API Product in registry for lifecycle state change
*
* @param apiProduct API Product Object
* @param currentStatus Current state of API Product
* @param newStatus New state of API Product
* @return boolean indicates success or failure
* @throws APIManagementException if there is an error when updating API Product for lifecycle state
* @throws FaultGatewaysException if there is an error when updating API Product for lifecycle state
*/
public void updateAPIProductForStateChange(APIProduct apiProduct, String currentStatus, String newStatus) throws APIManagementException, FaultGatewaysException {
String provider = apiProduct.getId().getProviderName();
boolean isTenantFlowStarted = false;
try {
String tenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(provider));
if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
isTenantFlowStarted = true;
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
}
if (!currentStatus.equals(newStatus)) {
apiProduct.setState(newStatus);
// If API status changed to publish we should add it to recently added APIs list
// this should happen in store-publisher cluster domain if deployment is distributed
// IF new API published we will add it to recently added APIs
Caching.getCacheManager(APIConstants.API_MANAGER_CACHE_MANAGER).getCache(APIConstants.RECENTLY_ADDED_API_CACHE_NAME).removeAll();
if (APIConstants.RETIRED.equals(newStatus)) {
cleanUpPendingSubscriptionCreationProcessesByAPI(apiProduct.getUuid());
apiMgtDAO.removeAllSubscriptions(apiProduct.getUuid());
deleteAPIProductRevisions(apiProduct.getUuid(), tenantDomain);
}
PublisherAPIProduct publisherAPIProduct = APIProductMapper.INSTANCE.toPublisherApiProduct(apiProduct);
try {
apiPersistenceInstance.updateAPIProduct(new Organization(apiProduct.getOrganization()), publisherAPIProduct);
} catch (APIPersistenceException e) {
handleException("Error while persisting the updated API Product", e);
}
}
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class APIProviderImpl method deleteAPIProduct.
public void deleteAPIProduct(APIProduct apiProduct) throws APIManagementException {
APIProductIdentifier identifier = apiProduct.getId();
try {
// int apiId = apiMgtDAO.getAPIID(identifier, null);
long subsCount = apiMgtDAO.getAPISubscriptionCountByAPI(identifier);
if (subsCount > 0) {
// Logging as a WARN since this isn't an error scenario.
String message = "Cannot remove the API Product as active subscriptions exist.";
log.warn(message);
throw new APIManagementException(message);
}
// gatewayType check is required when API Management is deployed on
// other servers to avoid synapse
deleteAPIProductRevisions(apiProduct.getUuid(), apiProduct.getOrganization());
apiPersistenceInstance.deleteAPIProduct(new Organization(apiProduct.getOrganization()), apiProduct.getUuid());
apiMgtDAO.deleteAPIProduct(identifier);
cleanUpPendingAPIStateChangeTask(apiProduct.getProductId(), true);
if (log.isDebugEnabled()) {
String logMessage = "API Product Name: " + identifier.getName() + ", API Product Version " + identifier.getVersion() + " successfully removed from the database.";
log.debug(logMessage);
}
JSONObject apiLogObject = new JSONObject();
apiLogObject.put(APIConstants.AuditLogConstants.NAME, identifier.getName());
apiLogObject.put(APIConstants.AuditLogConstants.VERSION, identifier.getVersion());
apiLogObject.put(APIConstants.AuditLogConstants.PROVIDER, identifier.getProviderName());
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.API_PRODUCT, apiLogObject.toString(), APIConstants.AuditLogConstants.DELETED, this.username);
GatewayArtifactsMgtDAO.getInstance().deleteGatewayArtifacts(apiProduct.getUuid());
} catch (APIPersistenceException e) {
handleException("Failed to remove the API product", e);
} catch (WorkflowException e) {
handleException("Error while removing the pending workflows of API Product", e);
}
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class APIProviderImpl method addAPIProductSwagger.
@Override
public void addAPIProductSwagger(String productId, Map<API, List<APIProductResource>> apiToProductResourceMapping, APIProduct apiProduct, String orgId) throws APIManagementException {
APIDefinition parser = new OAS3Parser();
SwaggerData swaggerData = new SwaggerData(apiProduct);
String apiProductSwagger = parser.generateAPIDefinition(swaggerData);
apiProductSwagger = OASParserUtil.updateAPIProductSwaggerOperations(apiToProductResourceMapping, apiProductSwagger);
saveSwaggerDefinition(productId, apiProductSwagger, orgId);
apiProduct.setDefinition(apiProductSwagger);
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class APIProviderImpl method searchPaginatedContent.
@Override
public Map<String, Object> searchPaginatedContent(String searchQuery, String organization, int start, int end) throws APIManagementException {
ArrayList<Object> compoundResult = new ArrayList<Object>();
Map<Documentation, API> docMap = new HashMap<Documentation, API>();
Map<Documentation, APIProduct> productDocMap = new HashMap<Documentation, APIProduct>();
Map<String, Object> result = new HashMap<String, Object>();
SortedSet<API> apiSet = new TreeSet<API>(new APINameComparator());
SortedSet<APIProduct> apiProductSet = new TreeSet<APIProduct>(new APIProductNameComparator());
String userame = userNameWithoutChange;
Organization org = new Organization(organization);
Map<String, Object> properties = APIUtil.getUserProperties(userame);
String[] roles = APIUtil.getFilteredUserRoles(userame);
UserContext ctx = new UserContext(userame, org, properties, roles);
try {
PublisherContentSearchResult results = apiPersistenceInstance.searchContentForPublisher(org, searchQuery, start, end, ctx);
if (results != null) {
List<SearchContent> resultList = results.getResults();
for (SearchContent item : resultList) {
if ("API".equals(item.getType())) {
PublisherSearchContent publiserAPI = (PublisherSearchContent) item;
API api = new API(new APIIdentifier(publiserAPI.getProvider(), publiserAPI.getName(), publiserAPI.getVersion()));
api.setUuid(publiserAPI.getId());
api.setContext(publiserAPI.getContext());
api.setContextTemplate(publiserAPI.getContext());
api.setStatus(publiserAPI.getStatus());
apiSet.add(api);
} else if ("APIProduct".equals(item.getType())) {
PublisherSearchContent publiserAPI = (PublisherSearchContent) item;
APIProduct api = new APIProduct(new APIProductIdentifier(publiserAPI.getProvider(), publiserAPI.getName(), publiserAPI.getVersion()));
api.setUuid(publiserAPI.getId());
api.setContextTemplate(publiserAPI.getContext());
api.setState(publiserAPI.getStatus());
apiProductSet.add(api);
} else if (item instanceof DocumentSearchContent) {
// doc item
DocumentSearchContent docItem = (DocumentSearchContent) item;
Documentation doc = new Documentation(DocumentationType.valueOf(docItem.getDocType().toString()), docItem.getName());
doc.setSourceType(DocumentSourceType.valueOf(docItem.getSourceType().toString()));
doc.setVisibility(DocumentVisibility.valueOf(docItem.getVisibility().toString()));
doc.setId(docItem.getId());
if ("API".equals(docItem.getAssociatedType())) {
API api = new API(new APIIdentifier(docItem.getApiProvider(), docItem.getApiName(), docItem.getApiVersion()));
api.setUuid(docItem.getApiUUID());
docMap.put(doc, api);
} else if ("APIProduct".equals(docItem.getAssociatedType())) {
APIProduct api = new APIProduct(new APIProductIdentifier(docItem.getApiProvider(), docItem.getApiName(), docItem.getApiVersion()));
api.setUuid(docItem.getApiUUID());
productDocMap.put(doc, api);
}
}
}
compoundResult.addAll(apiSet);
compoundResult.addAll(apiProductSet);
compoundResult.addAll(docMap.entrySet());
compoundResult.addAll(productDocMap.entrySet());
compoundResult.sort(new ContentSearchResultNameComparator());
result.put("length", results.getTotalCount());
} else {
result.put("length", compoundResult.size());
}
} catch (APIPersistenceException e) {
throw new APIManagementException("Error while searching content ", e);
}
result.put("apis", compoundResult);
return result;
}
use of org.wso2.carbon.apimgt.api.model.APIProduct in project carbon-apimgt by wso2.
the class APIProviderImpl method updateAPIProductSwagger.
@Override
public void updateAPIProductSwagger(String productId, Map<API, List<APIProductResource>> apiToProductResourceMapping, APIProduct apiProduct, String orgId) throws APIManagementException {
APIDefinition parser = new OAS3Parser();
SwaggerData updatedData = new SwaggerData(apiProduct);
String existingProductSwagger = getAPIDefinitionOfAPIProduct(apiProduct);
String updatedProductSwagger = parser.generateAPIDefinition(updatedData, existingProductSwagger);
updatedProductSwagger = OASParserUtil.updateAPIProductSwaggerOperations(apiToProductResourceMapping, updatedProductSwagger);
saveSwaggerDefinition(productId, updatedProductSwagger, orgId);
apiProduct.setDefinition(updatedProductSwagger);
}
Aggregations