use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.
the class APIProviderImpl method getAPILifeCycleStatus.
@Override
public String getAPILifeCycleStatus(APIIdentifier apiIdentifier) throws APIManagementException {
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(this.tenantDomain, true);
GenericArtifact apiArtifact = APIUtil.getAPIArtifact(apiIdentifier, registry);
if (apiArtifact == null) {
String errorMessage = "API artifact is null when retrieving lifecycle status of API " + apiIdentifier.getApiName();
log.error(errorMessage);
throw new APIManagementException(errorMessage);
}
return apiArtifact.getLifecycleState();
} catch (GovernanceException e) {
handleException("Failed to get the life cycle status : " + e.getMessage(), e);
return null;
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.
the class APIUtil method searchAPIsByDoc.
/**
* Search Apis by Doc Content
*
* @param registry - Registry which is searched
* @param tenantID - Tenant id of logged in domain
* @param username - Logged in username
* @param searchTerm - Search value for doc
* @param searchClient - Search client
* @return - Documentation to APIs map
* @throws APIManagementException - If failed to get ArtifactManager for given tenant
*/
public static Map<Documentation, API> searchAPIsByDoc(Registry registry, int tenantID, String username, String searchTerm, String searchClient) throws APIManagementException {
Map<Documentation, API> apiDocMap = new HashMap<Documentation, API>();
try {
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username);
GenericArtifactManager artifactManager = APIUtil.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 APIManagementException(errorMessage);
}
GenericArtifactManager docArtifactManager = APIUtil.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 APIManagementException(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(searchTerm, tenantID, fields);
org.wso2.carbon.user.api.AuthorizationManager manager = ServiceReferenceHolder.getInstance().getRealmService().getTenantUserRealm(tenantID).getAuthorizationManager();
username = MultitenantUtils.getTenantAwareUsername(username);
for (SolrDocument document : documentList) {
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);
API api = null;
Documentation doc = null;
boolean isAuthorized;
int indexOfContents = filePath.indexOf(APIConstants.INLINE_DOCUMENT_CONTENT_DIR);
String documentationPath = filePath.substring(0, indexOfContents) + fileName;
String path = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), APIUtil.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) {
Resource docResource = registry.get(documentationPath);
String docArtifactId = docResource.getUUID();
if (docArtifactId != null) {
GenericArtifact docArtifact = docArtifactManager.getGenericArtifact(docArtifactId);
doc = APIUtil.getDocumentation(docArtifact);
}
int indexOfDocumentation = filePath.indexOf(APIConstants.DOCUMENTATION_KEY);
String apiPath = documentationPath.substring(0, indexOfDocumentation) + APIConstants.API_KEY;
path = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), APIUtil.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 apiArtifact = artifactManager.getGenericArtifact(apiArtifactId);
api = APIUtil.getAPI(apiArtifact, registry);
} else {
throw new GovernanceException("artifact id is null of " + apiPath);
}
}
}
if (doc != null && api != null) {
if (APIConstants.STORE_CLIENT.equals(searchClient)) {
if (APIConstants.PUBLISHED.equals(api.getStatus()) || APIConstants.PROTOTYPED.equals(api.getStatus())) {
apiDocMap.put(doc, api);
}
} else {
apiDocMap.put(doc, api);
}
}
}
} catch (IndexerException e) {
handleException("Failed to search APIs with type Doc", e);
} catch (RegistryException e) {
handleException("Failed to search APIs with type Doc", e);
} catch (UserStoreException e) {
handleException("Failed to search APIs with type Doc", e);
}
return apiDocMap;
}
use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.
the class APIUtil method notifyAPIStateChangeToAssociatedDocuments.
/**
* Notify document artifacts if an api state change occured. This change is required to re-trigger the document
* indexer so that the documnet indexes will be updated with the new associated api status.
*
* @param apiArtifact
* @param registry
* @throws RegistryException
* @throws APIManagementException
*/
public static void notifyAPIStateChangeToAssociatedDocuments(GenericArtifact apiArtifact, Registry registry) throws RegistryException, APIManagementException {
Association[] docAssociations = registry.getAssociations(apiArtifact.getPath(), APIConstants.DOCUMENTATION_ASSOCIATION);
for (Association association : docAssociations) {
String documentResourcePath = association.getDestinationPath();
Resource docResource = registry.get(documentResourcePath);
String oldStateChangeIndicatorStatus = docResource.getProperty(APIConstants.API_STATE_CHANGE_INDICATOR);
String newStateChangeIndicatorStatus = "false";
if (oldStateChangeIndicatorStatus != null) {
newStateChangeIndicatorStatus = String.valueOf(!Boolean.parseBoolean(oldStateChangeIndicatorStatus));
}
docResource.setProperty(APIConstants.API_STATE_CHANGE_INDICATOR, "false");
registry.put(documentResourcePath, docResource);
}
}
use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.
the class APIUtil method getProvider.
/**
* This method used to get Provider from provider artifact
*
* @param artifact provider artifact
* @return Provider
* @throws APIManagementException if failed to get Provider from provider artifact.
*/
public static Provider getProvider(GenericArtifact artifact) throws APIManagementException {
Provider provider;
try {
provider = new Provider(artifact.getAttribute(APIConstants.PROVIDER_OVERVIEW_NAME));
provider.setDescription(artifact.getAttribute(APIConstants.PROVIDER_OVERVIEW_DESCRIPTION));
provider.setEmail(artifact.getAttribute(APIConstants.PROVIDER_OVERVIEW_EMAIL));
} catch (GovernanceException e) {
String msg = "Failed to get provider ";
log.error(msg, e);
throw new APIManagementException(msg, e);
}
return provider;
}
use of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact in project carbon-apimgt by wso2.
the class APIUtil method getAPIArtifact.
/**
* Util method to return the artifact from a registry resource path
*
* @param apiIdentifier
* @param registry
* @return
* @throws APIManagementException
*/
public static GenericArtifact getAPIArtifact(APIIdentifier apiIdentifier, Registry registry) throws APIManagementException {
String apiPath = APIUtil.getAPIPath(apiIdentifier);
GenericArtifactManager artifactManager = APIUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Artifact manager is null when getting generic artifact for API " + apiIdentifier.getApiName();
log.error(errorMessage);
throw new APIManagementException(errorMessage);
}
try {
Resource apiResource = registry.get(apiPath);
String artifactId = apiResource.getUUID();
if (artifactId == null) {
throw new APIManagementException("artifact id is null for : " + apiPath);
}
return artifactManager.getGenericArtifact(artifactId);
} catch (RegistryException e) {
handleException("Failed to get API artifact from : " + apiPath, e);
return null;
}
}
Aggregations