use of org.wso2.carbon.apimgt.persistence.dto.DevPortalAPISearchResult 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.apimgt.persistence.dto.DevPortalAPISearchResult in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method searchAPIsForDevPortal.
@Override
public DevPortalAPISearchResult searchAPIsForDevPortal(Organization org, String searchQuery, int start, int offset, UserContext ctx) throws APIPersistenceException {
String requestedTenantDomain = org.getName();
boolean isTenantFlowStarted = false;
DevPortalAPISearchResult result = null;
try {
RegistryHolder holder = getRegistry(ctx.getUserame(), requestedTenantDomain);
Registry userRegistry = holder.getRegistry();
int tenantIDLocal = holder.getTenantId();
isTenantFlowStarted = holder.isTenantFlowStarted();
log.debug("Requested query for devportal search: " + searchQuery);
String modifiedQuery = RegistrySearchUtil.getDevPortalSearchQuery(searchQuery, ctx, isAllowDisplayAPIsWithMultipleStatus(), isAllowDisplayAPIsWithMultipleVersions());
log.debug("Modified query for devportal search: " + modifiedQuery);
String userNameLocal;
if (holder.isAnonymousMode()) {
userNameLocal = APIConstants.WSO2_ANONYMOUS_USER;
} else {
userNameLocal = getTenantAwareUsername(ctx.getUserame());
}
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(userNameLocal);
if (searchQuery != null && searchQuery.startsWith(APIConstants.DOCUMENTATION_SEARCH_TYPE_PREFIX)) {
result = searchPaginatedDevPortalAPIsByDoc(userRegistry, tenantIDLocal, searchQuery.split(":")[1], userNameLocal, start, offset);
} else {
result = searchPaginatedDevPortalAPIs(userRegistry, tenantIDLocal, modifiedQuery, start, offset);
}
} catch (APIManagementException e) {
throw new APIPersistenceException("Error while searching APIs ", e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return result;
}
use of org.wso2.carbon.apimgt.persistence.dto.DevPortalAPISearchResult in project carbon-apimgt by wso2.
the class APIConsumerImpl method searchPaginatedAPIs.
@Override
public Map<String, Object> searchPaginatedAPIs(String searchQuery, String organization, int start, int end, String sortBy, String sortOrder) throws APIManagementException {
Map<String, Object> result = new HashMap<String, Object>();
if (log.isDebugEnabled()) {
log.debug("Original search query received : " + searchQuery);
}
Organization org = new Organization(organization);
String userName = (userNameWithoutChange != null) ? userNameWithoutChange : username;
String[] roles = APIUtil.getListOfRoles(userName);
Map<String, Object> properties = APIUtil.getUserProperties(userName);
UserContext userCtx = new UserContext(userNameWithoutChange, org, properties, roles);
try {
DevPortalAPISearchResult searchAPIs = apiPersistenceInstance.searchAPIsForDevPortal(org, searchQuery, start, end, userCtx);
if (log.isDebugEnabled()) {
log.debug("searched Devportal APIs for query : " + searchQuery + " :-->: " + searchAPIs.toString());
}
SortedSet<Object> apiSet = new TreeSet<>(new APIAPIProductNameComparator());
if (searchAPIs != null) {
List<DevPortalAPIInfo> list = searchAPIs.getDevPortalAPIInfoList();
List<Object> apiList = new ArrayList<>();
for (DevPortalAPIInfo devPortalAPIInfo : list) {
API mappedAPI = APIMapper.INSTANCE.toApi(devPortalAPIInfo);
mappedAPI.setRating(APIUtil.getAverageRating(mappedAPI.getUuid()));
apiList.add(mappedAPI);
}
apiSet.addAll(apiList);
result.put("apis", apiSet);
result.put("length", searchAPIs.getTotalAPIsCount());
result.put("isMore", true);
} else {
result.put("apis", apiSet);
result.put("length", 0);
result.put("isMore", false);
}
} catch (APIPersistenceException e) {
throw new APIManagementException("Error while searching the api ", e);
}
return result;
}
use of org.wso2.carbon.apimgt.persistence.dto.DevPortalAPISearchResult in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method searchPaginatedDevPortalAPIs.
private DevPortalAPISearchResult searchPaginatedDevPortalAPIs(Registry userRegistry, int tenantIDLocal, String searchQuery, int start, int offset) throws APIManagementException {
int totalLength = 0;
DevPortalAPISearchResult searchResults = new DevPortalAPISearchResult();
try {
final int maxPaginationLimit = getMaxPaginationLimit();
PaginationContext.init(start, offset, "ASC", APIConstants.API_OVERVIEW_NAME, maxPaginationLimit);
log.debug("Dev portal list apis query " + searchQuery);
List<GovernanceArtifact> governanceArtifacts = GovernanceUtils.findGovernanceArtifacts(searchQuery, userRegistry, APIConstants.API_RXT_MEDIA_TYPE, true);
totalLength = PaginationContext.getInstance().getLength();
boolean isFound = true;
if (governanceArtifacts == null || governanceArtifacts.size() == 0) {
if (searchQuery.contains(APIConstants.API_OVERVIEW_PROVIDER)) {
searchQuery = searchQuery.replaceAll(APIConstants.API_OVERVIEW_PROVIDER, APIConstants.API_OVERVIEW_OWNER);
governanceArtifacts = GovernanceUtils.findGovernanceArtifacts(searchQuery, userRegistry, APIConstants.API_RXT_MEDIA_TYPE, true);
if (governanceArtifacts == null || governanceArtifacts.size() == 0) {
isFound = false;
}
} else {
isFound = false;
}
}
if (!isFound) {
return searchResults;
}
// 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;
}
List<DevPortalAPIInfo> devPortalAPIInfoList = new ArrayList<DevPortalAPIInfo>();
int tempLength = 0;
for (GovernanceArtifact artifact : governanceArtifacts) {
DevPortalAPIInfo apiInfo = new DevPortalAPIInfo();
// devPortalAPIInfoList apiInfo = new devPortalAPIInfoList();
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(artifact.getAttribute(APIConstants.API_OVERVIEW_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));
String tiers = artifact.getAttribute(APIConstants.API_OVERVIEW_TIER);
Set<String> availableTiers = new HashSet<String>();
if (tiers != null) {
String[] tiersArray = tiers.split("\\|\\|");
for (String tierName : tiersArray) {
availableTiers.add(tierName);
}
}
apiInfo.setAvailableTierNames(availableTiers);
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);
// 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;
}
}
searchResults.setDevPortalAPIInfoList(devPortalAPIInfoList);
searchResults.setReturnedAPIsCount(devPortalAPIInfoList.size());
searchResults.setTotalAPIsCount(totalLength);
} catch (RegistryException e) {
String msg = "Failed to search APIs with type";
throw new APIManagementException(msg, e);
} finally {
PaginationContext.destroy();
}
return searchResults;
}
Aggregations