use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.
the class DocumentIndexer method fetchRequiredDetailsFromAssociatedAPI.
/**
* Fetch api status and access control details to document artifacts
*
* @param registry
* @param documentResource
* @param fields
* @throws RegistryException
* @throws APIManagementException
*/
private void fetchRequiredDetailsFromAssociatedAPI(Registry registry, Resource documentResource, Map<String, List<String>> fields) throws RegistryException, APIManagementException {
String pathToDocFile = documentResource.getPath();
String apiPath = pathToDocFile.substring(0, pathToDocFile.indexOf(APIConstants.DOC_DIR)) + APIConstants.API_KEY;
if (registry.resourceExists(apiPath)) {
Resource apiResource = registry.get(apiPath);
GenericArtifactManager apiArtifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiResource.getUUID());
String apiStatus = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_STATUS).toLowerCase();
String publisherRoles = apiResource.getProperty(APIConstants.PUBLISHER_ROLES);
fields.put(APIConstants.API_OVERVIEW_STATUS, Arrays.asList(apiStatus));
fields.put(APIConstants.PUBLISHER_ROLES, Arrays.asList(publisherRoles));
} else {
log.warn("API does not exist at " + apiPath);
}
}
use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.
the class APIUtilTest method testCreateDocArtifactContent.
@Test
public void testCreateDocArtifactContent() throws GovernanceException, APIManagementException {
API api = getUniqueAPI();
PowerMockito.mockStatic(CarbonUtils.class);
ServerConfiguration serverConfiguration = Mockito.mock(ServerConfiguration.class);
Mockito.when(serverConfiguration.getFirstProperty("WebContextRoot")).thenReturn("/abc").thenReturn("/");
PowerMockito.when(CarbonUtils.getServerConfiguration()).thenReturn(serverConfiguration);
GenericArtifact genericArtifact = Mockito.mock(GenericArtifact.class);
Documentation documentation = new Documentation(DocumentationType.HOWTO, "this is a doc");
documentation.setSourceType(Documentation.DocumentSourceType.FILE);
documentation.setCreatedDate(new Date(System.currentTimeMillis()));
documentation.setSummary("abcde");
documentation.setVisibility(Documentation.DocumentVisibility.API_LEVEL);
documentation.setSourceUrl("/abcd/def");
documentation.setOtherTypeName("aa");
APIUtil.createDocArtifactContent(genericArtifact, api.getId(), documentation);
documentation.setSourceType(Documentation.DocumentSourceType.INLINE);
APIUtil.createDocArtifactContent(genericArtifact, api.getId(), documentation);
documentation.setSourceType(Documentation.DocumentSourceType.URL);
APIUtil.createDocArtifactContent(genericArtifact, api.getId(), documentation);
try {
documentation.setSourceType(Documentation.DocumentSourceType.URL);
Mockito.doThrow(GovernanceException.class).when(genericArtifact).setAttribute(APIConstants.DOC_SOURCE_URL, documentation.getSourceUrl());
APIUtil.createDocArtifactContent(genericArtifact, api.getId(), documentation);
Assert.fail();
} catch (APIManagementException ex) {
Assert.assertTrue(ex.getMessage().contains("Failed to create doc artifact content from :"));
}
}
use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.
the class CustomAPIIndexerTest method testIndexingCustomProperties.
/**
* This method checks the indexer's behaviour for APIs which does not have the relevant custom properties.
*
* @throws RegistryException Registry Exception.
* @throws APIManagementException API Management Exception.
*/
@Test
public void testIndexingCustomProperties() throws RegistryException, APIManagementException {
Resource resource = new ResourceImpl();
PowerMockito.mockStatic(APIUtil.class);
Mockito.doReturn(resource).when(userRegistry).get(Mockito.anyString());
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
GenericArtifact genericArtifact = Mockito.mock(GenericArtifact.class);
PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
Mockito.when(artifactManager.getGenericArtifact(Mockito.anyString())).thenReturn(genericArtifact);
Mockito.when(genericArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY)).thenReturn("public");
PowerMockito.when(APIUtil.getAPI(genericArtifact, userRegistry)).thenReturn(Mockito.mock(API.class));
resource.setProperty(APIConstants.API_RELATED_CUSTOM_PROPERTIES_PREFIX + APIConstants.CUSTOM_API_INDEXER_PROPERTY, APIConstants.CUSTOM_API_INDEXER_PROPERTY);
Assert.assertEquals(APIConstants.OVERVIEW_PREFIX + APIConstants.API_RELATED_CUSTOM_PROPERTIES_PREFIX + APIConstants.CUSTOM_API_INDEXER_PROPERTY, indexer.getIndexedDocument(file2Index).getFields().keySet().toArray()[0].toString());
}
use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact 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.governance.api.generic.dataobjects.GenericArtifact 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();
}
}
}
Aggregations