use of org.wso2.carbon.registry.indexing.indexer.IndexerException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method searchPaginatedPublisherAPIsByDoc.
private PublisherAPISearchResult searchPaginatedPublisherAPIsByDoc(Registry registry, int tenantID, String searchQuery, String username, int start, int offset) throws APIPersistenceException {
PublisherAPISearchResult searchResults = new PublisherAPISearchResult();
try {
GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Artifact manager is null when searching APIs by docs in tenant ID " + tenantID;
log.error(errorMessage);
throw new APIPersistenceException(errorMessage);
}
GenericArtifactManager docArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
if (docArtifactManager == null) {
String errorMessage = "Doc artifact manager is null when searching APIs by docs in tenant ID " + tenantID;
log.error(errorMessage);
throw new APIPersistenceException(errorMessage);
}
SolrClient client = SolrClient.getInstance();
Map<String, String> fields = new HashMap<String, String>();
fields.put(APIConstants.DOCUMENTATION_SEARCH_PATH_FIELD, "*" + APIConstants.API_ROOT_LOCATION + "*");
fields.put(APIConstants.DOCUMENTATION_SEARCH_MEDIA_TYPE_FIELD, "*");
if (tenantID == -1) {
tenantID = MultitenantConstants.SUPER_TENANT_ID;
}
// PaginationContext.init(0, 10000, "ASC", APIConstants.DOCUMENTATION_SEARCH_PATH_FIELD, Integer.MAX_VALUE);
SolrDocumentList documentList = client.query(searchQuery, tenantID, fields);
org.wso2.carbon.user.api.AuthorizationManager manager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantID).getAuthorizationManager();
username = MultitenantUtils.getTenantAwareUsername(username);
List<PublisherAPIInfo> publisherAPIInfoList = new ArrayList<PublisherAPIInfo>();
for (SolrDocument document : documentList) {
PublisherAPIInfo apiInfo = new PublisherAPIInfo();
String filePath = (String) document.getFieldValue("path_s");
String fileName = (String) document.getFieldValue("resourceName_s");
int index = filePath.indexOf(APIConstants.APIMGT_REGISTRY_LOCATION);
filePath = filePath.substring(index);
boolean isAuthorized;
int indexOfContents = filePath.indexOf(APIConstants.INLINE_DOCUMENT_CONTENT_DIR);
String documentationPath = filePath.substring(0, indexOfContents) + fileName;
String path = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), RegistryPersistenceUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + documentationPath);
if (CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME.equalsIgnoreCase(username)) {
isAuthorized = manager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, path, ActionConstants.GET);
} else {
isAuthorized = manager.isUserAuthorized(username, path, ActionConstants.GET);
}
if (isAuthorized) {
int indexOfDocumentation = filePath.indexOf(APIConstants.DOCUMENTATION_KEY);
String apiPath = documentationPath.substring(0, indexOfDocumentation) + APIConstants.API_KEY;
path = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), RegistryPersistenceUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + apiPath);
if (CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME.equalsIgnoreCase(username)) {
isAuthorized = manager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, path, ActionConstants.GET);
} else {
isAuthorized = manager.isUserAuthorized(username, path, ActionConstants.GET);
}
if (isAuthorized) {
Resource resource = registry.get(apiPath);
String apiArtifactId = resource.getUUID();
if (apiArtifactId != null) {
GenericArtifact artifact = artifactManager.getGenericArtifact(apiArtifactId);
String status = artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS);
if (APIConstants.PUBLISHED.equals(status) || APIConstants.PROTOTYPED.equals(status)) {
apiInfo.setType(artifact.getAttribute(APIConstants.API_OVERVIEW_TYPE));
apiInfo.setId(artifact.getId());
apiInfo.setApiName(artifact.getAttribute(APIConstants.API_OVERVIEW_NAME));
apiInfo.setDescription(artifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION));
apiInfo.setContext(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE));
apiInfo.setProviderName(artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER));
apiInfo.setStatus(status);
apiInfo.setThumbnail(artifact.getAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL));
apiInfo.setCreatedTime(String.valueOf(resource.getCreatedTime().getTime()));
apiInfo.setUpdatedTime(resource.getLastModified());
apiInfo.setGatewayVendor(String.valueOf(artifact.getAttribute(APIConstants.API_GATEWAY_VENDOR)));
// apiInfo.setBusinessOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER));
apiInfo.setVersion(artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
apiInfo.setAdvertiseOnly(Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_ADVERTISE_ONLY)));
publisherAPIInfoList.add(apiInfo);
}
} else {
throw new GovernanceException("artifact id is null of " + apiPath);
}
}
}
}
// Sort the publisherAPIInfoList according to the API name.
Collections.sort(publisherAPIInfoList, new PublisherAPISearchResultComparator());
searchResults.setPublisherAPIInfoList(publisherAPIInfoList);
searchResults.setTotalAPIsCount(publisherAPIInfoList.size());
searchResults.setReturnedAPIsCount(publisherAPIInfoList.size());
} catch (RegistryException | UserStoreException | APIPersistenceException | IndexerException e) {
String msg = "Failed to search APIs with type";
throw new APIPersistenceException(msg, e);
} finally {
PaginationContext.destroy();
}
return searchResults;
}
use of org.wso2.carbon.registry.indexing.indexer.IndexerException 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;
}
use of org.wso2.carbon.registry.indexing.indexer.IndexerException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method searchPaginatedDevPortalAPIsByDoc.
private DevPortalAPISearchResult searchPaginatedDevPortalAPIsByDoc(Registry registry, int tenantID, String searchQuery, String username, int start, int offset) throws APIPersistenceException {
DevPortalAPISearchResult searchResults = new DevPortalAPISearchResult();
try {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username);
GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Artifact manager is null when searching APIs by docs in tenant ID " + tenantID;
log.error(errorMessage);
throw new APIPersistenceException(errorMessage);
}
GenericArtifactManager docArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
if (docArtifactManager == null) {
String errorMessage = "Doc artifact manager is null when searching APIs by docs in tenant ID " + tenantID;
log.error(errorMessage);
throw new APIPersistenceException(errorMessage);
}
SolrClient client = SolrClient.getInstance();
Map<String, String> fields = new HashMap<String, String>();
fields.put(APIConstants.DOCUMENTATION_SEARCH_PATH_FIELD, "*" + APIConstants.API_ROOT_LOCATION + "*");
fields.put(APIConstants.DOCUMENTATION_SEARCH_MEDIA_TYPE_FIELD, "*");
if (tenantID == -1) {
tenantID = MultitenantConstants.SUPER_TENANT_ID;
}
// PaginationContext.init(0, 10000, "ASC", APIConstants.DOCUMENTATION_SEARCH_PATH_FIELD, Integer.MAX_VALUE);
SolrDocumentList documentList = client.query(searchQuery, tenantID, fields);
org.wso2.carbon.user.api.AuthorizationManager manager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantID).getAuthorizationManager();
username = MultitenantUtils.getTenantAwareUsername(username);
List<DevPortalAPIInfo> devPortalAPIInfoList = new ArrayList<DevPortalAPIInfo>();
for (SolrDocument document : documentList) {
DevPortalAPIInfo apiInfo = new DevPortalAPIInfo();
String filePath = (String) document.getFieldValue("path_s");
String fileName = (String) document.getFieldValue("resourceName_s");
int index = filePath.indexOf(APIConstants.APIMGT_REGISTRY_LOCATION);
filePath = filePath.substring(index);
boolean isAuthorized;
int indexOfContents = filePath.indexOf(APIConstants.INLINE_DOCUMENT_CONTENT_DIR);
String documentationPath = filePath.substring(0, indexOfContents) + fileName;
String path = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), RegistryPersistenceUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + documentationPath);
if (CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME.equalsIgnoreCase(username)) {
isAuthorized = manager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, path, ActionConstants.GET);
} else {
isAuthorized = manager.isUserAuthorized(username, path, ActionConstants.GET);
}
if (isAuthorized) {
int indexOfDocumentation = filePath.indexOf(APIConstants.DOCUMENTATION_KEY);
String apiPath = documentationPath.substring(0, indexOfDocumentation) + APIConstants.API_KEY;
path = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), RegistryPersistenceUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + apiPath);
if (CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME.equalsIgnoreCase(username)) {
isAuthorized = manager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, path, ActionConstants.GET);
} else {
isAuthorized = manager.isUserAuthorized(username, path, ActionConstants.GET);
}
if (isAuthorized) {
Resource resource = registry.get(apiPath);
String apiArtifactId = resource.getUUID();
if (apiArtifactId != null) {
GenericArtifact artifact = artifactManager.getGenericArtifact(apiArtifactId);
String status = artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS);
if (APIConstants.PUBLISHED.equals(status) || APIConstants.PROTOTYPED.equals(status)) {
apiInfo.setType(artifact.getAttribute(APIConstants.API_OVERVIEW_TYPE));
apiInfo.setId(artifact.getId());
apiInfo.setApiName(artifact.getAttribute(APIConstants.API_OVERVIEW_NAME));
apiInfo.setDescription(artifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION));
apiInfo.setContext(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE));
apiInfo.setProviderName(artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER));
apiInfo.setStatus(status);
apiInfo.setThumbnail(artifact.getAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL));
apiInfo.setBusinessOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER));
apiInfo.setVersion(artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
apiInfo.setSubscriptionAvailability(artifact.getAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABILITY));
apiInfo.setSubscriptionAvailableOrgs(artifact.getAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABLE_TENANTS));
apiInfo.setGatewayVendor(artifact.getAttribute(APIConstants.API_GATEWAY_VENDOR));
devPortalAPIInfoList.add(apiInfo);
}
} else {
throw new GovernanceException("artifact id is null of " + apiPath);
}
}
}
}
searchResults.setDevPortalAPIInfoList(devPortalAPIInfoList);
searchResults.setTotalAPIsCount(devPortalAPIInfoList.size());
searchResults.setReturnedAPIsCount(devPortalAPIInfoList.size());
} catch (RegistryException | UserStoreException | APIPersistenceException | IndexerException e) {
String msg = "Failed to search APIs with type";
throw new APIPersistenceException(msg, e);
} finally {
PaginationContext.destroy();
}
return searchResults;
}
use of org.wso2.carbon.registry.indexing.indexer.IndexerException in project carbon-apimgt by wso2.
the class APIUtil method searchAPIsByDoc.
/**
* Search Apis by Doc Content
*
* @param registry - Registry which is searched
* @param tenantID - Tenant id of logged in domain
* @param username - Logged in username
* @param searchTerm - Search value for doc
* @param searchClient - Search client
* @return - Documentation to APIs map
* @throws APIManagementException - If failed to get ArtifactManager for given tenant
*/
public static Map<Documentation, API> searchAPIsByDoc(Registry registry, int tenantID, String username, String searchTerm, String searchClient) throws APIManagementException {
Map<Documentation, API> apiDocMap = new HashMap<Documentation, API>();
try {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username);
GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Artifact manager is null when searching APIs by docs in tenant ID " + tenantID;
log.error(errorMessage);
throw new APIManagementException(errorMessage);
}
GenericArtifactManager docArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
if (docArtifactManager == null) {
String errorMessage = "Doc artifact manager is null when searching APIs by docs in tenant ID " + tenantID;
log.error(errorMessage);
throw new APIManagementException(errorMessage);
}
SolrClient client = SolrClient.getInstance();
Map<String, String> fields = new HashMap<String, String>();
fields.put(APIConstants.DOCUMENTATION_SEARCH_PATH_FIELD, "*" + APIConstants.API_ROOT_LOCATION + "*");
fields.put(APIConstants.DOCUMENTATION_SEARCH_MEDIA_TYPE_FIELD, "*");
if (tenantID == -1) {
tenantID = MultitenantConstants.SUPER_TENANT_ID;
}
// PaginationContext.init(0, 10000, "ASC", APIConstants.DOCUMENTATION_SEARCH_PATH_FIELD, Integer.MAX_VALUE);
SolrDocumentList documentList = client.query(searchTerm, tenantID, fields);
org.wso2.carbon.user.api.AuthorizationManager manager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantID).getAuthorizationManager();
username = MultitenantUtils.getTenantAwareUsername(username);
for (SolrDocument document : documentList) {
String filePath = (String) document.getFieldValue("path_s");
String fileName = (String) document.getFieldValue("resourceName_s");
int index = filePath.indexOf(APIConstants.APIMGT_REGISTRY_LOCATION);
filePath = filePath.substring(index);
API api = null;
Documentation doc = null;
boolean isAuthorized;
int indexOfContents = filePath.indexOf(APIConstants.INLINE_DOCUMENT_CONTENT_DIR);
String documentationPath = filePath.substring(0, indexOfContents) + fileName;
String path = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), APIUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + documentationPath);
if (CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME.equalsIgnoreCase(username)) {
isAuthorized = manager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, path, ActionConstants.GET);
} else {
isAuthorized = manager.isUserAuthorized(username, path, ActionConstants.GET);
}
if (isAuthorized) {
Resource docResource = registry.get(documentationPath);
String docArtifactId = docResource.getUUID();
if (docArtifactId != null) {
GenericArtifact docArtifact = docArtifactManager.getGenericArtifact(docArtifactId);
doc = APIUtil.getDocumentation(docArtifact);
}
int indexOfDocumentation = filePath.indexOf(APIConstants.DOCUMENTATION_KEY);
String apiPath = documentationPath.substring(0, indexOfDocumentation) + APIConstants.API_KEY;
path = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), APIUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + apiPath);
if (CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME.equalsIgnoreCase(username)) {
isAuthorized = manager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, path, ActionConstants.GET);
} else {
isAuthorized = manager.isUserAuthorized(username, path, ActionConstants.GET);
}
if (isAuthorized) {
Resource resource = registry.get(apiPath);
String apiArtifactId = resource.getUUID();
if (apiArtifactId != null) {
GenericArtifact apiArtifact = artifactManager.getGenericArtifact(apiArtifactId);
api = APIUtil.getAPI(apiArtifact, registry);
} else {
throw new GovernanceException("artifact id is null of " + apiPath);
}
}
}
if (doc != null && api != null) {
if (APIConstants.STORE_CLIENT.equals(searchClient)) {
if (APIConstants.PUBLISHED.equals(api.getStatus()) || APIConstants.PROTOTYPED.equals(api.getStatus())) {
apiDocMap.put(doc, api);
}
} else {
apiDocMap.put(doc, api);
}
}
}
} catch (IndexerException e) {
handleException("Failed to search APIs with type Doc", e);
} catch (RegistryException e) {
handleException("Failed to search APIs with type Doc", e);
} catch (UserStoreException e) {
handleException("Failed to search APIs with type Doc", e);
}
return apiDocMap;
}
use of org.wso2.carbon.registry.indexing.indexer.IndexerException in project carbon-apimgt by wso2.
the class AbstractAPIManager method searchPaginatedAPIsByContent.
/**
* Search api resources by their content
*
* @param registry
* @param searchQuery
* @param start
* @param end
* @return
* @throws APIManagementException
*/
public Map<String, Object> searchPaginatedAPIsByContent(Registry registry, int tenantId, String searchQuery, int start, int end, boolean limitAttributes) throws APIManagementException {
SortedSet<API> apiSet = new TreeSet<API>(new APINameComparator());
SortedSet<APIProduct> apiProductSet = new TreeSet<APIProduct>(new APIProductNameComparator());
Map<Documentation, API> docMap = new HashMap<Documentation, API>();
Map<Documentation, APIProduct> productDocMap = new HashMap<Documentation, APIProduct>();
Map<String, Object> result = new HashMap<String, Object>();
int totalLength = 0;
boolean isMore = false;
// SortedSet<Object> compoundResult = new TreeSet<Object>(new ContentSearchResultNameComparator());
ArrayList<Object> compoundResult = new ArrayList<Object>();
try {
GenericArtifactManager apiArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
GenericArtifactManager docArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
String paginationLimit = getAPIManagerConfiguration().getFirstProperty(APIConstants.API_STORE_APIS_PER_PAGE);
// If the Config exists use it to set the pagination limit
final int maxPaginationLimit;
if (paginationLimit != null) {
// The additional 1 added to the maxPaginationLimit is to help us determine if more
// APIs may exist so that we know that we are unable to determine the actual total
// API count. We will subtract this 1 later on so that it does not interfere with
// the logic of the rest of the application
int pagination = Integer.parseInt(paginationLimit);
// leading to some of the APIs not being displayed
if (pagination < 11) {
pagination = 11;
log.warn("Value of '" + APIConstants.API_STORE_APIS_PER_PAGE + "' is too low, defaulting to 11");
}
maxPaginationLimit = start + pagination + 1;
} else // Else if the config is not specified we go with default functionality and load all
{
maxPaginationLimit = Integer.MAX_VALUE;
}
PaginationContext.init(start, end, "ASC", APIConstants.API_OVERVIEW_NAME, maxPaginationLimit);
if (tenantId == -1) {
tenantId = MultitenantConstants.SUPER_TENANT_ID;
}
UserRegistry systemUserRegistry = ServiceReferenceHolder.getInstance().getRegistryService().getRegistry(CarbonConstants.REGISTRY_SYSTEM_USERNAME, tenantId);
ContentBasedSearchService contentBasedSearchService = new ContentBasedSearchService();
String newSearchQuery = getSearchQuery(searchQuery);
String[] searchQueries = newSearchQuery.split("&");
String apiState = "";
String publisherRoles = "";
Map<String, String> attributes = new HashMap<String, String>();
for (String searchCriterea : searchQueries) {
String[] keyVal = searchCriterea.split("=");
if (APIConstants.STORE_VIEW_ROLES.equals(keyVal[0])) {
attributes.put("propertyName", keyVal[0]);
attributes.put("rightPropertyValue", keyVal[1]);
attributes.put("rightOp", "eq");
} else if (APIConstants.PUBLISHER_ROLES.equals(keyVal[0])) {
publisherRoles = keyVal[1];
} else {
if (APIConstants.LCSTATE_SEARCH_KEY.equals(keyVal[0])) {
apiState = keyVal[1];
continue;
}
attributes.put(keyVal[0], keyVal[1]);
}
}
// check whether the new document indexer is engaged
RegistryConfigLoader registryConfig = RegistryConfigLoader.getInstance();
Map<String, Indexer> indexerMap = registryConfig.getIndexerMap();
Indexer documentIndexer = indexerMap.get(APIConstants.DOCUMENT_MEDIA_TYPE_KEY);
String complexAttribute;
if (documentIndexer != null && documentIndexer instanceof DocumentIndexer) {
// field check on document_indexed was added to prevent unindexed(by new DocumentIndexer) from coming up as search results
// on indexed documents this property is always set to true
complexAttribute = ClientUtils.escapeQueryChars(APIConstants.API_RXT_MEDIA_TYPE) + " OR mediaType_s:(" + ClientUtils.escapeQueryChars(APIConstants.DOCUMENT_RXT_MEDIA_TYPE) + " AND document_indexed_s:true)";
// this was designed this way so that content search can be fully functional if registry is re-indexed after engaging DocumentIndexer
if (!StringUtils.isEmpty(publisherRoles)) {
complexAttribute = "(" + ClientUtils.escapeQueryChars(APIConstants.API_RXT_MEDIA_TYPE) + " AND publisher_roles_ss:" + publisherRoles + ") OR mediaType_s:(" + ClientUtils.escapeQueryChars(APIConstants.DOCUMENT_RXT_MEDIA_TYPE) + " AND publisher_roles_s:" + publisherRoles + ")";
}
} else {
// document indexer required for document content search is not engaged, therefore carry out the search only for api artifact contents
complexAttribute = ClientUtils.escapeQueryChars(APIConstants.API_RXT_MEDIA_TYPE);
if (!StringUtils.isEmpty(publisherRoles)) {
complexAttribute = "(" + ClientUtils.escapeQueryChars(APIConstants.API_RXT_MEDIA_TYPE) + " AND publisher_roles_ss:" + publisherRoles + ")";
}
}
attributes.put(APIConstants.DOCUMENTATION_SEARCH_MEDIA_TYPE_FIELD, complexAttribute);
attributes.put(APIConstants.API_OVERVIEW_STATUS, apiState);
SearchResultsBean resultsBean = contentBasedSearchService.searchByAttribute(attributes, systemUserRegistry);
String errorMsg = resultsBean.getErrorMessage();
if (errorMsg != null) {
handleException(errorMsg);
}
ResourceData[] resourceData = resultsBean.getResourceDataList();
if (resourceData == null || resourceData.length == 0) {
result.put("apis", compoundResult);
result.put("length", 0);
result.put("isMore", isMore);
}
totalLength = PaginationContext.getInstance().getLength();
// Check to see if we can speculate that there are more APIs to be loaded
if (maxPaginationLimit == totalLength) {
// More APIs exist, cannot determine total API count without incurring perf hit
isMore = true;
// Remove the additional 1 added earlier when setting max pagination limit
--totalLength;
}
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();
}
Resource docResource = registry.get(resourcePath);
String docArtifactId = docResource.getUUID();
GenericArtifact docArtifact = docArtifactManager.getGenericArtifact(docArtifactId);
Documentation doc = APIUtil.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();
if (apiArtifactId != null) {
GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId);
if (apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE).equals(APIConstants.AuditLogConstants.API_PRODUCT)) {
associatedAPIProduct = APIUtil.getAPIProduct(apiArtifact, registry);
} else {
associatedAPI = APIUtil.getAPI(apiArtifact, registry);
}
} else {
throw new GovernanceException("artifact id is null of " + apiPath);
}
if (associatedAPI != null && doc != null) {
docMap.put(doc, associatedAPI);
}
if (associatedAPIProduct != null && doc != null) {
productDocMap.put(doc, associatedAPIProduct);
}
} else {
String apiArtifactId = resource.getUUID();
API api;
APIProduct apiProduct;
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);
} else {
api = APIUtil.getAPI(apiArtifact, registry);
apiSet.add(api);
}
} else {
throw new GovernanceException("artifact id is null for " + resourcePath);
}
}
}
}
compoundResult.addAll(apiSet);
compoundResult.addAll(apiProductSet);
compoundResult.addAll(docMap.entrySet());
compoundResult.addAll(productDocMap.entrySet());
compoundResult.sort(new ContentSearchResultNameComparator());
} catch (RegistryException e) {
handleException("Failed to search APIs by content", e);
} catch (IndexerException e) {
handleException("Failed to search APIs by content", e);
}
result.put("apis", compoundResult);
result.put("length", totalLength);
result.put("isMore", isMore);
return result;
}
Aggregations