diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsCreateResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsCreateResolver.java index 17d0270..a2d1dc9 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsCreateResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsCreateResolver.java @@ -163,7 +163,9 @@ public class AnalyticsCreateResolver { if(!(e instanceof WebApplicationException)){ //UNEXPECTED EXCEPTION managing it as WebApplicationException - String error = "Error occurred on creating the Analytics for the request "+body+". Please, contact the support!"+"\nCause: "+e.getCause().getMessage(); + String error = "Error occurred on creating the Analytics for the request "+body+". Please, contact the support!"; + if(e.getCause()!=null) + error+="\n\nCaused: "+e.getCause().getMessage(); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); } //ALREADY MANAGED AS WebApplicationException diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsGetResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsGetResolver.java index 43a6de8..5daca05 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsGetResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsGetResolver.java @@ -88,7 +88,9 @@ public class AnalyticsGetResolver { if(!(e instanceof WebApplicationException)){ //UNEXPECTED EXCEPTION managing it as WebApplicationException - String error = "Error occurred on resolving the Analytics URL. Please, contact the support!"+"\nCause: "+e.getCause().getMessage(); + String error = "Error occurred on resolving the Analytics URL. Please, contact the support!"; + if(e.getCause()!=null) + error+="\n\nCaused: "+e.getCause().getMessage(); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); } //ALREADY MANAGED AS WebApplicationException diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java index bc900da..41dc4f7 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java @@ -80,7 +80,9 @@ public class CatalogueResolver { if(!(e instanceof WebApplicationException)){ //UNEXPECTED EXCEPTION managing it as WebApplicationException - String error = "Error occurred on resolving the Catalgoue URL. Please, contact the support!"+"\nCause: "+e.getCause().getMessage(); + String error = "Error occurred on resolving the Catalgoue URL. Please, contact the support!"; + if(e.getCause()!=null) + error+="\n\nCaused: "+e.getCause().getMessage(); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); } //ALREADY MANAGED AS WebApplicationException diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/GeonetworkResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/GeonetworkResolver.java index b00b372..d177d31 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/GeonetworkResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/GeonetworkResolver.java @@ -464,7 +464,9 @@ public class GeonetworkResolver { if(!(e instanceof WebApplicationException)){ //UNEXPECTED EXCEPTION managing it as WebApplicationException - String error = "Error during perform POST operation to: "+gnCSWlURL+". Please, contact the support!"+"\nCause: "+e.getCause().getMessage(); + String error = "Error during perform POST operation to: "+gnCSWlURL+". Please, contact the support!"; + if(e.getCause()!=null) + error+="\n\nCaused: "+e.getCause().getMessage(); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); } //ALREADY MANAGED AS WebApplicationException diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/GisResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/GisResolver.java index e42d229..3d0a116 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/GisResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/GisResolver.java @@ -166,7 +166,9 @@ public class GisResolver { if(!(e instanceof WebApplicationException)){ //UNEXPECTED EXCEPTION managing it as WebApplicationException - String error = "Sorry, an error occurred on resolving request with UUID "+gisUUID+" and scope "+scope+". Please, contact support!"+"\n Cause: "+e.getCause().getMessage(); + String error = "Sorry, an error occurred on resolving request with UUID "+gisUUID+" and scope "+scope+". Please, contact support!"; + if(e.getCause()!=null) + error+="\n\nCaused: "+e.getCause().getMessage(); throw ExceptionManager.internalErrorException(req, error, this.getClass(), help); } //ALREADY MANAGED AS WebApplicationException diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/KnimeCreateResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/KnimeCreateResolver.java index c95c9e2..8fca4f2 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/KnimeCreateResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/KnimeCreateResolver.java @@ -86,7 +86,9 @@ public class KnimeCreateResolver { if(!(e instanceof WebApplicationException)){ //UNEXPECTED EXCEPTION managing it as WebApplicationException - String error = "Sorry, an error occurred on creating Knime URL. Please, contact the support!"+"\n Cause: "+e.getCause().getMessage(); + String error = "Sorry, an error occurred on creating Knime URL. Please, contact the support!"; + if(e.getCause()!=null) + error+="\n\nCaused: "+e.getCause().getMessage(); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); } //ALREADY MANAGED AS WebApplicationException diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/KnimeGetResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/KnimeGetResolver.java index 3ba6450..748b2e8 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/KnimeGetResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/KnimeGetResolver.java @@ -86,7 +86,9 @@ public class KnimeGetResolver { if(!(e instanceof WebApplicationException)){ //UNEXPECTED EXCEPTION managing it as WebApplicationException - String error = "Sorry, an error occurred on resolving the Knime URL. Please, contact the support!"+"\n Cause: "+e.getCause().getMessage(); + String error = "Sorry, an error occurred on resolving the Knime URL. Please, contact the support!"; + if(e.getCause()!=null) + error+="\n\nCaused: "+e.getCause().getMessage(); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); } //ALREADY MANAGED AS WebApplicationException diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/PartheosRegistryResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/PartheosRegistryResolver.java index 5cfe3c3..8e73cb1 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/PartheosRegistryResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/PartheosRegistryResolver.java @@ -85,7 +85,9 @@ public class PartheosRegistryResolver { if(!(e instanceof WebApplicationException)){ //UNEXPECTED EXCEPTION managing it as WebApplicationException - String error = "Error occurred on resolving the path "+remainPathParthenosURL+". Please, contact the support!"+"\n Cause: "+e.getCause().getMessage(); + String error = "Error occurred on resolving the path "+remainPathParthenosURL+". Please, contact the support!"; + if(e.getCause()!=null) + error+="\n\nCaused: "+e.getCause().getMessage(); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); } //ALREADY MANAGED AS WebApplicationException @@ -133,7 +135,7 @@ public class PartheosRegistryResolver { if(!(e instanceof WebApplicationException)){ //UNEXPECTED EXCEPTION managing it as WebApplicationException String error = "Error occurred on transforming the "+jsonRequest+". Please, contact the support!"; - ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); } //ALREADY MANAGED AS WebApplicationException logger.error("Exception:", e); diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/SMPIDResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/SMPIDResolver.java index 2e73fcd..9ec8dca 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/SMPIDResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/SMPIDResolver.java @@ -8,6 +8,7 @@ import javax.servlet.http.HttpServletRequest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.QueryParam; +import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; @@ -28,32 +29,54 @@ public class SMPIDResolver { /** * */ - private static final String help = "https://wiki.gcube-system.org/gcube/URI_Resolver#SMP-ID_Resolver"; + private static final String helpURI = "https://wiki.gcube-system.org/gcube/URI_Resolver#SMP-ID_Resolver"; private static final String SMP_ID = "smp-id"; private static Logger logger = LoggerFactory.getLogger(SMPIDResolver.class); + + /** * Gets the smpid. * + * @param req the req * @param smpId the smp id * @param fileName the file name * @param contentType the content type * @param validation the validation * @return the smpid + * @throws WebApplicationException the web application exception */ @GET @Path("id") - public Response getSMPID(@Context HttpServletRequest httpRequest, @QueryParam(SMP_ID) @Nullable String smpId, @QueryParam(ConstantsResolver.QUERY_PARAM_FILE_NAME) String fileName, @QueryParam(ConstantsResolver.QUERY_PARAM_CONTENT_TYPE) String contentType, @QueryParam(ConstantsResolver.QUERY_PARAM_VALIDATION) Boolean validation){ + public Response getSMPID(@Context HttpServletRequest req, + @QueryParam(SMP_ID) @Nullable String smpId, + @QueryParam(ConstantsResolver.QUERY_PARAM_FILE_NAME) String fileName, + @QueryParam(ConstantsResolver.QUERY_PARAM_CONTENT_TYPE) String contentType, + @QueryParam(ConstantsResolver.QUERY_PARAM_VALIDATION) Boolean validation) throws WebApplicationException{ logger.info(this.getClass().getSimpleName()+" GET starts..."); - //Checking mandatory parameter smpId - if(smpId==null || smpId.isEmpty()){ - logger.error(SMP_ID+" not found"); - ExceptionManager.badRequestException(httpRequest, "Missing mandatory parameter "+SMP_ID, SMPIDResolver.class, help); + try{ + //Checking mandatory parameter smpId + if(smpId==null || smpId.isEmpty()){ + logger.error(SMP_ID+" not found"); + throw ExceptionManager.badRequestException(req, "Missing mandatory parameter "+SMP_ID, SMPIDResolver.class, helpURI); + } + + return StorageIDResolver.resolveStorageId(req, smpId, fileName, contentType, validation); + + }catch (Exception e) { + + if(!(e instanceof WebApplicationException)){ + //UNEXPECTED EXCEPTION managing it as WebApplicationException + String error = "Error occurred on resolving the "+SMP_ID+": "+smpId+". Please, contact the support!"; + if(e.getCause()!=null) + error+="\n\nCaused: "+e.getCause().getMessage(); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + } + //ALREADY MANAGED AS WebApplicationException + logger.error("Exception:", e); + throw (WebApplicationException) e; } - - return StorageIDResolver.resolveStorageId(httpRequest, smpId, fileName, contentType, validation); - } } diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/SMPResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/SMPResolver.java index 43744a7..045f6c5 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/SMPResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/SMPResolver.java @@ -21,8 +21,8 @@ import org.slf4j.LoggerFactory; /** * The Class SMPResolver. * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Oct 22, 2018 + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * Dec 14, 2018 */ @Path("/") public class SMPResolver { @@ -36,7 +36,6 @@ public class SMPResolver { private static Logger logger = LoggerFactory.getLogger(SMPResolver.class); - /** * Gets the smpuri. * @@ -60,11 +59,10 @@ public class SMPResolver { logger.info(this.getClass().getSimpleName()+" GET starts..."); try{ - //Checking mandatory parameter smpURI if(smpURI==null || smpURI.isEmpty()){ logger.error(SMP_URI+" not found"); - ExceptionManager.badRequestException(req, "Missing mandatory parameter "+SMP_URI, SMPResolver.class, helpURI); + throw ExceptionManager.badRequestException(req, "Missing mandatory parameter "+SMP_URI, SMPResolver.class, helpURI); } return StorageIDResolver.resolveStorageId(req, smpURI, fileName, contentType, validation); @@ -74,7 +72,7 @@ public class SMPResolver { if(!(e instanceof WebApplicationException)){ //UNEXPECTED EXCEPTION managing it as WebApplicationException String error = "Error occurred on resolving the smpURI "+smpURI+". Please, contact the support!"; - ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); } //ALREADY MANAGED AS WebApplicationException logger.error("Exception:", e); diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/StorageHubResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/StorageHubResolver.java index 3d22fea..1a7961a 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/StorageHubResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/StorageHubResolver.java @@ -5,6 +5,7 @@ import javax.servlet.http.HttpServletRequest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; @@ -15,6 +16,13 @@ import org.gcube.smartgears.utils.InnerMethodName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + +/** + * The Class StorageHubResolver. + * + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * Dec 14, 2018 + */ @Path("shub/{id}") public class StorageHubResolver { @@ -31,63 +39,103 @@ public class StorageHubResolver { @PathParam(STORAGE_HUB_ID) String id; + + /** + * Download. + * + * @param req the req + * @return the response + */ @GET @Path("") - public Response download(@Context HttpServletRequest httpRequest) { - InnerMethodName.instance.set("resolveStorageHubPublicLink"); + public Response download(@Context HttpServletRequest req) { logger.info(this.getClass().getSimpleName()+" GET starts..."); - StorageHubClient shc = new StorageHubClient(); - - //Checking mandatory parameter id - if(id==null || id.isEmpty()){ - logger.error("Path Parameter "+STORAGE_HUB_ID+" not found"); - ExceptionManager.badRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help); - } try{ - StreamDescriptor descriptor = shc.open(id).asFile().download(); - return Response - .ok(descriptor.getStream()) - .header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build(); + InnerMethodName.instance.set("resolveStorageHubPublicLink"); + StorageHubClient shc = new StorageHubClient(); - }catch(Exception e){ - logger.error("Error on getting file with "+id, e); - String errorMsg = "Error on getting file with hub id '"+id+"'. "+e.getMessage(); - ExceptionManager.internalErrorException(httpRequest, errorMsg, StorageHubResolver.class, help); - return null; + //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, StorageHubResolver.class, help); + } + + try{ + StreamDescriptor descriptor = shc.open(id).asFile().download(); + 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 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; } } + /** + * Download version. + * + * @param req the req + * @param version the version + * @return the response + */ @GET @Path("{version}") - public Response downloadVersion(@Context HttpServletRequest httpRequest, @PathParam("version") String version) { - InnerMethodName.instance.set("resolveStorageHubPublicLinkWithVersion"); - logger.info(this.getClass().getSimpleName() +" downloadVersion called"); - StorageHubClient shc = new StorageHubClient(); + public Response downloadVersion(@Context HttpServletRequest req, @PathParam("version") String version) { + logger.info(this.getClass().getSimpleName() +" GET downloadVersion called"); - //Checking mandatory parameter id - if(id==null || id.isEmpty()){ - logger.error("Path Parameter "+STORAGE_HUB_ID+" not found"); - ExceptionManager.badRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help); - } - - //Checking mandatory parameter id - if(version==null || version.isEmpty()){ - logger.error("Parameter 'version' not found"); - ExceptionManager.badRequestException(httpRequest, "Missing mandatory parameter 'version'", StorageHubResolver.class, help); - } try{ + InnerMethodName.instance.set("resolveStorageHubPublicLinkWithVersion"); + StorageHubClient shc = new StorageHubClient(); - StreamDescriptor descriptor = shc.open(id).asFile().downloadSpecificVersion(version); - return Response - .ok(descriptor.getStream()) - .header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build(); + //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, StorageHubResolver.class, help); + } - }catch(Exception e){ - String errorMsg = "Error on getting versioned file with hub id '"+id+ "' and version '"+version+"'"; - logger.error(errorMsg, e); - ExceptionManager.internalErrorException(httpRequest, errorMsg, StorageHubResolver.class, help); - return null; + //Checking mandatory parameter id + if(version==null || version.isEmpty()){ + logger.error("Parameter 'version' not found"); + throw ExceptionManager.badRequestException(req, "Missing mandatory parameter 'version'", StorageHubResolver.class, help); + } + 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 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+" and version: "+version+". Please, contact the support!"; + throw ExceptionManager.internalErrorException(req, error, this.getClass(), help); + } + //ALREADY MANAGED AS WebApplicationException + logger.error("Exception:", e); + throw (WebApplicationException) e; } } } diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/StorageIDResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/StorageIDResolver.java index f8a719a..30eb13f 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/StorageIDResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/StorageIDResolver.java @@ -3,13 +3,13 @@ package org.gcube.datatransfer.resolver.services; import java.io.IOException; import java.io.InputStream; -import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.GET; import javax.ws.rs.HEAD; 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.Context; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; @@ -29,13 +29,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; + /** - * The Class StorageManager. + * The Class StorageIDResolver. * * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) - * @author Lucio Lelii - * - * Oct 19, 2018 + * Dec 14, 2018 */ @Path("storage") public class StorageIDResolver { @@ -54,23 +53,43 @@ public class StorageIDResolver { /** * Gets the storage id. * - * @param httpRequest the http request + * @param req the req * @param storageId the storage id * @param fileName the file name * @param contentType the content type * @param validation the validation * @return the storage id + * @throws WebApplicationException the web application exception */ @GET @Path("{storage-id}") - public Response getStorageId(@Context HttpServletRequest httpRequest, @PathParam(STORAGE_ID) String storageId, @QueryParam(ConstantsResolver.QUERY_PARAM_FILE_NAME) String fileName, @QueryParam(ConstantsResolver.QUERY_PARAM_CONTENT_TYPE) String contentType, @QueryParam(ConstantsResolver.QUERY_PARAM_VALIDATION) Boolean validation) { + public Response getStorageId(@Context HttpServletRequest req, + @PathParam(STORAGE_ID) String storageId, + @QueryParam(ConstantsResolver.QUERY_PARAM_FILE_NAME) String fileName, + @QueryParam(ConstantsResolver.QUERY_PARAM_CONTENT_TYPE) String contentType, + @QueryParam(ConstantsResolver.QUERY_PARAM_VALIDATION) Boolean validation) throws WebApplicationException { + logger.info(this.getClass().getSimpleName()+" GET starts..."); - //Checking mandatory parameter storageId - if(storageId==null || storageId.isEmpty()){ - logger.error(STORAGE_ID+" not found"); - ExceptionManager.badRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help); + + try{ + if(storageId==null || storageId.isEmpty()){ + logger.error(STORAGE_ID+" not found"); + throw ExceptionManager.badRequestException(req, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help); + } + return resolveStorageId(req, storageId, fileName, contentType, validation); + }catch (Exception e) { + + if(!(e instanceof WebApplicationException)){ + //UNEXPECTED EXCEPTION managing it as WebApplicationException + String error = "Error occurred on resolving the Storage ID: "+storageId+". Please, contact the support!"; + if(e.getCause()!=null) + error+="\n\nCaused: "+e.getCause().getMessage(); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), help); + } + //ALREADY MANAGED AS WebApplicationException + logger.error("Exception:", e); + throw (WebApplicationException) e; } - return resolveStorageId(httpRequest, storageId, fileName, contentType, validation); } @@ -83,15 +102,16 @@ public class StorageIDResolver { * @param contentType the content type * @param validation the validation * @return the response + * @throws Exception the exception */ - protected static Response resolveStorageId(HttpServletRequest httpRequest, String storageId, String fileName, String contentType, Boolean validation) { + protected static Response resolveStorageId(HttpServletRequest httpRequest, String storageId, String fileName, String contentType, Boolean validation) throws Exception{ logger.info("storage-id: "+storageId+", fileName: "+fileName+", contentType: "+contentType+", validation: "+validation); //Checking mandatory parameter storageId if (storageId == null || storageId.isEmpty()) { logger.error("storageId not found"); - ExceptionManager.badRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help); + throw ExceptionManager.badRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help); } StorageClient client = getStorageClientInstance(storageId); @@ -104,12 +124,12 @@ public class StorageIDResolver { logger.info("Decoded ID"+" = "+ toSEID); }catch(Exception e){ logger.error("Storage Client Exception when getting file from storage: ", e); - ExceptionManager.notFoundException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help); + throw ExceptionManager.notFoundException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help); } if(toSEID==null || toSEID.isEmpty()){ logger.error("Decrypted id for storageId: "+storageId +" is null or empty!"); - ExceptionManager.notFoundException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help); + throw ExceptionManager.notFoundException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help); } long size = iClient.getSize().RFileById(toSEID); @@ -144,7 +164,10 @@ public class StorageIDResolver { .ok(so) .header(ConstantsResolver.CONTENT_DISPOSITION,"attachment; filename = \""+fileName+"\"") .header(ConstantsResolver.CONTENT_LENGTH, size); - if (contentType!= null) response.header("Content-Type",contentType); + + if (contentType!= null) + response.header("Content-Type",contentType); + return response.build(); } @@ -153,43 +176,60 @@ public class StorageIDResolver { /** * Http do head. * - * @param httpRequest the http request + * @param req the http request * @param storageId the storage id * @param hproxycheck the hproxycheck * @return the response - * @throws ServletException the servlet exception - * @throws IOException Signals that an I/O exception has occurred. + * @throws WebApplicationException the web application exception */ @HEAD @Path("{storage-id}") - public Response httpDoHead(@Context HttpServletRequest httpRequest, @PathParam(STORAGE_ID) String storageId, @QueryParam(ConstantsResolver.HPC) Boolean hproxycheck) throws ServletException, IOException { + public Response httpDoHead(@Context HttpServletRequest req, + @PathParam(STORAGE_ID) String storageId, + @QueryParam(ConstantsResolver.HPC) Boolean hproxycheck) throws WebApplicationException { + logger.info(this.getClass().getSimpleName()+" HEAD starts..."); - //THIS IS FOR HPROXY CHECK - if(hproxycheck==null || hproxycheck){ - logger.trace("returning status 200 for Hproxy check"); - ResponseBuilder response = Response.status(HttpStatus.SC_OK); - return response.build(); + try{ + //THIS IS FOR HPROXY CHECK + if(hproxycheck==null || hproxycheck){ + logger.trace("returning status 200 for Hproxy check"); + ResponseBuilder response = Response.status(HttpStatus.SC_OK); + return response.build(); + } + + return validationPayload(req, storageId); + + }catch (Exception e) { + + if(!(e instanceof WebApplicationException)){ + //UNEXPECTED EXCEPTION managing it as WebApplicationException + String error = "Error occurred on resolving the Storage ID: "+storageId+". Please, contact the support!"; + if(e.getCause()!=null) + error+="\n\nCaused: "+e.getCause().getMessage(); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), help); + } + //ALREADY MANAGED AS WebApplicationException + logger.error("Exception:", e); + throw (WebApplicationException) e; } - - return validationPayload(httpRequest, storageId); } - /** * Validation payload. * * @param httpRequest the http request * @param storageId the storage id * @return the response + * @throws Exception the exception */ - protected Response validationPayload(HttpServletRequest httpRequest, String storageId){ + protected Response validationPayload(HttpServletRequest httpRequest, String storageId) throws Exception{ //Checking mandatory parameter storageId if (storageId == null || storageId.isEmpty()) { logger.warn("storageId not found"); - ExceptionManager.badRequestException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, this.getClass(), help); + throw ExceptionManager.badRequestException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, this.getClass(), help); } StorageClient client = getStorageClientInstance(storageId); String toSEID = null; @@ -200,12 +240,12 @@ public class StorageIDResolver { logger.debug("Decoded ID"+" = "+ toSEID); }catch(Exception e){ logger.error("Storage Client Exception when getting file from storage: ", e); - ExceptionManager.internalErrorException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help); + throw ExceptionManager.internalErrorException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help); } if(toSEID==null || toSEID.isEmpty()){ logger.error("Decrypted id for storageId: "+storageId +" is null or empty!"); - ExceptionManager.notFoundException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help); + throw ExceptionManager.notFoundException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help); } @@ -223,15 +263,15 @@ public class StorageIDResolver { IOUtils.closeQuietly(streamToWrite); response = Response.status(HttpStatus.SC_OK); }else - ExceptionManager.notFoundException(httpRequest, "The file with id: "+storageId+" is missing in the storage", StorageIDResolver.class, help); + throw ExceptionManager.notFoundException(httpRequest, "The file with id: "+storageId+" is missing in the storage", StorageIDResolver.class, help); } catch (IOException e2) { logger.error("Error on validating the file: ",e2); - ExceptionManager.internalErrorException(httpRequest, "Error on validating the file with id: "+storageId, StorageIDResolver.class, help); + throw ExceptionManager.internalErrorException(httpRequest, "Error on validating the file with id: "+storageId, StorageIDResolver.class, help); } if(response==null) - ExceptionManager.internalErrorException(httpRequest, "Error on validating the file with id: "+storageId, StorageIDResolver.class, help); + throw ExceptionManager.internalErrorException(httpRequest, "Error on validating the file with id: "+storageId, StorageIDResolver.class, help); return response.build(); @@ -243,8 +283,9 @@ public class StorageIDResolver { * * @param storageId the storage id * @return the storage client instance + * @throws Exception the exception */ - protected static StorageClient getStorageClientInstance(String storageId){ + protected static StorageClient getStorageClientInstance(String storageId) throws Exception{ MemoryType memory=null; if(storageId.endsWith(org.gcube.contentmanagement.blobstorage.transport.backend.util.Costants.VOLATILE_URL_IDENTIFICATOR)){ diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/UriResolverIndex.java b/src/main/java/org/gcube/datatransfer/resolver/services/UriResolverIndex.java index 7b71c5b..44cc1c5 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/UriResolverIndex.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/UriResolverIndex.java @@ -11,11 +11,11 @@ import javax.servlet.http.HttpServletRequest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response.Status; -import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException; +import org.gcube.datatransfer.resolver.services.error.ExceptionManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,7 +32,7 @@ public class UriResolverIndex { @GET @Produces({MediaType.TEXT_HTML}) @Path("index") - public InputStream index(@Context HttpServletRequest req){ + public InputStream index(@Context HttpServletRequest req) throws WebApplicationException{ String indexFile = "/WEB-INF/jsp/index.jsp"; @@ -40,11 +40,17 @@ public class UriResolverIndex { logger.info("UriResolverIndex called"); String realPath = req.getServletContext().getRealPath(indexFile); return new FileInputStream(new File(realPath)); - }catch(Exception e){ - logger.error("Error on getting "+indexFile, e); - throw new InternalServerException(req, Status.NOT_FOUND, "Index.jsp not found", UriResolverIndex.class, null); - } + }catch (Exception e) { + if(!(e instanceof WebApplicationException)){ + //UNEXPECTED EXCEPTION managing it as WebApplicationException + String error = "Index.jsp not found. Please, contact the support!"; + throw ExceptionManager.internalErrorException(req, error, this.getClass(), null); + } + //ALREADY MANAGED AS WebApplicationException + logger.error("Exception:", e); + throw (WebApplicationException) e; + } } @GET @@ -53,12 +59,5 @@ public class UriResolverIndex { public InputStream info(@Context HttpServletRequest req){ return index(req); } - -// @GET -// @Produces({MediaType.TEXT_HTML}) -// @Path("") -// public InputStream emptyPath(@Context HttpServletRequest req){ -// return index(req); -// } }