use of org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException in project carbon-apimgt by wso2.
the class AbstractAPIManager method getAllDocumentation.
public List<Documentation> getAllDocumentation(String uuid, String organization) throws APIManagementException {
String username = CarbonContext.getThreadLocalCarbonContext().getUsername();
Organization org = new Organization(organization);
UserContext ctx = new UserContext(username, org, null, null);
List<Documentation> convertedList = null;
try {
DocumentSearchResult list = apiPersistenceInstance.searchDocumentation(org, uuid, 0, 0, null, ctx);
if (list != null) {
convertedList = new ArrayList<Documentation>();
List<org.wso2.carbon.apimgt.persistence.dto.Documentation> docList = list.getDocumentationList();
if (docList != null) {
for (int i = 0; i < docList.size(); i++) {
convertedList.add(DocumentMapper.INSTANCE.toDocumentation(docList.get(i)));
}
}
} else {
convertedList = new ArrayList<Documentation>();
}
} catch (DocumentationPersistenceException e) {
String msg = "Failed to get documentations for api/product " + uuid;
throw new APIManagementException(msg, e);
}
return convertedList;
}
use of org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException 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.exceptions.DocumentationPersistenceException 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.exceptions.DocumentationPersistenceException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method addDocumentation.
@Override
public Documentation addDocumentation(Organization org, String apiId, Documentation documentation) throws DocumentationPersistenceException {
boolean tenantFlowStarted = false;
try {
String tenantDomain = org.getName();
RegistryHolder holder = getRegistry(tenantDomain);
Registry registry = holder.getRegistry();
tenantFlowStarted = holder.isTenantFlowStarted();
GenericArtifactManager apiArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiId);
String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
apiProviderName = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
GenericArtifactManager docArtifactManager = new GenericArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
GenericArtifact docArtifact = docArtifactManager.newGovernanceArtifact(new QName(documentation.getName()));
docArtifactManager.addGenericArtifact(RegistryPersistenceDocUtil.createDocArtifactContent(docArtifact, apiName, apiVersion, apiProviderName, documentation));
String apiPath = RegistryPersistenceUtil.getAPIPath(apiName, apiVersion, apiProviderName);
String docVisibility = documentation.getVisibility().name();
String[] authorizedRoles = RegistryPersistenceUtil.getAuthorizedRoles(apiPath, tenantDomain);
String visibility = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY);
if (docVisibility != null) {
if (APIConstants.DOC_SHARED_VISIBILITY.equalsIgnoreCase(docVisibility)) {
authorizedRoles = null;
visibility = APIConstants.DOC_SHARED_VISIBILITY;
} else if (APIConstants.DOC_OWNER_VISIBILITY.equalsIgnoreCase(docVisibility)) {
authorizedRoles = null;
visibility = APIConstants.DOC_OWNER_VISIBILITY;
}
}
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, authorizedRoles, docArtifact.getPath(), registry);
String docFilePath = docArtifact.getAttribute(APIConstants.DOC_FILE_PATH);
if (docFilePath != null && !"".equals(docFilePath)) {
// The docFilePatch comes as
// /t/tenanatdoman/registry/resource/_system/governance/apimgt/applicationdata..
// We need to remove the /t/tenanatdoman/registry/resource/_system/governance section to set
// permissions.
int startIndex = docFilePath.indexOf(APIConstants.GOVERNANCE) + (APIConstants.GOVERNANCE).length();
String filePath = docFilePath.substring(startIndex, docFilePath.length());
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, authorizedRoles, filePath, registry);
}
documentation.setId(docArtifact.getId());
return documentation;
} catch (RegistryException | APIManagementException | UserStoreException | APIPersistenceException e) {
throw new DocumentationPersistenceException("Failed to add documentation", e);
} finally {
if (tenantFlowStarted) {
RegistryPersistenceUtil.endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getDocumentationContent.
@Override
public DocumentContent getDocumentationContent(Organization org, String apiId, String docId) throws DocumentationPersistenceException {
DocumentContent documentContent = null;
boolean tenantFlowStarted = false;
try {
String requestedTenantDomain = org.getName();
RegistryHolder holder = getRegistry(requestedTenantDomain);
Registry registryType = holder.getRegistry();
tenantFlowStarted = holder.isTenantFlowStarted();
GenericArtifactManager artifactManager = RegistryPersistenceDocUtil.getDocumentArtifactManager(registryType);
GenericArtifact artifact = artifactManager.getGenericArtifact(docId);
if (artifact == null) {
return null;
}
if (artifact != null) {
Documentation documentation = RegistryPersistenceDocUtil.getDocumentation(artifact);
if (documentation.getSourceType().equals(Documentation.DocumentSourceType.FILE)) {
String resource = documentation.getFilePath();
if (resource == null) {
return null;
}
String[] resourceSplitPath = resource.split(RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH);
if (resourceSplitPath.length == 2) {
resource = resourceSplitPath[1];
} else {
throw new DocumentationPersistenceException("Invalid resource Path " + resource);
}
if (registryType.resourceExists(resource)) {
documentContent = new DocumentContent();
Resource apiDocResource = registryType.get(resource);
String[] content = apiDocResource.getPath().split("/");
String name = content[content.length - 1];
documentContent.setSourceType(ContentSourceType.FILE);
ResourceFile resourceFile = new ResourceFile(apiDocResource.getContentStream(), apiDocResource.getMediaType());
resourceFile.setName(name);
documentContent.setResourceFile(resourceFile);
}
} else if (documentation.getSourceType().equals(Documentation.DocumentSourceType.INLINE) || documentation.getSourceType().equals(Documentation.DocumentSourceType.MARKDOWN)) {
String contentPath = artifact.getPath().replace(RegistryConstants.PATH_SEPARATOR + documentation.getName(), "") + RegistryConstants.PATH_SEPARATOR + APIConstants.INLINE_DOCUMENT_CONTENT_DIR + RegistryConstants.PATH_SEPARATOR + documentation.getName();
if (registryType.resourceExists(contentPath)) {
documentContent = new DocumentContent();
Resource docContent = registryType.get(contentPath);
Object content = docContent.getContent();
if (content != null) {
String contentStr = new String((byte[]) docContent.getContent(), Charset.defaultCharset());
documentContent.setTextContent(contentStr);
documentContent.setSourceType(ContentSourceType.valueOf(documentation.getSourceType().toString()));
}
}
} else if (documentation.getSourceType().equals(Documentation.DocumentSourceType.URL)) {
documentContent = new DocumentContent();
String sourceUrl = documentation.getSourceUrl();
documentContent.setTextContent(sourceUrl);
documentContent.setSourceType(ContentSourceType.valueOf(documentation.getSourceType().toString()));
}
}
} catch (RegistryException | APIPersistenceException e) {
String msg = "Failed to get documentation details";
throw new DocumentationPersistenceException(msg, e);
} finally {
if (tenantFlowStarted) {
RegistryPersistenceUtil.endTenantFlow();
}
}
return documentContent;
}
Aggregations