Search in sources :

Example 46 with Documentation

use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.

the class AbstractAPIManager method isDocumentationExist.

/**
 * Checks whether the given document already exists for the given api/product
 *
 * @param identifier API/Product Identifier
 * @param docName    Name of the document
 * @return true if document already exists for the given api/product
 * @throws APIManagementException if failed to check existence of the documentation
 */
public boolean isDocumentationExist(Identifier identifier, String docName) throws APIManagementException {
    String docPath = "";
    docPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + identifier.getProviderName() + RegistryConstants.PATH_SEPARATOR + identifier.getName() + RegistryConstants.PATH_SEPARATOR + identifier.getVersion() + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR + docName;
    try {
        return registry.resourceExists(docPath);
    } catch (RegistryException e) {
        String msg = "Failed to check existence of the document :" + docPath;
        throw new APIManagementException(msg, e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 47 with Documentation

use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.

the class UserAwareAPIProvider method searchAPIDoc.

@Override
public Map<Documentation, API> searchAPIDoc(Registry registry, int tenantID, String username, String searchTerm) throws APIManagementException {
    Map<Documentation, API> apiByDocumentation = APIUtil.searchAPIsByDoc(registry, tenantId, username, searchTerm, APIConstants.PUBLISHER_CLIENT);
    Map<Documentation, API> filteredAPIDocumentation = new HashMap<>();
    for (Map.Entry<Documentation, API> entry : apiByDocumentation.entrySet()) {
        API api = entry.getValue();
        if (api != null) {
            checkAccessControlPermission(api.getId());
            filteredAPIDocumentation.put(entry.getKey(), api);
        }
    }
    return filteredAPIDocumentation;
}
Also used : HashMap(java.util.HashMap) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) API(org.wso2.carbon.apimgt.api.model.API) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) HashMap(java.util.HashMap) Map(java.util.Map)

Example 48 with Documentation

use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.

the class ContentSearchResultNameComparator method compare.

@Override
public int compare(Object o1, Object o2) {
    if (o1 instanceof API && o2 instanceof API) {
        API api1 = (API) o1;
        API api2 = (API) o2;
        return nameComparator.compare(api1, api2);
    } else if (o1 instanceof APIProduct && o2 instanceof APIProduct) {
        APIProduct apiProduct1 = (APIProduct) o1;
        APIProduct apiProduct2 = (APIProduct) o2;
        return productNameComparator.compare(apiProduct1, apiProduct2);
    } else if (o1 instanceof API && o2 instanceof APIProduct) {
        API api = (API) o1;
        APIProduct apiProduct = (APIProduct) o2;
        return api.getId().getApiName().compareToIgnoreCase(apiProduct.getId().getName());
    } else if (o1 instanceof APIProduct && o2 instanceof API) {
        APIProduct apiProduct = (APIProduct) o1;
        API api = (API) o2;
        return apiProduct.getId().getName().compareToIgnoreCase(api.getId().getName());
    } else if (o1 instanceof API && o2 instanceof Map.Entry) {
        API api = (API) o1;
        Documentation doc = getDocumentFromEntry(o2);
        return api.getId().getApiName().compareToIgnoreCase(doc.getName());
    } else if (o1 instanceof Map.Entry && o2 instanceof API) {
        API api = (API) o2;
        Documentation doc = getDocumentFromEntry(o1);
        return api.getId().getApiName().compareToIgnoreCase(doc.getName());
    } else if (o1 instanceof APIProduct && o2 instanceof Map.Entry) {
        APIProduct apiProduct = (APIProduct) o1;
        Documentation doc = getDocumentFromEntry(o2);
        return apiProduct.getId().getName().compareToIgnoreCase(doc.getName());
    } else if (o1 instanceof Map.Entry && o2 instanceof APIProduct) {
        APIProduct apiProduct = (APIProduct) o2;
        Documentation doc = getDocumentFromEntry(o1);
        return apiProduct.getId().getName().compareToIgnoreCase(doc.getName());
    } else {
        Documentation doc1;
        Documentation doc2;
        if (((Map.Entry) o1).getValue() instanceof APIProduct && ((Map.Entry) o2).getValue() instanceof APIProduct) {
            Map.Entry<Documentation, APIProduct> documentEntry1 = (Map.Entry<Documentation, APIProduct>) o1;
            doc1 = documentEntry1.getKey();
            Map.Entry<Documentation, APIProduct> documentEntry2 = (Map.Entry<Documentation, APIProduct>) o2;
            doc2 = documentEntry2.getKey();
        } else if (((Map.Entry) o1).getValue() instanceof API && ((Map.Entry) o2).getValue() instanceof APIProduct) {
            Map.Entry<Documentation, API> documentEntry1 = (Map.Entry<Documentation, API>) o1;
            doc1 = documentEntry1.getKey();
            Map.Entry<Documentation, APIProduct> documentEntry2 = (Map.Entry<Documentation, APIProduct>) o2;
            doc2 = documentEntry2.getKey();
        } else if (((Map.Entry) o1).getValue() instanceof APIProduct && ((Map.Entry) o2).getValue() instanceof API) {
            Map.Entry<Documentation, APIProduct> documentEntry1 = (Map.Entry<Documentation, APIProduct>) o1;
            doc1 = documentEntry1.getKey();
            Map.Entry<Documentation, API> documentEntry2 = (Map.Entry<Documentation, API>) o2;
            doc2 = documentEntry2.getKey();
        } else {
            Map.Entry<Documentation, API> documentEntry1 = (Map.Entry<Documentation, API>) o1;
            doc1 = documentEntry1.getKey();
            Map.Entry<Documentation, API> documentEntry2 = (Map.Entry<Documentation, API>) o2;
            doc2 = documentEntry2.getKey();
        }
        return doc1.getName().compareToIgnoreCase(doc2.getName());
    }
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) API(org.wso2.carbon.apimgt.api.model.API) Map(java.util.Map)

Example 49 with Documentation

use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method searchContentForDevPortal.

@Override
public DevPortalContentSearchResult searchContentForDevPortal(Organization org, String searchQuery, int start, int offset, UserContext ctx) throws APIPersistenceException {
    log.debug("Requested query for devportal content search: " + searchQuery);
    Map<String, String> attributes = RegistrySearchUtil.getDevPortalSearchAttributes(searchQuery, ctx, isAllowDisplayAPIsWithMultipleStatus());
    if (log.isDebugEnabled()) {
        log.debug("Search attributes : " + attributes);
    }
    DevPortalContentSearchResult result = null;
    boolean isTenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(ctx.getUserame(), org.getName());
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        String tenantAwareUsername = getTenantAwareUsername(ctx.getUserame());
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(tenantAwareUsername);
        GenericArtifactManager apiArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
        GenericArtifactManager docArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
        int maxPaginationLimit = getMaxPaginationLimit();
        PaginationContext.init(start, offset, "ASC", APIConstants.API_OVERVIEW_NAME, maxPaginationLimit);
        int tenantId = holder.getTenantId();
        if (tenantId == -1) {
            tenantId = MultitenantConstants.SUPER_TENANT_ID;
        }
        UserRegistry systemUserRegistry = ServiceReferenceHolder.getInstance().getRegistryService().getRegistry(CarbonConstants.REGISTRY_SYSTEM_USERNAME, tenantId);
        ContentBasedSearchService contentBasedSearchService = new ContentBasedSearchService();
        SearchResultsBean resultsBean = contentBasedSearchService.searchByAttribute(attributes, systemUserRegistry);
        String errorMsg = resultsBean.getErrorMessage();
        if (errorMsg != null) {
            throw new APIPersistenceException("Error while searching " + errorMsg);
        }
        ResourceData[] resourceData = resultsBean.getResourceDataList();
        int totalLength = PaginationContext.getInstance().getLength();
        if (resourceData != null) {
            result = new DevPortalContentSearchResult();
            List<SearchContent> contentData = new ArrayList<SearchContent>();
            if (log.isDebugEnabled()) {
                log.debug("Number of records Found: " + resourceData.length);
            }
            for (ResourceData data : resourceData) {
                String resourcePath = data.getResourcePath();
                if (resourcePath.contains(APIConstants.APIMGT_REGISTRY_LOCATION)) {
                    int index = resourcePath.indexOf(APIConstants.APIMGT_REGISTRY_LOCATION);
                    resourcePath = resourcePath.substring(index);
                    Resource resource = registry.get(resourcePath);
                    if (APIConstants.DOCUMENT_RXT_MEDIA_TYPE.equals(resource.getMediaType()) || APIConstants.DOCUMENTATION_INLINE_CONTENT_TYPE.equals(resource.getMediaType())) {
                        if (resourcePath.contains(APIConstants.INLINE_DOCUMENT_CONTENT_DIR)) {
                            int indexOfContents = resourcePath.indexOf(APIConstants.INLINE_DOCUMENT_CONTENT_DIR);
                            resourcePath = resourcePath.substring(0, indexOfContents) + data.getName();
                        }
                        DocumentSearchContent docSearch = new DocumentSearchContent();
                        Resource docResource = registry.get(resourcePath);
                        String docArtifactId = docResource.getUUID();
                        GenericArtifact docArtifact = docArtifactManager.getGenericArtifact(docArtifactId);
                        Documentation doc = RegistryPersistenceDocUtil.getDocumentation(docArtifact);
                        int indexOfDocumentation = resourcePath.indexOf(APIConstants.DOCUMENTATION_KEY);
                        String apiPath = resourcePath.substring(0, indexOfDocumentation) + APIConstants.API_KEY;
                        Resource apiResource = registry.get(apiPath);
                        String apiArtifactId = apiResource.getUUID();
                        DevPortalAPI devAPI;
                        if (apiArtifactId != null) {
                            GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId);
                            devAPI = RegistryPersistenceUtil.getDevPortalAPIForSearch(apiArtifact);
                            docSearch.setApiName(devAPI.getApiName());
                            docSearch.setApiProvider(devAPI.getProviderName());
                            docSearch.setApiVersion(devAPI.getVersion());
                            docSearch.setApiUUID(devAPI.getId());
                            docSearch.setDocType(doc.getType());
                            docSearch.setId(doc.getId());
                            docSearch.setSourceType(doc.getSourceType());
                            docSearch.setVisibility(doc.getVisibility());
                            docSearch.setName(doc.getName());
                            contentData.add(docSearch);
                        } else {
                            throw new GovernanceException("artifact id is null of " + apiPath);
                        }
                    } else {
                        String apiArtifactId = resource.getUUID();
                        if (apiArtifactId != null) {
                            GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId);
                            DevPortalAPI devAPI = RegistryPersistenceUtil.getDevPortalAPIForSearch(apiArtifact);
                            DevPortalSearchContent content = new DevPortalSearchContent();
                            content.setContext(devAPI.getContext());
                            content.setDescription(devAPI.getDescription());
                            content.setId(devAPI.getId());
                            content.setName(devAPI.getApiName());
                            content.setProvider(RegistryPersistenceUtil.replaceEmailDomainBack(devAPI.getProviderName()));
                            content.setVersion(devAPI.getVersion());
                            content.setStatus(devAPI.getStatus());
                            content.setBusinessOwner(devAPI.getBusinessOwner());
                            content.setBusinessOwnerEmail(devAPI.getBusinessOwnerEmail());
                            contentData.add(content);
                        } else {
                            throw new GovernanceException("artifact id is null for " + resourcePath);
                        }
                    }
                }
            }
            result.setTotalCount(totalLength);
            result.setReturnedCount(contentData.size());
            result.setResults(contentData);
        }
    } catch (RegistryException | IndexerException | DocumentationPersistenceException e) {
        throw new APIPersistenceException("Error while searching for content ", e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return result;
}
Also used : DevPortalSearchContent(org.wso2.carbon.apimgt.persistence.dto.DevPortalSearchContent) DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) ArrayList(java.util.ArrayList) DevPortalContentSearchResult(org.wso2.carbon.apimgt.persistence.dto.DevPortalContentSearchResult) DevPortalAPI(org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI) SearchResultsBean(org.wso2.carbon.registry.indexing.service.SearchResultsBean) IndexerException(org.wso2.carbon.registry.indexing.indexer.IndexerException) DevPortalSearchContent(org.wso2.carbon.apimgt.persistence.dto.DevPortalSearchContent) DocumentSearchContent(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent) PublisherSearchContent(org.wso2.carbon.apimgt.persistence.dto.PublisherSearchContent) SearchContent(org.wso2.carbon.apimgt.persistence.dto.SearchContent) GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) ResourceData(org.wso2.carbon.registry.common.ResourceData) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) DocumentSearchContent(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchContent) Documentation(org.wso2.carbon.apimgt.persistence.dto.Documentation) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) GovernanceException(org.wso2.carbon.governance.api.exception.GovernanceException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) ContentBasedSearchService(org.wso2.carbon.registry.indexing.service.ContentBasedSearchService) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 50 with Documentation

use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method searchDocumentation.

@Override
public DocumentSearchResult searchDocumentation(Organization org, String apiId, int start, int offset, String searchQuery, UserContext ctx) throws DocumentationPersistenceException {
    DocumentSearchResult result = null;
    Registry registryType;
    String requestedTenantDomain = org.getName();
    boolean isTenantFlowStarted = false;
    try {
        RegistryHolder holder = getRegistry(requestedTenantDomain);
        registryType = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifactManager apiArtifactManager = RegistryPersistenceUtil.getArtifactManager(registryType, APIConstants.API_KEY);
        GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiId);
        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 apiOrAPIProductDocPath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.DOC_DIR + RegistryConstants.PATH_SEPARATOR;
        String pathToContent = apiOrAPIProductDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR;
        String pathToDocFile = apiOrAPIProductDocPath + APIConstants.DOCUMENT_FILE_DIR;
        if (registryType.resourceExists(apiOrAPIProductDocPath)) {
            List<Documentation> documentationList = new ArrayList<Documentation>();
            Resource resource = registryType.get(apiOrAPIProductDocPath);
            if (resource instanceof org.wso2.carbon.registry.core.Collection) {
                String[] docsPaths = ((org.wso2.carbon.registry.core.Collection) resource).getChildren();
                for (String docPath : docsPaths) {
                    if (!(docPath.equalsIgnoreCase(pathToContent) || docPath.equalsIgnoreCase(pathToDocFile))) {
                        Resource docResource = registryType.get(docPath);
                        GenericArtifactManager artifactManager = RegistryPersistenceDocUtil.getDocumentArtifactManager(registryType);
                        GenericArtifact docArtifact = artifactManager.getGenericArtifact(docResource.getUUID());
                        Documentation doc = RegistryPersistenceDocUtil.getDocumentation(docArtifact);
                        if (searchQuery != null) {
                            if (searchQuery.toLowerCase().startsWith("name:")) {
                                String requestedDocName = searchQuery.split(":")[1];
                                if (doc.getName().equalsIgnoreCase(requestedDocName)) {
                                    documentationList.add(doc);
                                }
                            } else {
                                log.warn("Document search not implemented for the query " + searchQuery);
                            }
                        } else {
                            documentationList.add(doc);
                        }
                    }
                }
            }
            result = new DocumentSearchResult();
            result.setDocumentationList(documentationList);
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Failed to get documentations for api/product " + apiId;
        throw new DocumentationPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return result;
}
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) ArrayList(java.util.ArrayList) 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) DocumentSearchResult(org.wso2.carbon.apimgt.persistence.dto.DocumentSearchResult) Collection(org.wso2.carbon.registry.core.Collection)

Aggregations

Documentation (org.wso2.carbon.apimgt.api.model.Documentation)56 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 Test (org.testng.annotations.Test)38 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)34 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)33 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)32 ArrayList (java.util.ArrayList)29 Resource (org.wso2.carbon.registry.core.Resource)27 HashMap (java.util.HashMap)26 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)26 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)24 API (org.wso2.carbon.apimgt.api.model.API)23 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)23 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)22 Test (org.junit.Test)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)18 Registry (org.wso2.carbon.registry.core.Registry)18 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)17 DocumentationPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException)17