use of org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method deleteDocumentation.
@Override
public void deleteDocumentation(Organization org, String apiId, String docId) throws DocumentationPersistenceException {
boolean isTenantFlowStarted = false;
try {
RegistryHolder holder = getRegistry(org.getName());
Registry registry = holder.getRegistry();
isTenantFlowStarted = holder.isTenantFlowStarted();
GenericArtifactManager artifactManager = RegistryPersistenceDocUtil.getDocumentArtifactManager(registry);
if (artifactManager == null) {
String errorMessage = "Failed to retrieve artifact manager when removing documentation of " + apiId + " Document ID " + docId;
log.error(errorMessage);
throw new DocumentationPersistenceException(errorMessage);
}
GenericArtifact artifact = artifactManager.getGenericArtifact(docId);
String docPath = artifact.getPath();
if (docPath != null) {
if (registry.resourceExists(docPath)) {
registry.delete(docPath);
}
}
} catch (RegistryException | APIPersistenceException e) {
throw new DocumentationPersistenceException("Failed to delete documentation", e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
use of org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException in project carbon-apimgt by wso2.
the class RegistryPersistenceDocUtil method createDocArtifactContent.
public static GenericArtifact createDocArtifactContent(GenericArtifact artifact, String apiName, String apiVersion, String apiProvider, Documentation documentation) throws DocumentationPersistenceException {
try {
artifact.setAttribute(APIConstants.DOC_NAME, documentation.getName());
artifact.setAttribute(APIConstants.DOC_SUMMARY, documentation.getSummary());
artifact.setAttribute(APIConstants.DOC_TYPE, documentation.getType().getType());
artifact.setAttribute(APIConstants.DOC_VISIBILITY, documentation.getVisibility().name());
Documentation.DocumentSourceType sourceType = documentation.getSourceType();
switch(sourceType) {
case INLINE:
sourceType = Documentation.DocumentSourceType.INLINE;
break;
case MARKDOWN:
sourceType = Documentation.DocumentSourceType.MARKDOWN;
break;
case URL:
sourceType = Documentation.DocumentSourceType.URL;
break;
case FILE:
{
sourceType = Documentation.DocumentSourceType.FILE;
}
break;
default:
throw new DocumentationPersistenceException("Unknown sourceType " + sourceType + " provided for documentation");
}
// Therefore setting a default value if it is not set.
if (documentation.getSourceUrl() == null) {
documentation.setSourceUrl(" ");
}
artifact.setAttribute(APIConstants.DOC_SOURCE_TYPE, sourceType.name());
artifact.setAttribute(APIConstants.DOC_SOURCE_URL, documentation.getSourceUrl());
artifact.setAttribute(APIConstants.DOC_FILE_PATH, documentation.getFilePath());
artifact.setAttribute(APIConstants.DOC_OTHER_TYPE_NAME, documentation.getOtherTypeName());
String basePath = RegistryPersistenceUtil.replaceEmailDomain(apiProvider) + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
artifact.setAttribute(APIConstants.DOC_API_BASE_PATH, basePath);
} catch (GovernanceException e) {
String msg = "Failed to create doc artifact content from :" + documentation.getName();
log.error(msg, e);
throw new DocumentationPersistenceException(msg, e);
}
return artifact;
}
use of org.wso2.carbon.apimgt.persistence.exceptions.DocumentationPersistenceException in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method searchContentForPublisher.
@Override
public PublisherContentSearchResult searchContentForPublisher(Organization org, String searchQuery, int start, int offset, UserContext ctx) throws APIPersistenceException {
log.debug("Requested query for publisher content search: " + searchQuery);
Map<String, String> attributes = RegistrySearchUtil.getPublisherSearchAttributes(searchQuery, ctx);
if (log.isDebugEnabled()) {
log.debug("Search attributes : " + attributes);
}
boolean isTenantFlowStarted = false;
PublisherContentSearchResult result = null;
try {
RegistryHolder holder = getRegistry(org.getName());
Registry registry = holder.getRegistry();
isTenantFlowStarted = holder.isTenantFlowStarted();
String requestedTenantDomain = org.getName();
String tenantAwareUsername = getTenantAwareUsername(RegistryPersistenceUtil.getTenantAdminUserName(requestedTenantDomain));
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 PublisherContentSearchResult();
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);
// 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();
PublisherAPI pubAPI;
if (apiArtifactId != null) {
GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiArtifactId);
String accociatedType;
if (apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE).equals(APIConstants.AuditLogConstants.API_PRODUCT)) {
// associatedAPIProduct = APIUtil.getAPIProduct(apiArtifact, registry);
accociatedType = APIConstants.API_PRODUCT;
} else {
// associatedAPI = APIUtil.getAPI(apiArtifact, registry);
accociatedType = APIConstants.API;
}
pubAPI = RegistryPersistenceUtil.getAPIForSearch(apiArtifact);
docSearch.setApiName(pubAPI.getApiName());
docSearch.setApiProvider(pubAPI.getProviderName());
docSearch.setApiVersion(pubAPI.getVersion());
docSearch.setApiUUID(pubAPI.getId());
docSearch.setAssociatedType(accociatedType);
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();
// API api;
// APIProduct apiProduct;
String type;
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);
type = APIConstants.API_PRODUCT;
} else {
// api = APIUtil.getAPI(apiArtifact, registry);
// apiSet.add(api);
type = APIConstants.API;
}
PublisherAPI pubAPI = RegistryPersistenceUtil.getAPIForSearch(apiArtifact);
PublisherSearchContent content = new PublisherSearchContent();
content.setContext(pubAPI.getContext());
content.setDescription(pubAPI.getDescription());
content.setId(pubAPI.getId());
content.setName(pubAPI.getApiName());
content.setProvider(RegistryPersistenceUtil.replaceEmailDomainBack(pubAPI.getProviderName()));
content.setType(type);
content.setVersion(pubAPI.getVersion());
content.setStatus(pubAPI.getStatus());
content.setAdvertiseOnly(pubAPI.isAdvertiseOnly());
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 | APIManagementException 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 updateDocumentation.
@Override
public Documentation updateDocumentation(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 artifactManager = RegistryPersistenceDocUtil.getDocumentArtifactManager(registry);
GenericArtifact artifact = artifactManager.getGenericArtifact(documentation.getId());
String docVisibility = documentation.getVisibility().name();
String[] authorizedRoles = new String[0];
String visibleRolesList = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES);
if (visibleRolesList != null) {
authorizedRoles = visibleRolesList.split(",");
}
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;
}
}
GenericArtifact updateApiArtifact = RegistryPersistenceDocUtil.createDocArtifactContent(artifact, apiProviderName, apiName, apiVersion, documentation);
artifactManager.updateGenericArtifact(updateApiArtifact);
RegistryPersistenceUtil.clearResourcePermissions(updateApiArtifact.getPath(), new APIIdentifier(apiProviderName, apiName, apiVersion), ((UserRegistry) registry).getTenantId());
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, authorizedRoles, artifact.getPath(), registry);
String docFilePath = artifact.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);
}
return documentation;
} catch (RegistryException | APIManagementException | APIPersistenceException e) {
throw new DocumentationPersistenceException("Failed to update 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 addDocumentationContent.
@Override
public DocumentContent addDocumentationContent(Organization org, String apiId, String docId, DocumentContent content) throws DocumentationPersistenceException {
boolean isTenantFlowStarted = false;
try {
String tenantDomain = org.getName();
RegistryHolder holder = getRegistry(tenantDomain);
Registry registry = holder.getRegistry();
isTenantFlowStarted = 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 = RegistryPersistenceDocUtil.getDocumentArtifactManager(registry);
GenericArtifact docArtifact = docArtifactManager.getGenericArtifact(docId);
Documentation doc = RegistryPersistenceDocUtil.getDocumentation(docArtifact);
if (DocumentContent.ContentSourceType.FILE.equals(content.getSourceType())) {
ResourceFile resource = content.getResourceFile();
String filePath = RegistryPersistenceDocUtil.getDocumentFilePath(apiProviderName, apiName, apiVersion, resource.getName());
String visibility = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY);
String visibleRolesList = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES);
String[] visibleRoles = new String[0];
if (visibleRolesList != null) {
visibleRoles = visibleRolesList.split(",");
}
RegistryPersistenceUtil.setResourcePermissions(RegistryPersistenceUtil.replaceEmailDomain(apiProviderName), visibility, visibleRoles, filePath, registry);
// documentation.setFilePath(addResourceFile(apiId, filePath, icon));
String savedFilePath = addResourceFile(filePath, resource, registry, tenantDomain);
// doc.setFilePath(savedFilePath);
docArtifact.setAttribute(APIConstants.DOC_FILE_PATH, savedFilePath);
docArtifactManager.updateGenericArtifact(docArtifact);
RegistryPersistenceUtil.setFilePermission(filePath);
} else {
String contentPath = RegistryPersistenceDocUtil.getDocumentContentPath(apiProviderName, apiName, apiVersion, doc.getName());
Resource docContent;
if (!registry.resourceExists(contentPath)) {
docContent = registry.newResource();
} else {
docContent = registry.get(contentPath);
}
String text = content.getTextContent();
if (!APIConstants.NO_CONTENT_UPDATE.equals(text)) {
docContent.setContent(text);
}
docContent.setMediaType(APIConstants.DOCUMENTATION_INLINE_CONTENT_TYPE);
registry.put(contentPath, docContent);
// Set resource permission
String apiPath = RegistryPersistenceUtil.getAPIPath(apiName, apiVersion, apiProviderName);
String docVisibility = doc.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, contentPath, registry);
GenericArtifact updateDocArtifact = RegistryPersistenceDocUtil.createDocArtifactContent(docArtifact, apiProviderName, apiName, apiVersion, doc);
Boolean toggle = Boolean.parseBoolean(updateDocArtifact.getAttribute("toggle"));
updateDocArtifact.setAttribute("toggle", Boolean.toString(!toggle));
docArtifactManager.updateGenericArtifact(updateDocArtifact);
}
} catch (APIPersistenceException | RegistryException | APIManagementException | PersistenceException | UserStoreException e) {
throw new DocumentationPersistenceException("Error while adding document content", e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
return null;
}
Aggregations