@ -87,6 +87,7 @@ import java.io.InputStream;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.Arrays ;
import java.util.Collection ;
import java.util.Collection ;
import java.util.Collections ;
import java.util.HashMap ;
import java.util.HashMap ;
import java.util.HashSet ;
import java.util.HashSet ;
import java.util.LinkedList ;
import java.util.LinkedList ;
@ -135,79 +136,126 @@ public class ApplicationManagerImpl implements ApplicationManager {
int tenantId = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( true ) ;
int tenantId = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getTenantId ( true ) ;
String userName = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getUsername ( ) ;
String userName = PrivilegedCarbonContext . getThreadLocalCarbonContext ( ) . getUsername ( ) ;
ApplicationDTO applicationDTO ;
Application application ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Application create request is received for the tenant : " + tenantId + " From" + " the user : "
log . debug ( "Application create request is received for the tenant : " + tenantId + " From" + " the user : "
+ userName ) ;
+ userName ) ;
}
}
ApplicationDTO applicationDTO ;
List < String > unrestrictedRoles ;
Optional < CategoryDTO > category ;
List < String > tags ;
//validating and verifying application data
try {
try {
ConnectionManagerUtil . openDBConnection ( ) ;
applicationDTO = appWrapperToAppDTO ( applicationWrapper ) ;
applicationDTO = appWrapperToAppDTO ( applicationWrapper ) ;
ApplicationReleaseDTO initialApplicationReleaseDTO = applicationDTO . getApplicationReleaseDTOs ( ) . get ( 0 ) ;
unrestrictedRoles = applicationWrapper . getUnrestrictedRoles ( ) ;
applicationDTO . getApplicationReleaseDTOs ( ) . clear ( ) ;
tags = applicationWrapper . getTags ( ) ;
ApplicationReleaseDTO applicationReleaseDTO = addApplicationReleaseArtifacts ( applicationDTO . getType ( ) ,
if ( unrestrictedRoles ! = null & & ! unrestrictedRoles . isEmpty ( ) ) {
applicationWrapper . getDeviceType ( ) , initialApplicationReleaseDTO , applicationArtifact , false ) ;
if ( ! isValidRestrictedRole ( unrestrictedRoles ) ) {
applicationDTO . getApplicationReleaseDTOs ( ) . add ( addImageArtifacts ( applicationReleaseDTO , applicationArtifact ) ) ;
String msg = "Unrestricted role list contain role/roles which are not in the user store." ;
} catch ( UnexpectedServerErrorException e ) {
String msg = "Error occurred when getting Device Type data." ;
log . error ( msg ) ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg , e ) ;
throw new ApplicationManagementException ( msg ) ;
} catch ( ResourceManagementException e ) {
}
String msg = "Error Occured when uploading artifacts of the application: " + applicationWrapper . getName ( ) ;
if ( ! hasUserRole ( unrestrictedRoles , userName ) ) {
String msg = "You are trying to restrict the visibility of the application for a role set, but "
+ "in order to perform the action at least one role should be assigned to user: "
+ userName ;
log . error ( msg ) ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg , e ) ;
throw new BadRequestException ( msg ) ;
}
}
}
try {
List < ApplicationReleaseDTO > applicationReleaseEntities = new ArrayList < > ( ) ;
ApplicationReleaseDTO applicationReleaseDTO ;
Filter filter = new Filter ( ) ;
Filter filter = new Filter ( ) ;
filter . setFullMatch ( true ) ;
filter . setFullMatch ( true ) ;
filter . setAppName ( applicationDTO . getName ( ) . trim ( ) ) ;
filter . setAppName ( applicationDTO . getName ( ) . trim ( ) ) ;
filter . setOffset ( 0 ) ;
filter . setOffset ( 0 ) ;
filter . setLimit ( 1 ) ;
filter . setLimit ( 1 ) ;
ConnectionManagerUtil . beginDBTransaction ( ) ;
List < ApplicationDTO > applicationList = applicationDAO
List < ApplicationDTO > applicationList = applicationDAO
. getApplications ( filter , applicationDTO . getDeviceTypeId ( ) , tenantId ) ;
. getApplications ( filter , applicationDTO . getDeviceTypeId ( ) , tenantId ) ;
if ( ! applicationList . isEmpty ( ) ) {
if ( ! applicationList . isEmpty ( ) ) {
String msg =
String msg = "Already an application registered with same name - " + applicationList . get ( 0 ) . getName ( )
"Already an application registered with same name - " + applicationList . get ( 0 )
+ " for the device type " + applicationWrapper . getDeviceType ( ) ;
. getName ( ) ;
log . error ( msg ) ;
log . error ( msg ) ;
throw new RequestValidatingException ( msg ) ;
throw new RequestValidatingException ( msg ) ;
}
}
List < CategoryDTO > registeredCategories = this . applicationDAO . getAllCategories ( tenantId ) ;
String categoryName = applicationWrapper . getAppCategory ( ) ;
if ( registeredCategories . isEmpty ( ) ) {
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
String msg = "Registered application category set is empty. Since it is mandatory to add application "
+ "category when adding new application, registered application category list shouldn't be null." ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg ) ;
}
category = registeredCategories . stream ( ) . filter ( obj - > obj . getCategoryName ( ) . equals ( categoryName ) )
. findAny ( ) ;
if ( ! category . isPresent ( ) ) {
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
String msg = "Request contains invalid category: " + categoryName ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg ) ;
}
} catch ( DBConnectionException e ) {
String msg = "Error occurred while getting database connection." ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg , e ) ;
} catch ( UnexpectedServerErrorException e ) {
String msg = "Error occurred when getting Device Type data." ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg , e ) ;
} catch ( ApplicationManagementDAOException e ) {
String msg = "Error occurred while getting data which is related to application. application name: "
+ applicationWrapper . getName ( ) + " and application type: " + applicationWrapper . getType ( ) ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg , e ) ;
} catch ( UserStoreException e ) {
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
String msg = "Error occurred when validating the unrestricted roles given for the application" ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg , e ) ;
} finally {
ConnectionManagerUtil . closeDBConnection ( ) ;
}
//uploading application artifacts
try {
ApplicationReleaseDTO applicationReleaseDTO = applicationDTO . getApplicationReleaseDTOs ( ) . get ( 0 ) ;
applicationReleaseDTO = addApplicationReleaseArtifacts ( applicationDTO . getType ( ) ,
applicationWrapper . getDeviceType ( ) , applicationReleaseDTO , applicationArtifact , false ) ;
applicationReleaseDTO = addImageArtifacts ( applicationReleaseDTO , applicationArtifact ) ;
applicationDTO . getApplicationReleaseDTOs ( ) . clear ( ) ;
applicationDTO . getApplicationReleaseDTOs ( ) . add ( applicationReleaseDTO ) ;
} catch ( ResourceManagementException e ) {
String msg = "Error Occured when uploading artifacts of the application: " + applicationWrapper . getName ( ) ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg , e ) ;
}
//insert application data into databse
ApplicationStorageManager applicationStorageManager = Util . getApplicationStorageManager ( ) ;
ApplicationReleaseDTO applicationReleaseDTO = applicationDTO . getApplicationReleaseDTOs ( ) . get ( 0 ) ;
try {
List < ApplicationReleaseDTO > applicationReleaseEntities = new ArrayList < > ( ) ;
ConnectionManagerUtil . beginDBTransaction ( ) ;
// Insert to application table
// Insert to application table
int appId = this . applicationDAO . createApplication ( applicationDTO , tenantId ) ;
int appId = this . applicationDAO . createApplication ( applicationDTO , tenantId ) ;
if ( appId = = - 1 ) {
if ( appId = = - 1 ) {
log . error ( "ApplicationDTO creation is Failed" ) ;
log . error ( "Application data storing is Failed. ") ;
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
deleteApplicationArtifacts ( Collections . singletonList ( applicationReleaseDTO . getAppHashValue ( ) ) ) ;
return null ;
return null ;
} else {
} else {
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "New ApplicationDTO entry added to AP_APP table. App Id:" + appId ) ;
log . debug ( "New ApplicationDTO entry added to AP_APP table. App Id:" + appId ) ;
}
}
//adding application unrestricted roles
//adding application unrestricted roles
List < String > unrestrictedRoles = applicationWrapper . getUnrestrictedRoles ( ) ;
if ( unrestrictedRoles ! = null & & ! unrestrictedRoles . isEmpty ( ) ) {
if ( ! unrestrictedRoles . isEmpty ( ) ) {
if ( ! isValidRestrictedRole ( unrestrictedRoles ) ) {
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
String msg = "Unrestricted role list contain role/roles which are not in the user store." ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg ) ;
}
if ( ! hasUserRole ( unrestrictedRoles , userName ) ) {
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
String msg =
"You are trying to restrict the visibility of the application for a role set, but in order to perform the action at least one role should be assigned to user: "
+ userName ;
log . error ( msg ) ;
throw new BadRequestException ( msg ) ;
}
this . visibilityDAO . addUnrestrictedRoles ( unrestrictedRoles , appId , tenantId ) ;
this . visibilityDAO . addUnrestrictedRoles ( unrestrictedRoles , appId , tenantId ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "New restricted roles to app ID mapping added to AP_UNRESTRICTED_ROLE table."
log . debug ( "New restricted roles to app ID mapping added to AP_UNRESTRICTED_ROLE table."
@ -215,25 +263,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
}
}
}
List < CategoryDTO > registeredCategories = this . applicationDAO . getAllCategories ( tenantId ) ;
String categoryName = applicationWrapper . getAppCategory ( ) ;
Optional < CategoryDTO > category = registeredCategories . stream ( )
. filter ( obj - > obj . getCategoryName ( ) . equals ( categoryName ) ) . findAny ( ) ;
if ( registeredCategories . isEmpty ( ) ) {
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
String msg = "Registered application category set is empty category: " + categoryName ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg ) ;
}
if ( ! category . isPresent ( ) ) {
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
String msg = "Request contains invalid category: " + categoryName ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg ) ;
}
/ *
/ *
In current flow , allow to add one category for an application . If it is required to add multiple
In current flow , allow to add one category for an application . If it is required to add multiple
categories DAO layer is implemented to match with that requirement . Hence logic is also implemented
categories DAO layer is implemented to match with that requirement . Hence logic is also implemented
@ -244,8 +273,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
this . applicationDAO . addCategoryMapping ( categoryIds , appId , tenantId ) ;
this . applicationDAO . addCategoryMapping ( categoryIds , appId , tenantId ) ;
//adding application tags
//adding application tags
List < String > tags = applicationWrapper . getTags ( ) ;
if ( tags ! = null & & ! tags . isEmpty ( ) ) {
if ( ! tags . isEmpty ( ) ) {
List < TagDTO > registeredTags = applicationDAO . getAllTags ( tenantId ) ;
List < TagDTO > registeredTags = applicationDAO . getAllTags ( tenantId ) ;
List < String > registeredTagNames = new ArrayList < > ( ) ;
List < String > registeredTagNames = new ArrayList < > ( ) ;
List < Integer > tagIds = new ArrayList < > ( ) ;
List < Integer > tagIds = new ArrayList < > ( ) ;
@ -277,36 +305,49 @@ public class ApplicationManagerImpl implements ApplicationManager {
log . debug ( "Creating a new release. App Id:" + appId ) ;
log . debug ( "Creating a new release. App Id:" + appId ) ;
}
}
String initialLifecycleState = lifecycleStateManager . getInitialState ( ) ;
String initialLifecycleState = lifecycleStateManager . getInitialState ( ) ;
applicationReleaseDTO = applicationDTO . getApplicationReleaseDTOs ( ) . get ( 0 ) ;
applicationReleaseDTO . setCurrentState ( initialLifecycleState ) ;
applicationReleaseDTO . setCurrentState ( initialLifecycleState ) ;
applicationReleaseDTO = this . applicationReleaseDAO . createRelease ( applicationReleaseDTO , appId , tenantId ) ;
applicationReleaseDTO = this . applicationReleaseDAO
. createRelease ( applicationReleaseDTO , appId , tenantId ) ;
LifecycleStateDTO lifecycleStateDTO = getLifecycleStateInstance ( initialLifecycleState ,
LifecycleStateDTO lifecycleStateDTO = getLifecycleStateInstance ( initialLifecycleState ,
initialLifecycleState ) ;
initialLifecycleState ) ;
this . lifecycleStateDAO
this . lifecycleStateDAO
. addLifecycleState ( lifecycleStateDTO , appId , applicationReleaseDTO . getUuid ( ) , tenantId ) ;
. addLifecycleState ( lifecycleStateDTO , appId , applicationReleaseDTO . getUuid ( ) , tenantId ) ;
applicationReleaseEntities . add ( applicationReleaseDTO ) ;
applicationReleaseEntities . add ( applicationReleaseDTO ) ;
applicationDTO . setApplicationReleaseDTOs ( applicationReleaseEntities ) ;
applicationDTO . setApplicationReleaseDTOs ( applicationReleaseEntities ) ;
application = appDtoToAppResponse ( applicationDTO ) ;
Application application = appDtoToAppResponse ( applicationDTO ) ;
ConnectionManagerUtil . commitDBTransaction ( ) ;
ConnectionManagerUtil . commitDBTransaction ( ) ;
}
return application ;
return application ;
}
} catch ( LifeCycleManagementDAOException e ) {
} catch ( LifeCycleManagementDAOException e ) {
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
String msg = "Error occurred while adding lifecycle state. application name: " + applicationWrapper . getName ( )
String msg =
"Error occurred while adding lifecycle state. application name: " + applicationWrapper . getName ( )
+ " application type: is " + applicationWrapper . getType ( ) ;
+ " application type: is " + applicationWrapper . getType ( ) ;
log . error ( msg ) ;
log . error ( msg ) ;
try {
applicationStorageManager . deleteAllApplicationReleaseArtifacts (
Collections . singletonList ( applicationReleaseDTO . getAppHashValue ( ) ) ) ;
} catch ( ApplicationStorageManagementException ex ) {
String errorLog =
"Error occurred when deleting application artifacts. Application artifacts are tried to "
+ "delete because of lifecycle state adding issue in the application creating operation." ;
log . error ( errorLog ) ;
throw new ApplicationManagementException ( errorLog , e ) ;
}
throw new ApplicationManagementException ( msg , e ) ;
throw new ApplicationManagementException ( msg , e ) ;
} catch ( ApplicationManagementDAOException e ) {
} catch ( ApplicationManagementDAOException e ) {
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
String msg = "Error occurred while adding application or application release. application name: "
String msg = "Error occurred while adding application or application release. application name: "
+ applicationWrapper . getName ( ) + " application type: " + applicationWrapper . getType ( ) ;
+ applicationWrapper . getName ( ) + " application type: " + applicationWrapper . getType ( ) ;
log . error ( msg ) ;
log . error ( msg ) ;
deleteApplicationArtifacts ( Collections . singletonList ( applicationReleaseDTO . getAppHashValue ( ) ) ) ;
throw new ApplicationManagementException ( msg , e ) ;
throw new ApplicationManagementException ( msg , e ) ;
} catch ( LifecycleManagementException e ) {
} catch ( LifecycleManagementException e ) {
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
String msg = "Error occurred when getting initial lifecycle state. application name: " + applicationWrapper
String msg = "Error occurred when getting initial lifecycle state. application name: " + applicationWrapper
. getName ( ) + " application type: is " + applicationWrapper . getType ( ) ;
. getName ( ) + " application type: is " + applicationWrapper . getType ( ) ;
log . error ( msg ) ;
log . error ( msg ) ;
deleteApplicationArtifacts ( Collections . singletonList ( applicationReleaseDTO . getAppHashValue ( ) ) ) ;
throw new ApplicationManagementException ( msg , e ) ;
throw new ApplicationManagementException ( msg , e ) ;
} catch ( DBConnectionException e ) {
} catch ( DBConnectionException e ) {
String msg = "Error occurred while getting database connection." ;
String msg = "Error occurred while getting database connection." ;
@ -314,24 +355,34 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ApplicationManagementException ( msg , e ) ;
throw new ApplicationManagementException ( msg , e ) ;
} catch ( VisibilityManagementDAOException e ) {
} catch ( VisibilityManagementDAOException e ) {
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
String msg = "Error occurred while adding unrestricted roles. application name: " + applicationWrapper . getName ( )
String msg =
"Error occurred while adding unrestricted roles. application name: " + applicationWrapper . getName ( )
+ " application type: " + applicationWrapper . getType ( ) ;
+ " application type: " + applicationWrapper . getType ( ) ;
log . error ( msg ) ;
log . error ( msg ) ;
deleteApplicationArtifacts ( Collections . singletonList ( applicationReleaseDTO . getAppHashValue ( ) ) ) ;
throw new ApplicationManagementException ( msg , e ) ;
throw new ApplicationManagementException ( msg , e ) ;
} catch ( TransactionManagementException e ) {
} catch ( TransactionManagementException e ) {
String msg = "Error occurred while disabling AutoCommit." ;
String msg = "Error occurred while disabling AutoCommit." ;
log . error ( msg ) ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg , e ) ;
throw new ApplicationManagementException ( msg , e ) ;
} catch ( UserStoreException e ) {
ConnectionManagerUtil . rollbackDBTransaction ( ) ;
String msg = "Error occurred when validating the unrestricted roles given for the application" ;
log . error ( msg ) ;
throw new ApplicationManagementException ( msg , e ) ;
} finally {
} finally {
ConnectionManagerUtil . closeDBConnection ( ) ;
ConnectionManagerUtil . closeDBConnection ( ) ;
}
}
}
}
private void deleteApplicationArtifacts ( List < String > directoryPaths ) throws ApplicationManagementException {
ApplicationStorageManager applicationStorageManager = Util . getApplicationStorageManager ( ) ;
try {
applicationStorageManager . deleteAllApplicationReleaseArtifacts ( directoryPaths ) ;
} catch ( ApplicationStorageManagementException e ) {
String errorLog = "Error occurred when deleting application artifacts. directory paths: ." + directoryPaths
. toString ( ) ;
log . error ( errorLog ) ;
throw new ApplicationManagementException ( errorLog , e ) ;
}
}
private ApplicationReleaseDTO addApplicationReleaseArtifacts ( String applicationType , String deviceType ,
private ApplicationReleaseDTO addApplicationReleaseArtifacts ( String applicationType , String deviceType ,
ApplicationReleaseDTO applicationReleaseDTO , ApplicationArtifact applicationArtifact , boolean isNewRelease )
ApplicationReleaseDTO applicationReleaseDTO , ApplicationArtifact applicationArtifact , boolean isNewRelease )
throws ResourceManagementException , ApplicationManagementException {
throws ResourceManagementException , ApplicationManagementException {
@ -576,9 +627,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
try {
try {
//set default values
//set default values
if ( StringUtils . isEmpty ( filter . getSortBy ( ) ) ) {
filter . setSortBy ( "ASC" ) ;
}
if ( filter . getLimit ( ) = = 0 ) {
if ( filter . getLimit ( ) = = 0 ) {
filter . setLimit ( 20 ) ;
filter . setLimit ( 20 ) ;
}
}
@ -586,16 +634,15 @@ public class ApplicationManagerImpl implements ApplicationManager {
if ( ! StringUtils . isEmpty ( deviceTypename ) ) {
if ( ! StringUtils . isEmpty ( deviceTypename ) ) {
deviceType = getDeviceTypeData ( deviceTypename ) ;
deviceType = getDeviceTypeData ( deviceTypename ) ;
}
}
if ( deviceType = = null ) {
deviceType = new DeviceType ( ) ;
deviceType . setId ( - 1 ) ;
}
ConnectionManagerUtil . openDBConnection ( ) ;
ConnectionManagerUtil . openDBConnection ( ) ;
validateFilter ( filter ) ;
validateFilter ( filter ) ;
if ( deviceType = = null ) {
appDTOs = applicationDAO . getApplications ( filter , 0 , tenantId ) ;
} else {
appDTOs = applicationDAO . getApplications ( filter , deviceType . getId ( ) , tenantId ) ;
appDTOs = applicationDAO . getApplications ( filter , deviceType . getId ( ) , tenantId ) ;
}
//todo as a performance improvement get these data from DB. Consider where in clause.
for ( ApplicationDTO app : appDTOs ) {
for ( ApplicationDTO app : appDTOs ) {
boolean isSearchingApp = true ;
boolean isSearchingApp = true ;
List < String > filteringTags = filter . getTags ( ) ;
List < String > filteringTags = filter . getTags ( ) ;
@ -905,6 +952,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
}
private String [ ] getRoleNames ( ) throws UserStoreException {
private String [ ] getRoleNames ( ) throws UserStoreException {
//todo check role by role
UserRealm userRealm = CarbonContext . getThreadLocalCarbonContext ( ) . getUserRealm ( ) ;
UserRealm userRealm = CarbonContext . getThreadLocalCarbonContext ( ) . getUserRealm ( ) ;
if ( userRealm ! = null ) {
if ( userRealm ! = null ) {
return userRealm . getUserStoreManager ( ) . getRoleNames ( ) ;
return userRealm . getUserStoreManager ( ) . getRoleNames ( ) ;