uri-resolver/src/main/java/org/gcube/datatransfer/resolver/services/StorageManager.java

94 lines
3.3 KiB
Java

package org.gcube.datatransfer.resolver.services;
import java.io.InputStream;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.StreamingOutput;
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.StorageClient;
import org.gcube.datatransfer.resolver.SingleFileStreamingOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Path("/")
public class StorageManager {
private static Logger logger = LoggerFactory.getLogger(StorageManager.class);
@GET
@Path("{(id)?}|{storage-id}")
public Response getFile(@PathParam("storage-id") String storageId, @QueryParam("smp-id") String smpId, @QueryParam("fileName") String fileName, @QueryParam("contentType") String contentType, @QueryParam("validation") String validation) {
logger.info("resolve Storage Id called");
logger.info("storage-id: "+storageId);
logger.info("smp-id: "+smpId);
logger.info("fileName: "+fileName);
logger.info("contentType: "+contentType);
//Checking to STORAGE-ID Resolver
boolean storageIdFound = true;
if (storageId == null || storageId.isEmpty()) {
logger.warn("storageId not found");
storageIdFound = false;
}
//If storageId not found, Checking to SMP-ID Resolver
if(!storageIdFound){
if(smpId==null || smpId.isEmpty()){
logger.warn("smp-id not found");
throw new WebApplicationException("Missing mandatory parameter 'smp-id' or 'storage-id'", Status.BAD_REQUEST);
}
}
StorageClient client = new StorageClient("DataTransfer", "UriResolver", "storageid-resolver", AccessType.PUBLIC);
IClient iClient = client.getClient();
String toSEID = iClient.getId(storageId); //to Storage Encrypted ID
logger.debug("Decoded ID"+" = "+ toSEID);
if(toSEID==null){
String error = "Decrypted id is null, thrown exception!";
throw new WebApplicationException(error, Status.BAD_REQUEST);
}
long size = iClient.getSize().RFileById(toSEID);
try{
MyFile file = client.getClient().getMetaFile().RFile(toSEID);
logger.debug("MetaFile retrieved from storage? "+ (file!=null));
fileName= file.getName();
//Reading the contentType from Storage Metadata only if the passed contentType is null
if(contentType==null)
contentType = file.getMimeType();
}catch (Exception e) {
logger.warn("Error when getting file metadata from storage, printing this warning and trying to continue..", e);
}
fileName = fileName==null || fileName.isEmpty()?"download":fileName;
logger.info("filename retrieved is {}",fileName);
//Building the response
InputStream streamToWrite=iClient.get().RFileAsInputStream(toSEID); //input stream
StreamingOutput so = new SingleFileStreamingOutput(streamToWrite);
ResponseBuilder response = Response
.ok(so)
.header("content-disposition","attachment; filename = \""+fileName+"\"")
.header("Content-Length", size);
if (contentType!= null) response.header("Content-Type",contentType);
return response.build();
}
}