From 9d128ce1a0e0a92e4c5c2301565963f786153f1b Mon Sep 17 00:00:00 2001 From: Roberto Cirillo Date: Fri, 3 Sep 2021 10:39:38 +0200 Subject: [PATCH] replace buildStorageClientInstance method, with getStorageClientInstance method --- .../resolver/services/StorageIDResolver.java | 108 +++++++++++++----- 1 file changed, 82 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/StorageIDResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/StorageIDResolver.java index 8a4d4ab..73a9492 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/StorageIDResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/StorageIDResolver.java @@ -2,6 +2,7 @@ package org.gcube.datatransfer.resolver.services; import java.io.IOException; import java.io.InputStream; +import java.util.Objects; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.GET; @@ -54,6 +55,7 @@ public class StorageIDResolver { private static String help = "https://wiki.gcube-system.org/gcube/URI_Resolver#STORAGE-ID_Resolver"; + protected static IClient storageManagerClient; /** * Gets the storage id. @@ -124,20 +126,35 @@ public class StorageIDResolver { LOG.error("storageId not found"); throw ExceptionManager.badRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help); } - - StorageClientInstance scInstance = buildStorageClientInstance(storageId); +/** START PATCH 2.6.1-SNAPSHOT **/ +// StorageClientInstance scInstance = buildStorageClientInstance(storageId); +// String toSEID = null; +// IClient iClient = null; +// storageId = scInstance.getStorageId(); //IT SHOULD BE CHANGED es. removing the suffix '-VLT' +// LOG.info("I'm using the storageId {}",storageId); +// try{ +// iClient = scInstance.getStorageClient().getClient(); +// toSEID = iClient.getId(storageId); //to Storage Encrypted ID +// LOG.info("Decoded ID"+" = "+ toSEID); +// }catch(Exception e){ +// LOG.error("Storage Client Exception when getting file from storage: ", e); +// throw ExceptionManager.notFoundException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help); +// } + + + IClient iClient = getStorageClientInstance(storageId); String toSEID = null; - IClient iClient = null; - storageId = scInstance.getStorageId(); //IT SHOULD BE CHANGED es. removing the suffix '-VLT' LOG.info("I'm using the storageId {}",storageId); try{ - iClient = scInstance.getStorageClient().getClient(); toSEID = iClient.getId(storageId); //to Storage Encrypted ID LOG.info("Decoded ID"+" = "+ toSEID); }catch(Exception e){ LOG.error("Storage Client Exception when getting file from storage: ", e); throw ExceptionManager.notFoundException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help); } + +/** END PATCH 2.6.1-SNAPSHOT **/ + if(toSEID==null || toSEID.isEmpty()){ LOG.error("Decrypted id for storageId: "+storageId +" is null or empty!"); @@ -188,7 +205,7 @@ public class StorageIDResolver { // if (contentType!= null) // response.header("Content-Type",contentType); - iClient.forceClose(); + return response.build(); } @@ -255,20 +272,34 @@ public class StorageIDResolver { LOG.warn("storageId not found"); throw ExceptionManager.badRequestException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, this.getClass(), help); } - StorageClientInstance client = buildStorageClientInstance(storageId); - String toSEID = null; - IClient iClient = null; - storageId = client.getStorageId(); //IT SHOULD BE CHANGED es. removing the suffix '-VLT' - LOG.info("I'm using the storageId {}",storageId); +/** START PATCH 2.6.1-SNAPSHOT **/ +// StorageClientInstance scInstance = buildStorageClientInstance(storageId); +// String toSEID = null; +// IClient iClient = null; +// storageId = scInstance.getStorageId(); //IT SHOULD BE CHANGED es. removing the suffix '-VLT' +// LOG.info("I'm using the storageId {}",storageId); +// try{ +// iClient = scInstance.getStorageClient().getClient(); +// toSEID = iClient.getId(storageId); //to Storage Encrypted ID +// LOG.info("Decoded ID"+" = "+ toSEID); +// }catch(Exception e){ +// LOG.error("Storage Client Exception when getting file from storage: ", e); +// throw ExceptionManager.notFoundException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help); +// } + + IClient iClient = getStorageClientInstance(storageId); + String toSEID = null; + LOG.info("I'm using the storageId {}",storageId); try{ - iClient = client.getStorageClient().getClient(); toSEID = iClient.getId(storageId); //to Storage Encrypted ID - LOG.debug("Decoded ID"+" = "+ toSEID); + LOG.info("Decoded ID"+" = "+ toSEID); }catch(Exception e){ LOG.error("Storage Client Exception when getting file from storage: ", e); - throw ExceptionManager.internalErrorException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help); + throw ExceptionManager.notFoundException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help); } + +/** END PATCH 2.6.1-SNAPSHOT **/ if(toSEID==null || toSEID.isEmpty()){ LOG.error("Decrypted id for storageId: "+storageId +" is null or empty!"); @@ -335,30 +366,55 @@ public class StorageIDResolver { } +// /** +// * Builds the storage client instance. +// * +// * @param storageId the storage id +// * @return the storage client instance +// * @throws Exception the exception +// */ +// protected static StorageClientInstance buildStorageClientInstance(String storageId) throws Exception{ +// +// MemoryType memory=null; +// if(storageId.endsWith(org.gcube.contentmanagement.blobstorage.transport.backend.util.Costants.VOLATILE_URL_IDENTIFICATOR)){ +// memory=MemoryType.VOLATILE; +// storageId=storageId.replace(org.gcube.contentmanagement.blobstorage.transport.backend.util.Costants.VOLATILE_URL_IDENTIFICATOR, ""); +// } +// +// StorageClient client; +// if(memory==null) +// memory=MemoryType.PERSISTENT; +// client=new StorageClient(StorageIDResolver.class.getName(), StorageIDResolver.class.getSimpleName(), STORAGEID_RESOLVER, AccessType.PUBLIC, memory, BackendType.MongoDB); +// return new StorageClientInstance(client, memory, storageId); +// } + /** - * Builds the storage client instance. + * Get a storage client instance. * * @param storageId the storage id * @return the storage client instance - * @throws Exception the exception */ - protected static StorageClientInstance buildStorageClientInstance(String storageId) throws Exception{ - + protected static IClient getStorageClientInstance(String storageId) { MemoryType memory=null; if(storageId.endsWith(org.gcube.contentmanagement.blobstorage.transport.backend.util.Costants.VOLATILE_URL_IDENTIFICATOR)){ memory=MemoryType.VOLATILE; storageId=storageId.replace(org.gcube.contentmanagement.blobstorage.transport.backend.util.Costants.VOLATILE_URL_IDENTIFICATOR, ""); - } - - StorageClient client; - if(memory==null) + }else { memory=MemoryType.PERSISTENT; - client=new StorageClient(StorageIDResolver.class.getName(), StorageIDResolver.class.getSimpleName(), STORAGEID_RESOLVER, AccessType.PUBLIC, memory, BackendType.MongoDB); - return new StorageClientInstance(client, memory, storageId); + } + if ((!Objects.isNull(storageManagerClient)) && (storageManagerClient.getGcubeMemoryType().equals(memory))) + return storageManagerClient; + else { + if(!Objects.isNull(storageManagerClient)) { + LOG.debug("going to close an old storage-client instance"); + storageManagerClient.forceClose(); + } + LOG.debug("initializing a new storage-client instance"); + storageManagerClient=new StorageClient(StorageIDResolver.class.getName(), StorageIDResolver.class.getSimpleName(), STORAGEID_RESOLVER, AccessType.PUBLIC, memory, BackendType.MongoDB).getClient(); + return storageManagerClient; + } } - - /** * Gets the storage metadata file. *