Search in sources :

Example 6 with IndexerException

use of org.wso2.carbon.registry.indexing.indexer.IndexerException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method searchContentForPublisher.

@Override
public PublisherContentSearchResult searchContentForPublisher(Organization org, String searchQuery, int start, int offset, UserContext ctx) throws APIPersistenceException {
    log.debug("Requested query for publisher content search: " + searchQuery);
    Map<String, String> attributes = RegistrySearchUtil.getPublisherSearchAttributes(searchQuery, ctx);
    if (log.isDebugEnabled()) {
        log.debug("Search attributes : " + attributes);
    }
    boolean isTenantFlowStarted = false;
    PublisherContentSearchResult result = null;
    try {
        RegistryHolder holder = getRegistry(org.getName());
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        String requestedTenantDomain = org.getName();
        String tenantAwareUsername = getTenantAwareUsername(RegistryPersistenceUtil.getTenantAdminUserName(requestedTenantDomain));
        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 PublisherContentSearchResult();
            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);
                        // API associatedAPI = null;
                        // APIProduct associatedAPIProduct = null;
                        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();
                        PublisherAPI pubAPI;
                        if (apiArtifactId != null) {
                            GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId);
                            String accociatedType;
                            if (apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE).equals(APIConstants.AuditLogConstants.API_PRODUCT)) {
                                // associatedAPIProduct = APIUtil.getAPIProduct(apiArtifact, registry);
                                accociatedType = APIConstants.API_PRODUCT;
                            } else {
                                // associatedAPI = APIUtil.getAPI(apiArtifact, registry);
                                accociatedType = APIConstants.API;
                            }
                            pubAPI = RegistryPersistenceUtil.getAPIForSearch(apiArtifact);
                            docSearch.setApiName(pubAPI.getApiName());
                            docSearch.setApiProvider(pubAPI.getProviderName());
                            docSearch.setApiVersion(pubAPI.getVersion());
                            docSearch.setApiUUID(pubAPI.getId());
                            docSearch.setAssociatedType(accociatedType);
                            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();
                        // API api;
                        // APIProduct apiProduct;
                        String type;
                        if (apiArtifactId != null) {
                            GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId);
                            if (apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE).equals(APIConstants.API_PRODUCT)) {
                                // apiProduct = APIUtil.getAPIProduct(apiArtifact, registry);
                                // apiProductSet.add(apiProduct);
                                type = APIConstants.API_PRODUCT;
                            } else {
                                // api = APIUtil.getAPI(apiArtifact, registry);
                                // apiSet.add(api);
                                type = APIConstants.API;
                            }
                            PublisherAPI pubAPI = RegistryPersistenceUtil.getAPIForSearch(apiArtifact);
                            PublisherSearchContent content = new PublisherSearchContent();
                            content.setContext(pubAPI.getContext());
                            content.setDescription(pubAPI.getDescription());
                            content.setId(pubAPI.getId());
                            content.setName(pubAPI.getApiName());
                            content.setProvider(RegistryPersistenceUtil.replaceEmailDomainBack(pubAPI.getProviderName()));
                            content.setType(type);
                            content.setVersion(pubAPI.getVersion());
                            content.setStatus(pubAPI.getStatus());
                            content.setAdvertiseOnly(pubAPI.isAdvertiseOnly());
                            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 | APIManagementException e) {
        throw new APIPersistenceException("Error while searching for content ", e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
    return result;
}
Also used : DocumentationPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException) ArrayList(java.util.ArrayList) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) PublisherAPI(org.wso2.carbon.apimgt.persistence.dto.PublisherAPI) 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) PublisherSearchContent(org.wso2.carbon.apimgt.persistence.dto.PublisherSearchContent) 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) PublisherContentSearchResult(org.wso2.carbon.apimgt.persistence.dto.PublisherContentSearchResult)

Example 7 with IndexerException

use of org.wso2.carbon.registry.indexing.indexer.IndexerException in project jaggery by wso2.

the class RegistryHostObject method search.

private ResourceData[] search(UserRegistry registry, String searchQuery) throws IndexerException, RegistryException {
    SolrClient client = SolrClient.getInstance();
    SolrDocumentList results = client.query(searchQuery, registry.getTenantId());
    if (log.isDebugEnabled())
        log.debug("result received " + results);
    List<ResourceData> filteredResults = new ArrayList<ResourceData>();
    for (int i = 0; i < results.getNumFound(); i++) {
        SolrDocument solrDocument = results.get(i);
        String path = getPathFromId((String) solrDocument.getFirstValue("id"));
        // if (AuthorizationUtils.authorize(path, ActionConstants.GET)){
        if ((registry.resourceExists(path)) && (isAuthorized(registry, path, ActionConstants.GET))) {
            filteredResults.add(loadResourceByPath(registry, path));
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("filtered results " + filteredResults + " for user " + registry.getUserName());
    }
    return filteredResults.toArray(new ResourceData[0]);
}
Also used : ResourceData(org.wso2.carbon.registry.common.ResourceData) SolrDocument(org.apache.solr.common.SolrDocument) SolrClient(org.wso2.carbon.registry.indexing.solr.SolrClient) SolrDocumentList(org.apache.solr.common.SolrDocumentList)

Aggregations

GovernanceException (org.wso2.carbon.governance.api.exception.GovernanceException)6 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)6 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)6 Resource (org.wso2.carbon.registry.core.Resource)6 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)6 IndexerException (org.wso2.carbon.registry.indexing.indexer.IndexerException)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)4 SolrDocument (org.apache.solr.common.SolrDocument)4 SolrDocumentList (org.apache.solr.common.SolrDocumentList)4 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)4 ResourceData (org.wso2.carbon.registry.common.ResourceData)4 SolrClient (org.wso2.carbon.registry.indexing.solr.SolrClient)4 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)3 ContentBasedSearchService (org.wso2.carbon.registry.indexing.service.ContentBasedSearchService)3 SearchResultsBean (org.wso2.carbon.registry.indexing.service.SearchResultsBean)3 UserStoreException (org.wso2.carbon.user.api.UserStoreException)3 LinkedHashMap (java.util.LinkedHashMap)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 API (org.wso2.carbon.apimgt.api.model.API)2