2018-10-12 16:20:46 +02:00
package org.gcube.datatransfer.resolver.services ;
2018-10-23 11:27:04 +02:00
import java.io.IOException ;
2018-10-12 16:20:46 +02:00
import java.io.InputStream ;
2021-09-03 10:39:38 +02:00
import java.util.Objects ;
2018-10-12 16:20:46 +02:00
2018-10-22 12:56:04 +02:00
import javax.servlet.http.HttpServletRequest ;
2018-10-18 18:24:49 +02:00
import javax.ws.rs.GET ;
2018-10-23 11:27:04 +02:00
import javax.ws.rs.HEAD ;
2018-10-12 16:20:46 +02:00
import javax.ws.rs.Path ;
import javax.ws.rs.PathParam ;
2018-10-19 11:40:52 +02:00
import javax.ws.rs.QueryParam ;
2018-12-14 14:26:37 +01:00
import javax.ws.rs.WebApplicationException ;
2018-10-22 12:56:04 +02:00
import javax.ws.rs.core.Context ;
2018-10-12 16:20:46 +02:00
import javax.ws.rs.core.Response ;
import javax.ws.rs.core.Response.ResponseBuilder ;
import javax.ws.rs.core.StreamingOutput ;
2018-10-23 11:27:04 +02:00
import org.apache.commons.io.IOUtils ;
import org.apache.http.HttpStatus ;
2018-10-12 16:20:46 +02:00
import org.gcube.contentmanagement.blobstorage.resource.MyFile ;
import org.gcube.contentmanagement.blobstorage.service.IClient ;
import org.gcube.contentmanager.storageclient.wrapper.AccessType ;
2021-09-02 15:44:35 +02:00
import org.gcube.contentmanager.storageclient.wrapper.BackendType ;
2018-10-19 17:55:31 +02:00
import org.gcube.contentmanager.storageclient.wrapper.MemoryType ;
2018-10-12 16:20:46 +02:00
import org.gcube.contentmanager.storageclient.wrapper.StorageClient ;
2018-11-15 16:21:31 +01:00
import org.gcube.datatransfer.resolver.ConstantsResolver ;
2019-10-02 12:43:35 +02:00
import org.gcube.datatransfer.resolver.ConstantsResolver.CONTENT_DISPOSITION_VALUE ;
2018-10-23 11:27:04 +02:00
import org.gcube.datatransfer.resolver.services.error.ExceptionManager ;
2019-04-02 15:19:52 +02:00
import org.gcube.datatransfer.resolver.storage.StorageClientInstance ;
2019-10-01 18:06:41 +02:00
import org.gcube.datatransfer.resolver.storage.StorageMetadataFile ;
2018-11-15 15:35:54 +01:00
import org.gcube.datatransfer.resolver.util.SingleFileStreamingOutput ;
2019-10-02 12:43:35 +02:00
import org.gcube.datatransfer.resolver.util.ValidateContentDisposition ;
2018-10-12 16:20:46 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2018-10-19 17:55:31 +02:00
2018-12-14 14:26:37 +01:00
2018-10-19 17:55:31 +02:00
/ * *
2018-12-14 14:26:37 +01:00
* The Class StorageIDResolver .
2018-10-19 17:55:31 +02:00
*
* @author Francesco Mangiacrapa at ISTI - CNR ( francesco . mangiacrapa @isti.cnr.it )
2018-12-14 14:26:37 +01:00
* Dec 14 , 2018
2018-10-19 17:55:31 +02:00
* /
2018-12-05 16:58:44 +01:00
@Path ( " storage " )
2018-10-22 15:49:19 +02:00
public class StorageIDResolver {
2018-10-12 16:20:46 +02:00
2018-10-19 17:55:31 +02:00
/ * *
*
* /
protected static final String STORAGEID_RESOLVER = " storageid-resolver " ;
private static final String STORAGE_ID = " storage-id " ;
2021-09-03 15:43:47 +02:00
private static final AccessType STORAGE_ACCESS_TYPE = AccessType . PUBLIC ;
private static final BackendType STORAGE_BACKEND_TYPE = BackendType . MongoDB ;
2019-10-02 12:43:35 +02:00
private static final Logger LOG = LoggerFactory . getLogger ( StorageIDResolver . class ) ;
2019-10-01 18:00:44 +02:00
2018-10-22 15:39:51 +02:00
private static String help = " https://wiki.gcube-system.org/gcube/URI_Resolver#STORAGE-ID_Resolver " ;
2021-09-03 10:39:38 +02:00
protected static IClient storageManagerClient ;
2021-09-03 11:13:41 +02:00
private static MemoryType memory ;
2018-10-19 17:55:31 +02:00
/ * *
2018-10-22 12:56:04 +02:00
* Gets the storage id .
2018-10-19 17:55:31 +02:00
*
2018-12-14 14:26:37 +01:00
* @param req the req
2018-10-22 12:56:04 +02:00
* @param storageId the storage id
2018-10-19 17:55:31 +02:00
* @param fileName the file name
* @param contentType the content type
* @param validation the validation
2018-10-22 12:56:04 +02:00
* @return the storage id
2018-12-14 14:26:37 +01:00
* @throws WebApplicationException the web application exception
2018-10-19 17:55:31 +02:00
* /
@GET
2019-02-19 15:18:19 +01:00
@Path ( " /{storage-id} " )
2018-12-14 14:26:37 +01:00
public Response getStorageId ( @Context HttpServletRequest req ,
@PathParam ( STORAGE_ID ) String storageId ,
@QueryParam ( ConstantsResolver . QUERY_PARAM_FILE_NAME ) String fileName ,
@QueryParam ( ConstantsResolver . QUERY_PARAM_CONTENT_TYPE ) String contentType ,
2019-10-02 12:43:35 +02:00
@QueryParam ( ConstantsResolver . QUERY_PARAM_CONTENTDISPOSITION ) String contentDisposition ,
@QueryParam ( ConstantsResolver . QUERY_PARAM_VALIDATION ) boolean validation ) throws WebApplicationException {
2018-12-14 14:26:37 +01:00
2019-10-02 12:43:35 +02:00
LOG . info ( this . getClass ( ) . getSimpleName ( ) + " GET starts... " ) ;
//Checking the optional parameter "Content-Disposition"
CONTENT_DISPOSITION_VALUE dispositionValue = CONTENT_DISPOSITION_VALUE . attachment ;
//Validating the Content-Disposition value
dispositionValue = ValidateContentDisposition . validValue ( req , this . getClass ( ) , help , contentDisposition ) ;
2018-12-14 14:26:37 +01:00
try {
if ( storageId = = null | | storageId . isEmpty ( ) ) {
2019-10-02 12:43:35 +02:00
LOG . error ( STORAGE_ID + " not found " ) ;
2018-12-14 14:26:37 +01:00
throw ExceptionManager . badRequestException ( req , " Missing mandatory path parameter " + STORAGE_ID , StorageIDResolver . class , help ) ;
}
2019-10-02 12:43:35 +02:00
return resolveStorageId ( req , storageId , fileName , contentType , dispositionValue , validation ) ;
2018-12-14 14:26:37 +01:00
} catch ( Exception e ) {
if ( ! ( e instanceof WebApplicationException ) ) {
//UNEXPECTED EXCEPTION managing it as WebApplicationException
String error = " Error occurred on resolving the Storage ID: " + storageId + " . Please, contact the support! " ;
if ( e . getCause ( ) ! = null )
error + = " \ n \ nCaused: " + e . getCause ( ) . getMessage ( ) ;
throw ExceptionManager . internalErrorException ( req , error , this . getClass ( ) , help ) ;
}
//ALREADY MANAGED AS WebApplicationException
2019-10-02 12:43:35 +02:00
LOG . error ( " Exception: " , e ) ;
2018-12-14 14:26:37 +01:00
throw ( WebApplicationException ) e ;
2018-10-19 17:55:31 +02:00
}
}
/ * *
2018-10-22 12:56:04 +02:00
* Resolve storage id .
2018-10-19 17:55:31 +02:00
*
2018-10-22 12:56:04 +02:00
* @param httpRequest the http request
2018-10-19 17:55:31 +02:00
* @param storageId the storage id
* @param fileName the file name
* @param contentType the content type
* @param validation the validation
2018-10-22 12:56:04 +02:00
* @return the response
2018-12-14 14:26:37 +01:00
* @throws Exception the exception
2018-10-19 17:55:31 +02:00
* /
2019-10-02 12:43:35 +02:00
protected static Response resolveStorageId ( HttpServletRequest httpRequest , String storageId , String fileName , String contentType , CONTENT_DISPOSITION_VALUE contentDisposition , boolean validation ) throws Exception {
2018-10-22 12:56:04 +02:00
2019-10-02 12:43:35 +02:00
LOG . info ( " storage-id: " + storageId + " , fileName: " + fileName + " , contentType: " + contentType + " , validation: " + validation ) ;
2018-10-19 11:40:52 +02:00
2018-10-22 12:56:04 +02:00
//Checking mandatory parameter storageId
2018-10-19 11:40:52 +02:00
if ( storageId = = null | | storageId . isEmpty ( ) ) {
2019-10-02 12:43:35 +02:00
LOG . error ( " storageId not found " ) ;
2018-12-14 14:26:37 +01:00
throw ExceptionManager . badRequestException ( httpRequest , " Missing mandatory path parameter " + STORAGE_ID , StorageIDResolver . class , help ) ;
2018-10-19 11:40:52 +02:00
}
2021-09-03 11:13:41 +02:00
storageId = identifyIdAndMemory ( storageId ) ;
IClient iClient = getStorageClientInstance ( memory ) ;
2018-10-19 17:55:31 +02:00
String toSEID = null ;
2019-10-02 12:43:35 +02:00
LOG . info ( " I'm using the storageId {} " , storageId ) ;
2018-10-19 17:55:31 +02:00
try {
toSEID = iClient . getId ( storageId ) ; //to Storage Encrypted ID
2019-10-02 12:43:35 +02:00
LOG . info ( " Decoded ID " + " = " + toSEID ) ;
2018-10-19 17:55:31 +02:00
} catch ( Exception e ) {
2019-10-02 12:43:35 +02:00
LOG . error ( " Storage Client Exception when getting file from storage: " , e ) ;
2018-12-14 14:26:37 +01:00
throw ExceptionManager . notFoundException ( httpRequest , " Storage Client Exception when getting file from storage with id: " + storageId , StorageIDResolver . class , help ) ;
2018-10-19 17:55:31 +02:00
}
2018-10-22 14:55:41 +02:00
if ( toSEID = = null | | toSEID . isEmpty ( ) ) {
2019-10-02 12:43:35 +02:00
LOG . error ( " Decrypted id for storageId: " + storageId + " is null or empty! " ) ;
2018-12-14 14:26:37 +01:00
throw ExceptionManager . notFoundException ( httpRequest , " Error on decrypting the " + STORAGE_ID + " ' " + storageId + " '. Is it a valid id? " , StorageIDResolver . class , help ) ;
2018-10-12 16:20:46 +02:00
}
2018-10-19 11:40:52 +02:00
2019-10-01 18:00:44 +02:00
//long size = iClient.getSize().RFileById(toSEID);
long size = 0 ;
//READING THE METADATA OF FILE FROM STORAGE
StorageMetadataFile metaFile = getStorageMetadataFile ( iClient , toSEID ) ;
2019-10-02 12:43:35 +02:00
LOG . debug ( " MetaFile retrieved from storage? " + ( metaFile ! = null ) ) ;
2019-10-01 18:00:44 +02:00
if ( metaFile ! = null ) {
2018-10-19 18:11:19 +02:00
//Reading the fileName from Storage Metadata only if the passed fileName is null
2019-10-02 12:43:35 +02:00
if ( fileName = = null | | fileName . isEmpty ( ) ) {
2019-10-01 18:00:44 +02:00
fileName = metaFile . getName ( ) ;
2019-10-02 12:43:35 +02:00
LOG . debug ( " Read filename {} from {} " , fileName , StorageMetadataFile . class . getSimpleName ( ) ) ;
}
2019-10-01 18:00:44 +02:00
2018-10-19 11:40:52 +02:00
//Reading the contentType from Storage Metadata only if the passed contentType is null
2019-10-02 12:43:35 +02:00
if ( contentType = = null | | contentType . isEmpty ( ) ) {
2019-10-01 18:00:44 +02:00
contentType = metaFile . getMimeType ( ) ;
2019-10-02 12:43:35 +02:00
LOG . debug ( " Read contentType {} from {} " , contentType , StorageMetadataFile . class . getSimpleName ( ) ) ;
}
2019-10-01 18:00:44 +02:00
//Reading the content size
size = metaFile . getSize ( ) ;
2019-10-02 12:43:35 +02:00
LOG . debug ( " Read size {} from {} " , size , StorageMetadataFile . class . getSimpleName ( ) ) ;
2018-10-12 16:20:46 +02:00
}
2018-10-19 11:40:52 +02:00
2019-10-01 18:00:44 +02:00
//CHECKING TO DEFAULT METADATA
2018-10-19 17:55:31 +02:00
fileName = fileName = = null | | fileName . isEmpty ( ) ? ConstantsResolver . DEFAULT_FILENAME_FROM_STORAGE_MANAGER : fileName ;
2019-10-02 12:43:35 +02:00
LOG . info ( " filename retrieved is {} " , fileName ) ;
2018-10-19 11:40:52 +02:00
2018-10-19 17:55:31 +02:00
contentType = contentType = = null | | contentType . isEmpty ( ) ? ConstantsResolver . DEFAULT_CONTENTTYPE_UNKNOWN_UNKNOWN : contentType ;
2019-10-02 12:43:35 +02:00
LOG . info ( " contentType used is {} " , contentType ) ;
2018-10-19 17:55:31 +02:00
2018-10-19 11:40:52 +02:00
//Building the response
2018-10-12 16:20:46 +02:00
InputStream streamToWrite = iClient . get ( ) . RFileAsInputStream ( toSEID ) ; //input stream
StreamingOutput so = new SingleFileStreamingOutput ( streamToWrite ) ;
2018-10-19 17:55:31 +02:00
2018-10-12 16:20:46 +02:00
ResponseBuilder response = Response
. ok ( so )
2019-10-02 12:43:35 +02:00
. header ( ConstantsResolver . CONTENT_DISPOSITION , contentDisposition . name ( ) + " ; filename= \" " + fileName + " \" " )
2019-10-01 18:00:44 +02:00
. header ( ConstantsResolver . CONTENT_LENGTH , size )
. header ( " Content-Type " , contentType ) ;
2018-12-14 14:26:37 +01:00
2019-10-01 18:00:44 +02:00
// if (contentType!= null)
// response.header("Content-Type",contentType);
2021-09-03 10:39:38 +02:00
2018-10-12 16:20:46 +02:00
return response . build ( ) ;
2018-10-22 12:56:04 +02:00
2018-10-12 16:20:46 +02:00
}
2021-09-03 11:13:41 +02:00
2019-10-01 18:00:44 +02:00
2018-10-19 17:55:31 +02:00
/ * *
* Http do head .
*
2018-12-14 14:26:37 +01:00
* @param req the http request
2018-10-19 17:55:31 +02:00
* @param storageId the storage id
2018-10-23 11:27:04 +02:00
* @param hproxycheck the hproxycheck
2018-10-19 17:55:31 +02:00
* @return the response
2018-12-14 14:26:37 +01:00
* @throws WebApplicationException the web application exception
2018-10-19 17:55:31 +02:00
* /
2018-10-23 11:27:04 +02:00
@HEAD
2019-02-19 15:18:19 +01:00
@Path ( " /{storage-id} " )
2018-12-14 14:26:37 +01:00
public Response httpDoHead ( @Context HttpServletRequest req ,
@PathParam ( STORAGE_ID ) String storageId ,
2019-10-01 18:00:44 +02:00
@QueryParam ( ConstantsResolver . HPC ) boolean hproxycheck ) throws WebApplicationException {
2018-12-14 14:26:37 +01:00
2019-10-02 12:43:35 +02:00
LOG . info ( this . getClass ( ) . getSimpleName ( ) + " HEAD starts... " ) ;
LOG . info ( " The query string is: {} " , req . getQueryString ( ) ) ;
LOG . info ( " Query parameter {} is {} " , ConstantsResolver . HPC , hproxycheck ) ;
2018-10-19 17:55:31 +02:00
2018-12-14 14:26:37 +01:00
try {
//THIS IS FOR HPROXY CHECK
2019-10-01 18:00:44 +02:00
if ( hproxycheck ) {
2019-10-02 12:43:35 +02:00
LOG . trace ( " returning status 200 for Hproxy check " ) ;
2018-12-14 14:26:37 +01:00
ResponseBuilder response = Response . status ( HttpStatus . SC_OK ) ;
return response . build ( ) ;
}
2018-10-19 17:55:31 +02:00
2018-12-14 14:26:37 +01:00
return validationPayload ( req , storageId ) ;
} catch ( Exception e ) {
2018-10-23 11:27:04 +02:00
2018-12-14 14:26:37 +01:00
if ( ! ( e instanceof WebApplicationException ) ) {
//UNEXPECTED EXCEPTION managing it as WebApplicationException
String error = " Error occurred on resolving the Storage ID: " + storageId + " . Please, contact the support! " ;
if ( e . getCause ( ) ! = null )
error + = " \ n \ nCaused: " + e . getCause ( ) . getMessage ( ) ;
throw ExceptionManager . internalErrorException ( req , error , this . getClass ( ) , help ) ;
}
//ALREADY MANAGED AS WebApplicationException
2019-10-02 12:43:35 +02:00
LOG . error ( " Exception: " , e ) ;
2018-12-14 14:26:37 +01:00
throw ( WebApplicationException ) e ;
}
}
2018-10-19 17:55:31 +02:00
/ * *
* Validation payload .
*
2018-10-23 11:27:04 +02:00
* @param httpRequest the http request
2018-10-19 17:55:31 +02:00
* @param storageId the storage id
* @return the response
2018-12-14 14:26:37 +01:00
* @throws Exception the exception
2018-10-19 17:55:31 +02:00
* /
2018-12-14 14:26:37 +01:00
protected Response validationPayload ( HttpServletRequest httpRequest , String storageId ) throws Exception {
2019-10-02 12:43:35 +02:00
LOG . info ( " validationPayload called " ) ;
2018-10-19 17:55:31 +02:00
2018-10-23 11:27:04 +02:00
//Checking mandatory parameter storageId
2018-10-19 17:55:31 +02:00
if ( storageId = = null | | storageId . isEmpty ( ) ) {
2019-10-02 12:43:35 +02:00
LOG . warn ( " storageId not found " ) ;
2018-12-14 14:26:37 +01:00
throw ExceptionManager . badRequestException ( httpRequest , " Storage Client Exception when getting file from storage with id: " + storageId , this . getClass ( ) , help ) ;
2021-09-03 15:43:47 +02:00
}
2021-09-03 11:13:41 +02:00
storageId = identifyIdAndMemory ( storageId ) ;
IClient iClient = getStorageClientInstance ( memory ) ;
2018-10-19 17:55:31 +02:00
String toSEID = null ;
2019-10-02 12:43:35 +02:00
LOG . info ( " I'm using the storageId {} " , storageId ) ;
2018-10-19 17:55:31 +02:00
try {
toSEID = iClient . getId ( storageId ) ; //to Storage Encrypted ID
2021-09-03 10:39:38 +02:00
LOG . info ( " Decoded ID " + " = " + toSEID ) ;
2018-10-19 17:55:31 +02:00
} catch ( Exception e ) {
2019-10-02 12:43:35 +02:00
LOG . error ( " Storage Client Exception when getting file from storage: " , e ) ;
2021-09-03 10:39:38 +02:00
throw ExceptionManager . notFoundException ( httpRequest , " Storage Client Exception when getting file from storage with id: " + storageId , StorageIDResolver . class , help ) ;
2018-10-19 17:55:31 +02:00
}
2018-10-23 11:27:04 +02:00
if ( toSEID = = null | | toSEID . isEmpty ( ) ) {
2019-10-02 12:43:35 +02:00
LOG . error ( " Decrypted id for storageId: " + storageId + " is null or empty! " ) ;
2018-12-14 14:26:37 +01:00
throw ExceptionManager . notFoundException ( httpRequest , " Error on decrypting the " + STORAGE_ID + " ' " + storageId + " '. Is it a valid id? " , StorageIDResolver . class , help ) ;
2018-10-19 17:55:31 +02:00
}
2019-10-01 18:00:44 +02:00
//SETTING DEFAULT METADATA
String fileName = ConstantsResolver . DEFAULT_FILENAME_FROM_STORAGE_MANAGER ;
String contentType = ConstantsResolver . DEFAULT_CONTENTTYPE_UNKNOWN_UNKNOWN ;
//READING THE METADATA OF FILE FROM STORAGE
StorageMetadataFile metadataFile = getStorageMetadataFile ( iClient , toSEID ) ;
2019-10-02 12:43:35 +02:00
LOG . debug ( " MetaFile retrieved from storage? " + ( metadataFile ! = null ) ) ;
2019-10-01 18:00:44 +02:00
long size = 0 ;
if ( metadataFile ! = null ) {
// Reading the fileName from Storage Metadata only if it is not null
2019-10-02 12:43:35 +02:00
if ( metadataFile . getName ( ) ! = null ) {
2019-10-01 18:00:44 +02:00
fileName = metadataFile . getName ( ) ;
2019-10-02 12:43:35 +02:00
LOG . debug ( " Read filename {} from {} " , fileName , StorageMetadataFile . class . getSimpleName ( ) ) ;
}
2019-10-01 18:00:44 +02:00
// Reading the contentType from Storage Metadata only if it is not null
2019-10-02 12:43:35 +02:00
if ( metadataFile . getMimeType ( ) ! = null ) {
2019-10-01 18:00:44 +02:00
contentType = metadataFile . getMimeType ( ) ;
2019-10-02 12:43:35 +02:00
LOG . debug ( " Read contentType {} from {} " , contentType , StorageMetadataFile . class . getSimpleName ( ) ) ;
}
2019-10-01 18:00:44 +02:00
size = metadataFile . getSize ( ) ;
2019-10-02 12:43:35 +02:00
LOG . debug ( " Read size {} from {} " , size , StorageMetadataFile . class . getSimpleName ( ) ) ;
2019-10-01 18:00:44 +02:00
}
2018-10-23 11:27:04 +02:00
2018-10-19 17:55:31 +02:00
//Building the response
InputStream streamToWrite = iClient . get ( ) . RFileAsInputStream ( toSEID ) ; //input stream
byte [ ] bytes = new byte [ 1 ] ; //1B
int c ;
2018-10-23 11:27:04 +02:00
ResponseBuilder response = null ;
2018-10-19 17:55:31 +02:00
try {
c = streamToWrite . read ( bytes ) ;
2019-10-02 12:43:35 +02:00
LOG . info ( c + " byte read from InputStream " ) ;
2018-10-19 17:55:31 +02:00
if ( c > 0 ) {
2019-10-02 12:43:35 +02:00
LOG . info ( " at least 1 byte read, returning status 200 " ) ;
2018-10-19 17:55:31 +02:00
IOUtils . closeQuietly ( streamToWrite ) ;
2018-10-23 11:27:04 +02:00
response = Response . status ( HttpStatus . SC_OK ) ;
2018-10-19 17:55:31 +02:00
} else
2018-12-14 14:26:37 +01:00
throw ExceptionManager . notFoundException ( httpRequest , " The file with id: " + storageId + " is missing in the storage " , StorageIDResolver . class , help ) ;
2018-10-19 17:55:31 +02:00
}
2018-10-23 11:27:04 +02:00
catch ( IOException e2 ) {
2019-10-02 12:43:35 +02:00
LOG . error ( " Error on validating the file: " , e2 ) ;
2018-12-14 14:26:37 +01:00
throw ExceptionManager . internalErrorException ( httpRequest , " Error on validating the file with id: " + storageId , StorageIDResolver . class , help ) ;
2018-10-19 17:55:31 +02:00
}
2018-10-23 11:27:04 +02:00
if ( response = = null )
2018-12-14 14:26:37 +01:00
throw ExceptionManager . internalErrorException ( httpRequest , " Error on validating the file with id: " + storageId , StorageIDResolver . class , help ) ;
2019-10-01 18:00:44 +02:00
response = response
. header ( " Content-Type " , contentType )
. header ( ConstantsResolver . CONTENT_LENGTH , size )
. header ( ConstantsResolver . CONTENT_DISPOSITION , " attachment; filename= \" " + fileName + " \" " ) ;
2021-09-02 14:43:10 +02:00
iClient . forceClose ( ) ;
2018-10-23 11:27:04 +02:00
return response . build ( ) ;
}
2021-09-03 10:39:38 +02:00
2018-10-19 17:55:31 +02:00
/ * *
2021-09-03 15:43:47 +02:00
* Get a storage - manager client instance . A new storage instance will be instantied only if the memory type is not the same of the previous one .
* In this case the old connection will be closed .
2018-10-19 17:55:31 +02:00
*
2021-09-03 15:43:47 +02:00
* @param memoryType indicates the memory type ( Persistent , Volatile or whatever )
2018-10-19 17:55:31 +02:00
* @return the storage client instance
* /
2021-09-03 11:13:41 +02:00
protected static IClient getStorageClientInstance ( MemoryType memory ) {
2021-09-22 10:46:39 +02:00
if ( ( ! Objects . isNull ( storageManagerClient ) ) & & ( storageManagerClient . getGcubeMemoryType ( ) . toString ( ) = = memory . toString ( ) ) ) {
2021-09-22 10:20:00 +02:00
LOG . debug ( " reusing the same storage-client object " ) ;
2021-09-03 10:39:38 +02:00
return storageManagerClient ;
2021-09-22 10:20:00 +02:00
} else {
2021-09-03 10:39:38 +02:00
if ( ! Objects . isNull ( storageManagerClient ) ) {
2021-09-22 10:20:00 +02:00
LOG . debug ( " going to close an old storage-client instance on " + storageManagerClient . getGcubeMemoryType ( ) ) ;
2021-09-22 09:52:52 +02:00
try {
storageManagerClient . forceClose ( ) ;
2021-09-22 10:00:15 +02:00
LOG . debug ( " storage-client closed " ) ;
2021-09-22 09:52:52 +02:00
} catch ( Exception e ) {
LOG . warn ( " Some problem occurred during the close operation: " + e . getMessage ( ) + " Going to open another connection " ) ;
}
2021-09-03 10:39:38 +02:00
}
2021-09-22 10:20:00 +02:00
LOG . debug ( " initializing a new storage-client instance on " + memory ) ;
2021-09-03 15:43:47 +02:00
storageManagerClient = new StorageClient ( StorageIDResolver . class . getName ( ) , StorageIDResolver . class . getSimpleName ( ) , STORAGEID_RESOLVER , STORAGE_ACCESS_TYPE , memory , STORAGE_BACKEND_TYPE ) . getClient ( ) ;
2021-09-03 10:39:38 +02:00
return storageManagerClient ;
}
2018-10-19 17:55:31 +02:00
}
2019-10-01 18:00:44 +02:00
2021-09-03 15:43:47 +02:00
/ * *
* Set the right memory type extrapolated from the storageid
* @param storageId storageid with backend type specification ( if present )
* @return storageId without backend type specification
* /
2021-09-03 11:13:41 +02:00
private static String identifyIdAndMemory ( String storageId ) {
if ( storageId . endsWith ( org . gcube . contentmanagement . blobstorage . transport . backend . util . Costants . VOLATILE_URL_IDENTIFICATOR ) ) {
LOG . info ( " identificated a VOLATILE url " ) ;
memory = MemoryType . VOLATILE ;
storageId = storageId . replace ( org . gcube . contentmanagement . blobstorage . transport . backend . util . Costants . VOLATILE_URL_IDENTIFICATOR , " " ) ;
} else {
memory = MemoryType . PERSISTENT ;
}
return storageId ;
}
2019-10-01 18:00:44 +02:00
/ * *
* Gets the storage metadata file .
*
* @param iClient the i client
* @param toSEID the to SEID
* @return the storage metadata file
* /
public static StorageMetadataFile getStorageMetadataFile ( IClient iClient , String toSEID ) {
2018-10-19 17:55:31 +02:00
2019-10-01 18:00:44 +02:00
try {
if ( iClient = = null )
throw new Exception ( " The " + IClient . class . getName ( ) + " is not instancied correclty. It is null! " ) ;
if ( toSEID = = null | | toSEID . isEmpty ( ) )
throw new Exception ( " Invalid input identifier. The input Storage Encrypted ID is null or empty! " ) ;
long size = iClient . getSize ( ) . RFileById ( toSEID ) ;
MyFile myFile = iClient . getMetaFile ( ) . RFile ( toSEID ) ;
return new StorageMetadataFile ( myFile , size ) ;
2018-10-23 11:27:04 +02:00
2019-10-01 18:00:44 +02:00
} catch ( Exception e ) {
2019-10-02 12:43:35 +02:00
LOG . warn ( " Error on getting file metadata from storage, printing this warning and trying to continue.. " , e ) ;
2019-10-01 18:00:44 +02:00
return null ;
}
2018-10-23 11:27:04 +02:00
2019-10-01 18:00:44 +02:00
}
2018-10-23 11:27:04 +02:00
2018-10-12 16:20:46 +02:00
}