From 1e35ef9bb745e7a2f340ddce7f0d835279ab0a50 Mon Sep 17 00:00:00 2001 From: thisarawelmilla Date: Tue, 20 Aug 2024 22:47:45 +0530 Subject: [PATCH] Add support get action by action Id. --- .../management/ActionManagementService.java | 5 +- .../ActionManagementServiceImpl.java | 11 ++-- .../constant/ActionMgtConstants.java | 4 +- .../management/dao/ActionManagementDAO.java | 4 +- .../dao/impl/ActionManagementDAOImpl.java | 18 +++--- .../dao/impl/CacheBackedActionMgtDAO.java | 60 +++++++++++++++++-- 6 files changed, 81 insertions(+), 21 deletions(-) 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 9f6416320bde..6b4b358600c4 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 @@ -108,12 +108,13 @@ Action updateAction(String actionType, String actionId, Action action, String te /** * Get Action of a given Action ID. * - * @param actionId Action ID. + * @param actionType Action Type. + * @param actionId Action Id. * @param tenantDomain Tenant domain. * @return Action response. * @throws ActionMgtException If an error occurs while retrieving the Action of a given Action ID. */ - Action getActionByActionId(String actionId, String tenantDomain) throws ActionMgtException; + Action getActionByActionId(String actionType, String actionId, String tenantDomain) throws ActionMgtException; /** * Update the authentication of the action endpoint. diff --git a/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/ActionManagementServiceImpl.java b/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/ActionManagementServiceImpl.java index e1e0dd84b923..def0115f5140 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/ActionManagementServiceImpl.java +++ b/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/ActionManagementServiceImpl.java @@ -140,12 +140,14 @@ public Map getActionsCountPerType(String tenantDomain) throws A } @Override - public Action getActionByActionId(String actionId, String tenantDomain) throws ActionMgtException { + public Action getActionByActionId(String actionType, String actionId, String tenantDomain) + throws ActionMgtException { if (LOG.isDebugEnabled()) { LOG.debug(String.format("Retrieving Action of Action ID: %s", actionId)); } - return CACHE_BACKED_DAO.getActionByActionId(actionId, IdentityTenantUtil.getTenantId(tenantDomain)); + return CACHE_BACKED_DAO.getActionByActionId(getActionTypeFromPath(actionType), actionId, + IdentityTenantUtil.getTenantId(tenantDomain)); } @Override @@ -208,7 +210,8 @@ private void validateMaxActionsPerType(String actionType, String tenantDomain) t private Action checkIfActionExists(String actionType, String actionId, String tenantDomain) throws ActionMgtException { - Action action = CACHE_BACKED_DAO.getActionByActionId(actionId, IdentityTenantUtil.getTenantId(tenantDomain)); + Action action = CACHE_BACKED_DAO.getActionByActionId(actionType, actionId, + IdentityTenantUtil.getTenantId(tenantDomain)); if (action == null || !actionType.equals(action.getType().name())) { throw ActionManagementUtil.handleClientException( ActionMgtConstants.ErrorMessages.ERROR_NO_ACTION_CONFIGURED_ON_GIVEN_ACTION_TYPE_AND_ID); @@ -259,7 +262,7 @@ private Action updateEndpointAuthenticationProperties(String actionType, String LOG.debug(String.format("Updating endpoint authentication properties of Action Type: %s " + "Action ID: %s and AuthType: %s", actionType, actionId, authentication.getType().name())); } - return CACHE_BACKED_DAO.updateActionEndpointAuthProperties(actionId, authentication, + return CACHE_BACKED_DAO.updateActionEndpointAuthProperties(actionType, actionId, authentication, IdentityTenantUtil.getTenantId(tenantDomain)); } } diff --git a/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/constant/ActionMgtConstants.java b/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/constant/ActionMgtConstants.java index 02cffaaf25f0..7784beecc18a 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/constant/ActionMgtConstants.java +++ b/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/constant/ActionMgtConstants.java @@ -70,7 +70,9 @@ public enum ErrorMessages { "Error while retrieving Action basic info from the system."), ERROR_WHILE_DECRYPTING_ACTION_ENDPOINT_AUTH_PROPERTIES("65012", "Error while decrypting Action Endpoint Authentication properties", - "Error while decrypting Action Endpoint Authentication properties in the system."); + "Error while decrypting Action Endpoint Authentication properties in the system."), + ERROR_ACTION_TYPE_MISMATCH("65013", "Given action type does not match", + "The action type of the retrieved action does not match with the given action type."); private final String code; private final String message; diff --git a/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/dao/ActionManagementDAO.java b/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/dao/ActionManagementDAO.java index 901f706ec6a7..3b15aadab696 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/dao/ActionManagementDAO.java +++ b/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/dao/ActionManagementDAO.java @@ -117,7 +117,7 @@ Action updateAction(String actionType, String actionId, Action updatingAction, A * @return Action. * @throws ActionMgtException If an error occurs while retrieving the Action of a given Action ID. */ - Action getActionByActionId(String actionId, Integer tenantId) throws ActionMgtException; + Action getActionByActionId(String actionType, String actionId, Integer tenantId) throws ActionMgtException; /** * Update the endpoint authentication properties of an {@link Action} by given Action ID. @@ -128,7 +128,7 @@ Action updateAction(String actionType, String actionId, Action updatingAction, A * @return Updated Action. * @throws ActionMgtException If an error occurs while updating the Action endpoint authentication properties. */ - Action updateActionEndpointAuthProperties(String actionId, AuthType authentication, int tenantId) + Action updateActionEndpointAuthProperties(String actionType, String actionId, AuthType authentication, int tenantId) 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 6a881812cbd6..7319f9658bc4 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 @@ -84,7 +84,7 @@ public Action addAction(String actionType, String actionId, Action action, Integ action.getEndpoint().getAuthentication().getType().name(), encryptedAuthProperties), tenantId); IdentityDatabaseUtil.commitTransaction(dbConnection); - return getActionByActionId(actionId, tenantId); + return getActionByActionId(actionType, actionId, tenantId); } catch (SQLException | ActionMgtException e) { if (LOG.isDebugEnabled()) { LOG.debug(String.format("Error while creating the Action of Action Type: %s in Tenant Domain: %s." + @@ -160,7 +160,7 @@ public Action updateAction(String actionType, String actionId, Action updatingAc tenantId); IdentityDatabaseUtil.commitTransaction(dbConnection); - return getActionByActionId(actionId, tenantId); + return getActionByActionId(actionType, actionId, tenantId); } catch (SQLException | ActionMgtException e) { if (LOG.isDebugEnabled()) { LOG.debug(String.format("Error while updating the Action of Action Type: %s and Action ID: %s in" + @@ -241,12 +241,16 @@ public Map getActionsCountPerType(Integer tenantId) throws Acti } @Override - public Action getActionByActionId(String actionId, Integer tenantId) throws ActionMgtException { + public Action getActionByActionId(String actionType, String actionId, Integer tenantId) throws ActionMgtException { try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(false)) { Action action = getActionBasicInfoById(dbConnection, actionId, tenantId); if (action != null) { action.setEndpoint(getActionEndpointConfigById(dbConnection, actionId, tenantId)); + if (action.getType() != null && StringUtils.equals(action.getType().getActionType(), actionType)) { + throw ActionManagementUtil.handleServerException( + ActionMgtConstants.ErrorMessages.ERROR_ACTION_TYPE_MISMATCH, null); + } } return action; @@ -257,13 +261,13 @@ public Action getActionByActionId(String actionId, Integer tenantId) throws Acti } @Override - public Action updateActionEndpointAuthProperties(String actionId, AuthType authentication, int tenantId) - throws ActionMgtException { + public Action updateActionEndpointAuthProperties(String actionType, String actionId, AuthType authentication, + int tenantId) throws ActionMgtException { Connection dbConnection = IdentityDatabaseUtil.getDBConnection(true); updateActionEndpointAuthProperties(dbConnection, actionId, authentication, tenantId); IdentityDatabaseUtil.closeConnection(dbConnection); - return getActionByActionId(actionId, tenantId); + return getActionByActionId(actionType, actionId, tenantId); } @Override @@ -274,7 +278,7 @@ public Action updateActionEndpoint(String actionType, String actionId, EndpointC Connection dbConnection = IdentityDatabaseUtil.getDBConnection(true); updateActionEndpoint(dbConnection, actionType, actionId, endpoint, currentAuthentication, tenantId); IdentityDatabaseUtil.closeConnection(dbConnection); - return getActionByActionId(actionId, tenantId); + return getActionByActionId(actionType, actionId, tenantId); } /** diff --git a/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/dao/impl/CacheBackedActionMgtDAO.java b/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/dao/impl/CacheBackedActionMgtDAO.java index ac352e3f8d7f..ef5e3846e79b 100644 --- a/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/dao/impl/CacheBackedActionMgtDAO.java +++ b/components/action-mgt/org.wso2.carbon.identity.action.management/src/main/java/org/wso2/carbon/identity/action/management/dao/impl/CacheBackedActionMgtDAO.java @@ -18,6 +18,7 @@ package org.wso2.carbon.identity.action.management.dao.impl; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.identity.action.management.cache.ActionCacheByType; @@ -29,6 +30,8 @@ import org.wso2.carbon.identity.action.management.model.AuthType; import org.wso2.carbon.identity.action.management.model.EndpointConfig; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -124,16 +127,45 @@ public Map getActionsCountPerType(Integer tenantId) throws Acti } @Override - public Action getActionByActionId(String actionId, Integer tenantId) throws ActionMgtException { + public Action getActionByActionId(String actionType, String actionId, Integer tenantId) throws ActionMgtException { - return actionManagementDAO.getActionByActionId(actionId, tenantId); + ActionTypeCacheKey cacheKey = new ActionTypeCacheKey(actionType); + ActionCacheEntry entry = actionCacheByType.getValueFromCache(cacheKey, tenantId); + List actionsFromCache = new ArrayList<>(); + + /* If the entry for the given action type is not null, get the action list from cache and iterate to get the + action by matching action id. */ + if (entry != null) { + actionsFromCache = entry.getActions(); + for (Action action: actionsFromCache) { + if (StringUtils.equals(action.getId(), actionId)) { + LOG.debug("Action is found from the cache with action Id " + actionId); + return action; + } + } + } + + if (LOG.isDebugEnabled()) { + LOG.debug("Action is not found from the cache with action Id " + actionId + ". Fetching entry from DB."); + } + + Action action = actionManagementDAO.getActionByActionId(actionType, actionId, tenantId); + if (action != null) { + updateCache(action, entry, cacheKey, actionsFromCache, tenantId); + } else { + if (LOG.isDebugEnabled()) { + LOG.debug("Action with action Id " + actionId + " is not found in cache or DB."); + } + } + + return action; } @Override - public Action updateActionEndpointAuthProperties(String actionId, AuthType authentication, int tenantId) - throws ActionMgtException { + public Action updateActionEndpointAuthProperties(String actionType, String actionId, AuthType authentication, + int tenantId) throws ActionMgtException { - return actionManagementDAO.updateActionEndpointAuthProperties(actionId, authentication, tenantId); + return actionManagementDAO.updateActionEndpointAuthProperties(actionType, actionId, authentication, tenantId); } @Override @@ -145,4 +177,22 @@ public Action updateActionEndpoint(String actionType, String actionId, EndpointC return actionManagementDAO.updateActionEndpoint(actionType, actionId, endpoint, currentAuthentication, tenantId); } + + private void updateCache(Action action, ActionCacheEntry entry, ActionTypeCacheKey cacheKey, + List actionsFromCache, int tenantId) { + + if (LOG.isDebugEnabled()) { + LOG.debug("Entry fetched from DB for Action Id " + action.getId() + ". Updating cache."); + } + /* If the entry for the given action type is not null, add the fetched action to the entry. Then, clear the + cache and add the updated entry to the cache. If the entry is null, create a new cache entry.*/ + if (entry != null) { + actionsFromCache.add(action); + actionCacheByType.clearCacheEntry(cacheKey, tenantId); + actionCacheByType.addToCache(cacheKey, new ActionCacheEntry(actionsFromCache), tenantId); + } else { + actionCacheByType.addToCache(cacheKey, new ActionCacheEntry( + new ArrayList<>(Collections.singletonList(action))), tenantId); + } + } }