Search in sources :

Example 96 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getWSDL.

@Override
public ResourceFile getWSDL(Organization org, String apiId) throws WSDLPersistenceException {
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifact apiArtifact = getAPIArtifact(apiId, registry);
        if (apiArtifact == null) {
            return null;
        }
        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);
        String apiPath = GovernanceUtils.getArtifactPath(registry, apiId);
        int prependIndex = apiPath.lastIndexOf("/api");
        String apiSourcePath = apiPath.substring(0, prependIndex);
        String wsdlResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.createWsdlFileName(apiProviderName, apiName, apiVersion);
        String wsdlResourcePathOld = APIConstants.API_WSDL_RESOURCE_LOCATION + RegistryPersistenceUtil.createWsdlFileName(apiProviderName, apiName, apiVersion);
        String resourceFileName = apiProviderName + "-" + apiName + "-" + apiVersion;
        if (registry.resourceExists(wsdlResourcePath)) {
            Resource resource = registry.get(wsdlResourcePath);
            ResourceFile returnResource = new ResourceFile(resource.getContentStream(), resource.getMediaType());
            returnResource.setName(resourceFileName);
            return returnResource;
        } else if (registry.resourceExists(wsdlResourcePathOld)) {
            Resource resource = registry.get(wsdlResourcePathOld);
            ResourceFile returnResource = new ResourceFile(resource.getContentStream(), resource.getMediaType());
            returnResource.setName(resourceFileName);
            return returnResource;
        } else {
            wsdlResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiProviderName + APIConstants.WSDL_PROVIDER_SEPERATOR + apiName + apiVersion + APIConstants.ZIP_FILE_EXTENSION;
            wsdlResourcePathOld = APIConstants.API_WSDL_RESOURCE_LOCATION + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiProviderName + APIConstants.WSDL_PROVIDER_SEPERATOR + apiName + apiVersion + APIConstants.ZIP_FILE_EXTENSION;
            if (registry.resourceExists(wsdlResourcePath)) {
                Resource resource = registry.get(wsdlResourcePath);
                ResourceFile returnResource = new ResourceFile(resource.getContentStream(), resource.getMediaType());
                returnResource.setName(resourceFileName);
                return returnResource;
            } else if (registry.resourceExists(wsdlResourcePathOld)) {
                Resource resource = registry.get(wsdlResourcePathOld);
                ResourceFile returnResource = new ResourceFile(resource.getContentStream(), resource.getMediaType());
                returnResource.setName(resourceFileName);
                return returnResource;
            } else {
                throw new WSDLPersistenceException("No WSDL found for the API: " + apiId, ExceptionCodes.from(ExceptionCodes.NO_WSDL_AVAILABLE_FOR_API, apiName, apiVersion));
            }
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Error while getting wsdl file from the registry for API: " + apiId.toString();
        throw new WSDLPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) ResourceFile(org.wso2.carbon.apimgt.persistence.dto.ResourceFile) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) 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 97 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getOASDefinition.

@Override
public String getOASDefinition(Organization org, String apiId) throws OASPersistenceException {
    String apiTenantDomain = org.getName();
    String definition = null;
    boolean tenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(apiTenantDomain);
        Registry registryType = holder.getRegistry();
        tenantFlowStarted = holder.isTenantFlowStarted;
        GenericArtifact apiArtifact = getAPIArtifact(apiId, registryType);
        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_OAS_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 swagger documentation of API : " + apiId;
        throw new OASPersistenceException(msg, e);
    } finally {
        if (tenantFlowStarted) {
            RegistryPersistenceUtil.endTenantFlow();
        }
    }
    return definition;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) 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 98 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException 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 99 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getSoapToRestSequences.

protected List<SOAPToRestSequence> getSoapToRestSequences(Registry registry, API api, Direction direction) throws RegistryException, APIPersistenceException {
    String resourcePath = APIConstants.API_LOCATION + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.replaceEmailDomain(api.getId().getProviderName()) + RegistryConstants.PATH_SEPARATOR + api.getId().getName() + RegistryConstants.PATH_SEPARATOR + api.getId().getVersion() + RegistryConstants.PATH_SEPARATOR + "soap_to_rest" + RegistryConstants.PATH_SEPARATOR;
    if (direction == Direction.IN) {
        resourcePath = resourcePath + "in";
    } else if (direction == Direction.OUT) {
        resourcePath = resourcePath + "out";
    } else {
        throw new APIPersistenceException("Invalid sequence type");
    }
    List<SOAPToRestSequence> sequences = new ArrayList<SOAPToRestSequence>();
    if (registry.resourceExists(resourcePath)) {
        Collection collection = (Collection) registry.get(resourcePath);
        String[] resources = collection.getChildren();
        for (String path : resources) {
            Resource resource = registry.get(path);
            String content = new String((byte[]) resource.getContent(), Charset.defaultCharset());
            String resourceName;
            if (resource.getProperty("resourcePath") != null) {
                resourceName = resource.getProperty("resourcePath");
            } else {
                resourceName = ((ResourceImpl) resource).getName();
            }
            resourceName = resourceName.replaceAll("\\.xml", "");
            resourceName = resourceName.split("_")[0];
            String httpMethod = resource.getProperty("method");
            SOAPToRestSequence seq = new SOAPToRestSequence(httpMethod, resourceName, content, direction);
            seq.setUuid(resource.getUUID());
            sequences.add(seq);
        }
    }
    return sequences;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) Collection(org.wso2.carbon.registry.core.Collection) SOAPToRestSequence(org.wso2.carbon.apimgt.api.model.SOAPToRestSequence)

Example 100 with APIPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method deleteAPIRevision.

@Override
public void deleteAPIRevision(Organization org, String apiUUID, String revisionUUID, int revisionId) throws APIPersistenceException {
    String revisionTargetPath = APIConstants.API_REVISION_LOCATION + RegistryConstants.PATH_SEPARATOR + apiUUID + RegistryConstants.PATH_SEPARATOR + revisionId;
    boolean transactionCommitted = false;
    Registry registry = null;
    boolean tenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(org.getName());
        registry = holder.getRegistry();
        tenantFlowStarted = holder.isTenantFlowStarted();
        registry.beginTransaction();
        registry.delete(revisionTargetPath);
        registry.commitTransaction();
        transactionCommitted = true;
        if (log.isDebugEnabled()) {
            String logMessage = "Revision ID:" + revisionId + " for API : " + apiUUID + " deleted";
            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 delete 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 delete for API: " + apiUUID, ex);
        }
    }
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)83 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)55 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)49 Registry (org.wso2.carbon.registry.core.Registry)49 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)47 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)44 Resource (org.wso2.carbon.registry.core.Resource)40 API (org.wso2.carbon.apimgt.api.model.API)35 PublisherAPI (org.wso2.carbon.apimgt.persistence.dto.PublisherAPI)34 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)32 Test (org.junit.Test)29 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)27 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 ArrayList (java.util.ArrayList)21 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)19 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)18 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)18 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)16