|
|
|
@ -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;
|
|
|
|
@ -20,6 +21,7 @@ import org.apache.http.HttpStatus;
|
|
|
|
|
import org.gcube.contentmanagement.blobstorage.resource.MyFile;
|
|
|
|
|
import org.gcube.contentmanagement.blobstorage.service.IClient;
|
|
|
|
|
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
|
|
|
|
|
import org.gcube.contentmanager.storageclient.wrapper.BackendType;
|
|
|
|
|
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
|
|
|
|
|
import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
|
|
|
|
|
import org.gcube.datatransfer.resolver.ConstantsResolver;
|
|
|
|
@ -48,11 +50,14 @@ public class StorageIDResolver {
|
|
|
|
|
*/
|
|
|
|
|
protected static final String STORAGEID_RESOLVER = "storageid-resolver";
|
|
|
|
|
private static final String STORAGE_ID = "storage-id";
|
|
|
|
|
|
|
|
|
|
private static final AccessType STORAGE_ACCESS_TYPE=AccessType.PUBLIC;
|
|
|
|
|
private static final BackendType STORAGE_BACKEND_TYPE=BackendType.MongoDB;
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(StorageIDResolver.class);
|
|
|
|
|
|
|
|
|
|
private static String help = "https://wiki.gcube-system.org/gcube/URI_Resolver#STORAGE-ID_Resolver";
|
|
|
|
|
|
|
|
|
|
protected static IClient storageManagerClient;
|
|
|
|
|
private static MemoryType memory;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the storage id.
|
|
|
|
@ -123,21 +128,17 @@ 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);
|
|
|
|
|
storageId = identifyIdAndMemory(storageId);
|
|
|
|
|
IClient iClient = getStorageClientInstance(memory);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(toSEID==null || toSEID.isEmpty()){
|
|
|
|
|
LOG.error("Decrypted id for storageId: "+storageId +" is null or empty!");
|
|
|
|
|
throw ExceptionManager.notFoundException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help);
|
|
|
|
@ -193,6 +194,9 @@ public class StorageIDResolver {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Http do head.
|
|
|
|
|
*
|
|
|
|
@ -254,26 +258,21 @@ 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);
|
|
|
|
|
storageId = identifyIdAndMemory(storageId);
|
|
|
|
|
IClient iClient = getStorageClientInstance(memory);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(toSEID==null || toSEID.isEmpty()){
|
|
|
|
|
LOG.error("Decrypted id for storageId: "+storageId +" is null or empty!");
|
|
|
|
|
throw ExceptionManager.notFoundException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//SETTING DEFAULT METADATA
|
|
|
|
|
String fileName = ConstantsResolver.DEFAULT_FILENAME_FROM_STORAGE_MANAGER;
|
|
|
|
|
String contentType = ConstantsResolver.DEFAULT_CONTENTTYPE_UNKNOWN_UNKNOWN;
|
|
|
|
@ -328,38 +327,48 @@ public class StorageIDResolver {
|
|
|
|
|
.header("Content-Type", contentType)
|
|
|
|
|
.header(ConstantsResolver.CONTENT_LENGTH, size)
|
|
|
|
|
.header(ConstantsResolver.CONTENT_DISPOSITION,"attachment; filename=\""+fileName+"\"");
|
|
|
|
|
|
|
|
|
|
iClient.forceClose();
|
|
|
|
|
return response.build();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @param memoryType indicates the memory type (Persistent, Volatile or whatever)
|
|
|
|
|
* @return the storage client instance
|
|
|
|
|
*/
|
|
|
|
|
protected static IClient getStorageClientInstance(MemoryType memory) {
|
|
|
|
|
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, STORAGE_ACCESS_TYPE, memory, STORAGE_BACKEND_TYPE).getClient();
|
|
|
|
|
return storageManagerClient;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Builds the storage client instance.
|
|
|
|
|
*
|
|
|
|
|
* @param storageId the storage id
|
|
|
|
|
* @return the storage client instance
|
|
|
|
|
* @throws Exception the exception
|
|
|
|
|
* Set the right memory type extrapolated from the storageid
|
|
|
|
|
* @param storageId storageid with backend type specification (if present)
|
|
|
|
|
* @return storageId without backend type specification
|
|
|
|
|
*/
|
|
|
|
|
protected static StorageClientInstance buildStorageClientInstance(String storageId) throws Exception{
|
|
|
|
|
|
|
|
|
|
MemoryType memory=null;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StorageClient client;
|
|
|
|
|
if(memory==null)
|
|
|
|
|
client=new StorageClient(StorageIDResolver.class.getName(), StorageIDResolver.class.getSimpleName(), STORAGEID_RESOLVER, AccessType.PUBLIC);
|
|
|
|
|
else
|
|
|
|
|
client=new StorageClient(StorageIDResolver.class.getName(), StorageIDResolver.class.getSimpleName(), STORAGEID_RESOLVER, AccessType.PUBLIC, memory);
|
|
|
|
|
|
|
|
|
|
return new StorageClientInstance(client, memory, storageId);
|
|
|
|
|
return storageId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the storage metadata file.
|
|
|
|
|
*
|
|
|
|
|