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){
logger.error("Path Parameter 'mode' not found");
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) {
//silent
@ -48,7 +48,7 @@ public class GisResolver {
if(scope==null || scope.isEmpty()){
logger.error("Path Parameter 'scope' not found");
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) {
//silent
@ -59,7 +59,7 @@ public class GisResolver {
if(visibility==null){
logger.error("Path Parameter 'visibility' not found");
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) {
//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.dsl.StorageHubClient;
import org.gcube.datatransfer.resolver.services.exceptions.BadRequestException;
import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Path("shub/{id}")
public class StorageHub {
public class StorageHubResolver {
/**
*
*/
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";
@ -37,36 +38,44 @@ public class StorageHub {
@GET
@Path("")
public Response download(@Context HttpServletRequest httpRequest) {
logger.info(StorageHubResolver.class.getSimpleName() +" download called");
StorageHubClient shc = new StorageHubClient();
//Checking mandatory parameter id
if(id==null || id.isEmpty()){
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
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) {
//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
.ok(descriptor.getStream())
.header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build();
}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 new InternalServerException(httpRequest, Status.INTERNAL_SERVER_ERROR, errorMsg, StorageHubResolver.class, null);
}
}
@GET
@Path("{version}")
public Response downloadVersion(@Context HttpServletRequest httpRequest, @PathParam("version") String version) {
logger.info(StorageHubResolver.class.getSimpleName() +" downloadVersion called");
StorageHubClient shc = new StorageHubClient();
//Checking mandatory parameter id
if(id==null || id.isEmpty()){
logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
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) {
//silent
@ -77,16 +86,24 @@ public class StorageHub {
if(version==null || version.isEmpty()){
logger.error("Parameter 'version' not found");
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) {
//silent
}
}
try{
StreamDescriptor descriptor = shc.open(id).asFile().downloadSpecificVersion(version);
return Response
.ok(descriptor.getStream())
.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
*/
@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) {
logger.info("resolve Storage-Id called");
//Checking mandatory parameter storageId