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;
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
import javax.ws.rs.PathParam;
|
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;
|
|
|
|
|
|
|
|
import org.gcube.common.storagehub.client.StreamDescriptor;
|
|
|
|
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
|
2018-10-23 11:27:04 +02:00
|
|
|
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
|
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-10-19 17:06:53 +02:00
|
|
|
|
|
|
|
@Path("shub/{id}")
|
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-10-22 15:39:51 +02:00
|
|
|
private String help = "https://wiki.gcube-system.org/gcube/URI_Resolver";
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@Path("")
|
2018-10-22 14:29:57 +02:00
|
|
|
public Response download(@Context HttpServletRequest httpRequest) {
|
2018-10-24 10:08:13 +02:00
|
|
|
InnerMethodName.instance.set("resolveStorageHubPublicLink");
|
2018-10-23 15:02:50 +02:00
|
|
|
logger.info(this.getClass().getSimpleName()+" GET starts...");
|
2018-10-19 17:06:53 +02:00
|
|
|
StorageHubClient shc = new StorageHubClient();
|
2018-10-22 14:29:57 +02:00
|
|
|
|
|
|
|
//Checking mandatory parameter id
|
|
|
|
if(id==null || id.isEmpty()){
|
|
|
|
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
|
2018-10-23 11:27:04 +02:00
|
|
|
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help);
|
2018-10-22 14:29:57 +02:00
|
|
|
}
|
|
|
|
|
2018-10-22 17:09:05 +02:00
|
|
|
try{
|
|
|
|
StreamDescriptor descriptor = shc.open(id).asFile().download();
|
|
|
|
return Response
|
|
|
|
.ok(descriptor.getStream())
|
|
|
|
.header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build();
|
2018-10-22 14:29:57 +02:00
|
|
|
|
2018-10-22 17:09:05 +02:00
|
|
|
}catch(Exception e){
|
|
|
|
logger.error("Error on getting file with "+id, e);
|
2018-10-22 17:09:54 +02:00
|
|
|
String errorMsg = "Error on getting file with hub id '"+id+"'. "+e.getMessage();
|
2018-10-23 11:27:04 +02:00
|
|
|
ExceptionManager.throwInternalErrorException(httpRequest, errorMsg, StorageHubResolver.class, help);
|
|
|
|
return null;
|
2018-10-22 17:09:05 +02:00
|
|
|
}
|
2018-10-19 17:06:53 +02:00
|
|
|
}
|
2018-10-22 14:29:57 +02:00
|
|
|
|
2018-10-19 17:06:53 +02:00
|
|
|
@GET
|
|
|
|
@Path("{version}")
|
2018-10-22 14:29:57 +02:00
|
|
|
public Response downloadVersion(@Context HttpServletRequest httpRequest, @PathParam("version") String version) {
|
2018-10-24 10:08:13 +02:00
|
|
|
InnerMethodName.instance.set("resolveStorageHubPublicLinkWithVersion");
|
2018-10-22 17:09:05 +02:00
|
|
|
logger.info(StorageHubResolver.class.getSimpleName() +" downloadVersion called");
|
2018-10-19 17:06:53 +02:00
|
|
|
StorageHubClient shc = new StorageHubClient();
|
2018-10-22 14:29:57 +02:00
|
|
|
|
|
|
|
//Checking mandatory parameter id
|
|
|
|
if(id==null || id.isEmpty()){
|
|
|
|
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
|
2018-10-23 11:27:04 +02:00
|
|
|
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help);
|
2018-10-22 14:29:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Checking mandatory parameter id
|
|
|
|
if(version==null || version.isEmpty()){
|
|
|
|
logger.error("Parameter 'version' not found");
|
2018-10-23 11:27:04 +02:00
|
|
|
ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory parameter 'version'", StorageHubResolver.class, help);
|
2018-10-22 14:29:57 +02:00
|
|
|
}
|
2018-10-22 17:09:05 +02:00
|
|
|
try{
|
|
|
|
|
2018-10-22 14:29:57 +02:00
|
|
|
|
2018-10-23 11:27:04 +02:00
|
|
|
StreamDescriptor descriptor = shc.open(id).asFile().downloadSpecificVersion(version);
|
|
|
|
return Response
|
|
|
|
.ok(descriptor.getStream())
|
|
|
|
.header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build();
|
2018-10-22 17:09:05 +02:00
|
|
|
|
|
|
|
}catch(Exception e){
|
2018-10-22 17:09:54 +02:00
|
|
|
String errorMsg = "Error on getting versioned file with hub id '"+id+ "' and version '"+version+"'";
|
2018-10-22 17:09:05 +02:00
|
|
|
logger.error(errorMsg, e);
|
2018-10-23 11:27:04 +02:00
|
|
|
ExceptionManager.throwInternalErrorException(httpRequest, errorMsg, StorageHubResolver.class, help);
|
|
|
|
return null;
|
2018-10-22 17:09:05 +02:00
|
|
|
}
|
2018-10-19 17:06:53 +02:00
|
|
|
}
|
|
|
|
}
|