Skip to content

Commit

Permalink
Add minor changes
Browse files Browse the repository at this point in the history
Remove unused methods
Fix API total in GET key manager API usage response
Refactor code
  • Loading branch information
SavinduDimal committed Mar 23, 2024
1 parent 2be69b2 commit e0434ae
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -519,10 +519,12 @@ Map<String, Object> 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<KeyManagerConfigurationDTO> getGlobalKeyManagerConfigurations(String organization) throws APIManagementException;
List<KeyManagerConfigurationDTO> getGlobalKeyManagerConfigurations(String organization)
throws APIManagementException;

/**
* This method used to retrieve global key manager with Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -679,12 +678,6 @@ public KeyManagerApplicationUsages getApplicationsOfKeyManager(String keyManager
return keyManagerApplicationUsages;
}

public List<ApplicationInfoKeyManager> 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())) {
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -1748,8 +1741,7 @@ private static IdentityProvider cloneIdentityProvider(IdentityProvider identityP
}

@Override
public List<KeyManagerConfigurationDTO> getGlobalKeyManagerConfigurations()
throws APIManagementException {
public List<KeyManagerConfigurationDTO> getGlobalKeyManagerConfigurations() throws APIManagementException {
List<KeyManagerConfigurationDTO> keyManagerConfigurations = apiMgtDAO.getKeyManagerConfigurationsByOrganization(
APIConstants.GLOBAL_KEY_MANAGER_TENANT_DOMAIN);
for (KeyManagerConfigurationDTO keyManagerConfigurationDTO : keyManagerConfigurations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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<Topic> getTopics(String apiId) throws APIManagementException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -20199,8 +20180,8 @@ private Set<String> getAllClonedPolicyIdsForAPI(Connection connection, String ap
return policyIds;
}

public List<ApplicationInfoKeyManager> getAllApplicationsOfKeyManager(String keyManagerId) throws
APIManagementException {
public List<ApplicationInfoKeyManager> getAllApplicationsOfKeyManager(String keyManagerId)
throws APIManagementException {

ArrayList<ApplicationInfoKeyManager> applicationsList = new ArrayList<>();
String sqlQuery = SQLConstants.GET_APPLICATIONS_OF_KEY_MANAGERS_SQL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit e0434ae

Please sign in to comment.