Search in sources :

Example 6 with PublisherAPIProduct

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct in project carbon-apimgt by wso2.

the class APIProviderImpl method getAPIProductbyUUID.

public APIProduct getAPIProductbyUUID(String uuid, String organization) throws APIManagementException {
    try {
        Organization org = new Organization(organization);
        PublisherAPIProduct publisherAPIProduct = apiPersistenceInstance.getPublisherAPIProduct(org, uuid);
        if (publisherAPIProduct != null) {
            APIProduct product = APIProductMapper.INSTANCE.toApiProduct(publisherAPIProduct);
            product.setID(new APIProductIdentifier(publisherAPIProduct.getProviderName(), publisherAPIProduct.getApiProductName(), publisherAPIProduct.getVersion(), uuid));
            checkAccessControlPermission(userNameWithoutChange, product.getAccessControl(), product.getAccessControlRoles());
            populateRevisionInformation(product, uuid);
            populateAPIProductInformation(uuid, organization, product);
            populateAPIStatus(product);
            populateAPITier(product);
            return product;
        } else {
            String msg = "Failed to get API Product. API Product artifact corresponding to artifactId " + uuid + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } catch (APIPersistenceException | OASPersistenceException | ParseException e) {
        String msg = "Failed to get API Product";
        throw new APIManagementException(msg, e);
    }
}
Also used : PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) ParseException(org.json.simple.parser.ParseException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 7 with PublisherAPIProduct

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct in project carbon-apimgt by wso2.

the class APIProviderImpl method updateApiProductArtifact.

/**
 * Update API Product Artifact in Registry
 *
 * @param apiProduct
 * @param updateMetadata
 * @param updatePermissions
 * @throws APIManagementException
 */
private void updateApiProductArtifact(APIProduct apiProduct, boolean updateMetadata, boolean updatePermissions) throws APIManagementException {
    // 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.updateAPIProduct(new Organization(CarbonContext.getThreadLocalCarbonContext().getTenantDomain()), publisherAPIProduct);
    } catch (APIPersistenceException e) {
        throw new APIManagementException("Error while creating API product ");
    }
}
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)

Example 8 with PublisherAPIProduct

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method updateAPIProduct.

@Override
public PublisherAPIProduct updateAPIProduct(Organization org, PublisherAPIProduct publisherAPIProduct) throws APIPersistenceException {
    String requestedTenantDomain = org.getName();
    boolean isTenantFlowStarted = false;
    boolean transactionCommitted = false;
    APIProduct apiProduct;
    Registry registry = null;
    try {
        RegistryHolder holder = getRegistry(requestedTenantDomain);
        registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        registry.beginTransaction();
        GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
        if (artifactManager == null) {
            String errorMessage = "Artifact manager is null when updating API Product with artifact ID " + publisherAPIProduct.getId();
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        GenericArtifact artifact = artifactManager.getGenericArtifact(publisherAPIProduct.getId());
        apiProduct = APIProductMapper.INSTANCE.toApiProduct(publisherAPIProduct);
        APIProductIdentifier id = new APIProductIdentifier(publisherAPIProduct.getProviderName(), publisherAPIProduct.getApiProductName(), publisherAPIProduct.getVersion());
        apiProduct.setID(id);
        GenericArtifact updateApiProductArtifact = RegistryPersistenceUtil.createAPIProductArtifactContent(artifact, apiProduct);
        String artifactPath = GovernanceUtils.getArtifactPath(registry, updateApiProductArtifact.getId());
        artifactManager.updateGenericArtifact(updateApiProductArtifact);
        String visibleRolesList = apiProduct.getVisibleRoles();
        String[] visibleRoles = new String[0];
        if (visibleRolesList != null) {
            visibleRoles = visibleRolesList.split(",");
        }
        org.wso2.carbon.registry.core.Tag[] oldTags = registry.getTags(artifactPath);
        if (oldTags != null) {
            for (org.wso2.carbon.registry.core.Tag tag : oldTags) {
                registry.removeTag(artifactPath, tag.getTagName());
            }
        }
        Set<String> tagSet = apiProduct.getTags();
        if (tagSet != null) {
            for (String tag : tagSet) {
                registry.applyTag(artifactPath, tag);
            }
        }
        String publisherAccessControlRoles = apiProduct.getAccessControlRoles();
        updateRegistryResources(registry, artifactPath, publisherAccessControlRoles, apiProduct.getAccessControl(), apiProduct.getAdditionalProperties());
        RegistryPersistenceUtil.setResourcePermissions(apiProduct.getId().getProviderName(), apiProduct.getVisibility(), visibleRoles, artifactPath, registry);
        registry.commitTransaction();
        transactionCommitted = true;
        return publisherAPIProduct;
    } catch (Exception e) {
        try {
            registry.rollbackTransaction();
        } catch (RegistryException re) {
            // Throwing an error from this level will mask the original exception
            log.error("Error while rolling back the transaction for API Product: " + publisherAPIProduct.getApiProductName(), re);
        }
        throw new APIPersistenceException("Error while performing registry transaction operation", e);
    } finally {
        try {
            if (!transactionCommitted) {
                registry.rollbackTransaction();
            }
        } catch (RegistryException ex) {
            throw new APIPersistenceException("Error occurred while rolling back the transaction.", ex);
        }
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) XMLStreamException(javax.xml.stream.XMLStreamException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) PersistenceUtil.handleException(org.wso2.carbon.apimgt.persistence.utils.PersistenceUtil.handleException) IOException(java.io.IOException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) IndexerException(org.wso2.carbon.registry.indexing.indexer.IndexerException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Tag(org.wso2.carbon.apimgt.api.model.Tag)

Example 9 with PublisherAPIProduct

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method addAPIProduct.

@Override
public PublisherAPIProduct addAPIProduct(Organization org, PublisherAPIProduct publisherAPIProduct) throws APIPersistenceException {
    Registry registry = null;
    boolean isTenantFlowStarted = false;
    boolean transactionCommitted = false;
    APIProduct apiProduct;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        registry.beginTransaction();
        GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
        GenericArtifact genericArtifact = artifactManager.newGovernanceArtifact(new QName(publisherAPIProduct.getApiProductName()));
        apiProduct = APIProductMapper.INSTANCE.toApiProduct(publisherAPIProduct);
        APIProductIdentifier id = new APIProductIdentifier(publisherAPIProduct.getProviderName(), publisherAPIProduct.getApiProductName(), publisherAPIProduct.getVersion());
        apiProduct.setID(id);
        if (genericArtifact == null) {
            String errorMessage = "Generic artifact is null when creating API Product" + apiProduct.getId().getName();
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        GenericArtifact artifact = RegistryPersistenceUtil.createAPIProductArtifactContent(genericArtifact, apiProduct);
        artifactManager.addGenericArtifact(artifact);
        artifact.attachLifecycle(APIConstants.API_LIFE_CYCLE);
        String artifactPath = GovernanceUtils.getArtifactPath(registry, artifact.getId());
        String providerPath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + id.getProviderName();
        // provider ------provides----> APIProduct
        registry.addAssociation(providerPath, artifactPath, APIConstants.PROVIDER_ASSOCIATION);
        String apiProductStatus = apiProduct.getState();
        saveAPIStatus(registry, artifactPath, apiProductStatus);
        Set<String> tagSet = apiProduct.getTags();
        if (tagSet != null) {
            for (String tag : tagSet) {
                registry.applyTag(artifactPath, tag);
            }
        }
        String visibleRolesList = apiProduct.getVisibleRoles();
        String[] visibleRoles = new String[0];
        if (visibleRolesList != null) {
            visibleRoles = visibleRolesList.split(",");
        }
        String publisherAccessControlRoles = apiProduct.getAccessControlRoles();
        updateRegistryResources(registry, artifactPath, publisherAccessControlRoles, apiProduct.getAccessControl(), apiProduct.getAdditionalProperties());
        RegistryPersistenceUtil.setResourcePermissions(apiProduct.getId().getProviderName(), apiProduct.getVisibility(), visibleRoles, artifactPath, registry);
        registry.commitTransaction();
        transactionCommitted = true;
        if (log.isDebugEnabled()) {
            String logMessage = "API Product Name: " + apiProduct.getId().getName() + ", API Product Version " + apiProduct.getId().getVersion() + " created";
            log.debug(logMessage);
        }
        publisherAPIProduct.setCreatedTime(String.valueOf(new Date().getTime()));
        publisherAPIProduct.setId(artifact.getId());
        return publisherAPIProduct;
    } catch (RegistryException e) {
        try {
            registry.rollbackTransaction();
        } catch (RegistryException re) {
            // Throwing an error here would mask the original exception
            log.error("Error while rolling back the transaction for API Product : " + publisherAPIProduct.getApiProductName(), re);
        }
        throw new APIPersistenceException("Error while performing registry transaction operation", e);
    } catch (APIManagementException e) {
        throw new APIPersistenceException("Error while creating API Product", e);
    } finally {
        try {
            if (!transactionCommitted) {
                registry.rollbackTransaction();
            }
        } catch (RegistryException ex) {
            throw new APIPersistenceException("Error while rolling back the transaction for API Product : " + publisherAPIProduct.getApiProductName(), ex);
        }
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) QName(javax.xml.namespace.QName) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Date(java.util.Date) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 10 with PublisherAPIProduct

use of org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct in project carbon-apimgt by wso2.

the class RegistryPersistenceImplTestCase method testAddAPIProduct.

@Test
public void testAddAPIProduct() throws RegistryException, APIPersistenceException, APIManagementException {
    GenericArtifact artifact = PersistenceHelper.getSampleAPIProductArtifact();
    PublisherAPIProduct publisherAPI = new PublisherAPIProduct();
    publisherAPI.setApiProductName(artifact.getAttribute(APIConstants.API_OVERVIEW_NAME));
    publisherAPI.setProviderName(artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER));
    publisherAPI.setVersion(artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
    APIProduct api = APIProductMapper.INSTANCE.toApiProduct(publisherAPI);
    Registry registry = Mockito.mock(UserRegistry.class);
    Resource resource = new ResourceImpl();
    Mockito.when(registry.get(anyString())).thenReturn(resource);
    Tag[] tags = new Tag[0];
    Mockito.when(registry.getTags(anyString())).thenReturn(tags);
    PowerMockito.mockStatic(RegistryPersistenceUtil.class);
    GenericArtifactManager manager = Mockito.mock(GenericArtifactManager.class);
    PowerMockito.when(RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY)).thenReturn(manager);
    PowerMockito.when(RegistryPersistenceUtil.createAPIProductArtifactContent(any(GenericArtifact.class), any(APIProduct.class))).thenReturn(artifact);
    GenericArtifact newArtifact = Mockito.mock(GenericArtifact.class);
    Mockito.when(manager.newGovernanceArtifact(new QName(publisherAPI.getApiProductName()))).thenReturn(newArtifact);
    Mockito.when(manager.getGenericArtifact(any(String.class))).thenReturn(newArtifact);
    Mockito.doNothing().when(newArtifact).invokeAction("Publish", APIConstants.API_LIFE_CYCLE);
    Organization org = new Organization(SUPER_TENANT_DOMAIN);
    APIPersistence apiPersistenceInstance = new RegistryPersistenceImplWrapper(registry, artifact);
    apiPersistenceInstance.addAPIProduct(org, publisherAPI);
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) Matchers.anyString(org.mockito.Matchers.anyString) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) PublisherAPIProduct(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProduct) Tag(org.wso2.carbon.registry.core.Tag) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

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