2018-10-19 17:06:53 +02:00
|
|
|
package org.gcube.datatransfer.resolver.services;
|
|
|
|
|
|
|
|
import javax.enterprise.context.RequestScoped;
|
2018-10-22 14:29:57 +02:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2018-10-19 17:06:53 +02:00
|
|
|
import javax.ws.rs.GET;
|
2018-12-28 11:02:53 +01:00
|
|
|
import javax.ws.rs.HEAD;
|
2018-10-19 17:06:53 +02:00
|
|
|
import javax.ws.rs.Path;
|
|
|
|
import javax.ws.rs.PathParam;
|
2019-10-02 12:43:35 +02:00
|
|
|
import javax.ws.rs.QueryParam;
|
2018-12-14 14:26:37 +01:00
|
|
|
import javax.ws.rs.WebApplicationException;
|
2018-10-22 14:29:57 +02:00
|
|
|
import javax.ws.rs.core.Context;
|
2018-10-19 17:06:53 +02:00
|
|
|
import javax.ws.rs.core.Response;
|
2018-12-18 15:13:57 +01:00
|
|
|
import javax.ws.rs.core.Response.ResponseBuilder;
|
2018-10-19 17:06:53 +02:00
|
|
|
|
|
|
|
import org.gcube.common.storagehub.client.StreamDescriptor;
|
2018-12-17 14:54:57 +01:00
|
|
|
import org.gcube.common.storagehub.client.plugins.AbstractPlugin;
|
|
|
|
import org.gcube.common.storagehub.client.proxies.ItemManagerClient;
|
2019-09-27 11:56:00 +02:00
|
|
|
import org.gcube.datatransfer.resolver.ConstantsResolver;
|
|
|
|
import org.gcube.datatransfer.resolver.ConstantsResolver.CONTENT_DISPOSITION_VALUE;
|
2018-10-23 11:27:04 +02:00
|
|
|
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
|
2019-10-02 12:43:35 +02:00
|
|
|
import org.gcube.datatransfer.resolver.shub.StorageHubMetadataResponseBuilder;
|
|
|
|
import org.gcube.datatransfer.resolver.util.ValidateContentDisposition;
|
2018-10-24 10:08:13 +02:00
|
|
|
import org.gcube.smartgears.utils.InnerMethodName;
|
2018-10-22 14:29:57 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2018-12-14 14:26:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Class StorageHubResolver.
|
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
|
|
|
* Dec 14, 2018
|
|
|
|
*/
|
2019-02-19 15:18:19 +01:00
|
|
|
@Path("shub")
|
2018-10-22 17:09:05 +02:00
|
|
|
public class StorageHubResolver {
|
2018-10-22 14:29:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static final String STORAGE_HUB_ID = "id";
|
|
|
|
|
2018-10-22 17:09:05 +02:00
|
|
|
private static Logger logger = LoggerFactory.getLogger(StorageHubResolver.class);
|
2018-10-22 14:29:57 +02:00
|
|
|
|
2018-12-07 10:41:18 +01:00
|
|
|
private String help = "https://wiki.gcube-system.org/gcube/URI_Resolver#STORAGE-HUB_Resolver";
|
2018-10-22 15:39:51 +02:00
|
|
|
|
2018-10-19 17:06:53 +02:00
|
|
|
@RequestScoped
|
2018-10-22 14:29:57 +02:00
|
|
|
@PathParam(STORAGE_HUB_ID)
|
2018-10-19 17:06:53 +02:00
|
|
|
String id;
|
|
|
|
|
2018-12-14 14:26:37 +01:00
|
|
|
|
2018-12-28 11:02:53 +01:00
|
|
|
/**
|
|
|
|
* Gets the metadata.
|
|
|
|
*
|
|
|
|
* @param req the req
|
|
|
|
* @return the metadata
|
|
|
|
*/
|
|
|
|
@HEAD
|
2019-02-19 15:18:19 +01:00
|
|
|
@Path("/{id}")
|
2018-12-28 11:02:53 +01:00
|
|
|
public Response getMetadata(@Context HttpServletRequest req) {
|
|
|
|
logger.info(this.getClass().getSimpleName()+" HEAD getMetadata called");
|
|
|
|
|
|
|
|
try{
|
|
|
|
//TODO Do we need to check the token?
|
|
|
|
|
|
|
|
//Checking mandatory parameter id
|
|
|
|
if(id==null || id.isEmpty()){
|
|
|
|
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
|
|
|
|
throw ExceptionManager.badRequestException(req, "Missing mandatory path parameter "+STORAGE_HUB_ID, this.getClass(), help);
|
|
|
|
}
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
ItemManagerClient client = AbstractPlugin.item().build();
|
|
|
|
StreamDescriptor descriptor = client.resolvePublicLink(id);
|
2021-04-08 10:25:48 +02:00
|
|
|
ResponseBuilder response = Response.ok();
|
2019-09-27 11:56:00 +02:00
|
|
|
response = new StorageHubMetadataResponseBuilder(req, response).fillMetadata(descriptor, id, CONTENT_DISPOSITION_VALUE.attachment);
|
2018-12-28 11:02:53 +01:00
|
|
|
return response.build();
|
|
|
|
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Error on getting metadata for file with "+id, e);
|
|
|
|
String errorMsg = "Error on getting metadata for file with hub id '"+id+"'. "+e.getMessage();
|
|
|
|
throw ExceptionManager.internalErrorException(req, errorMsg, this.getClass(), help);
|
|
|
|
}
|
|
|
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
|
|
|
if(!(e instanceof WebApplicationException)){
|
|
|
|
//UNEXPECTED EXCEPTION managing it as WebApplicationException
|
|
|
|
String error = "Error occurred on resolving the StorageHub URL with id: "+id+". Please, contact the support!";
|
|
|
|
throw ExceptionManager.internalErrorException(req, error, this.getClass(), help);
|
|
|
|
}
|
|
|
|
//ALREADY MANAGED as WebApplicationException
|
|
|
|
logger.error("Exception:", e);
|
|
|
|
throw (WebApplicationException) e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-14 14:26:37 +01:00
|
|
|
/**
|
|
|
|
* Download.
|
|
|
|
*
|
|
|
|
* @param req the req
|
|
|
|
* @return the response
|
|
|
|
*/
|
2018-10-19 17:06:53 +02:00
|
|
|
@GET
|
2019-02-19 15:18:19 +01:00
|
|
|
@Path("/{id}")
|
2019-10-02 12:43:35 +02:00
|
|
|
public Response download(@Context HttpServletRequest req, @QueryParam(ConstantsResolver.QUERY_PARAM_CONTENTDISPOSITION) String contentDisposition) {
|
2018-12-28 11:04:10 +01:00
|
|
|
logger.info(this.getClass().getSimpleName()+" GET download called");
|
2018-10-22 14:29:57 +02:00
|
|
|
|
2018-10-22 17:09:05 +02:00
|
|
|
try{
|
2018-12-14 14:26:37 +01:00
|
|
|
InnerMethodName.instance.set("resolveStorageHubPublicLink");
|
2018-12-18 15:13:57 +01:00
|
|
|
|
2019-09-27 11:56:00 +02:00
|
|
|
//Checking mandatory parameter "id"
|
2018-12-14 14:26:37 +01:00
|
|
|
if(id==null || id.isEmpty()){
|
|
|
|
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
|
|
|
|
throw ExceptionManager.badRequestException(req, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help);
|
|
|
|
}
|
2019-09-27 11:56:00 +02:00
|
|
|
|
|
|
|
//Checking the optional parameter "Content-Disposition"
|
2019-10-02 12:43:35 +02:00
|
|
|
CONTENT_DISPOSITION_VALUE dispositionValue = CONTENT_DISPOSITION_VALUE.attachment;
|
2019-09-27 11:56:00 +02:00
|
|
|
//Validating the Content-Disposition value
|
2019-10-02 12:43:35 +02:00
|
|
|
dispositionValue = ValidateContentDisposition.validValue(req, this.getClass(), help, contentDisposition);
|
2018-12-14 14:26:37 +01:00
|
|
|
|
|
|
|
try{
|
2018-12-28 11:04:10 +01:00
|
|
|
|
2018-12-28 11:02:53 +01:00
|
|
|
ItemManagerClient client = AbstractPlugin.item().build();
|
2018-12-17 14:54:57 +01:00
|
|
|
StreamDescriptor descriptor = client.resolvePublicLink(id);
|
2018-12-28 11:02:53 +01:00
|
|
|
ResponseBuilder response = Response.ok(descriptor.getStream());
|
2018-12-18 15:13:57 +01:00
|
|
|
|
2019-10-02 12:43:35 +02:00
|
|
|
response = new StorageHubMetadataResponseBuilder(req, response).fillMetadata(descriptor, id, dispositionValue);
|
2018-12-18 15:13:57 +01:00
|
|
|
return response.build();
|
2018-12-14 14:26:37 +01:00
|
|
|
|
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Error on getting file with "+id, e);
|
|
|
|
String errorMsg = "Error on getting file with hub id '"+id+"'. "+e.getMessage();
|
|
|
|
throw ExceptionManager.internalErrorException(req, errorMsg, StorageHubResolver.class, help);
|
|
|
|
}
|
|
|
|
|
|
|
|
}catch (Exception e) {
|
|
|
|
|
|
|
|
if(!(e instanceof WebApplicationException)){
|
|
|
|
//UNEXPECTED EXCEPTION managing it as WebApplicationException
|
|
|
|
String error = "Error occurred on resolving the StorageHub URL with id: "+id+". Please, contact the support!";
|
|
|
|
throw ExceptionManager.internalErrorException(req, error, this.getClass(), help);
|
|
|
|
}
|
|
|
|
//ALREADY MANAGED AS WebApplicationException
|
|
|
|
logger.error("Exception:", e);
|
|
|
|
throw (WebApplicationException) e;
|
2018-10-22 17:09:05 +02:00
|
|
|
}
|
2018-10-19 17:06:53 +02:00
|
|
|
}
|
|
|
|
}
|