Search in sources :

Example 1 with ThumbnailPersistenceException

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

the class RegistryPersistenceImpl method deleteThumbnail.

@Override
public void deleteThumbnail(Organization org, String apiId) throws ThumbnailPersistenceException {
    Registry registry;
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifact apiArtifact = getAPIArtifact(apiId, registry);
        if (apiArtifact == null) {
            throw new ThumbnailPersistenceException("API not found for id " + apiId, ExceptionCodes.API_NOT_FOUND);
        }
        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 artifactOldPath = APIConstants.API_IMAGE_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
        String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
        String oldThumbPath = artifactOldPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
        String thumbPath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
        if (registry.resourceExists(thumbPath)) {
            registry.delete(thumbPath);
        }
        if (registry.resourceExists(oldThumbPath)) {
            registry.delete(oldThumbPath);
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Error while loading API icon of API " + apiId + " from the registry";
        throw new ThumbnailPersistenceException(msg, 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) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 2 with ThumbnailPersistenceException

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

the class RegistryPersistenceImpl method getThumbnail.

@Override
public ResourceFile getThumbnail(Organization org, String apiId) throws ThumbnailPersistenceException {
    Registry registry;
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        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 artifactOldPath = APIConstants.API_IMAGE_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
        String apiPath = GovernanceUtils.getArtifactPath(registry, apiId);
        int prependIndex = apiPath.lastIndexOf("/api");
        String artifactPath = apiPath.substring(0, prependIndex);
        String oldThumbPath = artifactOldPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
        String thumbPath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
        if (registry.resourceExists(thumbPath)) {
            Resource res = registry.get(thumbPath);
            return new ResourceFile(res.getContentStream(), res.getMediaType());
        } else if (registry.resourceExists(oldThumbPath)) {
            Resource res = registry.get(oldThumbPath);
            return new ResourceFile(res.getContentStream(), res.getMediaType());
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Error while loading API icon of API " + apiId + " from the registry";
        throw new ThumbnailPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return null;
}
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) Resource(org.wso2.carbon.registry.core.Resource) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 3 with ThumbnailPersistenceException

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

the class RegistryPersistenceImpl method saveThumbnail.

@Override
public void saveThumbnail(Organization org, String apiId, ResourceFile resourceFile) throws ThumbnailPersistenceException {
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifactManager apiArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
        GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiId);
        if (apiArtifact == null) {
            throw new ThumbnailPersistenceException("API not found. ", ExceptionCodes.API_NOT_FOUND);
        }
        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 artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
        String filePath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
        String savedFilePath = addResourceFile(filePath, resourceFile, registry, tenantDomain);
        RegistryPersistenceUtil.setResourcePermissions(apiProviderName, null, null, filePath);
        apiArtifact.setAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL, savedFilePath);
        apiArtifactManager.updateGenericArtifact(apiArtifact);
    } catch (APIPersistenceException | GovernanceException | PersistenceException | APIManagementException e) {
        throw new ThumbnailPersistenceException("Error while saving thumbnail for api " + apiId, 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) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) PersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException) GraphQLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException) MediationPolicyPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) OASPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException) AsyncSpecPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry)

Example 4 with ThumbnailPersistenceException

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

the class APIProviderImpl method setThumbnailToAPI.

@Override
public void setThumbnailToAPI(String apiId, ResourceFile resource, String organization) throws APIManagementException {
    try {
        org.wso2.carbon.apimgt.persistence.dto.ResourceFile iconResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile(resource.getContent(), resource.getContentType());
        apiPersistenceInstance.saveThumbnail(new Organization(organization), apiId, iconResourceFile);
    } catch (ThumbnailPersistenceException e) {
        if (e.getErrorHandler() == ExceptionCodes.API_NOT_FOUND) {
            throw new APIMgtResourceNotFoundException(e);
        } else {
            throw new APIManagementException("Error while saving thumbnail ", e);
        }
    }
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ThumbnailPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Aggregations

ThumbnailPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException)4 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)3 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)3 Registry (org.wso2.carbon.registry.core.Registry)3 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)2 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)1 ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)1 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)1 ResourceFile (org.wso2.carbon.apimgt.persistence.dto.ResourceFile)1 AsyncSpecPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.AsyncSpecPersistenceException)1 DocumentationPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException)1 GraphQLPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.GraphQLPersistenceException)1 MediationPolicyPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.MediationPolicyPersistenceException)1 OASPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.OASPersistenceException)1 PersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.PersistenceException)1 WSDLPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException)1 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)1 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)1