Search in sources :

Example 1 with ResourceFile

use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.

the class AbstractAPIManager method getWSDL.

/**
 * Returns the wsdl content in registry specified by the wsdl name
 *
 * @param apiId Api Identifier
 * @return wsdl content matching name if exist else null
 */
@Override
public ResourceFile getWSDL(APIIdentifier apiId) throws APIManagementException {
    String apiPath = APIUtil.getAPIPath(apiId);
    int prependIndex = apiPath.indexOf(apiId.getVersion()) + apiId.getVersion().length();
    String apiSourcePath = apiPath.substring(0, prependIndex);
    String wsdlResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIUtil.createWsdlFileName(apiId.getProviderName(), apiId.getApiName(), apiId.getVersion());
    String wsdlResourcePathOld = APIConstants.API_WSDL_RESOURCE_LOCATION + APIUtil.createWsdlFileName(apiId.getProviderName(), apiId.getApiName(), apiId.getVersion());
    String tenantDomain = getTenantDomain(apiId);
    boolean isTenantFlowStarted = false;
    try {
        Registry registry;
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            isTenantFlowStarted = true;
            startTenantFlow(tenantDomain);
            int id = getTenantManager().getTenantId(tenantDomain);
            registry = getRegistryService().getGovernanceSystemRegistry(id);
        } else {
            if (this.tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(this.tenantDomain)) {
                registry = getRegistryService().getGovernanceUserRegistry(apiId.getProviderName(), MultitenantConstants.SUPER_TENANT_ID);
            } else {
                registry = this.registry;
            }
        }
        if (registry.resourceExists(wsdlResourcePath)) {
            Resource resource = registry.get(wsdlResourcePath);
            return new ResourceFile(resource.getContentStream(), resource.getMediaType());
        } else if (registry.resourceExists(wsdlResourcePathOld)) {
            Resource resource = registry.get(wsdlResourcePathOld);
            return new ResourceFile(resource.getContentStream(), resource.getMediaType());
        } else {
            wsdlResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiId.getProviderName() + APIConstants.WSDL_PROVIDER_SEPERATOR + apiId.getApiName() + apiId.getVersion() + APIConstants.ZIP_FILE_EXTENSION;
            wsdlResourcePathOld = APIConstants.API_WSDL_RESOURCE_LOCATION + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiId.getProviderName() + APIConstants.WSDL_PROVIDER_SEPERATOR + apiId.getApiName() + apiId.getVersion() + APIConstants.ZIP_FILE_EXTENSION;
            if (registry.resourceExists(wsdlResourcePath)) {
                Resource resource = registry.get(wsdlResourcePath);
                return new ResourceFile(resource.getContentStream(), resource.getMediaType());
            } else if (registry.resourceExists(wsdlResourcePathOld)) {
                Resource resource = registry.get(wsdlResourcePathOld);
                return new ResourceFile(resource.getContentStream(), resource.getMediaType());
            } else {
                throw new APIManagementException("No WSDL found for the API: " + apiId, ExceptionCodes.from(ExceptionCodes.NO_WSDL_AVAILABLE_FOR_API, apiId.getApiName(), apiId.getVersion()));
            }
        }
    } catch (RegistryException | org.wso2.carbon.user.api.UserStoreException e) {
        String msg = "Error while getting wsdl file from the registry for API: " + apiId.toString();
        throw new APIManagementException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            endTenantFlow();
        }
    }
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) UserStoreException(org.wso2.carbon.user.core.UserStoreException) 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 ResourceFile

use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.

the class AbstractAPIManager method getWSDL.

@Override
public ResourceFile getWSDL(String apiId, String organization) throws APIManagementException {
    try {
        org.wso2.carbon.apimgt.persistence.dto.ResourceFile resource = apiPersistenceInstance.getWSDL(new Organization(organization), apiId);
        if (resource != null) {
            ResourceFile resourceFile = new ResourceFile(resource.getContent(), resource.getContentType());
            resourceFile.setName(resource.getName());
            return resourceFile;
        } else {
            String msg = "Failed to get WSDL. Artifact corresponding to artifactId " + apiId + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } catch (WSDLPersistenceException e) {
        throw new APIManagementException("Error while retrieving wsdl resource for api " + apiId, 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) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 3 with ResourceFile

use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.

the class AbstractAPIManager method getIcon.

public ResourceFile getIcon(APIIdentifier identifier) throws APIManagementException {
    String artifactOldPath = APIConstants.API_IMAGE_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
    String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getApiName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion();
    String tenantDomain = getTenantDomain(identifier);
    Registry registry;
    boolean isTenantFlowStarted = false;
    try {
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            startTenantFlow(tenantDomain);
            isTenantFlowStarted = true;
        }
        /* If the API provider is a tenant, load tenant registry*/
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            int id = getTenantManager().getTenantId(tenantDomain);
            registry = getRegistryService().getGovernanceSystemRegistry(id);
        } else {
            if (this.tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(this.tenantDomain)) {
                registry = getRegistryService().getGovernanceUserRegistry(identifier.getProviderName(), MultitenantConstants.SUPER_TENANT_ID);
            } else {
                registry = this.registry;
            }
        }
        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 e) {
        String msg = "Error while loading API icon of API " + identifier.getApiName() + ":" + identifier.getVersion() + " from the registry";
        throw new APIManagementException(msg, e);
    } catch (org.wso2.carbon.user.api.UserStoreException e) {
        String msg = "Error while loading API icon of API " + identifier.getApiName() + ":" + identifier.getVersion();
        throw new APIManagementException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            endTenantFlow();
        }
    }
    return null;
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) 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)

Example 4 with ResourceFile

use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getDocumentationContent.

@Override
public DocumentContent getDocumentationContent(Organization org, String apiId, String docId) throws DocumentationPersistenceException {
    DocumentContent documentContent = null;
    boolean tenantFlowStarted = false;
    try {
        String requestedTenantDomain = org.getName();
        RegistryHolder holder = getRegistry(requestedTenantDomain);
        Registry registryType = holder.getRegistry();
        tenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifactManager artifactManager = RegistryPersistenceDocUtil.getDocumentArtifactManager(registryType);
        GenericArtifact artifact = artifactManager.getGenericArtifact(docId);
        if (artifact == null) {
            return null;
        }
        if (artifact != null) {
            Documentation documentation = RegistryPersistenceDocUtil.getDocumentation(artifact);
            if (documentation.getSourceType().equals(Documentation.DocumentSourceType.FILE)) {
                String resource = documentation.getFilePath();
                if (resource == null) {
                    return null;
                }
                String[] resourceSplitPath = resource.split(RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH);
                if (resourceSplitPath.length == 2) {
                    resource = resourceSplitPath[1];
                } else {
                    throw new DocumentationPersistenceException("Invalid resource Path " + resource);
                }
                if (registryType.resourceExists(resource)) {
                    documentContent = new DocumentContent();
                    Resource apiDocResource = registryType.get(resource);
                    String[] content = apiDocResource.getPath().split("/");
                    String name = content[content.length - 1];
                    documentContent.setSourceType(ContentSourceType.FILE);
                    ResourceFile resourceFile = new ResourceFile(apiDocResource.getContentStream(), apiDocResource.getMediaType());
                    resourceFile.setName(name);
                    documentContent.setResourceFile(resourceFile);
                }
            } else if (documentation.getSourceType().equals(Documentation.DocumentSourceType.INLINE) || documentation.getSourceType().equals(Documentation.DocumentSourceType.MARKDOWN)) {
                String contentPath = artifact.getPath().replace(RegistryConstants.PATH_SEPARATOR + documentation.getName(), "") + RegistryConstants.PATH_SEPARATOR + APIConstants.INLINE_DOCUMENT_CONTENT_DIR + RegistryConstants.PATH_SEPARATOR + documentation.getName();
                if (registryType.resourceExists(contentPath)) {
                    documentContent = new DocumentContent();
                    Resource docContent = registryType.get(contentPath);
                    Object content = docContent.getContent();
                    if (content != null) {
                        String contentStr = new String((byte[]) docContent.getContent(), Charset.defaultCharset());
                        documentContent.setTextContent(contentStr);
                        documentContent.setSourceType(ContentSourceType.valueOf(documentation.getSourceType().toString()));
                    }
                }
            } else if (documentation.getSourceType().equals(Documentation.DocumentSourceType.URL)) {
                documentContent = new DocumentContent();
                String sourceUrl = documentation.getSourceUrl();
                documentContent.setTextContent(sourceUrl);
                documentContent.setSourceType(ContentSourceType.valueOf(documentation.getSourceType().toString()));
            }
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Failed to get documentation details";
        throw new DocumentationPersistenceException(msg, e);
    } finally {
        if (tenantFlowStarted) {
            RegistryPersistenceUtil.endTenantFlow();
        }
    }
    return documentContent;
}
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) 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) ResourceFile(org.wso2.carbon.apimgt.persistence.dto.ResourceFile) DocumentContent(org.wso2.carbon.apimgt.persistence.dto.DocumentContent)

Example 5 with ResourceFile

use of org.wso2.carbon.apimgt.persistence.dto.ResourceFile 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)

Aggregations

ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)27 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)24 Resource (org.wso2.carbon.registry.core.Resource)13 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)11 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)10 Registry (org.wso2.carbon.registry.core.Registry)9 InputStream (java.io.InputStream)8 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)8 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)7 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)6 WSDLPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException)6 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)5 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)5 ResourceFile (org.wso2.carbon.apimgt.persistence.dto.ResourceFile)4 ThumbnailPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.ThumbnailPersistenceException)4 IOException (java.io.IOException)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3