use of org.wso2.carbon.governance.api.generic.GenericArtifactManager 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.GenericArtifactManager in project carbon-apimgt by wso2.
the class APIProviderImplTest method mockDocumentationCreation.
private void mockDocumentationCreation() throws Exception {
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.whenNew(GenericArtifactManager.class).withAnyArguments().thenReturn(artifactManager);
GenericArtifact artifact = Mockito.mock(GenericArtifact.class);
Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
}
use of org.wso2.carbon.governance.api.generic.GenericArtifactManager in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method saveWSDL.
@Override
public void saveWSDL(Organization org, String apiId, ResourceFile wsdlResourceFile) throws WSDLPersistenceException {
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);
String apiSourcePath = RegistryPersistenceUtil.getAPIBasePath(apiProviderName, apiName, apiVersion);
String wsdlResourcePath = null;
boolean isZip = false;
String wsdlResourcePathArchive = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiProviderName + APIConstants.WSDL_PROVIDER_SEPERATOR + apiName + apiVersion + APIConstants.ZIP_FILE_EXTENSION;
String wsdlResourcePathFile = apiSourcePath + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.createWsdlFileName(apiProviderName, apiName, apiVersion);
if (APIConstants.APPLICATION_ZIP.equals(wsdlResourceFile.getContentType())) {
wsdlResourcePath = wsdlResourcePathArchive;
isZip = true;
} else {
wsdlResourcePath = wsdlResourcePathFile;
}
String visibility = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY);
String visibleRolesList = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES);
Resource wsdlResource = registry.newResource();
;
wsdlResource.setContentStream(wsdlResourceFile.getContent());
if (wsdlResourceFile.getContentType() != null) {
wsdlResource.setMediaType(wsdlResourceFile.getContentType());
}
registry.put(wsdlResourcePath, wsdlResource);
// set the anonymous role for wsld resource to avoid basicauth security.
String[] visibleRoles = null;
if (visibleRolesList != null) {
visibleRoles = visibleRolesList.split(",");
}
RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, visibleRoles, wsdlResourcePath);
if (isZip) {
// Delete any WSDL file if exists
if (registry.resourceExists(wsdlResourcePathFile)) {
registry.delete(wsdlResourcePathFile);
}
} else {
// Delete any WSDL archives if exists
if (registry.resourceExists(wsdlResourcePathArchive)) {
registry.delete(wsdlResourcePathArchive);
}
}
String absoluteWSDLResourcePath = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + wsdlResourcePath;
String wsdlRegistryPath;
if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(tenantDomain)) {
wsdlRegistryPath = RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + absoluteWSDLResourcePath;
} else {
wsdlRegistryPath = "/t/" + tenantDomain + RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + absoluteWSDLResourcePath;
}
apiArtifact.setAttribute(APIConstants.API_OVERVIEW_WSDL, wsdlRegistryPath);
apiArtifactManager.updateGenericArtifact(apiArtifact);
} catch (APIPersistenceException | APIManagementException | RegistryException e) {
throw new WSDLPersistenceException("Error while saving the wsdl for api " + apiId, e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
use of org.wso2.carbon.governance.api.generic.GenericArtifactManager 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.GenericArtifactManager in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetAllPublishedAPIs.
@Test
public void testGetAllPublishedAPIs() throws APIManagementException, GovernanceException {
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper();
APINameComparator apiNameComparator = Mockito.mock(APINameComparator.class);
SortedSet<API> apiSortedSet = new TreeSet<API>(apiNameComparator);
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(APIUtil.getArtifactManager(apiConsumer.registry, APIConstants.API_KEY)).thenReturn(artifactManager);
GenericArtifact artifact = Mockito.mock(GenericArtifact.class);
GenericArtifact[] genericArtifacts = new GenericArtifact[] { artifact };
APIIdentifier apiId1 = new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, SAMPLE_API_VERSION);
API api = new API(apiId1);
Mockito.when(artifactManager.getAllGenericArtifacts()).thenReturn(genericArtifacts);
Mockito.when(artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS)).thenReturn("PUBLISHED");
Mockito.when(APIUtil.getAPI(artifact)).thenReturn(api);
Map<String, API> latestPublishedAPIs = new HashMap<String, API>();
latestPublishedAPIs.put("user:key", api);
apiSortedSet.addAll(latestPublishedAPIs.values());
}
Aggregations