managed completely the exceptions

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@174894 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-12-14 13:26:37 +00:00
parent dd7102e835
commit 63a1755653
13 changed files with 242 additions and 117 deletions

View File

@ -163,7 +163,9 @@ public class AnalyticsCreateResolver {
if(!(e instanceof WebApplicationException)){ if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as 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); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI);
} }
//ALREADY MANAGED AS WebApplicationException //ALREADY MANAGED AS WebApplicationException

View File

@ -88,7 +88,9 @@ public class AnalyticsGetResolver {
if(!(e instanceof WebApplicationException)){ if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as 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); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI);
} }
//ALREADY MANAGED AS WebApplicationException //ALREADY MANAGED AS WebApplicationException

View File

@ -80,7 +80,9 @@ public class CatalogueResolver {
if(!(e instanceof WebApplicationException)){ if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as 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); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI);
} }
//ALREADY MANAGED AS WebApplicationException //ALREADY MANAGED AS WebApplicationException

View File

@ -464,7 +464,9 @@ public class GeonetworkResolver {
if(!(e instanceof WebApplicationException)){ if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as 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); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI);
} }
//ALREADY MANAGED AS WebApplicationException //ALREADY MANAGED AS WebApplicationException

View File

@ -166,7 +166,9 @@ public class GisResolver {
if(!(e instanceof WebApplicationException)){ if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as 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); throw ExceptionManager.internalErrorException(req, error, this.getClass(), help);
} }
//ALREADY MANAGED AS WebApplicationException //ALREADY MANAGED AS WebApplicationException

View File

@ -86,7 +86,9 @@ public class KnimeCreateResolver {
if(!(e instanceof WebApplicationException)){ if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as 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); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI);
} }
//ALREADY MANAGED AS WebApplicationException //ALREADY MANAGED AS WebApplicationException

View File

@ -86,7 +86,9 @@ public class KnimeGetResolver {
if(!(e instanceof WebApplicationException)){ if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as 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); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI);
} }
//ALREADY MANAGED AS WebApplicationException //ALREADY MANAGED AS WebApplicationException

View File

@ -85,7 +85,9 @@ public class PartheosRegistryResolver {
if(!(e instanceof WebApplicationException)){ if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as 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); throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI);
} }
//ALREADY MANAGED AS WebApplicationException //ALREADY MANAGED AS WebApplicationException
@ -133,7 +135,7 @@ public class PartheosRegistryResolver {
if(!(e instanceof WebApplicationException)){ if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as WebApplicationException //UNEXPECTED EXCEPTION managing it as WebApplicationException
String error = "Error occurred on transforming the "+jsonRequest+". Please, contact the support!"; 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 //ALREADY MANAGED AS WebApplicationException
logger.error("Exception:", e); logger.error("Exception:", e);

View File

@ -8,6 +8,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; 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 final String SMP_ID = "smp-id";
private static Logger logger = LoggerFactory.getLogger(SMPIDResolver.class); private static Logger logger = LoggerFactory.getLogger(SMPIDResolver.class);
/** /**
* Gets the smpid. * Gets the smpid.
* *
* @param req the req
* @param smpId the smp id * @param smpId the smp id
* @param fileName the file name * @param fileName the file name
* @param contentType the content type * @param contentType the content type
* @param validation the validation * @param validation the validation
* @return the smpid * @return the smpid
* @throws WebApplicationException the web application exception
*/ */
@GET @GET
@Path("id") @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..."); logger.info(this.getClass().getSimpleName()+" GET starts...");
//Checking mandatory parameter smpId try{
if(smpId==null || smpId.isEmpty()){ //Checking mandatory parameter smpId
logger.error(SMP_ID+" not found"); if(smpId==null || smpId.isEmpty()){
ExceptionManager.badRequestException(httpRequest, "Missing mandatory parameter "+SMP_ID, SMPIDResolver.class, help); 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);
} }
} }

View File

@ -21,8 +21,8 @@ import org.slf4j.LoggerFactory;
/** /**
* The Class SMPResolver. * The Class SMPResolver.
* *
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Oct 22, 2018 * Dec 14, 2018
*/ */
@Path("/") @Path("/")
public class SMPResolver { public class SMPResolver {
@ -36,7 +36,6 @@ public class SMPResolver {
private static Logger logger = LoggerFactory.getLogger(SMPResolver.class); private static Logger logger = LoggerFactory.getLogger(SMPResolver.class);
/** /**
* Gets the smpuri. * Gets the smpuri.
* *
@ -60,11 +59,10 @@ public class SMPResolver {
logger.info(this.getClass().getSimpleName()+" GET starts..."); logger.info(this.getClass().getSimpleName()+" GET starts...");
try{ try{
//Checking mandatory parameter smpURI //Checking mandatory parameter smpURI
if(smpURI==null || smpURI.isEmpty()){ if(smpURI==null || smpURI.isEmpty()){
logger.error(SMP_URI+" not found"); 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); return StorageIDResolver.resolveStorageId(req, smpURI, fileName, contentType, validation);
@ -74,7 +72,7 @@ public class SMPResolver {
if(!(e instanceof WebApplicationException)){ if(!(e instanceof WebApplicationException)){
//UNEXPECTED EXCEPTION managing it as WebApplicationException //UNEXPECTED EXCEPTION managing it as WebApplicationException
String error = "Error occurred on resolving the smpURI "+smpURI+". Please, contact the support!"; 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 //ALREADY MANAGED AS WebApplicationException
logger.error("Exception:", e); logger.error("Exception:", e);

View File

@ -5,6 +5,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@ -15,6 +16,13 @@ import org.gcube.smartgears.utils.InnerMethodName;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/**
* The Class StorageHubResolver.
*
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Dec 14, 2018
*/
@Path("shub/{id}") @Path("shub/{id}")
public class StorageHubResolver { public class StorageHubResolver {
@ -31,63 +39,103 @@ public class StorageHubResolver {
@PathParam(STORAGE_HUB_ID) @PathParam(STORAGE_HUB_ID)
String id; String id;
/**
* Download.
*
* @param req the req
* @return the response
*/
@GET @GET
@Path("") @Path("")
public Response download(@Context HttpServletRequest httpRequest) { public Response download(@Context HttpServletRequest req) {
InnerMethodName.instance.set("resolveStorageHubPublicLink");
logger.info(this.getClass().getSimpleName()+" GET starts..."); 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{ try{
StreamDescriptor descriptor = shc.open(id).asFile().download(); InnerMethodName.instance.set("resolveStorageHubPublicLink");
return Response StorageHubClient shc = new StorageHubClient();
.ok(descriptor.getStream())
.header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build();
}catch(Exception e){ //Checking mandatory parameter id
logger.error("Error on getting file with "+id, e); if(id==null || id.isEmpty()){
String errorMsg = "Error on getting file with hub id '"+id+"'. "+e.getMessage(); logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
ExceptionManager.internalErrorException(httpRequest, errorMsg, StorageHubResolver.class, help); throw ExceptionManager.badRequestException(req, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help);
return null; }
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 @GET
@Path("{version}") @Path("{version}")
public Response downloadVersion(@Context HttpServletRequest httpRequest, @PathParam("version") String version) { public Response downloadVersion(@Context HttpServletRequest req, @PathParam("version") String version) {
InnerMethodName.instance.set("resolveStorageHubPublicLinkWithVersion"); logger.info(this.getClass().getSimpleName() +" GET downloadVersion called");
logger.info(this.getClass().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");
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{ try{
InnerMethodName.instance.set("resolveStorageHubPublicLinkWithVersion");
StorageHubClient shc = new StorageHubClient();
StreamDescriptor descriptor = shc.open(id).asFile().downloadSpecificVersion(version); //Checking mandatory parameter id
return Response if(id==null || id.isEmpty()){
.ok(descriptor.getStream()) logger.error("Path Parameter "+STORAGE_HUB_ID+" not found");
.header("content-disposition","attachment; filename = \""+descriptor.getFileName()+"\"").build(); throw ExceptionManager.badRequestException(req, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help);
}
}catch(Exception e){ //Checking mandatory parameter id
String errorMsg = "Error on getting versioned file with hub id '"+id+ "' and version '"+version+"'"; if(version==null || version.isEmpty()){
logger.error(errorMsg, e); logger.error("Parameter 'version' not found");
ExceptionManager.internalErrorException(httpRequest, errorMsg, StorageHubResolver.class, help); throw ExceptionManager.badRequestException(req, "Missing mandatory parameter 'version'", StorageHubResolver.class, help);
return null; }
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;
} }
} }
} }

View File

@ -3,13 +3,13 @@ package org.gcube.datatransfer.resolver.services;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.HEAD; import javax.ws.rs.HEAD;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.ResponseBuilder;
@ -29,13 +29,12 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* The Class StorageManager. * The Class StorageIDResolver.
* *
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* @author Lucio Lelii * Dec 14, 2018
*
* Oct 19, 2018
*/ */
@Path("storage") @Path("storage")
public class StorageIDResolver { public class StorageIDResolver {
@ -54,23 +53,43 @@ public class StorageIDResolver {
/** /**
* Gets the storage id. * Gets the storage id.
* *
* @param httpRequest the http request * @param req the req
* @param storageId the storage id * @param storageId the storage id
* @param fileName the file name * @param fileName the file name
* @param contentType the content type * @param contentType the content type
* @param validation the validation * @param validation the validation
* @return the storage id * @return the storage id
* @throws WebApplicationException the web application exception
*/ */
@GET @GET
@Path("{storage-id}") @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..."); logger.info(this.getClass().getSimpleName()+" GET starts...");
//Checking mandatory parameter storageId
if(storageId==null || storageId.isEmpty()){ try{
logger.error(STORAGE_ID+" not found"); if(storageId==null || storageId.isEmpty()){
ExceptionManager.badRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help); 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 contentType the content type
* @param validation the validation * @param validation the validation
* @return the response * @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); logger.info("storage-id: "+storageId+", fileName: "+fileName+", contentType: "+contentType+", validation: "+validation);
//Checking mandatory parameter storageId //Checking mandatory parameter storageId
if (storageId == null || storageId.isEmpty()) { if (storageId == null || storageId.isEmpty()) {
logger.error("storageId not found"); 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); StorageClient client = getStorageClientInstance(storageId);
@ -104,12 +124,12 @@ public class StorageIDResolver {
logger.info("Decoded ID"+" = "+ toSEID); logger.info("Decoded ID"+" = "+ toSEID);
}catch(Exception e){ }catch(Exception e){
logger.error("Storage Client Exception when getting file from storage: ", 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()){ if(toSEID==null || toSEID.isEmpty()){
logger.error("Decrypted id for storageId: "+storageId +" is null or empty!"); 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); long size = iClient.getSize().RFileById(toSEID);
@ -144,7 +164,10 @@ public class StorageIDResolver {
.ok(so) .ok(so)
.header(ConstantsResolver.CONTENT_DISPOSITION,"attachment; filename = \""+fileName+"\"") .header(ConstantsResolver.CONTENT_DISPOSITION,"attachment; filename = \""+fileName+"\"")
.header(ConstantsResolver.CONTENT_LENGTH, size); .header(ConstantsResolver.CONTENT_LENGTH, size);
if (contentType!= null) response.header("Content-Type",contentType);
if (contentType!= null)
response.header("Content-Type",contentType);
return response.build(); return response.build();
} }
@ -153,43 +176,60 @@ public class StorageIDResolver {
/** /**
* Http do head. * Http do head.
* *
* @param httpRequest the http request * @param req the http request
* @param storageId the storage id * @param storageId the storage id
* @param hproxycheck the hproxycheck * @param hproxycheck the hproxycheck
* @return the response * @return the response
* @throws ServletException the servlet exception * @throws WebApplicationException the web application exception
* @throws IOException Signals that an I/O exception has occurred.
*/ */
@HEAD @HEAD
@Path("{storage-id}") @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..."); logger.info(this.getClass().getSimpleName()+" HEAD starts...");
//THIS IS FOR HPROXY CHECK try{
if(hproxycheck==null || hproxycheck){ //THIS IS FOR HPROXY CHECK
logger.trace("returning status 200 for Hproxy check"); if(hproxycheck==null || hproxycheck){
ResponseBuilder response = Response.status(HttpStatus.SC_OK); logger.trace("returning status 200 for Hproxy check");
return response.build(); 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. * Validation payload.
* *
* @param httpRequest the http request * @param httpRequest the http request
* @param storageId the storage id * @param storageId the storage id
* @return the response * @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 //Checking mandatory parameter storageId
if (storageId == null || storageId.isEmpty()) { if (storageId == null || storageId.isEmpty()) {
logger.warn("storageId not found"); 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); StorageClient client = getStorageClientInstance(storageId);
String toSEID = null; String toSEID = null;
@ -200,12 +240,12 @@ public class StorageIDResolver {
logger.debug("Decoded ID"+" = "+ toSEID); logger.debug("Decoded ID"+" = "+ toSEID);
}catch(Exception e){ }catch(Exception e){
logger.error("Storage Client Exception when getting file from storage: ", 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()){ if(toSEID==null || toSEID.isEmpty()){
logger.error("Decrypted id for storageId: "+storageId +" is null or empty!"); 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); IOUtils.closeQuietly(streamToWrite);
response = Response.status(HttpStatus.SC_OK); response = Response.status(HttpStatus.SC_OK);
}else }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) { catch (IOException e2) {
logger.error("Error on validating the file: ",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) 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(); return response.build();
@ -243,8 +283,9 @@ public class StorageIDResolver {
* *
* @param storageId the storage id * @param storageId the storage id
* @return the storage client instance * @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; MemoryType memory=null;
if(storageId.endsWith(org.gcube.contentmanagement.blobstorage.transport.backend.util.Costants.VOLATILE_URL_IDENTIFICATOR)){ if(storageId.endsWith(org.gcube.contentmanagement.blobstorage.transport.backend.util.Costants.VOLATILE_URL_IDENTIFICATOR)){

View File

@ -11,11 +11,11 @@ import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -32,7 +32,7 @@ public class UriResolverIndex {
@GET @GET
@Produces({MediaType.TEXT_HTML}) @Produces({MediaType.TEXT_HTML})
@Path("index") @Path("index")
public InputStream index(@Context HttpServletRequest req){ public InputStream index(@Context HttpServletRequest req) throws WebApplicationException{
String indexFile = "/WEB-INF/jsp/index.jsp"; String indexFile = "/WEB-INF/jsp/index.jsp";
@ -40,11 +40,17 @@ public class UriResolverIndex {
logger.info("UriResolverIndex called"); logger.info("UriResolverIndex called");
String realPath = req.getServletContext().getRealPath(indexFile); String realPath = req.getServletContext().getRealPath(indexFile);
return new FileInputStream(new File(realPath)); return new FileInputStream(new File(realPath));
}catch(Exception e){ }catch (Exception e) {
logger.error("Error on getting "+indexFile, e);
throw new InternalServerException(req, Status.NOT_FOUND, "Index.jsp not found", UriResolverIndex.class, null);
}
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 @GET
@ -53,12 +59,5 @@ public class UriResolverIndex {
public InputStream info(@Context HttpServletRequest req){ public InputStream info(@Context HttpServletRequest req){
return index(req); return index(req);
} }
// @GET
// @Produces({MediaType.TEXT_HTML})
// @Path("")
// public InputStream emptyPath(@Context HttpServletRequest req){
// return index(req);
// }
} }