added try catch to handle error from StorageHubResolver

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@173751 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-10-22 15:09:05 +00:00
parent 88e87cfe43
commit d2ebd20f3c
3 changed files with 31 additions and 13 deletions

View File

@ -38,7 +38,7 @@ public class GisResolver {
if(mode==null){ if(mode==null){
logger.error("Path Parameter 'mode' not found"); logger.error("Path Parameter 'mode' not found");
try { try {
throw new BadRequestException(req, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter MODE", StorageHub.class, new URI(help)); throw new BadRequestException(req, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter MODE", StorageHubResolver.class, new URI(help));
} }
catch (URISyntaxException e) { catch (URISyntaxException e) {
//silent //silent
@ -48,7 +48,7 @@ public class GisResolver {
if(scope==null || scope.isEmpty()){ if(scope==null || scope.isEmpty()){
logger.error("Path Parameter 'scope' not found"); logger.error("Path Parameter 'scope' not found");
try { try {
throw new BadRequestException(req, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter MODE", StorageHub.class, new URI(help)); throw new BadRequestException(req, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter MODE", StorageHubResolver.class, new URI(help));
} }
catch (URISyntaxException e) { catch (URISyntaxException e) {
//silent //silent
@ -59,7 +59,7 @@ public class GisResolver {
if(visibility==null){ if(visibility==null){
logger.error("Path Parameter 'visibility' not found"); logger.error("Path Parameter 'visibility' not found");
try { try {
throw new BadRequestException(req, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter MODE", StorageHub.class, new URI(help)); throw new BadRequestException(req, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter MODE", StorageHubResolver.class, new URI(help));
} }
catch (URISyntaxException e) { catch (URISyntaxException e) {
//silent //silent

View File

@ -15,18 +15,19 @@ import javax.ws.rs.core.Response.Status;
import org.gcube.common.storagehub.client.StreamDescriptor; import org.gcube.common.storagehub.client.StreamDescriptor;
import org.gcube.common.storagehub.client.dsl.StorageHubClient; import org.gcube.common.storagehub.client.dsl.StorageHubClient;
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException; import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@Path("shub/{id}") @Path("shub/{id}")
public class StorageHub { public class StorageHubResolver {
/** /**
* *
*/ */
public static final String STORAGE_HUB_ID = "id"; public static final String STORAGE_HUB_ID = "id";
private static Logger logger = LoggerFactory.getLogger(StorageHub.class); private static Logger logger = LoggerFactory.getLogger(StorageHubResolver.class);
private String help = "https://wiki.gcube-system.org/gcube/URI_Resolver"; private String help = "https://wiki.gcube-system.org/gcube/URI_Resolver";
@ -37,36 +38,44 @@ public class StorageHub {
@GET @GET
@Path("") @Path("")
public Response download(@Context HttpServletRequest httpRequest) { public Response download(@Context HttpServletRequest httpRequest) {
logger.info(StorageHubResolver.class.getSimpleName() +" download called");
StorageHubClient shc = new StorageHubClient(); StorageHubClient shc = new StorageHubClient();
//Checking mandatory parameter id //Checking mandatory parameter id
if(id==null || id.isEmpty()){ if(id==null || id.isEmpty()){
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found"); logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
try { try {
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHub.class, new URI(help)); throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, new URI(help));
} }
catch (URISyntaxException e) { catch (URISyntaxException e) {
//silent //silent
} }
} }
StreamDescriptor descriptor = shc.open(id).asFile().download(); try{
StreamDescriptor descriptor = shc.open(id).asFile().download();
return Response
.ok(descriptor.getStream())
.header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build();
return Response }catch(Exception e){
.ok(descriptor.getStream()) logger.error("Error on getting file with "+id, e);
.header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build(); String errorMsg = "Error on getting file with hub id "+id+". "+e.getMessage();
throw new InternalServerException(httpRequest, Status.INTERNAL_SERVER_ERROR, errorMsg, StorageHubResolver.class, null);
}
} }
@GET @GET
@Path("{version}") @Path("{version}")
public Response downloadVersion(@Context HttpServletRequest httpRequest, @PathParam("version") String version) { public Response downloadVersion(@Context HttpServletRequest httpRequest, @PathParam("version") String version) {
logger.info(StorageHubResolver.class.getSimpleName() +" downloadVersion called");
StorageHubClient shc = new StorageHubClient(); StorageHubClient shc = new StorageHubClient();
//Checking mandatory parameter id //Checking mandatory parameter id
if(id==null || id.isEmpty()){ if(id==null || id.isEmpty()){
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found"); logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
try { try {
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHub.class, new URI(help)); throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, new URI(help));
} }
catch (URISyntaxException e) { catch (URISyntaxException e) {
//silent //silent
@ -77,16 +86,24 @@ public class StorageHub {
if(version==null || version.isEmpty()){ if(version==null || version.isEmpty()){
logger.error("Parameter 'version' not found"); logger.error("Parameter 'version' not found");
try { try {
throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory parameter 'version'", StorageHub.class, new URI(help)); throw new BadRequestException(httpRequest, Status.NOT_ACCEPTABLE, "Missing mandatory parameter 'version'", StorageHubResolver.class, new URI(help));
} }
catch (URISyntaxException e) { catch (URISyntaxException e) {
//silent //silent
} }
} }
try{
StreamDescriptor descriptor = shc.open(id).asFile().downloadSpecificVersion(version); StreamDescriptor descriptor = shc.open(id).asFile().downloadSpecificVersion(version);
return Response return Response
.ok(descriptor.getStream()) .ok(descriptor.getStream())
.header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build(); .header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build();
}catch(Exception e){
String errorMsg = "Error on getting versioned file with hub id "+id+ " and version: "+version;
logger.error(errorMsg, e);
throw new InternalServerException(httpRequest, Status.INTERNAL_SERVER_ERROR, errorMsg, StorageHubResolver.class, null);
}
} }
} }

View File

@ -61,7 +61,8 @@ public class StorageIDResolver {
* @return the storage id * @return the storage id
*/ */
@GET @GET
@Path("{storage-id:(?!id)[^/?$]*}") //@Path("{storage-id:(?!index.jsp)[^/?$]*}")
@Path("{storage-id}")
public Response getStorageId(@Context HttpServletRequest httpRequest, @PathParam(STORAGE_ID) String storageId, @QueryParam(ConstantsResolver.FILE_NAME) String fileName, @QueryParam(ConstantsResolver.CONTENT_TYPE) String contentType, @QueryParam(ConstantsResolver.VALIDATION) Boolean validation) { public Response getStorageId(@Context HttpServletRequest httpRequest, @PathParam(STORAGE_ID) String storageId, @QueryParam(ConstantsResolver.FILE_NAME) String fileName, @QueryParam(ConstantsResolver.CONTENT_TYPE) String contentType, @QueryParam(ConstantsResolver.VALIDATION) Boolean validation) {
logger.info("resolve Storage-Id called"); logger.info("resolve Storage-Id called");
//Checking mandatory parameter storageId //Checking mandatory parameter storageId