use of org.wso2.carbon.registry.api.Registry 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;
}
use of org.wso2.carbon.registry.api.Registry 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.registry.api.Registry in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getRegistry.
protected RegistryHolder getRegistry(String requestedTenantDomain) throws APIPersistenceException {
String userTenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
log.debug("Accessing system registry in tenant domain " + userTenantDomain + ". Requested tenant domain: " + requestedTenantDomain);
boolean tenantFlowStarted = false;
Registry registry;
RegistryHolder holder = new RegistryHolder();
try {
if (requestedTenantDomain != null) {
int id = getTenantManager().getTenantId(requestedTenantDomain);
RegistryPersistenceUtil.startTenantFlow(requestedTenantDomain);
tenantFlowStarted = true;
if (userTenantDomain != null && !userTenantDomain.equals(requestedTenantDomain)) {
// cross tenant
log.debug("Cross tenant user from tenant " + userTenantDomain + " accessing " + requestedTenantDomain + " registry");
loadTenantRegistry(id);
registry = getRegistryService().getGovernanceSystemRegistry(id);
holder.setTenantId(id);
ServiceReferenceHolder.setUserRealm((ServiceReferenceHolder.getInstance().getRealmService().getBootstrapRealm()));
} else {
log.debug("Same tenant accessing registry of tenant " + userTenantDomain + ":" + tenantId);
loadTenantRegistry(tenantId);
registry = getRegistryService().getGovernanceSystemRegistry(tenantId);
RegistryPersistenceUtil.loadloadTenantAPIRXT(null, tenantId);
holder.setTenantId(tenantId);
ServiceReferenceHolder.setUserRealm((UserRealm) (ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)));
}
} else {
log.debug("Same tenant user accessing registry of tenant " + userTenantDomain + ":" + tenantId);
loadTenantRegistry(tenantId);
registry = getRegistryService().getGovernanceSystemRegistry(tenantId);
RegistryPersistenceUtil.loadloadTenantAPIRXT(null, tenantId);
ServiceReferenceHolder.setUserRealm((UserRealm) (ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantId)));
holder.setTenantId(tenantId);
}
} catch (RegistryException | UserStoreException | APIManagementException e) {
String msg = "Failed to get API";
throw new APIPersistenceException(msg, e);
}
holder.setRegistry(registry);
holder.setTenantFlowStarted(tenantFlowStarted);
return holder;
}
use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method saveAPIStatus.
/**
* Persist API Status into a property of API Registry resource
*
* @param artifactId API artifact ID
* @param apiStatus Current status of the API
* @throws APIManagementException on error
*/
private void saveAPIStatus(Registry registry, String artifactId, String apiStatus) throws APIManagementException {
try {
Resource resource = registry.get(artifactId);
if (resource != null) {
String propValue = resource.getProperty(APIConstants.API_STATUS);
if (propValue == null) {
resource.addProperty(APIConstants.API_STATUS, apiStatus);
} else {
resource.setProperty(APIConstants.API_STATUS, apiStatus);
}
registry.put(artifactId, resource);
}
} catch (RegistryException e) {
handleException("Error while adding API", e);
}
}
use of org.wso2.carbon.registry.api.Registry in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method getbasicAPIInfo.
private BasicAPI getbasicAPIInfo(String uuid, Registry registry) throws APIPersistenceException, GovernanceException {
BasicAPI api = new BasicAPI();
GenericArtifact apiArtifact = getAPIArtifact(uuid, registry);
if (apiArtifact == null) {
return null;
}
String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
api.apiProvider = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
api.apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
api.apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
String visibleRolesList = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES);
if (visibleRolesList != null) {
api.visibleRoles = visibleRolesList.split(",");
}
api.visibility = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY);
return api;
}
Aggregations