Search in sources :

Example 71 with Registry

use of org.wso2.carbon.registry.api.Registry 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 72 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method searchAPIProductsForPublisher.

@Override
public PublisherAPIProductSearchResult searchAPIProductsForPublisher(Organization org, String searchQuery, int start, int offset, UserContext ctx) throws APIPersistenceException {
    String requestedTenantDomain = org.getName();
    boolean isTenantFlowStarted = false;
    PublisherAPIProductSearchResult result = new PublisherAPIProductSearchResult();
    try {
        RegistryHolder holder = getRegistry(ctx.getUserame(), requestedTenantDomain);
        Registry userRegistry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        log.debug("Requested query for publisher product search: " + searchQuery);
        String modifiedQuery = RegistrySearchUtil.getPublisherProductSearchQuery(searchQuery, ctx);
        log.debug("Modified query for publisher product search: " + modifiedQuery);
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(ctx.getUserame());
        final int maxPaginationLimit = getMaxPaginationLimit();
        PaginationContext.init(start, offset, "ASC", APIConstants.API_OVERVIEW_NAME, maxPaginationLimit);
        List<GovernanceArtifact> governanceArtifacts = GovernanceUtils.findGovernanceArtifacts(modifiedQuery, userRegistry, APIConstants.API_RXT_MEDIA_TYPE, true);
        int totalLength = PaginationContext.getInstance().getLength();
        // Check to see if we can speculate that there are more APIs to be loaded
        if (maxPaginationLimit == totalLength) {
            // Remove the additional 1 added earlier when setting max pagination limit
            --totalLength;
        }
        int tempLength = 0;
        List<PublisherAPIProductInfo> publisherAPIProductInfoList = new ArrayList<PublisherAPIProductInfo>();
        for (GovernanceArtifact artifact : governanceArtifacts) {
            PublisherAPIProductInfo info = new PublisherAPIProductInfo();
            info.setProviderName(artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER));
            info.setContext(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT));
            info.setId(artifact.getId());
            info.setApiProductName(artifact.getAttribute(APIConstants.API_OVERVIEW_NAME));
            info.setState(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS));
            info.setType(artifact.getAttribute(APIConstants.API_OVERVIEW_TYPE));
            info.setVersion(artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
            info.setApiSecurity(artifact.getAttribute(APIConstants.API_OVERVIEW_API_SECURITY));
            publisherAPIProductInfoList.add(info);
            // Ensure the APIs returned matches the length, there could be an additional API
            // returned due incrementing the pagination limit when getting from registry
            tempLength++;
            if (tempLength >= totalLength) {
                break;
            }
        }
        result.setPublisherAPIProductInfoList(publisherAPIProductInfoList);
        result.setReturnedAPIsCount(publisherAPIProductInfoList.size());
        result.setTotalAPIsCount(totalLength);
    } catch (GovernanceException e) {
        throw new APIPersistenceException("Error while searching APIs ", e);
    } finally {
        PaginationContext.destroy();
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return result;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GovernanceArtifact(org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact) ArrayList(java.util.ArrayList) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) PublisherAPIProductSearchResult(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProductSearchResult) PublisherAPIProductInfo(org.wso2.carbon.apimgt.persistence.dto.PublisherAPIProductInfo)

Example 73 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getRegistry.

protected RegistryHolder getRegistry(String requestedTenantDomain) throws APIPersistenceException {
    String userTenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
    log.debug("Accessing system registry in tenant domain " + userTenantDomain + ". Requested tenant domain: " + requestedTenantDomain);
    boolean tenantFlowStarted = false;
    Registry registry;
    RegistryHolder holder = new RegistryHolder();
    try {
        if (requestedTenantDomain != null) {
            int id = getTenantManager().getTenantId(requestedTenantDomain);
            RegistryPersistenceUtil.startTenantFlow(requestedTenantDomain);
            tenantFlowStarted = true;
            if (userTenantDomain != null && !userTenantDomain.equals(requestedTenantDomain)) {
                // cross tenant
                log.debug("Cross tenant user from tenant " + userTenantDomain + " accessing " + requestedTenantDomain + " registry");
                loadTenantRegistry(id);
                registry = getRegistryService().getGovernanceSystemRegistry(id);
                holder.setTenantId(id);
                ServiceReferenceHolder.setUserRealm((ServiceReferenceHolder.getInstance().getRealmService().getBootstrapRealm()));
            } else {
                log.debug("Same tenant accessing registry of tenant " + userTenantDomain + ":" + tenantId);
                loadTenantRegistry(tenantId);
                registry = getRegistryService().getGovernanceSystemRegistry(tenantId);
                RegistryPersistenceUtil.loadloadTenantAPIRXT(null, tenantId);
                holder.setTenantId(tenantId);
                ServiceReferenceHolder.setUserRealm((UserRealm) (ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)));
            }
        } else {
            log.debug("Same tenant user accessing registry of tenant " + userTenantDomain + ":" + tenantId);
            loadTenantRegistry(tenantId);
            registry = getRegistryService().getGovernanceSystemRegistry(tenantId);
            RegistryPersistenceUtil.loadloadTenantAPIRXT(null, tenantId);
            ServiceReferenceHolder.setUserRealm((UserRealm) (ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)));
            holder.setTenantId(tenantId);
        }
    } catch (RegistryException | UserStoreException | APIManagementException e) {
        String msg = "Failed to get API";
        throw new APIPersistenceException(msg, e);
    }
    holder.setRegistry(registry);
    holder.setTenantFlowStarted(tenantFlowStarted);
    return holder;
}
Also used : APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.api.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 74 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method saveAPIStatus.

/**
 * Persist API Status into a property of API Registry resource
 *
 * @param artifactId API artifact ID
 * @param apiStatus  Current status of the API
 * @throws APIManagementException on error
 */
private void saveAPIStatus(Registry registry, String artifactId, String apiStatus) throws APIManagementException {
    try {
        Resource resource = registry.get(artifactId);
        if (resource != null) {
            String propValue = resource.getProperty(APIConstants.API_STATUS);
            if (propValue == null) {
                resource.addProperty(APIConstants.API_STATUS, apiStatus);
            } else {
                resource.setProperty(APIConstants.API_STATUS, apiStatus);
            }
            registry.put(artifactId, resource);
        }
    } catch (RegistryException e) {
        handleException("Error while adding API", e);
    }
}
Also used : Resource(org.wso2.carbon.registry.core.Resource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 75 with Registry

use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getbasicAPIInfo.

private BasicAPI getbasicAPIInfo(String uuid, Registry registry) throws APIPersistenceException, GovernanceException {
    BasicAPI api = new BasicAPI();
    GenericArtifact apiArtifact = getAPIArtifact(uuid, registry);
    if (apiArtifact == null) {
        return null;
    }
    String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
    api.apiProvider = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
    api.apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
    api.apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
    String visibleRolesList = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES);
    if (visibleRolesList != null) {
        api.visibleRoles = visibleRolesList.split(",");
    }
    api.visibility = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY);
    return api;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)

Aggregations

RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)230 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)225 Resource (org.wso2.carbon.registry.core.Resource)210 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)197 Registry (org.wso2.carbon.registry.core.Registry)125 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)114 Test (org.junit.Test)111 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)107 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)91 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)89 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)80 IOException (java.io.IOException)75 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)74 UserStoreException (org.wso2.carbon.user.api.UserStoreException)71 API (org.wso2.carbon.apimgt.api.model.API)70 ArrayList (java.util.ArrayList)67 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)58 GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)51 HashMap (java.util.HashMap)48 OMElement (org.apache.axiom.om.OMElement)48