use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.
the class AbstractAPIManager method getAPIorAPIProductByUUID.
/**
* Get API or APIProduct by registry artifact id
*
* @param uuid Registry artifact id
* @param requestedTenantDomain tenantDomain for the registry
* @return ApiTypeWrapper wrapping the API or APIProduct of the provided artifact id
* @throws APIManagementException
*/
public ApiTypeWrapper getAPIorAPIProductByUUID(String uuid, String requestedTenantDomain) throws APIManagementException {
boolean tenantFlowStarted = false;
try {
Registry registry;
if (requestedTenantDomain != null) {
int id = getTenantManager().getTenantId(requestedTenantDomain);
startTenantFlow(requestedTenantDomain);
tenantFlowStarted = true;
if (APIConstants.WSO2_ANONYMOUS_USER.equals(this.username)) {
registry = getRegistryService().getGovernanceUserRegistry(this.username, id);
} else if (this.tenantDomain != null && !this.tenantDomain.equals(requestedTenantDomain)) {
registry = getRegistryService().getGovernanceSystemRegistry(id);
} else {
registry = this.registry;
}
} else {
registry = this.registry;
}
GenericArtifactManager artifactManager = getAPIGenericArtifactManagerFromUtil(registry, APIConstants.API_KEY);
GenericArtifact apiArtifact = artifactManager.getGenericArtifact(uuid);
if (apiArtifact != null) {
String type = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_TYPE);
if (APIConstants.API_PRODUCT.equals(type)) {
APIProduct apiProduct = getApiProduct(registry, apiArtifact);
String productTenantDomain = getTenantDomain(apiProduct.getId());
if (APIConstants.API_GLOBAL_VISIBILITY.equals(apiProduct.getVisibility())) {
return new ApiTypeWrapper(apiProduct);
}
if (this.tenantDomain == null || !this.tenantDomain.equals(productTenantDomain)) {
throw new APIManagementException("User " + username + " does not have permission to view API Product : " + apiProduct.getId().getName());
}
return new ApiTypeWrapper(apiProduct);
} else {
API api = getApiForPublishing(registry, apiArtifact);
String apiTenantDomain = getTenantDomain(api.getId());
if (APIConstants.API_GLOBAL_VISIBILITY.equals(api.getVisibility())) {
return new ApiTypeWrapper(api);
}
if (this.tenantDomain == null || !this.tenantDomain.equals(apiTenantDomain)) {
throw new APIManagementException("User " + username + " does not have permission to view API : " + api.getId().getApiName());
}
return new ApiTypeWrapper(api);
}
} else {
String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
throw new APIMgtResourceNotFoundException(msg);
}
} catch (RegistryException | org.wso2.carbon.user.api.UserStoreException e) {
String msg = "Failed to get API";
throw new APIManagementException(msg, e);
} finally {
if (tenantFlowStarted) {
endTenantFlow();
}
}
}
use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.
the class APIStateChangeWSWorkflowExecutor method complete.
/**
* Complete the API state change workflow process.
*/
@Override
public WorkflowResponse complete(WorkflowDTO workflowDTO) throws WorkflowException {
if (log.isDebugEnabled()) {
log.debug("Completing API State change Workflow..");
log.debug("response: " + workflowDTO.toString());
}
workflowDTO.setUpdatedTime(System.currentTimeMillis());
super.complete(workflowDTO);
String action = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_API_LC_ACTION);
String apiName = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_APINAME);
String providerName = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_APIPROVIDER);
String version = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_APIVERSION);
String invoker = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_INVOKER);
String currentStatus = workflowDTO.getAttributes().get(PayloadConstants.VARIABLE_APISTATE);
int tenantId = workflowDTO.getTenantId();
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
try {
// tenant flow is already started from the rest api service impl. no need to start from here
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(invoker);
Registry registry = ServiceReferenceHolder.getInstance().getRegistryService().getGovernanceUserRegistry(invoker, tenantId);
APIIdentifier apiIdentifier = new APIIdentifier(providerName, apiName, version);
GenericArtifact apiArtifact = APIUtil.getAPIArtifact(apiIdentifier, registry);
if (WorkflowStatus.APPROVED.equals(workflowDTO.getStatus())) {
String targetStatus;
apiArtifact.invokeAction(action, APIConstants.API_LIFE_CYCLE);
targetStatus = apiArtifact.getLifecycleState();
if (!currentStatus.equals(targetStatus)) {
apiMgtDAO.recordAPILifeCycleEvent(apiArtifact.getId(), currentStatus.toUpperCase(), targetStatus.toUpperCase(), invoker, tenantId);
}
if (log.isDebugEnabled()) {
String logMessage = "API Status changed successfully. API Name: " + apiIdentifier.getApiName() + ", API Version " + apiIdentifier.getVersion() + ", New Status : " + targetStatus;
log.debug(logMessage);
}
}
} catch (RegistryException e) {
String errorMsg = "Could not complete api state change workflow";
log.error(errorMsg, e);
throw new WorkflowException(errorMsg, e);
} catch (APIManagementException e) {
String errorMsg = "Could not complete api state change workflow";
log.error(errorMsg, e);
throw new WorkflowException(errorMsg, e);
}
return new GeneralWorkflowResponse();
}
use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact 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.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method searchPaginatedDevPortalAPIsByDoc.
private DevPortalAPISearchResult searchPaginatedDevPortalAPIsByDoc(Registry registry, int tenantID, String searchQuery, String username, int start, int offset) throws APIPersistenceException {
DevPortalAPISearchResult searchResults = new DevPortalAPISearchResult();
try {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username);
GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Artifact manager is null when searching APIs by docs in tenant ID " + tenantID;
log.error(errorMessage);
throw new APIPersistenceException(errorMessage);
}
GenericArtifactManager docArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.DOCUMENTATION_KEY);
if (docArtifactManager == null) {
String errorMessage = "Doc artifact manager is null when searching APIs by docs in tenant ID " + tenantID;
log.error(errorMessage);
throw new APIPersistenceException(errorMessage);
}
SolrClient client = SolrClient.getInstance();
Map<String, String> fields = new HashMap<String, String>();
fields.put(APIConstants.DOCUMENTATION_SEARCH_PATH_FIELD, "*" + APIConstants.API_ROOT_LOCATION + "*");
fields.put(APIConstants.DOCUMENTATION_SEARCH_MEDIA_TYPE_FIELD, "*");
if (tenantID == -1) {
tenantID = MultitenantConstants.SUPER_TENANT_ID;
}
// PaginationContext.init(0, 10000, "ASC", APIConstants.DOCUMENTATION_SEARCH_PATH_FIELD, Integer.MAX_VALUE);
SolrDocumentList documentList = client.query(searchQuery, tenantID, fields);
org.wso2.carbon.user.api.AuthorizationManager manager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantID).getAuthorizationManager();
username = MultitenantUtils.getTenantAwareUsername(username);
List<DevPortalAPIInfo> devPortalAPIInfoList = new ArrayList<DevPortalAPIInfo>();
for (SolrDocument document : documentList) {
DevPortalAPIInfo apiInfo = new DevPortalAPIInfo();
String filePath = (String) document.getFieldValue("path_s");
String fileName = (String) document.getFieldValue("resourceName_s");
int index = filePath.indexOf(APIConstants.APIMGT_REGISTRY_LOCATION);
filePath = filePath.substring(index);
boolean isAuthorized;
int indexOfContents = filePath.indexOf(APIConstants.INLINE_DOCUMENT_CONTENT_DIR);
String documentationPath = filePath.substring(0, indexOfContents) + fileName;
String path = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), RegistryPersistenceUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + documentationPath);
if (CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME.equalsIgnoreCase(username)) {
isAuthorized = manager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, path, ActionConstants.GET);
} else {
isAuthorized = manager.isUserAuthorized(username, path, ActionConstants.GET);
}
if (isAuthorized) {
int indexOfDocumentation = filePath.indexOf(APIConstants.DOCUMENTATION_KEY);
String apiPath = documentationPath.substring(0, indexOfDocumentation) + APIConstants.API_KEY;
path = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), RegistryPersistenceUtil.getMountedPath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + apiPath);
if (CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME.equalsIgnoreCase(username)) {
isAuthorized = manager.isRoleAuthorized(APIConstants.ANONYMOUS_ROLE, path, ActionConstants.GET);
} else {
isAuthorized = manager.isUserAuthorized(username, path, ActionConstants.GET);
}
if (isAuthorized) {
Resource resource = registry.get(apiPath);
String apiArtifactId = resource.getUUID();
if (apiArtifactId != null) {
GenericArtifact artifact = artifactManager.getGenericArtifact(apiArtifactId);
String status = artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS);
if (APIConstants.PUBLISHED.equals(status) || APIConstants.PROTOTYPED.equals(status)) {
apiInfo.setType(artifact.getAttribute(APIConstants.API_OVERVIEW_TYPE));
apiInfo.setId(artifact.getId());
apiInfo.setApiName(artifact.getAttribute(APIConstants.API_OVERVIEW_NAME));
apiInfo.setDescription(artifact.getAttribute(APIConstants.API_OVERVIEW_DESCRIPTION));
apiInfo.setContext(artifact.getAttribute(APIConstants.API_OVERVIEW_CONTEXT_TEMPLATE));
apiInfo.setProviderName(artifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER));
apiInfo.setStatus(status);
apiInfo.setThumbnail(artifact.getAttribute(APIConstants.API_OVERVIEW_THUMBNAIL_URL));
apiInfo.setBusinessOwner(artifact.getAttribute(APIConstants.API_OVERVIEW_BUSS_OWNER));
apiInfo.setVersion(artifact.getAttribute(APIConstants.API_OVERVIEW_VERSION));
apiInfo.setSubscriptionAvailability(artifact.getAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABILITY));
apiInfo.setSubscriptionAvailableOrgs(artifact.getAttribute(APIConstants.API_OVERVIEW_SUBSCRIPTION_AVAILABLE_TENANTS));
apiInfo.setGatewayVendor(artifact.getAttribute(APIConstants.API_GATEWAY_VENDOR));
devPortalAPIInfoList.add(apiInfo);
}
} else {
throw new GovernanceException("artifact id is null of " + apiPath);
}
}
}
}
searchResults.setDevPortalAPIInfoList(devPortalAPIInfoList);
searchResults.setTotalAPIsCount(devPortalAPIInfoList.size());
searchResults.setReturnedAPIsCount(devPortalAPIInfoList.size());
} catch (RegistryException | UserStoreException | APIPersistenceException | IndexerException e) {
String msg = "Failed to search APIs with type";
throw new APIPersistenceException(msg, e);
} finally {
PaginationContext.destroy();
}
return searchResults;
}
use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method deleteThumbnail.
@Override
public void deleteThumbnail(Organization org, String apiId) throws ThumbnailPersistenceException {
Registry registry;
boolean isTenantFlowStarted = false;
try {
String tenantDomain = org.getName();
RegistryHolder holder = getRegistry(tenantDomain);
registry = holder.getRegistry();
isTenantFlowStarted = holder.isTenantFlowStarted();
GenericArtifact apiArtifact = getAPIArtifact(apiId, registry);
if (apiArtifact == null) {
throw new ThumbnailPersistenceException("API not found for id " + apiId, ExceptionCodes.API_NOT_FOUND);
}
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 artifactOldPath = APIConstants.API_IMAGE_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
String artifactPath = APIConstants.API_ROOT_LOCATION + RegistryConstants.PATH_SEPARATOR + apiProviderName + RegistryConstants.PATH_SEPARATOR + apiName + RegistryConstants.PATH_SEPARATOR + apiVersion;
String oldThumbPath = artifactOldPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
String thumbPath = artifactPath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_ICON_IMAGE;
if (registry.resourceExists(thumbPath)) {
registry.delete(thumbPath);
}
if (registry.resourceExists(oldThumbPath)) {
registry.delete(oldThumbPath);
}
} catch (RegistryException | APIPersistenceException e) {
String msg = "Error while loading API icon of API " + apiId + " from the registry";
throw new ThumbnailPersistenceException(msg, e);
} finally {
if (isTenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
Aggregations