Search in sources :

Example 51 with GenericArtifactManager

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

the class AbstractAPIManager method getDocumentation.

public Documentation getDocumentation(APIIdentifier apiId, DocumentationType docType, String docName) throws APIManagementException {
    Documentation documentation = null;
    String docPath = APIUtil.getAPIDocPath(apiId) + docName;
    GenericArtifactManager artifactManager = getAPIGenericArtifactManagerFromUtil(registry, APIConstants.DOCUMENTATION_KEY);
    try {
        Resource docResource = registry.get(docPath);
        GenericArtifact artifact = artifactManager.getGenericArtifact(docResource.getUUID());
        documentation = APIUtil.getDocumentation(artifact);
    } catch (RegistryException e) {
        String msg = "Failed to get documentation details";
        throw new APIManagementException(msg, e);
    }
    return documentation;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 52 with GenericArtifactManager

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

the class APIProviderImpl method updateWsdlFromUrl.

/**
 * This method is used to save the wsdl file in the registry
 * This is used when user starts api creation with a soap endpoint
 *
 * @param api api object
 * @throws APIManagementException
 * @throws RegistryException
 */
public void updateWsdlFromUrl(API api) throws APIManagementException {
    boolean transactionCommitted = false;
    try {
        registry.beginTransaction();
        String apiArtifactId = registry.get(APIUtil.getAPIPath(api.getId())).getUUID();
        GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
        if (artifactManager == null) {
            String errorMessage = "Artifact manager is null when updating WSDL of API " + api.getId().getApiName();
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        GenericArtifact artifact = artifactManager.getGenericArtifact(apiArtifactId);
        GenericArtifact apiArtifact = APIUtil.createAPIArtifactContent(artifact, api);
        String artifactPath = GovernanceUtils.getArtifactPath(registry, apiArtifact.getId());
        if (APIUtil.isValidWSDLURL(api.getWsdlUrl(), false)) {
            String path = APIUtil.createWSDL(registry, api);
            updateWSDLUriInAPIArtifact(path, artifactManager, apiArtifact, artifactPath);
        }
        registry.commitTransaction();
        transactionCommitted = true;
    } catch (RegistryException e) {
        try {
            registry.rollbackTransaction();
        } catch (RegistryException ex) {
            handleException("Error occurred while rolling back the transaction.", ex);
        }
        throw new APIManagementException("Error occurred while saving the wsdl in the registry.", e);
    } finally {
        try {
            if (!transactionCommitted) {
                registry.rollbackTransaction();
            }
        } catch (RegistryException ex) {
            handleException("Error occurred while rolling back the transaction.", ex);
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 53 with GenericArtifactManager

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

the class APIProviderImpl method isAPIUpdateValid.

public boolean isAPIUpdateValid(API api) throws APIManagementException {
    String apiSourcePath = APIUtil.getAPIPath(api.getId());
    boolean isValid = false;
    try {
        Resource apiSourceArtifact = registry.get(apiSourcePath);
        GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
        if (artifactManager == null) {
            String errorMessage = "Failed to retrieve artifact manager when checking validity of API update for " + api.getId().getApiName();
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        GenericArtifact artifact = artifactManager.getGenericArtifact(apiSourceArtifact.getUUID());
        String status = APIUtil.getLcStateFromArtifact(artifact);
        if (!APIConstants.CREATED.equals(status) && !APIConstants.PROTOTYPED.equals(status)) {
            // api at least is in published status
            if (APIUtil.hasPermission(getUserNameWithoutChange(), APIConstants.Permissions.API_PUBLISH)) {
                // user has publish permission
                isValid = true;
            }
        } else if (APIConstants.CREATED.equals(status) || APIConstants.PROTOTYPED.equals(status)) {
            // api in create status
            if (APIUtil.hasPermission(getUserNameWithoutChange(), APIConstants.Permissions.API_CREATE) || APIUtil.hasPermission(getUserNameWithoutChange(), APIConstants.Permissions.API_PUBLISH)) {
                // user has creat or publish permission
                isValid = true;
            }
        }
    } catch (RegistryException ex) {
        handleException("Error while validate user for API publishing", ex);
    }
    return isValid;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 54 with GenericArtifactManager

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

the class AbstractAPIManager method getAPIProduct.

/**
 * Get API Product by product identifier
 *
 * @param identifier APIProductIdentifier
 * @return API product identified by provider identifier
 * @throws APIManagementException
 */
public APIProduct getAPIProduct(APIProductIdentifier identifier) throws APIManagementException {
    String apiProductPath = APIUtil.getAPIProductPath(identifier);
    Registry registry;
    try {
        String productTenantDomain = MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(identifier.getProviderName()));
        int productTenantId = getTenantManager().getTenantId(productTenantDomain);
        if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(productTenantDomain)) {
            APIUtil.loadTenantRegistry(productTenantId);
        }
        if (this.tenantDomain == null || !this.tenantDomain.equals(productTenantDomain)) {
            // cross tenant scenario
            registry = getRegistryService().getGovernanceUserRegistry(getTenantAwareUsername(APIUtil.replaceEmailDomainBack(identifier.getProviderName())), productTenantId);
        } else {
            registry = this.registry;
        }
        GenericArtifactManager artifactManager = getAPIGenericArtifactManagerFromUtil(registry, APIConstants.API_KEY);
        Resource productResource = registry.get(apiProductPath);
        String artifactId = productResource.getUUID();
        if (artifactId == null) {
            throw new APIManagementException("artifact id is null for : " + apiProductPath);
        }
        GenericArtifact productArtifact = artifactManager.getGenericArtifact(artifactId);
        APIProduct apiProduct = APIUtil.getAPIProduct(productArtifact, registry);
        return apiProduct;
    } catch (RegistryException e) {
        String msg = "Failed to get API Product from : " + apiProductPath;
        throw new APIManagementException(msg, e);
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        String msg = "Failed to get API Product from : " + apiProductPath;
        throw new APIManagementException(msg, e);
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 55 with GenericArtifactManager

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

the class APIProviderImpl method updateWsdlFromResourceFile.

public void updateWsdlFromResourceFile(API api) throws APIManagementException {
    boolean transactionCommitted = false;
    try {
        registry.beginTransaction();
        String apiArtifactId = registry.get(APIUtil.getAPIPath(api.getId())).getUUID();
        GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
        if (artifactManager == null) {
            String errorMessage = "Artifact manager is null when updating WSDL of API " + api.getId().getApiName();
            log.error(errorMessage);
            throw new APIManagementException(errorMessage);
        }
        GenericArtifact artifact = artifactManager.getGenericArtifact(apiArtifactId);
        GenericArtifact apiArtifact = APIUtil.createAPIArtifactContent(artifact, api);
        String artifactPath = GovernanceUtils.getArtifactPath(registry, apiArtifact.getId());
        if (api.getWsdlResource() != null) {
            String path = APIUtil.saveWSDLResource(registry, api);
            // reset the wsdl path
            apiArtifact.setAttribute(APIConstants.API_OVERVIEW_WSDL, api.getWsdlUrl());
            // update the  artifact
            artifactManager.updateGenericArtifact(apiArtifact);
            registry.commitTransaction();
            transactionCommitted = true;
        }
    } catch (RegistryException e) {
        try {
            registry.rollbackTransaction();
        } catch (RegistryException ex) {
            handleException("Error occurred while rolling back the transaction.", ex);
        }
    } finally {
        try {
            if (!transactionCommitted) {
                registry.rollbackTransaction();
            }
        } catch (RegistryException ex) {
            handleException("Error occurred while rolling back the transaction.", ex);
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)95 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)90 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)70 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)63 Registry (org.wso2.carbon.registry.core.Registry)57 Resource (org.wso2.carbon.registry.core.Resource)54 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)51 API (org.wso2.carbon.apimgt.api.model.API)37 Test (org.junit.Test)29 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)29 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)29 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)28 UserStoreException (org.wso2.carbon.user.api.UserStoreException)27 ArrayList (java.util.ArrayList)26 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)26 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)20 HashMap (java.util.HashMap)18 DocumentationPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException)17 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)16