diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIAdmin.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIAdmin.java index e1fcad758af1..4e8ab9666be1 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIAdmin.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIAdmin.java @@ -519,10 +519,12 @@ Map searchPaginatedApis(String searchQuery, String organization, /** * This method used to retrieve global key manager configurations with usage check + * @param organization organization * @return KeyManagerConfigurationDTO list * @throws APIManagementException if error occurred */ - List getGlobalKeyManagerConfigurations(String organization) throws APIManagementException; + List getGlobalKeyManagerConfigurations(String organization) + throws APIManagementException; /** * This method used to retrieve global key manager with Id diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIConsumer.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIConsumer.java index 2e04e0a5df5c..9a7b53622d3f 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIConsumer.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIConsumer.java @@ -721,17 +721,6 @@ ResourceFile getWSDL(API api, String environmentName, String environmentType, St */ String getRequestedTenant(); - /** - * Get consumer key and key manager name by application id and key mapping id - * @param applicationId application id - * @param keyMappingId key mapping id - * @return KeyManagerApplicationInfo - */ - KeyManagerApplicationInfo getKeyManagerNameAndConsumerKeyByAppIDAndKeyMapping(int applicationId, - String keyMappingId) - throws APIManagementException; - - /** * * @param apiId API UUID diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIAdminImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIAdminImpl.java index e9fc8c2e4b44..e2f15bd6aac6 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIAdminImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIAdminImpl.java @@ -656,8 +656,7 @@ public KeyManagerConfigurationDTO addKeyManagerConfiguration( } public AdminContentSearchResult getAPIUsagesByKeyManagerNameAndOrganization(String org, String keyManagerName, - int offset, int limit) - throws APIManagementException { + int offset, int limit) throws APIManagementException { APIPersistence apiPersistenceInstance = PersistenceFactory.getAPIPersistenceInstance(); String searchQuery = APIConstants.API_USAGE_BY_KEY_MANAGER_QUERY.replace("$1", keyManagerName); @@ -679,12 +678,6 @@ public KeyManagerApplicationUsages getApplicationsOfKeyManager(String keyManager return keyManagerApplicationUsages; } - public List getAllApplicationsOfKeyManager(String keyManagerId) - throws APIManagementException { - ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance(); - return apiMgtDAO.getAllApplicationsOfKeyManager(keyManagerId); - } - private void validateKeyManagerEndpointConfiguration(KeyManagerConfigurationDTO keyManagerConfigurationDTO) throws APIManagementException { if (!APIConstants.KeyManager.DEFAULT_KEY_MANAGER.equals(keyManagerConfigurationDTO.getName())) { @@ -984,7 +977,7 @@ public void deleteKeyManagerConfigurationById(String organization, KeyManagerCon ExceptionCodes.KEY_MANAGER_DELETE_FAILED); } } else { - throw new APIManagementException("Key Manager is already used by an API or and Application.", + throw new APIManagementException("Key Manager is already used by an API or an Application.", ExceptionCodes.KEY_MANAGER_DELETE_FAILED); } } @@ -1748,8 +1741,7 @@ private static IdentityProvider cloneIdentityProvider(IdentityProvider identityP } @Override - public List getGlobalKeyManagerConfigurations() - throws APIManagementException { + public List getGlobalKeyManagerConfigurations() throws APIManagementException { List keyManagerConfigurations = apiMgtDAO.getKeyManagerConfigurationsByOrganization( APIConstants.GLOBAL_KEY_MANAGER_TENANT_DOMAIN); for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurations) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java index e7f9a892e49a..186ddfd32c10 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConsumerImpl.java @@ -3602,6 +3602,7 @@ public void cleanUpApplicationRegistrationByApplicationIdAndKeyMappingId(int app public boolean removalKeys(Application application, String keyMappingId, String xWSO2Tenant) throws APIManagementException { + try { APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(this.username); @@ -3619,8 +3620,8 @@ public boolean removalKeys(Application application, String keyMappingId, String String keyManagerName = APIConstants.KeyManager.DEFAULT_KEY_MANAGER; ApiMgtDAO apiMgtDAO = ApiMgtDAO.getInstance(); - KeyManagerApplicationInfo KeyManagerApplicationInfo = apiMgtDAO - .getKeyManagerNameAndConsumerKeyByAppIdAndKeyMappingId(application.getId(), keyMappingId); + KeyManagerApplicationInfo KeyManagerApplicationInfo = + apiMgtDAO.getKeyManagerNameAndConsumerKeyByAppIdAndKeyMappingId(application.getId(), keyMappingId); String keyManagerNameResult = KeyManagerApplicationInfo.getKeyManagerName(); if (!StringUtils.isEmpty(keyManagerNameResult)) { keyManagerName = keyManagerNameResult; @@ -3640,8 +3641,7 @@ public boolean removalKeys(Application application, String keyMappingId, String UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.REMOVE_APPLICATION_KEYMAPPING.name(), APIUtil.getTenantIdFromTenantDomain(tenantDomain), application.getOrganization(), - application.getId(), application.getUUID(), consumerKey, application.getKeyType(), - keyManagerName); + application.getId(), application.getUUID(), consumerKey, application.getKeyType(), keyManagerName); APIUtil.sendNotification(removeEntryTrigger, APIConstants.NotifierType.APPLICATION_REGISTRATION.name()); return true; } catch (APIManagementException e) { @@ -3679,14 +3679,6 @@ public APIKey getApplicationKeyByAppIDAndKeyMapping(int applicationId, String ke return apiKey; } - @Override - public KeyManagerApplicationInfo getKeyManagerNameAndConsumerKeyByAppIDAndKeyMapping(int applicationId, String keyMappingId) - throws APIManagementException { - return apiMgtDAO - .getKeyManagerNameAndConsumerKeyByAppIdAndKeyMappingId(applicationId, keyMappingId); - } - - @Override public Set getTopics(String apiId) throws APIManagementException { diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java index ffe49e5d2041..8fcb44a815a2 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java @@ -9833,25 +9833,6 @@ public KeyManagerApplicationInfo getKeyManagerNameAndConsumerKeyByAppIdAndKeyMap return null; } - public String getKeyManagerNameFromKeyMappingId(String keyMappingId) - throws APIManagementException { - - final String query = "SELECT NAME AS KEY_MANAGER_NAME FROM AM_KEY_MANAGER AKM, AM_APPLICATION_KEY_MAPPING " + - "AAKM WHERE AAKM.UUID = ? AND AKM.UUID=AAKM.KEY_MANAGER;"; - try (Connection connection = APIMgtDBUtil.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(query)) { - preparedStatement.setString(1, keyMappingId); - try (ResultSet resultSet = preparedStatement.executeQuery()) { - if (resultSet.next()) { - return resultSet.getString("KEY_MANAGER_NAME"); - } - } - } catch (SQLException e) { - throw new APIManagementException("Error while Retrieving Key Mapping ", e); - } - return null; - } - public void deleteApplicationKeyMappingByMappingId(String keyMappingId) throws APIManagementException { Connection connection = null; @@ -20199,8 +20180,8 @@ private Set getAllClonedPolicyIdsForAPI(Connection connection, String ap return policyIds; } - public List getAllApplicationsOfKeyManager(String keyManagerId) throws - APIManagementException { + public List getAllApplicationsOfKeyManager(String keyManagerId) + throws APIManagementException { ArrayList applicationsList = new ArrayList<>(); String sqlQuery = SQLConstants.GET_APPLICATIONS_OF_KEY_MANAGERS_SQL; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java index 2be4c28da890..8097b6ec0478 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/KeyManagersApiServiceImpl.java @@ -39,8 +39,6 @@ import java.util.List; import javax.ws.rs.core.Response; -import static org.wso2.carbon.utils.multitenancy.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME; - public class KeyManagersApiServiceImpl implements KeyManagersApiService { private static final Log log = LogFactory.getLog(KeyManagersApiServiceImpl.class); @@ -170,8 +168,8 @@ public Response keyManagersKeyManagerIdApiUsagesGet(String keyManagerId, Integer } else { throw new APIManagementException("Requested KeyManager not found", ExceptionCodes.KEY_MANAGER_NOT_FOUND); } - AdminContentSearchResult result = - apiAdmin.getAPIUsagesByKeyManagerNameAndOrganization(organization, KeyManagerName, offset, limit); + AdminContentSearchResult result = apiAdmin.getAPIUsagesByKeyManagerNameAndOrganization(organization, + KeyManagerName, offset, limit); return Response.ok().entity(KeyManagerMappingUtil.toKeyManagerAPIUsagesDTO(result)).build(); } @@ -192,7 +190,7 @@ public Response keyManagersKeyManagerIdAppUsagesGet(String keyManagerId, Integer throw new APIManagementException("Requested KeyManager not found", ExceptionCodes.KEY_MANAGER_NOT_FOUND); } KeyManagerApplicationUsages result = apiAdmin.getApplicationsOfKeyManager(keyManagerId, offset, limit); - return Response.ok().entity(result).build(); + return Response.ok().entity(KeyManagerMappingUtil.toKeyManagerAppUsagesDTO(result)).build(); } public Response keyManagersPost(KeyManagerDTO body, MessageContext messageContext) throws APIManagementException { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/RestApiAdminUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/RestApiAdminUtils.java index 651836c22d88..474d3dd48751 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/RestApiAdminUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/RestApiAdminUtils.java @@ -24,7 +24,6 @@ import org.wso2.carbon.apimgt.api.APIAdmin; import org.wso2.carbon.apimgt.api.APIManagementException; import org.wso2.carbon.apimgt.api.ExceptionCodes; -import org.wso2.carbon.apimgt.api.model.ApplicationInfoKeyManager; import org.wso2.carbon.apimgt.api.model.BlockConditionsDTO; import org.wso2.carbon.apimgt.api.model.policy.Policy; import org.wso2.carbon.apimgt.impl.APIAdminImpl; @@ -41,9 +40,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.Arrays; -import java.util.Collections; import java.util.HashSet; -import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.Set; diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/KeyManagerMappingUtil.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/KeyManagerMappingUtil.java index ffa7527d8d49..42ec4fb3673c 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/KeyManagerMappingUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/utils/mappings/KeyManagerMappingUtil.java @@ -398,7 +398,7 @@ public static KeyManagerAPIUsagesDTO toKeyManagerAPIUsagesDTO(AdminContentSearch apis.add(apiInfoKeyManagerDTO); } } - keyManagerAPIUsagesDTO.setApiCount(result.getApiCount()); + keyManagerAPIUsagesDTO.setApiCount(result.getApiTotal()); keyManagerAPIUsagesDTO.setApis(apis); } return keyManagerAPIUsagesDTO; @@ -444,7 +444,7 @@ public static ApplicationInfoKeyManagerDTO toApplicationInfoKeyManagerDTO( ApplicationInfoKeyManager applicationInfoKeyManager) { ApplicationInfoKeyManagerDTO applicationInfoKeyManagerDTO = new ApplicationInfoKeyManagerDTO(); - applicationInfoKeyManagerDTO.setName(applicationInfoKeyManager.getUuid()); + applicationInfoKeyManagerDTO.setName(applicationInfoKeyManager.getName()); applicationInfoKeyManagerDTO.setUuid(applicationInfoKeyManager.getUuid()); applicationInfoKeyManagerDTO.setOrganization(applicationInfoKeyManager.getOrganization()); applicationInfoKeyManagerDTO.setOrganizationId(applicationInfoKeyManager.getOrganizationId()); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java index 98d587da020c..42882fdc900e 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/ApplicationsApiServiceImpl.java @@ -1202,22 +1202,21 @@ public Response applicationsApplicationIdOauthKeysKeyMappingIdCleanUpPost(String @Override public Response applicationsApplicationIdOauthKeysKeyMappingIdDelete(String applicationId, String keyMappingId, - String xWSO2Tenant, - MessageContext messageContext) - throws APIManagementException { + String xWSO2Tenant, MessageContext messageContext) throws APIManagementException { + String username = RestApiCommonUtil.getLoggedInUsername(); try { APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(username); Application application = apiConsumer.getLightweightApplicationByUUID(applicationId); boolean result = apiConsumer.removalKeys(application, keyMappingId, xWSO2Tenant); - if(result) { + if (result) { return Response.ok().build(); } else { RestApiUtil.handleResourceNotFoundError(ExceptionCodes.KEYS_DELETE_FAILED.getErrorMessage(), keyMappingId, log); } } catch (APIManagementException e) { - RestApiUtil.handleInternalServerError("Error occurred while application key cleanup process", e, log); + RestApiUtil.handleInternalServerError("Error occurred while application key delete process", e, log); } return null; }