use of org.wso2.carbon.apimgt.api.model.KeyManager in project carbon-apimgt by wso2.
the class OrganizationPurgeDAO method deleteApplicationList.
/**
* Deletes Applications along with subscriptions, keys and registration data
*
* @param organization Organization
* @throws APIManagementException if failed to delete applications for organization
*/
public void deleteApplicationList(String organization) throws APIManagementException {
try (Connection connection = APIMgtDBUtil.getConnection()) {
connection.setAutoCommit(false);
if (multiGroupAppSharingEnabled) {
updateGroupIDMappingsBulk(connection, organization);
}
try (PreparedStatement prepStmtGetConsumerKey = connection.prepareStatement(OrganizationPurgeConstants.GET_CONSUMER_KEYS_OF_APPLICATION_LIST_SQL);
PreparedStatement deleteDomainApp = connection.prepareStatement(SQLConstants.REMOVE_APPLICATION_FROM_DOMAIN_MAPPINGS_SQL)) {
prepStmtGetConsumerKey.setString(1, organization);
try (ResultSet rs = prepStmtGetConsumerKey.executeQuery()) {
while (rs.next()) {
String consumerKey = rs.getString(APIConstants.FIELD_CONSUMER_KEY);
String keyManagerName = rs.getString("NAME");
String keyManagerOrganization = rs.getString("ORGANIZATION");
// This is true when OAuth App has been created by pasting consumer key/secret in the screen.
String mode = rs.getString("CREATE_MODE");
if (consumerKey != null) {
deleteDomainApp.setString(1, consumerKey);
deleteDomainApp.addBatch();
KeyManager keyManager = KeyManagerHolder.getKeyManagerInstance(keyManagerOrganization, keyManagerName);
if (keyManager != null) {
try {
keyManager.deleteMappedApplication(consumerKey);
log.info("Mapped application deleted for consumer key: " + consumerKey + " and organization: " + organization);
} catch (APIManagementException e) {
handleException("Error while Deleting Client Application for consumer key: " + consumerKey + " and organization: " + organization, e);
}
}
// call delete.
if (!APIConstants.OAuthAppMode.MAPPED.name().equals(mode)) {
// delete on oAuthorization server.
if (log.isDebugEnabled()) {
log.debug("Deleting Oauth application with consumer key " + consumerKey + " from the " + "Oauth server for organization: " + organization);
}
if (keyManager != null) {
try {
keyManager.deleteApplication(consumerKey);
log.info("Client application deleted for consumer key: " + consumerKey + " and organization: " + organization);
} catch (APIManagementException e) {
handleException("Error while Deleting Client Application for organization: " + organization, e);
}
}
}
}
}
}
deleteDomainApp.executeBatch();
} catch (SQLException domainAppsException) {
connection.rollback();
log.error("Failed to rollback removing domain applications for organization: " + organization, domainAppsException);
}
if (log.isDebugEnabled()) {
log.debug("Subscription Key mapping details are deleted successfully for Applications for " + "organization: " + organization);
}
try (PreparedStatement deleteApp = connection.prepareStatement(OrganizationPurgeConstants.REMOVE_APPLICATION_LIST_FROM_APPLICATIONS_SQL)) {
deleteApp.setString(1, organization);
deleteApp.execute();
} catch (SQLException appDeletionException) {
connection.rollback();
log.error("Failed to rollback removing applications for organization: " + organization, appDeletionException);
}
if (log.isDebugEnabled()) {
log.debug("Applications are deleted successfully for organization: " + organization);
}
connection.commit();
} catch (SQLException e) {
handleException("Error while removing application details from the database for organization: " + organization, e);
}
}
use of org.wso2.carbon.apimgt.api.model.KeyManager in project carbon-apimgt by wso2.
the class ApiMgtDAO method deleteApplication.
/**
* Deletes an Application along with subscriptions, keys and registration data
*
* @param application Application object to be deleted from the database which has the application Id
* @throws APIManagementException
*/
public void deleteApplication(Application application) throws APIManagementException {
Connection connection = null;
PreparedStatement deleteMappingQuery = null;
PreparedStatement prepStmt = null;
PreparedStatement prepStmtGetConsumerKey = null;
PreparedStatement deleteRegistrationQuery = null;
PreparedStatement deleteSubscription = null;
PreparedStatement deleteDomainApp = null;
PreparedStatement deleteAppKey = null;
PreparedStatement deleteApp = null;
ResultSet rs = null;
String getSubscriptionsQuery = SQLConstants.GET_SUBSCRIPTION_ID_OF_APPLICATION_SQL;
String getConsumerKeyQuery = SQLConstants.GET_CONSUMER_KEY_OF_APPLICATION_SQL;
String deleteSubscriptionsQuery = SQLConstants.REMOVE_APPLICATION_FROM_SUBSCRIPTIONS_SQL;
String deleteApplicationKeyQuery = SQLConstants.REMOVE_APPLICATION_FROM_APPLICATION_KEY_MAPPINGS_SQL;
String deleteDomainAppQuery = SQLConstants.REMOVE_APPLICATION_FROM_DOMAIN_MAPPINGS_SQL;
String deleteApplicationQuery = SQLConstants.REMOVE_APPLICATION_FROM_APPLICATIONS_SQL;
String deleteRegistrationEntry = SQLConstants.REMOVE_APPLICATION_FROM_APPLICATION_REGISTRATIONS_SQL;
boolean transactionCompleted = true;
try {
connection = APIMgtDBUtil.getConnection();
connection.setAutoCommit(false);
prepStmt = connection.prepareStatement(getSubscriptionsQuery);
prepStmt.setInt(1, application.getId());
rs = prepStmt.executeQuery();
if (multiGroupAppSharingEnabled) {
transactionCompleted = updateGroupIDMappings(connection, application.getId(), null, null);
}
List<Integer> subscriptions = new ArrayList<Integer>();
while (rs.next()) {
subscriptions.add(rs.getInt("SUBSCRIPTION_ID"));
}
prepStmtGetConsumerKey = connection.prepareStatement(getConsumerKeyQuery);
prepStmtGetConsumerKey.setInt(1, application.getId());
rs = prepStmtGetConsumerKey.executeQuery();
deleteDomainApp = connection.prepareStatement(deleteDomainAppQuery);
while (rs.next()) {
String consumerKey = rs.getString(APIConstants.FIELD_CONSUMER_KEY);
String keyManagerName = rs.getString("NAME");
String keyManagerOrganization = rs.getString("ORGANIZATION");
// This is true when OAuth App has been created by pasting consumer key/secret in the screen.
String mode = rs.getString("CREATE_MODE");
if (consumerKey != null) {
deleteDomainApp.setString(1, consumerKey);
deleteDomainApp.addBatch();
KeyManager keyManager = KeyManagerHolder.getKeyManagerInstance(keyManagerOrganization, keyManagerName);
if (keyManager != null) {
try {
keyManager.deleteMappedApplication(consumerKey);
} catch (APIManagementException e) {
log.error("Error while Deleting Client Application", e);
}
}
// call delete.
if (!APIConstants.OAuthAppMode.MAPPED.name().equals(mode)) {
// delete on oAuthorization server.
if (log.isDebugEnabled()) {
log.debug("Deleting Oauth application with consumer key " + consumerKey + " from the " + "Oauth server");
}
if (keyManager != null) {
try {
keyManager.deleteApplication(consumerKey);
} catch (APIManagementException e) {
log.error("Error while Deleting Client Application", e);
}
}
}
}
}
if (log.isDebugEnabled()) {
log.debug("Subscription Key mapping details are deleted successfully for Application - " + application.getName());
}
deleteRegistrationQuery = connection.prepareStatement(deleteRegistrationEntry);
deleteRegistrationQuery.setInt(1, application.getId());
deleteRegistrationQuery.execute();
if (log.isDebugEnabled()) {
log.debug("Application Registration details are deleted successfully for Application - " + application.getName());
}
deleteSubscription = connection.prepareStatement(deleteSubscriptionsQuery);
deleteSubscription.setInt(1, application.getId());
deleteSubscription.execute();
if (log.isDebugEnabled()) {
log.debug("Subscription details are deleted successfully for Application - " + application.getName());
}
deleteDomainApp.executeBatch();
deleteAppKey = connection.prepareStatement(deleteApplicationKeyQuery);
deleteAppKey.setInt(1, application.getId());
deleteAppKey.execute();
if (log.isDebugEnabled()) {
log.debug("Application Key Mapping details are deleted successfully for Application - " + application.getName());
}
deleteApp = connection.prepareStatement(deleteApplicationQuery);
deleteApp.setInt(1, application.getId());
deleteApp.execute();
if (log.isDebugEnabled()) {
log.debug("Application " + application.getName() + " is deleted successfully.");
}
if (transactionCompleted) {
connection.commit();
}
} catch (SQLException e) {
handleException("Error while removing application details from the database", e);
} finally {
APIMgtDBUtil.closeAllConnections(prepStmtGetConsumerKey, connection, rs);
APIMgtDBUtil.closeAllConnections(prepStmt, null, rs);
APIMgtDBUtil.closeAllConnections(deleteApp, null, null);
APIMgtDBUtil.closeAllConnections(deleteAppKey, null, null);
APIMgtDBUtil.closeAllConnections(deleteMappingQuery, null, null);
APIMgtDBUtil.closeAllConnections(deleteRegistrationQuery, null, null);
APIMgtDBUtil.closeAllConnections(deleteSubscription, null, null);
APIMgtDBUtil.closeAllConnections(deleteDomainApp, null, null);
APIMgtDBUtil.closeAllConnections(deleteAppKey, null, null);
APIMgtDBUtil.closeAllConnections(deleteApp, null, null);
}
}
use of org.wso2.carbon.apimgt.api.model.KeyManager in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method getApplicationKeyMapping.
/*
* @param consumerKey : consumer key of an application
* @return {@link ApplicationKeyMapping}
*
* */
public ApplicationKeyMapping getApplicationKeyMapping(String consumerKey, String keymanager, String tenantDomain) {
try (Connection conn = APIMgtDBUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(SubscriptionValidationSQLConstants.GET_AM_KEY_MAPPING_BY_CONSUMER_KEY_SQL)) {
ps.setString(1, consumerKey);
ps.setString(2, keymanager);
ps.setString(3, tenantDomain);
try (ResultSet resultSet = ps.executeQuery()) {
if (resultSet.next()) {
ApplicationKeyMapping keyMapping = new ApplicationKeyMapping();
keyMapping.setApplicationId(resultSet.getInt("APPLICATION_ID"));
keyMapping.setConsumerKey(resultSet.getString("CONSUMER_KEY"));
keyMapping.setKeyType(resultSet.getString("KEY_TYPE"));
keyMapping.setKeyManager(resultSet.getString("KEY_MANAGER"));
keyMapping.setApplicationUUID(resultSet.getString("UUID"));
return keyMapping;
}
}
} catch (SQLException e) {
log.error("Error in loading Application Key Mapping for consumer key : " + consumerKey, e);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.KeyManager in project carbon-apimgt by wso2.
the class APIProviderImplTest method testEmailSentWhenPropergateAPIStatusChangeToGateways.
@Test
public void testEmailSentWhenPropergateAPIStatusChangeToGateways() throws Exception {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.0");
API api = new API(apiId);
api.setContext("/test");
api.setStatus(APIConstants.CREATED);
api.setOrganization("carbon.super");
ApiMgtDAO apiMgtDAO = Mockito.mock(ApiMgtDAO.class);
Mockito.when(ApiMgtDAO.getInstance()).thenReturn(apiMgtDAO);
TestUtils.mockRegistryAndUserRealm(-1);
Resource resource = PowerMockito.mock(Resource.class);
JSONObject tenantConfig = PowerMockito.mock(JSONObject.class);
NotificationExecutor notificationExecutor = PowerMockito.mock(NotificationExecutor.class);
NotificationDTO notificationDTO = PowerMockito.mock(NotificationDTO.class);
UserRegistry configRegistry = PowerMockito.mock(UserRegistry.class);
RegistryService registryService = PowerMockito.mock(RegistryService.class);
Mockito.when(apimgtDAO.addAPI(api, -1, "testOrg")).thenReturn(1);
Mockito.doNothing().when(apimgtDAO).addURITemplates(1, api, -1);
Mockito.doNothing().when(keyManager).attachResourceScopes(api, api.getUriTemplates());
Mockito.when(artifactManager.newGovernanceArtifact(Matchers.any(QName.class))).thenReturn(artifact);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
Map<String, String> failedToPubGWEnv = new HashMap<String, String>();
APIManagerConfigurationService configurationService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService()).thenReturn(configurationService);
PowerMockito.when(configurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
PowerMockito.when(apiManagerConfiguration.getApiRecommendationEnvironment()).thenReturn(null);
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO, null, null);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
apiProvider.addAPI(api);
PowerMockito.when(APIUtil.replaceEmailDomain(apiId.getProviderName())).thenReturn("admin");
PowerMockito.when(APIUtil.replaceEmailDomainBack(api.getId().getProviderName())).thenReturn("admin");
PowerMockito.when(apimgtDAO.getPublishedDefaultVersion(api.getId())).thenReturn("1.0.0");
// Change to PUBLISHED state
// Existing APIs of the provider
API api1 = new API(new APIIdentifier("admin", "API1", "0.0.5"));
api1.setStatus(APIConstants.PUBLISHED);
API api2 = new API(new APIIdentifier("admin", "API2", "1.0.0"));
prepareForGetAPIsByProvider(artifactManager, apiProvider, "admin", api1, api2);
PowerMockito.when(registryService.getConfigSystemRegistry(-1)).thenReturn(configRegistry);
Mockito.when(resource.getContent()).thenReturn(getTenantConfigContent());
PowerMockito.when(tenantConfig.get(NotifierConstants.NOTIFICATIONS_ENABLED)).thenReturn("true");
PowerMockito.when(tenantConfig.get(APIConstants.EXTENSION_HANDLER_POSITION)).thenReturn("bottom");
PowerMockito.whenNew(NotificationDTO.class).withAnyArguments().thenReturn(notificationDTO);
PowerMockito.whenNew(NotificationExecutor.class).withAnyArguments().thenReturn(notificationExecutor);
APIManagerConfigurationService amConfigService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration amConfig = Mockito.mock(APIManagerConfiguration.class);
PowerMockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(amConfigService);
PowerMockito.when(amConfigService.getAPIManagerConfiguration()).thenReturn(amConfig);
PowerMockito.when(amConfig.getFirstProperty(APIConstants.API_GATEWAY_TYPE)).thenReturn(APIConstants.API_GATEWAY_TYPE_SYNAPSE);
UserRegistry registry = Mockito.mock(UserRegistry.class);
PowerMockito.when(registryService.getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, -1234)).thenReturn(registry);
PowerMockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
PowerMockito.when(tenantManager.getTenantId(Matchers.anyString())).thenReturn(-1234);
apiProvider.propergateAPIStatusChangeToGateways(APIConstants.PUBLISHED, api);
// Mockito.verify(notificationExecutor).sendAsyncNotifications(notificationDTO); Not valid. notification logic moved outside
}
use of org.wso2.carbon.apimgt.api.model.KeyManager in project carbon-apimgt by wso2.
the class APIProviderImplTest method testAddAPI.
@Test
public void testAddAPI() throws Exception {
APIIdentifier apiId = new APIIdentifier("admin", "API1", "1.0.1");
API api = new API(apiId);
api.setContext("/test");
api.setStatus(APIConstants.CREATED);
api.setOrganization("carbon.super");
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apiPersistenceInstance, apimgtDAO, scopesDAO);
Mockito.when(artifactManager.newGovernanceArtifact(any(QName.class))).thenReturn(artifact);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
UserRegistry userRegistry = Mockito.mock(UserRegistry.class);
RegistryService registryService = Mockito.mock(RegistryService.class);
ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
RealmService realmService = Mockito.mock(RealmService.class);
TenantManager tenantManager = Mockito.mock(TenantManager.class);
Mockito.when(artifactManager.newGovernanceArtifact(Matchers.any(QName.class))).thenReturn(artifact);
Mockito.when(APIUtil.createAPIArtifactContent(artifact, api)).thenReturn(artifact);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getRegistryService()).thenReturn(registryService);
Mockito.when(registryService.getConfigSystemRegistry(Mockito.anyInt())).thenReturn(userRegistry);
Mockito.when(serviceReferenceHolder.getRealmService()).thenReturn(realmService);
Mockito.when(realmService.getTenantManager()).thenReturn(tenantManager);
Mockito.when(apimgtDAO.addAPI(api, -1234, "testOrg")).thenReturn(1);
Mockito.doNothing().when(apimgtDAO).addURITemplates(1, api, -1234);
Mockito.doNothing().when(keyManager).attachResourceScopes(api, api.getUriTemplates());
PublisherAPI publisherAPI = Mockito.mock(PublisherAPI.class);
PowerMockito.when(apiPersistenceInstance.addAPI(any(Organization.class), any(PublisherAPI.class))).thenReturn(publisherAPI);
try {
apiProvider.addAPI(api);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
Aggregations