Search in sources :

Example 11 with GenericArtifact

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

the class RegistryPersistenceImpl method getAsyncDefinition.

@Override
public String getAsyncDefinition(Organization org, String apiId) throws AsyncSpecPersistenceException {
    String apiTenantDomain = org.getName();
    String definition = null;
    boolean tenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(apiTenantDomain);
        Registry registryType = holder.getRegistry();
        tenantFlowStarted = holder.isTenantFlowStarted;
        GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registryType, APIConstants.API_KEY);
        GenericArtifact apiArtifact = artifactManager.getGenericArtifact(apiId);
        if (apiArtifact != null) {
            String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
            String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
            String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
            String apiPath = GovernanceUtils.getArtifactPath(registryType, apiId);
            int prependIndex = apiPath.lastIndexOf("/api");
            String apiSourcePath = apiPath.substring(0, prependIndex);
            String definitionPath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ASYNC_API_DEFINITION_RESOURCE_NAME;
            if (registryType.resourceExists(definitionPath)) {
                Resource apiDocResource = registryType.get(definitionPath);
                definition = new String((byte[]) apiDocResource.getContent(), Charset.defaultCharset());
                return definition;
            }
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Failed to get specification of API : " + apiId;
        throw new AsyncSpecPersistenceException(msg, e);
    } finally {
        if (tenantFlowStarted) {
            RegistryPersistenceUtil.endTenantFlow();
        }
    }
    return definition;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) 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) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 12 with GenericArtifact

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

the class RegistryPersistenceImpl method deleteAPIProduct.

@Override
public void deleteAPIProduct(Organization org, String apiId) throws APIPersistenceException {
    boolean tenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(org.getName());
        tenantFlowStarted = holder.isTenantFlowStarted();
        Registry registry = holder.getRegistry();
        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 Product" + apiId;
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        GenericArtifact apiProductArtifact = artifactManager.getGenericArtifact(apiId);
        APIProductIdentifier identifier = new APIProductIdentifier(apiProductArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER), apiProductArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME), apiProductArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
        // this is the product resource collection path
        String productResourcePath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(identifier.getProviderName()) + RegistryConstants.PATH_SEPARATOR + identifier.getName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
        // this is the product rxt instance path
        String apiProductArtifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(identifier.getProviderName()) + RegistryConstants.PATH_SEPARATOR + identifier.getName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + APIConstants.API_RESOURCE_NAME;
        Resource apiProductResource = registry.get(productResourcePath);
        String productResourceUUID = apiProductResource.getUUID();
        if (productResourceUUID == null) {
            throw new APIManagementException("artifact id is null for : " + productResourcePath);
        }
        Resource apiArtifactResource = registry.get(apiProductArtifactPath);
        String apiArtifactResourceUUID = apiArtifactResource.getUUID();
        if (apiArtifactResourceUUID == null) {
            throw new APIManagementException("artifact id is null for : " + apiProductArtifactPath);
        }
        // Delete the dependencies associated with the api product artifact
        GovernanceArtifact[] dependenciesArray = apiProductArtifact.getDependencies();
        if (dependenciesArray.length > 0) {
            for (GovernanceArtifact artifact : dependenciesArray) {
                registry.delete(artifact.getPath());
            }
        }
        // delete registry resources
        artifactManager.removeGenericArtifact(apiProductArtifact);
        artifactManager.removeGenericArtifact(productResourceUUID);
        /* remove empty directories */
        String apiProductCollectionPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getName();
        if (registry.resourceExists(apiProductCollectionPath)) {
            // at the moment product versioning is not supported so we are directly deleting this collection as
            // this is known to be empty
            registry.delete(apiProductCollectionPath);
        }
        String productProviderPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getName();
        if (registry.resourceExists(productProviderPath)) {
            Resource providerCollection = registry.get(productProviderPath);
            CollectionImpl collection = (CollectionImpl) providerCollection;
            // if there is no api product for given provider delete the provider directory
            if (collection.getChildCount() == 0) {
                if (log.isDebugEnabled()) {
                    log.debug("No more API Products from the provider " + identifier.getProviderName() + " found. " + "Removing provider collection from registry");
                }
                registry.delete(productProviderPath);
            }
        }
    } 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) 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) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CollectionImpl(org.wso2.carbon.registry.core.CollectionImpl)

Example 13 with GenericArtifact

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

the class RegistryPersistenceImpl method searchDocumentation.

@Override
public DocumentSearchResult searchDocumentation(Organization org, String apiId, int start, int offset, String searchQuery, UserContext ctx) throws DocumentationPersistenceException {
    DocumentSearchResult result = null;
    Registry registryType;
    String requestedTenantDomain = org.getName();
    boolean isTenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(requestedTenantDomain);
        registryType = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifactManager apiArtifactManager = RegistryPersistenceUtil.getArtifactManager(registryType, APIConstants.API_KEY);
        GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiId);
        String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
        String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
        String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
        String apiPath = GovernanceUtils.getArtifactPath(registryType, apiId);
        int prependIndex = apiPath.lastIndexOf("/api");
        String apiSourcePath = apiPath.substring(0, prependIndex);
        String apiOrAPIProductDocPath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR;
        String pathToContent = apiOrAPIProductDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR;
        String pathToDocFile = apiOrAPIProductDocPath + APIConstants.DOCUMENT_FILE_DIR;
        if (registryType.resourceExists(apiOrAPIProductDocPath)) {
            List<Documentation> documentationList = new ArrayList<Documentation>();
            Resource resource = registryType.get(apiOrAPIProductDocPath);
            if (resource instanceof org.wso2.carbon.registry.core.Collection) {
                String[] docsPaths = ((org.wso2.carbon.registry.core.Collection) resource).getChildren();
                for (String docPath : docsPaths) {
                    if (!(docPath.equalsIgnoreCase(pathToContent) || docPath.equalsIgnoreCase(pathToDocFile))) {
                        Resource docResource = registryType.get(docPath);
                        GenericArtifactManager artifactManager = RegistryPersistenceDocUtil.getDocumentArtifactManager(registryType);
                        GenericArtifact docArtifact = artifactManager.getGenericArtifact(docResource.getUUID());
                        Documentation doc = RegistryPersistenceDocUtil.getDocumentation(docArtifact);
                        if (searchQuery != null) {
                            if (searchQuery.toLowerCase().startsWith("name:")) {
                                String requestedDocName = searchQuery.split(":")[1];
                                if (doc.getName().equalsIgnoreCase(requestedDocName)) {
                                    documentationList.add(doc);
                                }
                            } else {
                                log.warn("Document search not implemented for the query " + searchQuery);
                            }
                        } else {
                            documentationList.add(doc);
                        }
                    }
                }
            }
            result = new DocumentSearchResult();
            result.setDocumentationList(documentationList);
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Failed to get documentations for api/product " + apiId;
        throw new DocumentationPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return result;
}
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) Documentation(org.wso2.carbon.apimgt.persistence.dto.Documentation) ArrayList(java.util.ArrayList) 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) DocumentSearchResult(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchResult) Collection(org.wso2.carbon.registry.core.Collection)

Example 14 with GenericArtifact

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

the class RegistryPersistenceImpl method addDocumentation.

@Override
public Documentation addDocumentation(Organization org, String apiId, Documentation documentation) throws DocumentationPersistenceException {
    boolean tenantFlowStarted = false;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        Registry registry = holder.getRegistry();
        tenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifactManager apiArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
        GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiId);
        String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
        apiProviderName = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
        String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
        String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
        GenericArtifactManager docArtifactManager = new GenericArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
        GenericArtifact docArtifact = docArtifactManager.newGovernanceArtifact(new QName(documentation.getName()));
        docArtifactManager.addGenericArtifact(RegistryPersistenceDocUtil.createDocArtifactContent(docArtifact, apiName, apiVersion, apiProviderName, documentation));
        String apiPath = RegistryPersistenceUtil.getAPIPath(apiName, apiVersion, apiProviderName);
        String docVisibility = documentation.getVisibility().name();
        String[] authorizedRoles = RegistryPersistenceUtil.getAuthorizedRoles(apiPath, tenantDomain);
        String visibility = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY);
        if (docVisibility != null) {
            if (APIConstants.DOC_SHARED_VISIBILITY.equalsIgnoreCase(docVisibility)) {
                authorizedRoles = null;
                visibility = APIConstants.DOC_SHARED_VISIBILITY;
            } else if (APIConstants.DOC_OWNER_VISIBILITY.equalsIgnoreCase(docVisibility)) {
                authorizedRoles = null;
                visibility = APIConstants.DOC_OWNER_VISIBILITY;
            }
        }
        RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, authorizedRoles, docArtifact.getPath(), registry);
        String docFilePath = docArtifact.getAttribute(APIConstants.DOC_FILE_PATH);
        if (docFilePath != null && !"".equals(docFilePath)) {
            // The docFilePatch comes as
            // /t/tenanatdoman/registry/resource/_system/governance/apimgt/applicationdata..
            // We need to remove the /t/tenanatdoman/registry/resource/_system/governance section to set
            // permissions.
            int startIndex = docFilePath.indexOf(APIConstants.GOVERNANCE) + (APIConstants.GOVERNANCE).length();
            String filePath = docFilePath.substring(startIndex, docFilePath.length());
            RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, authorizedRoles, filePath, registry);
        }
        documentation.setId(docArtifact.getId());
        return documentation;
    } catch (RegistryException | APIManagementException | UserStoreException | APIPersistenceException e) {
        throw new DocumentationPersistenceException("Failed to add documentation", e);
    } finally {
        if (tenantFlowStarted) {
            RegistryPersistenceUtil.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) 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) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.api.UserStoreException)

Example 15 with GenericArtifact

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

the class RegistryPersistenceImpl method restoreAPIRevision.

@Override
public void restoreAPIRevision(Organization org, String apiUUID, String revisionUUID, int revisionId) throws APIPersistenceException {
    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);
        String lcState = ((GenericArtifactImpl) apiArtifact).getLcState();
        if (apiArtifact != null) {
            String apiPath = GovernanceUtils.getArtifactPath(registry, apiUUID);
            int prependIndex = apiPath.lastIndexOf("/api");
            String apiSourcePath = apiPath.substring(0, prependIndex);
            String revisionTargetPath = RegistryPersistenceUtil.getRevisionPath(apiUUID, revisionId);
            registry.delete(apiSourcePath);
            registry.copy(revisionTargetPath, apiSourcePath);
            Resource newAPIArtifact = registry.get(apiPath);
            newAPIArtifact.setUUID(apiUUID);
            newAPIArtifact.setProperty("registry.lifecycle.APILifeCycle.state", java.util.Arrays.asList((lcState)));
            registry.put(apiPath, newAPIArtifact);
        }
        registry.commitTransaction();
        transactionCommitted = true;
        if (log.isDebugEnabled()) {
            String logMessage = "Revision ID" + revisionId + " for API UUID: " + apiUUID + " restored";
            log.debug(logMessage);
        }
    } 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 restore for API: " + apiUUID, re);
        }
        throw new APIPersistenceException("Error while performing registry transaction operation", 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 restore for API: " + apiUUID, 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) 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) GenericArtifactImpl(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifactImpl)

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