2019-01-10 12:23:59 +01:00
package org.gcube.storagehub ;
import java.io.InputStream ;
import java.io.StringWriter ;
import java.net.URL ;
import java.util.List ;
2021-12-07 14:31:30 +01:00
import org.gcube.common.authorization.utils.manager.SecretManager ;
2022-02-28 14:33:48 +01:00
import org.gcube.common.authorization.utils.manager.SecretManagerProvider ;
2019-01-10 12:23:59 +01:00
import org.gcube.common.scope.impl.ScopeBean ;
import org.gcube.common.scope.impl.ScopeBean.Type ;
import org.gcube.common.storagehub.client.dsl.ContainerType ;
import org.gcube.common.storagehub.client.dsl.FileContainer ;
import org.gcube.common.storagehub.client.dsl.FolderContainer ;
import org.gcube.common.storagehub.client.dsl.ItemContainer ;
import org.gcube.common.storagehub.client.dsl.ListResolver ;
import org.gcube.common.storagehub.client.dsl.ListResolverTyped ;
import org.gcube.common.storagehub.client.dsl.StorageHubClient ;
import org.gcube.common.storagehub.model.Metadata ;
2019-04-08 16:20:02 +02:00
import org.gcube.common.storagehub.model.exceptions.StorageHubException ;
2019-01-10 12:23:59 +01:00
import org.gcube.common.storagehub.model.items.Item ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
public class StorageHubManagement {
private static final Logger logger = LoggerFactory . getLogger ( StorageHubManagement . class ) ;
protected MetadataMatcher metadataMatcher ;
protected final StorageHubClient storageHubClient ;
2020-01-22 17:34:33 +01:00
protected FileContainer persitedFile ;
2019-01-10 12:23:59 +01:00
protected String mimeType ;
public StorageHubManagement ( ) {
storageHubClient = new StorageHubClient ( ) ;
}
2023-07-18 16:04:45 +02:00
public void setMetadataMatcher ( MetadataMatcher checkMetadata ) {
2019-01-10 12:23:59 +01:00
this . metadataMatcher = checkMetadata ;
}
public String getMimeType ( ) {
return mimeType ;
}
2020-01-22 17:34:33 +01:00
public FileContainer getPersistedFile ( ) {
return persitedFile ;
2019-01-10 12:23:59 +01:00
}
2019-04-08 16:20:02 +02:00
protected void recursiveList ( FolderContainer folder , int level ) throws StorageHubException {
2019-01-10 12:23:59 +01:00
ListResolverTyped listResolverTyped = folder . list ( ) ;
List < ItemContainer < ? extends Item > > containers = listResolverTyped . includeHidden ( ) . getContainers ( ) ;
for ( ItemContainer < ? extends Item > itemContainer : containers ) {
ContainerType containerType = itemContainer . getType ( ) ;
2023-07-04 15:22:44 +02:00
logItem ( itemContainer , level ) ;
2019-01-10 12:23:59 +01:00
switch ( containerType ) {
case FOLDER :
FolderContainer folderContainer = ( FolderContainer ) itemContainer ;
recursiveList ( folderContainer , level + 1 ) ;
break ;
case FILE :
break ;
case GENERIC_ITEM :
break ;
default :
break ;
}
}
}
protected FolderContainer getWorkspaceRoot ( ) {
2021-06-10 16:22:25 +02:00
return storageHubClient . getWSRoot ( ) ;
2019-01-10 12:23:59 +01:00
}
protected FolderContainer getOrCreateFolder ( FolderContainer parent , String name , String description , boolean hidden )
throws Exception {
FolderContainer destinationFolder = null ;
ListResolverTyped listResolverTyped = parent . list ( ) ;
List < ItemContainer < ? extends Item > > containers = listResolverTyped . includeHidden ( ) . getContainers ( ) ;
for ( ItemContainer < ? extends Item > itemContainer : containers ) {
if ( itemContainer instanceof FolderContainer ) {
if ( itemContainer . get ( ) . getName ( ) . compareTo ( name ) = = 0 ) {
destinationFolder = ( FolderContainer ) itemContainer ;
}
}
}
if ( destinationFolder = = null ) {
if ( hidden ) {
destinationFolder = parent . newHiddenFolder ( name , description ) ;
} else {
destinationFolder = parent . newFolder ( name , description ) ;
}
}
return destinationFolder ;
}
protected FolderContainer getContextFolder ( ) throws Exception {
FolderContainer destinationFolder = getWorkspaceRoot ( ) ;
2022-02-28 14:33:48 +01:00
String currentContext = SecretManagerProvider . instance . get ( ) . getContext ( ) ;
2019-01-10 12:23:59 +01:00
ScopeBean scopeBean = new ScopeBean ( currentContext ) ;
switch ( scopeBean . type ( ) ) {
case INFRASTRUCTURE :
case VO :
String folderName = currentContext . replaceFirst ( " / " , " " ) . replace ( " / " , " _ " ) ;
destinationFolder = getOrCreateFolder ( destinationFolder , folderName , " " , false ) ;
break ;
case VRE :
2021-06-10 16:22:25 +02:00
destinationFolder = storageHubClient . openVREFolder ( ) ;
2019-01-10 12:23:59 +01:00
break ;
default :
break ;
}
return destinationFolder ;
}
public FolderContainer getApplicationFolder ( ) throws Exception {
FolderContainer destinationFolder = getContextFolder ( ) ;
2022-02-28 14:33:48 +01:00
SecretManager secretManager = SecretManagerProvider . instance . get ( ) ;
String currentContext = secretManager . getContext ( ) ;
2019-01-10 12:23:59 +01:00
ScopeBean scopeBean = new ScopeBean ( currentContext ) ;
if ( scopeBean . is ( Type . VRE ) ) {
2022-02-28 14:33:48 +01:00
String username = secretManager . getUser ( ) . getUsername ( ) ;
2019-01-10 12:23:59 +01:00
destinationFolder = getOrCreateFolder ( destinationFolder , username , " Folder Created for user/application " , true ) ;
}
return destinationFolder ;
}
public FolderContainer getDestinationFolder ( String mimeType ) throws Exception {
FolderContainer destinationFolder = getApplicationFolder ( ) ;
String [ ] splittedMimeType = mimeType . split ( " / " ) ;
for ( String name : splittedMimeType ) {
destinationFolder = getOrCreateFolder ( destinationFolder , name , " Folder Created using mimetype " , false ) ;
}
2019-12-19 16:21:01 +01:00
return destinationFolder ;
2019-01-10 12:23:59 +01:00
}
2023-07-20 15:21:45 +02:00
protected boolean isPersistedFile ( FileContainer fileContainer , String filename ) {
2019-01-10 12:23:59 +01:00
// Checking if the file is already a persisted file of the workspace
2023-07-20 15:21:45 +02:00
if ( fileContainer . get ( ) . getName ( ) . startsWith ( filename ) ) {
if ( metadataMatcher ! = null ) {
Metadata metadata = fileContainer . get ( ) . getMetadata ( ) ;
return metadataMatcher . check ( metadata ) ;
} else {
return true ;
}
2019-01-10 12:23:59 +01:00
}
2023-07-20 15:21:45 +02:00
return false ;
2019-01-10 12:23:59 +01:00
}
2023-07-04 15:22:44 +02:00
protected void logItem ( ItemContainer < ? extends Item > itemContainer ) {
logItem ( itemContainer , 0 ) ;
}
protected void logItem ( ItemContainer < ? extends Item > itemContainer , int level ) {
StringWriter indent = new StringWriter ( level + 1 ) ;
for ( int i = 0 ; i < level + 1 ; i + + ) {
indent . append ( '-' ) ;
}
indent . append ( " " ) ;
Item item = itemContainer . get ( ) ;
logger . debug ( " {}{} {} (ID:{}){} " , indent . toString ( ) , itemContainer . getType ( ) , item . getName ( ) , itemContainer . getId ( ) , item . isHidden ( ) ? " (hidden) " : " " ) ;
}
2019-01-10 12:23:59 +01:00
protected void tree ( FolderContainer folderContainer ) throws Exception {
2023-07-04 15:22:44 +02:00
logItem ( folderContainer , 0 ) ;
recursiveList ( folderContainer , 1 ) ;
2019-01-10 12:23:59 +01:00
}
2023-07-18 16:04:45 +02:00
public URL persistFile ( InputStream inputStream , String fileName , String mimeType )
2019-01-10 12:23:59 +01:00
throws Exception {
this . mimeType = mimeType ;
FolderContainer destinationFolder = getDestinationFolder ( mimeType ) ;
2020-01-22 17:34:33 +01:00
persitedFile = destinationFolder . uploadFile ( inputStream , fileName ,
2019-01-10 12:23:59 +01:00
" This file has been created to ensure persistence " ) ;
2023-07-18 16:04:45 +02:00
if ( metadataMatcher ! = null ) {
persitedFile . setMetadata ( metadataMatcher . getMetadata ( ) ) ;
2019-01-10 12:23:59 +01:00
}
2020-01-22 17:34:33 +01:00
URL finalURL = persitedFile . getPublicLink ( ) ;
2019-01-10 12:23:59 +01:00
logger . debug ( " File persistence has been ensured. The file is available at {} " , finalURL ) ;
return finalURL ;
}
2020-01-22 17:34:33 +01:00
public FileContainer getPersistedFile ( String filename , String mimeType ) throws Exception {
2019-01-10 12:23:59 +01:00
FolderContainer destinationFolder = getDestinationFolder ( mimeType ) ;
ListResolver listResolver = destinationFolder . findByName ( filename ) ;
2019-12-19 16:21:01 +01:00
List < ItemContainer < ? extends Item > > itemContainers = listResolver . withMetadata ( ) . getContainers ( ) ;
2019-01-10 12:23:59 +01:00
for ( ItemContainer < ? extends Item > itemContainer : itemContainers ) {
if ( itemContainer . getType ( ) = = ContainerType . FILE ) {
2023-07-20 15:21:45 +02:00
if ( isPersistedFile ( ( FileContainer ) itemContainer , filename ) ) {
logger . debug ( " The file with mimetype {} and name {} was found in the expected folder (i.e. id:{}, path:{}) and the check on metadata succeded. The file is the one expected. " ,
mimeType , filename , destinationFolder . getId ( ) , destinationFolder . get ( ) . getPath ( ) ) ;
2020-01-22 17:34:33 +01:00
this . persitedFile = ( FileContainer ) itemContainer ;
return this . persitedFile ;
2023-07-20 15:18:03 +02:00
} else {
logger . warn ( " The file with mimetype {} and name {} was found in the expected folder (i.e. id:{}, path:{}) but the check on metadata failed. The file is not the one expected. " ,
mimeType , filename , destinationFolder . getId ( ) , destinationFolder . get ( ) . getPath ( ) ) ;
2019-01-10 12:23:59 +01:00
}
}
}
2023-07-18 16:04:45 +02:00
logger . warn ( " Unable to find file with mimetype {} and name {} in the expected folder (i.e. id:{}, path:{}) " ,
mimeType , filename , destinationFolder . getId ( ) , destinationFolder . get ( ) . getPath ( ) ) ;
return null ;
2020-01-22 17:34:33 +01:00
}
public void removePersistedFile ( String filename , String mimeType ) throws Exception {
2023-07-18 16:04:45 +02:00
persitedFile = getPersistedFile ( filename , mimeType ) ;
if ( persitedFile ! = null ) {
2023-07-20 15:21:45 +02:00
logger . info ( " Persited file with mimetype {} and name {} was found. Goign to remove it. " , mimeType , filename ) ;
2023-07-18 16:04:45 +02:00
persitedFile . delete ( ) ;
2020-01-22 17:34:33 +01:00
}
2019-01-10 12:23:59 +01:00
}
2023-07-18 16:04:45 +02:00
2019-01-10 12:23:59 +01:00
}