Search in sources :

Example 1 with PublisherAPIProduct

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();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 2 with PublisherAPIProduct

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());
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with PublisherAPIProduct

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);
}
Also used : PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with PublisherAPIProduct

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();
        }
    }
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct)

Example 5 with PublisherAPIProduct

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;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct)

Aggregations

PublisherAPIProduct (org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct)12 APIProduct (org.wso2.carbon.apimgt.api.model.APIProduct)8 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)8 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)7 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)6 Registry (org.wso2.carbon.registry.core.Registry)6 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)6 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)4 Resource (org.wso2.carbon.registry.core.Resource)4 Matchers.anyString (org.mockito.Matchers.anyString)3 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)3 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)3 ResourceImpl (org.wso2.carbon.registry.core.ResourceImpl)3 Tag (org.wso2.carbon.registry.core.Tag)3 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)3 QName (javax.xml.namespace.QName)2 OASPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException)2