use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.
the class WorkflowUtils method completeStateChangeWorkflow.
/**
* Complete the lifecycle state change workflow
*
* @param workflowDTO Workflow DTO object
* @throws WorkflowException Exception when completing the workflow
*/
protected static void completeStateChangeWorkflow(WorkflowDTO workflowDTO) throws WorkflowException {
String externalWorkflowRef = workflowDTO.getExternalWorkflowReference();
try {
ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance();
Workflow workflow = apiMgtDAO.getworkflowReferenceByExternalWorkflowReference(externalWorkflowRef);
String apiName = workflow.getMetadata("ApiName");
String action = workflow.getMetadata("Action");
String providerName = workflow.getMetadata("ApiProvider");
String version = workflow.getMetadata("ApiVersion");
String invoker = workflow.getMetadata("Invoker");
String currentStatus = workflow.getMetadata("CurrentState");
int tenantId = workflowDTO.getTenantId();
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);
}
}
use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.
the class TenantWorkflowConfigHolder method load.
public void load() throws WorkflowException, RegistryException {
workflowExecutorMap = new ConcurrentHashMap<>();
InputStream in = null;
try {
String workFlowConfig = ServiceReferenceHolder.getInstance().getApimConfigService().getWorkFlowConfig(tenantDomain);
if (StringUtils.isNotEmpty(workFlowConfig)) {
in = new ByteArrayInputStream(workFlowConfig.getBytes());
StAXOMBuilder builder = new StAXOMBuilder(in);
secretResolver = SecretResolverFactory.create(builder.getDocumentElement(), true);
OMElement workflowExtensionsElem = builder.getDocument().getFirstChildWithName(new QName(WorkflowConstants.WORKFLOW_EXTENSIONS));
OMElement workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.APPLICATION_CREATION));
String executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
Class clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
WorkflowExecutor workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_APPLICATION_CREATION, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.PRODUCTION_APPLICATION_REGISTRATION));
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_PRODUCTION, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.SANDBOX_APPLICATION_REGISTRATION));
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_APPLICATION_REGISTRATION_SANDBOX, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.USER_SIGN_UP));
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_USER_SIGNUP, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.SUBSCRIPTION_CREATION));
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_CREATION, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.SUBSCRIPTION_UPDATE));
if (workflowElem != null) {
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE, workFlowExecutor);
} else {
executorClass = DEFAULT_SUBSCRIPTION_UPDATE_EXECUTOR_CLASS;
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_UPDATE, workFlowExecutor);
}
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.SUBSCRIPTION_DELETION));
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_SUBSCRIPTION_DELETION, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.APPLICATION_DELETION));
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_APPLICATION_DELETION, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.API_STATE_CHANGE));
if (workflowElem == null) {
// TO handle migrated environment, create the default simple workflow executor
workflowElem = OMAbstractFactory.getOMFactory().createOMElement(new QName(WorkflowConstants.API_STATE_CHANGE));
executorClass = WorkflowConstants.DEFAULT_EXECUTOR_API_STATE_CHANGE;
workflowElem.addAttribute(WorkflowConstants.EXECUTOR, executorClass, null);
} else {
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
}
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_API_STATE, workFlowExecutor);
workflowElem = workflowExtensionsElem.getFirstChildWithName(new QName(WorkflowConstants.API_PRODUCT_STATE_CHANGE));
if (workflowElem == null) {
// To handle migration, create the default simple workflow executor
workflowElem = OMAbstractFactory.getOMFactory().createOMElement(new QName(WorkflowConstants.API_PRODUCT_STATE_CHANGE));
executorClass = WorkflowConstants.DEFAULT_EXECUTOR_API_PRODUCT_STATE_CHANGE;
workflowElem.addAttribute(WorkflowConstants.EXECUTOR, executorClass, null);
} else {
executorClass = workflowElem.getAttributeValue(new QName(WorkflowConstants.EXECUTOR));
}
clazz = TenantWorkflowConfigHolder.class.getClassLoader().loadClass(executorClass);
workFlowExecutor = (WorkflowExecutor) clazz.newInstance();
loadProperties(workflowElem, workFlowExecutor);
workflowExecutorMap.put(WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE, workFlowExecutor);
}
} catch (XMLStreamException e) {
log.error("Error building xml", e);
handleException("Error building xml", e);
} catch (ClassNotFoundException e) {
log.error("Unable to find class", e);
handleException("Unable to find class", e);
} catch (InstantiationException e) {
log.error("Unable to instantiate class", e);
handleException("Unable to instantiate class", e);
} catch (IllegalAccessException e) {
log.error("Illegal attempt to invoke class methods", e);
handleException("Illegal attempt to invoke class methods", e);
} catch (WorkflowException e) {
log.error("Unable to load workflow executor class", e);
handleException("Unable to load workflow executor class", e);
} catch (APIManagementException e) {
log.error("Unable to retrieve workflow configurations", e);
handleException("Unable to retrieve workflow configurations", e);
} finally {
IOUtils.closeQuietly(in);
}
}
use of org.wso2.carbon.registry.core.exceptions.RegistryException 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.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.
the class WorkflowExecutorFactory method getWorkflowConfigurations.
public TenantWorkflowConfigHolder getWorkflowConfigurations() throws WorkflowException {
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
String cacheName = tenantDomain + "_" + APIConstants.WORKFLOW_CACHE_NAME;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
// synchronized (cacheName.intern()){
Cache workflowCache = Caching.getCacheManager(APIConstants.API_MANAGER_CACHE_MANAGER).getCache(APIConstants.WORKFLOW_CACHE_NAME);
TenantWorkflowConfigHolder workflowConfig = (TenantWorkflowConfigHolder) workflowCache.get(cacheName);
if (workflowConfig != null) {
return workflowConfig;
} else {
TenantWorkflowConfigHolder configHolder = new TenantWorkflowConfigHolder(tenantDomain, tenantId);
try {
configHolder.load();
workflowCache.put(cacheName, configHolder);
return configHolder;
} catch (WorkflowException e) {
handleException("Error occurred while creating workflow configurations for tenant " + tenantDomain, e);
} catch (RegistryException e) {
handleException("Error occurred while creating workflow configurations for tenant " + tenantDomain, e);
}
}
return null;
}
use of org.wso2.carbon.registry.core.exceptions.RegistryException in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testGetTopRatedAPIs.
@Test
public void testGetTopRatedAPIs() throws APIManagementException, RegistryException {
Registry userRegistry = Mockito.mock(Registry.class);
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(userRegistry, apiMgtDAO);
GenericArtifactManager artifactManager = Mockito.mock(GenericArtifactManager.class);
PowerMockito.when(APIUtil.getArtifactManager((UserRegistry) (Mockito.anyObject()), Mockito.anyString())).thenReturn(artifactManager);
GenericArtifact artifact = Mockito.mock(GenericArtifact.class);
GenericArtifact[] genericArtifacts = new GenericArtifact[] { artifact };
Mockito.when(artifactManager.getAllGenericArtifacts()).thenReturn(genericArtifacts);
Mockito.when(artifact.getAttribute(Mockito.anyString())).thenReturn("PUBLISHED");
Mockito.when(artifact.getPath()).thenReturn("testPath");
Mockito.when(userRegistry.getAverageRating("testPath")).thenReturn((float) 20.0);
APIIdentifier apiId1 = new APIIdentifier("admin", "API1", "1.0.0");
API api = new API(apiId1);
Mockito.when(APIUtil.getAPI(artifact, userRegistry)).thenReturn(api);
assertNotNull(apiConsumer.getTopRatedAPIs(10));
}
Aggregations