use of org.wso2.carbon.apimgt.persistence.dto.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);
}
}
use of org.wso2.carbon.apimgt.persistence.dto.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;
}
use of org.wso2.carbon.apimgt.persistence.dto.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());
}
}
use of org.wso2.carbon.apimgt.persistence.dto.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;
}
use of org.wso2.carbon.apimgt.persistence.dto.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;
}
Aggregations