Looking for a workaround for a storagehub issue

This commit is contained in:
luca.frosini 2023-07-11 11:47:27 +02:00
parent 69fb4eafa0
commit d5137f8a82
1 changed files with 18 additions and 15 deletions

View File

@ -10,6 +10,7 @@ import org.gcube.common.authorization.utils.secret.Secret;
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.common.storagehub.model.items.AbstractFileItem;
import org.gcube.gcat.utils.Constants;
import org.gcube.storagehub.StorageHubManagement;
import org.glassfish.jersey.media.multipart.ContentDisposition;
@ -88,18 +89,27 @@ public class CatalogueStorageHubManagement {
}
protected void internalAddRevisionID(String resourceID, String revisionID) throws Exception {
FileContainer fileContainer = storageHubManagement.getPersistedFile();
if(fileContainer != null) {
Metadata metadata = fileContainer.get().getMetadata();
try {
FileContainer fileContainer = null;
AbstractFileItem fileItem = null;
try {
fileContainer = storageHubManagement.getPersistedFile();
fileItem = fileContainer.get();
}catch (Exception e) {
fileContainer = storageHubManagement.getPersistedFile(resourceID, mimeType);
fileItem = fileContainer.get();
}
Metadata metadata = fileItem.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 {
} catch (Exception e) {
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.",
"Unable to set revision id {} to the file of resource with id {} because the file was NOT found on storage-hub.",
revisionID, resourceID);
throw e;
}
}
@ -128,26 +138,19 @@ public class CatalogueStorageHubManagement {
}
}
public FileContainer retrievePersistedFile(String id, String mimeType) throws Exception {
public FileContainer retrievePersistedFile(String filename, String mimeType) throws Exception {
SecretManager secretManager = SecretManagerProvider.instance.get();
Secret secret = Constants.getCatalogueSecret();
try {
secretManager.startSession(secret);
return storageHubManagement.getPersistedFile(id, mimeType);
return storageHubManagement.getPersistedFile(filename, mimeType);
} finally {
secretManager.endSession();
}
}
public FileContainer getPersistedFile() throws Exception {
SecretManager secretManager = SecretManagerProvider.instance.get();
Secret secret = Constants.getCatalogueSecret();
try {
secretManager.startSession(secret);
return storageHubManagement.getPersistedFile();
} finally {
secretManager.endSession();
}
return storageHubManagement.getPersistedFile();
}
}