use of org.wso2.carbon.apimgt.persistence.dto.UserContext 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;
}
use of org.wso2.carbon.apimgt.persistence.dto.UserContext 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;
}
use of org.wso2.carbon.apimgt.persistence.dto.UserContext in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getAllTags.
@Override
public Set<Tag> getAllTags(Organization org, UserContext ctx) throws APIPersistenceException {
TreeSet<Tag> tempTagSet = new TreeSet<Tag>(new Comparator<Tag>() {
@Override
public int compare(Tag o1, Tag o2) {
return o1.getName().compareTo(o2.getName());
}
});
Registry userRegistry = null;
boolean tenantFlowStarted = false;
String tagsQueryPath = null;
try {
RegistryHolder holder = getRegistry(org.getName());
tenantFlowStarted = holder.isTenantFlowStarted();
userRegistry = holder.getRegistry();
tagsQueryPath = RegistryConstants.QUERIES_COLLECTION_PATH + "/tag-summary";
Map<String, String> params = new HashMap<String, String>();
params.put(RegistryConstants.RESULT_TYPE_PROPERTY_NAME, RegistryConstants.TAG_SUMMARY_RESULT_TYPE);
String userNameLocal;
if (holder.isAnonymousMode()) {
userNameLocal = APIConstants.WSO2_ANONYMOUS_USER;
} else {
userNameLocal = getTenantAwareUsername(ctx.getUserame());
}
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(userNameLocal);
Map<String, Tag> tagsData = new HashMap<String, Tag>();
Map<String, List<String>> criteriaPublished = new HashMap<String, List<String>>();
criteriaPublished.put(APIConstants.LCSTATE_SEARCH_KEY, new ArrayList<String>() {
{
add(APIConstants.PUBLISHED);
}
});
// rxt api media type
List<TermData> termsPublished = GovernanceUtils.getTermDataList(criteriaPublished, APIConstants.API_OVERVIEW_TAG, APIConstants.API_RXT_MEDIA_TYPE, true);
if (termsPublished != null) {
for (TermData data : termsPublished) {
tempTagSet.add(new Tag(data.getTerm(), (int) data.getFrequency()));
}
}
Map<String, List<String>> criteriaPrototyped = new HashMap<String, List<String>>();
criteriaPrototyped.put(APIConstants.LCSTATE_SEARCH_KEY, new ArrayList<String>() {
{
add(APIConstants.PROTOTYPED);
}
});
// rxt api media type
List<TermData> termsPrototyped = GovernanceUtils.getTermDataList(criteriaPrototyped, APIConstants.API_OVERVIEW_TAG, APIConstants.API_RXT_MEDIA_TYPE, true);
if (termsPrototyped != null) {
for (TermData data : termsPrototyped) {
tempTagSet.add(new Tag(data.getTerm(), (int) data.getFrequency()));
}
}
return tempTagSet;
} catch (RegistryException e) {
try {
// give a warn.
if (userRegistry != null && !userRegistry.resourceExists(tagsQueryPath)) {
log.warn("Failed to retrieve tags query resource at " + tagsQueryPath);
return Collections.EMPTY_SET;
}
} catch (RegistryException e1) {
// Even if we should ignore this exception, we are logging this as a warn log.
// The reason is that, this error happens when we try to add some additional logs in an error
// scenario and it does not affect the execution path.
log.warn("Unable to execute the resource exist method for tags query resource path : " + tagsQueryPath, e1);
}
throw new APIPersistenceException("Failed to get all the tags", e);
} finally {
if (tenantFlowStarted) {
RegistryPersistenceUtil.endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.persistence.dto.UserContext 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.apimgt.persistence.dto.UserContext 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;
}
Aggregations