diff --git a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/ActionExecutorService.java b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/ActionExecutorService.java index afdf9fa6e26a..d40dd9e11b8a 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/ActionExecutorService.java +++ b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/ActionExecutorService.java @@ -51,7 +51,16 @@ public interface ActionExecutorService { ActionExecutionStatus execute(ActionType actionType, Map eventContext, String tenantDomain) throws ActionExecutionException; - + /** + * Execute the action based on the action id and the event context. + * + * @param actionType Action Type + * @param actionId Action ID + * @param eventContext Context information required for the action execution. + * @param tenantDomain Tenant Domain + * @return {@link ActionExecutionStatus} The status of the action execution and the response context. + * @throws ActionExecutionException If an error occurs while executing the action. + */ ActionExecutionStatus execute(ActionType actionType, String actionId, Map eventContext, String tenantDomain) throws ActionExecutionException; } diff --git a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/ActionExecutorServiceImpl.java b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/ActionExecutorServiceImpl.java index a774c9da1507..6c512cf24226 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/ActionExecutorServiceImpl.java +++ b/components/action-mgt/org.wso2.carbon.identity.action.execution/src/main/java/org/wso2/carbon/identity/action/execution/ActionExecutorServiceImpl.java @@ -22,7 +22,6 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.identity.action.execution.exception.ActionExecutionException; @@ -86,37 +85,36 @@ public boolean isExecutionEnabled(ActionType actionType) { public ActionExecutionStatus execute(ActionType actionType, Map eventContext, String tenantDomain) throws ActionExecutionException { - return execute(actionType, null, eventContext, tenantDomain); + try { + List actions = getActionsByActionType(actionType, tenantDomain); + validateActions(actions, actionType); + // As of now only one action is allowed. + Action action = actions.get(0); + return execute(action, actionType, eventContext); + } catch (ActionExecutionRuntimeException e) { + // todo: add to diagnostics + LOG.debug("Skip executing actions for action type: " + actionType.name(), e); + return new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext); + } } public ActionExecutionStatus execute(ActionType actionType, String actionId, Map eventContext, String tenantDomain) throws ActionExecutionException { - try { - Action action; - if (StringUtils.isBlank(actionId)) { - List actions = getActionsByActionType(actionType, tenantDomain); - validateActions(actions, actionType); - // As of now only one action is allowed. - action = actions.get(0); - } else { - action = getActionByActionId(actionId, tenantDomain); - } + return execute(getActionByActionId(actionType, actionId, tenantDomain), actionType, eventContext); + } - ActionExecutionRequest actionRequest = buildActionExecutionRequest(actionType, eventContext); - ActionExecutionResponseProcessor actionExecutionResponseProcessor = getResponseProcessor(actionType); + private ActionExecutionStatus execute(Action action, ActionType actionType, Map eventContext) + throws ActionExecutionException { - return Optional.ofNullable(action) - .filter(activeAction -> activeAction.getStatus() == Action.Status.ACTIVE) - .map(activeAction -> executeAction(activeAction, actionRequest, eventContext, - actionExecutionResponseProcessor)) - .orElse(new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext)); - } catch (ActionExecutionRuntimeException e) { - // todo: add to diagnostics - LOG.debug("Skip executing actions for action type: " + actionType.name(), e); - return new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext); + ActionExecutionRequest actionRequest = buildActionExecutionRequest(actionType, eventContext); + ActionExecutionResponseProcessor actionExecutionResponseProcessor = getResponseProcessor(actionType); - } + return Optional.ofNullable(action) + .filter(activeAction -> activeAction.getStatus() == Action.Status.ACTIVE) + .map(activeAction -> executeAction(activeAction, actionRequest, eventContext, + actionExecutionResponseProcessor)) + .orElse(new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext)); } private List getActionsByActionType(ActionType actionType, String tenantDomain) throws @@ -130,13 +128,14 @@ private List getActionsByActionType(ActionType actionType, String tenant } } - private Action getActionByActionId(String actionId, String tenantDomain) throws ActionExecutionRuntimeException { + private Action getActionByActionId(ActionType actionType, String actionId, String tenantDomain) + throws ActionExecutionRuntimeException { try { - return ActionExecutionServiceComponentHolder.getInstance().getActionManagementService() - .getActionByActionId(actionId, tenantDomain); + return ActionExecutionServiceComponentHolder.getInstance().getActionManagementService().getActionByActionId( + Action.ActionTypes.valueOf(actionType.name()).getPathParam(), actionId, tenantDomain); } catch (ActionMgtException e) { - throw new ActionExecutionRuntimeException("Error occurred while retrieving actions.", e); + throw new ActionExecutionRuntimeException("Error occurred while retrieving action by action Id .", e); } } diff --git a/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/ActionManagementService.java b/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/ActionManagementService.java index 7c55ce66cf1b..82df3e1eaf12 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/ActionManagementService.java +++ b/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/ActionManagementService.java @@ -52,12 +52,13 @@ public interface ActionManagementService { List getActionsByActionType(String actionType, String tenantDomain) throws ActionMgtException; /** - * Get Actions of a given Action Type. + * Get Actions of a given Action Id. * * @param actionType Action Type. + * @param actionId Action Id. * @param tenantDomain Tenant domain. * @return List of Actions. - * @throws ActionMgtException If an error occurs while retrieving the Actions of a given Action Type. + * @throws ActionMgtException If an error occurs while retrieving the Actions of a given Action Id. */ Action getActionByActionId(String actionType, String actionId, String tenantDomain) throws ActionMgtException; diff --git a/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/dao/impl/ActionManagementDAOImpl.java b/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/dao/impl/ActionManagementDAOImpl.java index e6142de2dc04..17b32c310506 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/dao/impl/ActionManagementDAOImpl.java +++ b/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/dao/impl/ActionManagementDAOImpl.java @@ -242,6 +242,7 @@ public Map getActionsCountPerType(Integer tenantId) throws Acti @Override public Action getActionByActionId(String actionType, String actionId, Integer tenantId) throws ActionMgtException { + return getActionByActionId(actionId, tenantId); }