Skip to content

Commit

Permalink
Add support get action by action Id.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Aug 22, 2024
1 parent ea4291b commit 71825d0
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,14 @@ public Map<String, Integer> 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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Action updateAction(String actionType, String actionId, Action updatingAction, A
* @return <code>Action</code>.
* @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.
Expand All @@ -128,7 +128,7 @@ Action updateAction(String actionType, String actionId, Action updatingAction, A
* @return Updated <code>Action</code>.
* @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;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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." +
Expand Down Expand Up @@ -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" +
Expand Down Expand Up @@ -241,12 +241,16 @@ public Map<String, Integer> 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;
Expand All @@ -257,13 +261,13 @@ public Action getActionByActionId(String actionId, Integer tenantId) throws Acti
}

@Override
public Action updateActionEndpointAuthProperties(String actionId, AuthType authentication, int tenantId)
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
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -124,16 +127,45 @@ public Map<String, Integer> 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<Action> 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 " + action.getId() + " is not found in cache or DB.");
}
}

return action;
}

@Override
public Action updateActionEndpointAuthProperties(String actionId, AuthType authentication, int tenantId)
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
Expand All @@ -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<Action> 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);
}
}
}

0 comments on commit 71825d0

Please sign in to comment.