2019-01-10 12:29:47 +01:00
package org.gcube.gcat.workspace ;
import java.net.HttpURLConnection ;
import java.net.URL ;
import java.util.Map ;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest ;
import org.gcube.common.storagehub.client.dsl.FileContainer ;
import org.gcube.common.storagehub.model.Metadata ;
import org.gcube.gcat.utils.Constants ;
import org.gcube.storagehub.ApplicationMode ;
import org.gcube.storagehub.StorageHubManagement ;
import org.glassfish.jersey.media.multipart.ContentDisposition ;
2020-01-23 15:04:51 +01:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2019-01-10 12:29:47 +01:00
public class CatalogueStorageHubManagement {
2020-01-23 15:04:51 +01:00
private static final Logger logger = LoggerFactory . getLogger ( CatalogueStorageHubManagement . class ) ;
2019-01-10 12:29:47 +01:00
protected StorageHubManagement storageHubManagement ;
2019-09-16 14:48:18 +02:00
protected String originalFilename ;
2019-01-10 12:29:47 +01:00
protected String mimeType ;
public String getOriginalFilename ( ) {
return originalFilename ;
}
public String getMimeType ( ) {
return mimeType ;
}
2019-09-16 14:48:18 +02:00
2019-01-10 12:29:47 +01:00
public CatalogueStorageHubManagement ( ) {
this . storageHubManagement = new StorageHubManagement ( ) ;
}
2021-04-20 15:41:54 +02:00
protected String getOriginalFileName ( HttpURLConnection httpURLConnection ) throws Exception {
String contentDisposition = httpURLConnection . getHeaderField ( " Content-Disposition " ) ;
2019-01-10 12:29:47 +01:00
contentDisposition = contentDisposition . replaceAll ( " = " , " = " ) . replaceAll ( " = " , " = " ) ;
ContentDisposition formDataContentDisposition = new ContentDisposition ( contentDisposition ) ;
return formDataContentDisposition . getFileName ( ) ;
}
public URL ensureResourcePersistence ( URL persistedURL , String itemID , String resourceID ) throws Exception {
ApplicationMode applicationMode = new ApplicationMode ( Constants . getCatalogueApplicationToken ( ) ) ;
try {
applicationMode . start ( ) ;
GXHTTPStringRequest gxhttpStringRequest = GXHTTPStringRequest . newRequest ( persistedURL . toString ( ) ) ;
2019-02-08 11:10:59 +01:00
gxhttpStringRequest . from ( Constants . CATALOGUE_NAME ) ;
2019-01-10 12:29:47 +01:00
gxhttpStringRequest . isExternalCall ( true ) ;
HttpURLConnection httpURLConnection = gxhttpStringRequest . get ( ) ;
mimeType = httpURLConnection . getContentType ( ) . split ( " ; " ) [ 0 ] ;
originalFilename = getOriginalFileName ( httpURLConnection ) ;
CatalogueMetadata catalogueMetadata = new CatalogueMetadata ( itemID ) ;
storageHubManagement . setCheckMetadata ( catalogueMetadata ) ;
Metadata metadata = catalogueMetadata . getMetadata ( persistedURL , originalFilename , resourceID ) ;
2019-09-16 14:48:18 +02:00
persistedURL = storageHubManagement . persistFile ( httpURLConnection . getInputStream ( ) , resourceID , mimeType ,
metadata ) ;
2019-01-10 12:29:47 +01:00
mimeType = storageHubManagement . getMimeType ( ) ;
return persistedURL ;
2021-04-20 15:41:54 +02:00
} catch ( Exception e ) {
logger . error ( " Error while trying to persists the resource " , e ) ;
throw e ;
2019-01-10 12:29:47 +01:00
} finally {
applicationMode . end ( ) ;
}
}
2019-09-16 14:48:18 +02:00
2019-01-10 12:29:47 +01:00
public void deleteResourcePersistence ( String itemID , String resourceID , String mimeType ) throws Exception {
ApplicationMode applicationMode = new ApplicationMode ( Constants . getCatalogueApplicationToken ( ) ) ;
try {
applicationMode . start ( ) ;
storageHubManagement = new StorageHubManagement ( ) ;
CatalogueMetadata catalogueMetadata = new CatalogueMetadata ( itemID ) ;
storageHubManagement . setCheckMetadata ( catalogueMetadata ) ;
storageHubManagement . removePersistedFile ( resourceID , mimeType ) ;
} finally {
applicationMode . end ( ) ;
}
}
2019-05-20 17:23:49 +02:00
protected void internalAddRevisionID ( String resourceID , String revisionID ) throws Exception {
2020-01-22 17:35:48 +01:00
FileContainer fileContainer = storageHubManagement . getPersistedFile ( ) ;
2020-01-23 15:04:51 +01:00
if ( fileContainer ! = null ) {
Metadata metadata = fileContainer . get ( ) . getMetadata ( ) ;
Map < String , Object > map = metadata . getMap ( ) ;
map . put ( CatalogueMetadata . CATALOGUE_RESOURCE_ID , resourceID ) ;
map . put ( CatalogueMetadata . CATALOGUE_RESOURCE_REVISION_ID , revisionID ) ;
metadata . setMap ( map ) ;
fileContainer . setMetadata ( metadata ) ;
} else {
logger . warn (
" Unable to set revision id {} to the file of resource with id {} because the file was NOT found on storage-hub. This could be related to an item not created via gCat. Skipping it. " ,
revisionID , resourceID ) ;
}
2019-01-10 12:29:47 +01:00
}
2019-05-20 17:23:49 +02:00
public void renameFile ( String resourceID , String revisionID ) throws Exception {
2019-01-10 12:29:47 +01:00
ApplicationMode applicationMode = new ApplicationMode ( Constants . getCatalogueApplicationToken ( ) ) ;
try {
applicationMode . start ( ) ;
2020-01-22 17:35:48 +01:00
FileContainer createdfile = storageHubManagement . getPersistedFile ( ) ;
2019-01-10 12:29:47 +01:00
createdfile . rename ( resourceID ) ;
internalAddRevisionID ( resourceID , revisionID ) ;
2019-09-16 14:48:18 +02:00
} finally {
2019-01-10 12:29:47 +01:00
applicationMode . end ( ) ;
}
}
2019-05-20 17:23:49 +02:00
public void addRevisionID ( String resourceID , String revisionID ) throws Exception {
2019-01-10 12:29:47 +01:00
ApplicationMode applicationMode = new ApplicationMode ( Constants . getCatalogueApplicationToken ( ) ) ;
try {
applicationMode . start ( ) ;
internalAddRevisionID ( resourceID , revisionID ) ;
2019-09-16 14:48:18 +02:00
} finally {
2019-01-10 12:29:47 +01:00
applicationMode . end ( ) ;
}
}
2020-01-23 15:04:51 +01:00
2020-02-03 15:50:58 +01:00
public FileContainer retrievePersistedFile ( String id , String mimeType ) throws Exception {
2020-01-22 17:35:48 +01:00
ApplicationMode applicationMode = new ApplicationMode ( Constants . getCatalogueApplicationToken ( ) ) ;
try {
applicationMode . start ( ) ;
2020-02-03 15:50:58 +01:00
return storageHubManagement . getPersistedFile ( id , mimeType ) ;
2020-01-22 17:35:48 +01:00
} finally {
applicationMode . end ( ) ;
}
}
2019-01-10 12:29:47 +01:00
2020-02-03 15:50:58 +01:00
public FileContainer getPersistedFile ( ) {
return storageHubManagement . getPersistedFile ( ) ;
}
2019-01-10 12:29:47 +01:00
}