Search in sources :

Example 26 with GenericArtifact

use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method addAPIRevision.

@Override
public String addAPIRevision(Organization org, String apiUUID, int revisionId) throws APIPersistenceException {
    String revisionUUID;
    boolean transactionCommitted = false;
    Registry registry = null;
    boolean tenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(org.getName());
        registry = holder.getRegistry();
        tenantFlowStarted = holder.isTenantFlowStarted();
        registry.beginTransaction();
        GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
        GenericArtifact apiArtifact = artifactManager.getGenericArtifact(apiUUID);
        if (apiArtifact != null) {
            API api = RegistryPersistenceUtil.getApiForPublishing(registry, apiArtifact);
            APIIdentifier apiId = api.getId();
            String apiPath = RegistryPersistenceUtil.getAPIPath(apiId);
            int prependIndex = apiPath.lastIndexOf("/api");
            String apiSourcePath = apiPath.substring(0, prependIndex);
            String revisionTargetPath = RegistryPersistenceUtil.getRevisionPath(apiId.getUUID(), revisionId);
            if (registry.resourceExists(revisionTargetPath)) {
                throw new APIManagementException("API revision already exists with id: " + revisionId, ExceptionCodes.from(ExceptionCodes.EXISTING_API_REVISION_FOUND, String.valueOf(revisionId)));
            }
            registry.copy(apiSourcePath, revisionTargetPath);
            Resource apiRevisionArtifact = registry.get(revisionTargetPath + "api");
            registry.commitTransaction();
            transactionCommitted = true;
            if (log.isDebugEnabled()) {
                String logMessage = "Revision for API Name: " + apiId.getApiName() + ", API Version " + apiId.getVersion() + " created";
                log.debug(logMessage);
            }
            revisionUUID = apiRevisionArtifact.getUUID();
        } else {
            String msg = "Failed to get API. API artifact corresponding to artifactId " + apiUUID + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } 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 Revision create for API: " + apiUUID, re);
        }
        throw new APIPersistenceException("Error while performing registry transaction operation", e);
    } catch (APIManagementException e) {
        throw new APIPersistenceException("Error while creating API Revision", e);
    } finally {
        try {
            if (tenantFlowStarted) {
                RegistryPersistenceUtil.endTenantFlow();
            }
            if (!transactionCommitted) {
                registry.rollbackTransaction();
            }
        } catch (RegistryException ex) {
            throw new APIPersistenceException("Error while rolling back the transaction for API Revision create for API: " + apiUUID, ex);
        }
    }
    return revisionUUID;
}
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) Resource(org.wso2.carbon.registry.core.Resource) 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) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) API(org.wso2.carbon.apimgt.api.model.API) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 27 with GenericArtifact

use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method deleteAPI.

@Override
public void deleteAPI(Organization org, String apiId) throws APIPersistenceException {
    boolean transactionCommitted = false;
    boolean tenantFlowStarted = false;
    Registry registry = null;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        registry = holder.getRegistry();
        tenantFlowStarted = holder.isTenantFlowStarted();
        registry.beginTransaction();
        GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry);
        GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
        if (artifactManager == null) {
            String errorMessage = "Failed to retrieve artifact manager when deleting API " + apiId;
            log.error(errorMessage);
            throw new APIPersistenceException(errorMessage);
        }
        GenericArtifact apiArtifact = artifactManager.getGenericArtifact(apiId);
        APIIdentifier identifier = new APIIdentifier(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER), apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME), apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
        // Delete the dependencies associated  with the api artifact
        GovernanceArtifact[] dependenciesArray = apiArtifact.getDependencies();
        if (dependenciesArray.length > 0) {
            for (GovernanceArtifact artifact : dependenciesArray) {
                registry.delete(artifact.getPath());
            }
        }
        artifactManager.removeGenericArtifact(apiArtifact);
        String path = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
        Resource apiResource = registry.get(path);
        String artifactId = apiResource.getUUID();
        artifactManager.removeGenericArtifact(artifactId);
        String thumbPath = RegistryPersistenceUtil.getIconPath(identifier);
        if (registry.resourceExists(thumbPath)) {
            registry.delete(thumbPath);
        }
        String wsdlArchivePath = RegistryPersistenceUtil.getWsdlArchivePath(identifier);
        if (registry.resourceExists(wsdlArchivePath)) {
            registry.delete(wsdlArchivePath);
        }
        /*Remove API Definition Resource - swagger*/
        String apiDefinitionFilePath = APIConstants.API_DOC_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + '-' + identifier.getVersion() + '-' + identifier.getProviderName();
        if (registry.resourceExists(apiDefinitionFilePath)) {
            registry.delete(apiDefinitionFilePath);
        }
        /*remove empty directories*/
        String apiCollectionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName();
        if (registry.resourceExists(apiCollectionPath)) {
            Resource apiCollection = registry.get(apiCollectionPath);
            CollectionImpl collection = (CollectionImpl) apiCollection;
            // if there is no other versions of apis delete the directory of the api
            if (collection.getChildCount() == 0) {
                if (log.isDebugEnabled()) {
                    log.debug("No more versions of the API found, removing API collection from registry");
                }
                registry.delete(apiCollectionPath);
            }
        }
        String apiProviderPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName();
        if (registry.resourceExists(apiProviderPath)) {
            Resource providerCollection = registry.get(apiProviderPath);
            CollectionImpl collection = (CollectionImpl) providerCollection;
            // if there is no api for given provider delete the provider directory
            if (collection.getChildCount() == 0) {
                if (log.isDebugEnabled()) {
                    log.debug("No more APIs from the provider " + identifier.getProviderName() + " found. " + "Removing provider collection from registry");
                }
                registry.delete(apiProviderPath);
            }
        }
        registry.commitTransaction();
        transactionCommitted = true;
    } catch (RegistryException e) {
        throw new APIPersistenceException("Failed to remove the API : " + apiId, e);
    } finally {
        if (tenantFlowStarted) {
            RegistryPersistenceUtil.endTenantFlow();
        }
        try {
            if (!transactionCommitted) {
                registry.rollbackTransaction();
            }
        } catch (RegistryException ex) {
            throw new APIPersistenceException("Error occurred while rolling back the transaction. ", ex);
        }
    }
}
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) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 28 with GenericArtifact

use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact 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 29 with GenericArtifact

use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method deleteDocumentation.

@Override
public void deleteDocumentation(Organization org, String apiId, String docId) throws DocumentationPersistenceException {
    boolean isTenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(org.getName());
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifactManager artifactManager = RegistryPersistenceDocUtil.getDocumentArtifactManager(registry);
        if (artifactManager == null) {
            String errorMessage = "Failed to retrieve artifact manager when removing documentation of " + apiId + " Document ID " + docId;
            log.error(errorMessage);
            throw new DocumentationPersistenceException(errorMessage);
        }
        GenericArtifact artifact = artifactManager.getGenericArtifact(docId);
        String docPath = artifact.getPath();
        if (docPath != null) {
            if (registry.resourceExists(docPath)) {
                registry.delete(docPath);
            }
        }
    } catch (RegistryException | APIPersistenceException e) {
        throw new DocumentationPersistenceException("Failed to delete documentation", e);
    } finally {
        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) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 30 with GenericArtifact

use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.

the class RegistryPersistenceUtil method getAPIForSearch.

public static PublisherAPI getAPIForSearch(GenericArtifact apiArtifact) throws APIPersistenceException {
    PublisherAPI api = new PublisherAPI();
    try {
        api.setContext(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE));
        api.setDescription(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION));
        api.setId(apiArtifact.getId());
        api.setStatus(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_STATUS));
        api.setApiName(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME));
        api.setProviderName(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER));
        ;
        api.setVersion(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
        api.setAdvertiseOnly(Boolean.parseBoolean(apiArtifact.getAttribute(APIConstants.API_OVERVIEW_ADVERTISE_ONLY)));
    } catch (GovernanceException e) {
        throw new APIPersistenceException("Error while extracting api attributes ", e);
    }
    return api;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException)

Aggregations

GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)141 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)84 Resource (org.wso2.carbon.registry.core.Resource)74 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)74 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)73 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)66 Registry (org.wso2.carbon.registry.core.Registry)65 Test (org.junit.Test)54 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)54 API (org.wso2.carbon.apimgt.api.model.API)51 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)40 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)38 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)36 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)35 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)35 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)26 UserStoreException (org.wso2.carbon.user.api.UserStoreException)24 ArrayList (java.util.ArrayList)23 QName (javax.xml.namespace.QName)22 Documentation (org.wso2.carbon.apimgt.api.model.Documentation)19