use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getPublisherAPIProduct.
@Override
public PublisherAPIProduct getPublisherAPIProduct(Organization org, String apiProductId) throws APIPersistenceException {
boolean tenantFlowStarted = false;
try {
RegistryHolder holder = getRegistry(org.getName());
tenantFlowStarted = holder.isTenantFlowStarted();
Registry registry = holder.getRegistry();
GenericArtifact apiArtifact = getAPIArtifact(apiProductId, registry);
if (apiArtifact != null) {
APIProduct apiProduct = RegistryPersistenceUtil.getAPIProduct(apiArtifact, registry);
String definitionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(apiProduct.getId().getProviderName()) + RegistryConstants.PATH_SEPARATOR + apiProduct.getId().getName() + RegistryConstants.PATH_SEPARATOR + apiProduct.getId().getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME;
if (registry.resourceExists(definitionPath)) {
Resource apiDocResource = registry.get(definitionPath);
String apiDocContent = new String((byte[]) apiDocResource.getContent(), Charset.defaultCharset());
apiProduct.setDefinition(apiDocContent);
}
PublisherAPIProduct pubApi = APIProductMapper.INSTANCE.toPublisherApiProduct(apiProduct);
pubApi.setApiProductName(apiProduct.getId().getName());
pubApi.setProviderName(apiProduct.getId().getProviderName());
pubApi.setVersion(apiProduct.getId().getVersion());
if (log.isDebugEnabled()) {
log.debug("API Product for id " + apiProductId + " : " + pubApi.toString());
}
return pubApi;
} else {
String msg = "Failed to get API. API artifact corresponding to artifactId " + apiProductId + " does not exist";
throw new APIMgtResourceNotFoundException(msg);
}
} catch (RegistryException e) {
String msg = "Failed to get API";
throw new APIPersistenceException(msg, e);
} catch (APIManagementException e) {
String msg = "Failed to get API";
throw new APIPersistenceException(msg, e);
} finally {
if (tenantFlowStarted) {
RegistryPersistenceUtil.endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct in project carbon-apimgt by wso2.
the class APIMapperTestCase method testAPIProductToPublisherAPIProductAndBack.
@Test
public void testAPIProductToPublisherAPIProductAndBack() throws GovernanceException, APIManagementException {
PublisherAPIProduct pubAPI = APIProductMapper.INSTANCE.toPublisherApiProduct(product);
Assert.assertEquals("API product uuid does not match", product.getUuid(), pubAPI.getId());
Assert.assertEquals("API product type does not match", product.getType(), pubAPI.getType());
APIProduct mappedProduct = APIProductMapper.INSTANCE.toApiProduct(pubAPI);
Assert.assertEquals("Mapped product uuid does not match", mappedProduct.getUuid(), product.getUuid());
}
use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct in project carbon-apimgt by wso2.
the class APIProviderImplTest method testUpdateAPIProductForStateChange.
@Test
public void testUpdateAPIProductForStateChange() throws Exception {
String provider = "admin";
PowerMockito.mockStatic(MultitenantUtils.class);
Mockito.when(MultitenantUtils.getTenantDomain(provider)).thenReturn(APIConstants.SUPER_TENANT_DOMAIN);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
APIProduct apiProduct = createMockAPIProduct(provider);
prepareForLCStateChangeOfAPIProduct(apiProvider, apiProduct);
PublisherAPIProduct publisherAPIProduct = APIProductMapper.INSTANCE.toPublisherApiProduct(apiProduct);
Organization organization = new Organization(APIConstants.SUPER_TENANT_DOMAIN);
Mockito.when(apiPersistenceInstance.updateAPIProduct(organization, publisherAPIProduct)).thenReturn(publisherAPIProduct);
apiProvider.updateAPIProductForStateChange(apiProduct, APIConstants.CREATED, APIConstants.PUBLISHED);
}
use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct 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.persistence.dto.PublisherAPIProduct in project carbon-apimgt by wso2.
the class APIProviderImpl method createAPIProduct.
/**
* Create an Api Product
*
* @param apiProduct API Product
* @throws APIManagementException if failed to create APIProduct
*/
protected String createAPIProduct(APIProduct apiProduct) throws APIManagementException {
String apiProductUUID = null;
// Validate Transports and Security
validateAndSetTransports(apiProduct);
validateAndSetAPISecurity(apiProduct);
PublisherAPIProduct publisherAPIProduct = APIProductMapper.INSTANCE.toPublisherApiProduct(apiProduct);
PublisherAPIProduct addedAPIProduct;
try {
publisherAPIProduct.setApiProductName(apiProduct.getId().getName());
publisherAPIProduct.setProviderName(apiProduct.getId().getProviderName());
publisherAPIProduct.setVersion(apiProduct.getId().getVersion());
addedAPIProduct = apiPersistenceInstance.addAPIProduct(new Organization(CarbonContext.getThreadLocalCarbonContext().getTenantDomain()), publisherAPIProduct);
apiProductUUID = addedAPIProduct.getId();
} catch (APIPersistenceException e) {
throw new APIManagementException("Error while creating API product ", e);
}
return apiProductUUID;
}
Aggregations