From 3cc7ad3212f84f2d6ec8b7ab752dbf606a7edafc Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Fri, 14 Dec 2018 10:53:50 +0000 Subject: [PATCH] changing exception manager git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@174889 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../services/AnalyticsCreateResolver.java | 181 ++++++++++-------- .../services/AnalyticsGetResolver.java | 23 ++- .../resolver/services/CatalogueResolver.java | 6 +- .../resolver/services/GeonetworkResolver.java | 34 ++-- .../resolver/services/GisResolver.java | 14 +- .../services/KnimeCreateResolver.java | 4 +- .../resolver/services/KnimeGetResolver.java | 8 +- .../services/PartheosRegistryResolver.java | 20 +- .../resolver/services/SMPIDResolver.java | 2 +- .../resolver/services/SMPResolver.java | 47 +++-- .../resolver/services/StorageHubResolver.java | 10 +- .../resolver/services/StorageIDResolver.java | 20 +- .../services/error/ExceptionManager.java | 50 ++--- 13 files changed, 238 insertions(+), 181 deletions(-) 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 f37b97b..3b4caf5 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsCreateResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsCreateResolver.java @@ -15,6 +15,7 @@ import javax.ws.rs.Consumes; import javax.ws.rs.POST; 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; @@ -62,101 +63,113 @@ public class AnalyticsCreateResolver { @Path("/create") @Consumes(MediaType.TEXT_PLAIN) @Produces(MediaType.TEXT_PLAIN) - public Response createAnalyticsURL(@Context HttpServletRequest req, String body) { + public Response createAnalyticsURL(@Context HttpServletRequest req, String body) throws WebApplicationException{ logger.info(this.getClass().getSimpleName()+" POST starts..."); - logger.info("body is: "+body); - DataMinerInvocation jsonRequest = null; - try { + try{ + logger.info("body is: "+body); - jsonRequest = DataMinerInvocationManager.getInstance().unmarshaling(IOUtils.toInputStream(body), org.gcube.data.analysis.dminvocation.MediaType.ApplicationJSON, true); - } - catch (IOException | JAXBException | SAXException e1) { - logger.error("The body is not a valid DataMinerInvocation JSON request",e1); - ExceptionManager.throwBadRequestException(req, "Bad 'dataminer-invocation' JSON request: \n"+e1.getCause().getMessage(), this.getClass(), helpURI); - } - - logger.debug("The body contains the request: "+jsonRequest.toString()); - - String contextToken = SecurityTokenProvider.instance.get(); - String scope = ScopeProvider.instance.get(); -// logger.info("SecurityTokenProvider contextToken: "+contextToken); - logger.info("ScopeProvider has scope: "+scope); - - String appToken = req.getServletContext().getInitParameter(TokenSetter.ROOT_APP_TOKEN); - - if(contextToken.compareTo(appToken)==0){ - logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"..."); - ExceptionManager.throwUnauthorizedException(req, "You are not authorized. You must pass a token of VRE", this.getClass(), helpURI); - } - - String operatorID = jsonRequest.getOperatorId(); - - if(scope==null || scope.isEmpty()){ - logger.error("The parameter 'scope' not found or empty in the JSON object"); - ExceptionManager.throwBadRequestException(req, "Mandatory body parameter 'scope' not found or empty in the JSON object", this.getClass(), helpURI); - } - - - if(operatorID==null || operatorID.isEmpty()){ - logger.error("The parameter 'operatorId' not found or empty in the JSON object"); - ExceptionManager.throwBadRequestException(req, "Mandatory body parameter 'operatorId' not found or empty in the JSON object", this.getClass(), helpURI); - } - - ScopeBean scopeBean = new ScopeBean(scope); - String publicLinkToDMInvFile = ""; - - if(scopeBean.is(Type.VRE)){ - String vreName = scopeBean.name(); - - String analyticsGetResolverURL = String.format("%s/%s", Util.getServerURL(req), "analytics/get"); - //Creating DM invocation file - if(jsonRequest.getActionType()==null) - jsonRequest.setActionType(ActionType.RUN); - - File tempInvocationFile = null; + DataMinerInvocation jsonRequest = null; try { - ByteArrayOutputStream xmlByteArray = DataMinerInvocationManager.getInstance().marshaling(jsonRequest, org.gcube.data.analysis.dminvocation.MediaType.ApplicationXML, true); - String uniqueName = createDMInvocationFileName(jsonRequest.getOperatorId()); - tempInvocationFile = createTempFile(uniqueName, ".xml", xmlByteArray.toByteArray()); - - //CREATE THE FILE ON STORAGE HUB - StorageHubClient shc = new StorageHubClient(); - logger.info("Created StorageHubClient Instance, uploading file: "+tempInvocationFile.getName()); - FileContainer fileContainer = shc.getWSRoot().uploadFile(new FileInputStream(tempInvocationFile), tempInvocationFile.getName(), "DataMinerInvocation Request created by "+this.getClass().getSimpleName()); - logger.info("UPLOADED FILE at: "+fileContainer.getPublicLink()); - URL thePublicLink = fileContainer.getPublicLink(); - publicLinkToDMInvFile = thePublicLink!=null?thePublicLink.toString():null; + jsonRequest = DataMinerInvocationManager.getInstance().unmarshaling(IOUtils.toInputStream(body), org.gcube.data.analysis.dminvocation.MediaType.ApplicationJSON, true); } - catch (Exception e) { - logger.error("Error on creating 'dataminer-invocation:", e); - ExceptionManager.throwBadRequestException(req, "Error on creating your 'dataminer-invocation' request with "+jsonRequest+". \nPlease contact the support", this.getClass(), helpURI); - }finally{ - try{ - //DELETING THE TEMP FILE - if(tempInvocationFile!=null && tempInvocationFile.exists()) - tempInvocationFile.delete(); - }catch(Exception e){ - //silent + catch (IOException | JAXBException | SAXException e1) { + logger.error("The body is not a valid DataMinerInvocation JSON request",e1); + throw ExceptionManager.badRequestException(req, "Bad 'dataminer-invocation' JSON request: \n"+e1.getCause().getMessage(), this.getClass(), helpURI); + } + + logger.debug("The body contains the request: "+jsonRequest.toString()); + + String contextToken = SecurityTokenProvider.instance.get(); + String scope = ScopeProvider.instance.get(); + // logger.info("SecurityTokenProvider contextToken: "+contextToken); + logger.info("ScopeProvider has scope: "+scope); + + String appToken = req.getServletContext().getInitParameter(TokenSetter.ROOT_APP_TOKEN); + + if(contextToken.compareTo(appToken)==0){ + logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"..."); + throw ExceptionManager.unauthorizedException(req, "You are not authorized. You must pass a token of VRE", this.getClass(), helpURI); + } + + String operatorID = jsonRequest.getOperatorId(); + + if(scope==null || scope.isEmpty()){ + logger.error("The parameter 'scope' not found or empty in the JSON object"); + throw ExceptionManager.badRequestException(req, "Mandatory body parameter 'scope' not found or empty in the JSON object", this.getClass(), helpURI); + } + + + if(operatorID==null || operatorID.isEmpty()){ + logger.error("The parameter 'operatorId' not found or empty in the JSON object"); + throw ExceptionManager.badRequestException(req, "Mandatory body parameter 'operatorId' not found or empty in the JSON object", this.getClass(), helpURI); + } + + ScopeBean scopeBean = new ScopeBean(scope); + String publicLinkToDMInvFile = ""; + + if(scopeBean.is(Type.VRE)){ + String vreName = scopeBean.name(); + + String analyticsGetResolverURL = String.format("%s/%s", Util.getServerURL(req), "analytics/get"); + //Creating DM invocation file + if(jsonRequest.getActionType()==null) + jsonRequest.setActionType(ActionType.RUN); + + File tempInvocationFile = null; + try { + + ByteArrayOutputStream xmlByteArray = DataMinerInvocationManager.getInstance().marshaling(jsonRequest, org.gcube.data.analysis.dminvocation.MediaType.ApplicationXML, true); + String uniqueName = createDMInvocationFileName(jsonRequest.getOperatorId()); + tempInvocationFile = createTempFile(uniqueName, ".xml", xmlByteArray.toByteArray()); + + //CREATE THE FILE ON STORAGE HUB + StorageHubClient shc = new StorageHubClient(); + logger.info("Created StorageHubClient Instance, uploading file: "+tempInvocationFile.getName()); + FileContainer fileContainer = shc.getWSRoot().uploadFile(new FileInputStream(tempInvocationFile), tempInvocationFile.getName(), "DataMinerInvocation Request created by "+this.getClass().getSimpleName()); + logger.info("UPLOADED FILE at: "+fileContainer.getPublicLink()); + URL thePublicLink = fileContainer.getPublicLink(); + publicLinkToDMInvFile = thePublicLink!=null?thePublicLink.toString():null; } + catch (Exception e) { + logger.error("Error on creating 'dataminer-invocation:", e); + throw ExceptionManager.badRequestException(req, "Error on creating your 'dataminer-invocation' request with "+jsonRequest+". \nPlease contact the support", this.getClass(), helpURI); + }finally{ + try{ + //DELETING THE TEMP FILE + if(tempInvocationFile!=null && tempInvocationFile.exists()) + tempInvocationFile.delete(); + }catch(Exception e){ + //silent + } + } + + if(publicLinkToDMInvFile==null){ + logger.error("Error on creating the public link to file"); + throw ExceptionManager.badRequestException(req, "Error on getting link to your 'dataminer-invocation' request. Plese contact the support "+jsonRequest, this.getClass(), helpURI); + } + + String dataMinerURL = String.format("%s/%s?%s=%s", analyticsGetResolverURL, vreName, DATAMINER_INVOCATION_MODEL, publicLinkToDMInvFile); + logger.info("Returning Analytics URL: "+dataMinerURL); + return Response.ok(dataMinerURL).header("Location", dataMinerURL).build(); + + }else{ + logger.error("The input scope "+scope+" is not a VRE"); + throw ExceptionManager.badRequestException(req, "Working in the "+scope+" scope that is not a VRE. Use a token of VRE", this.getClass(), helpURI); } - if(publicLinkToDMInvFile==null){ - logger.error("Error on creating the public link to file"); - ExceptionManager.throwBadRequestException(req, "Error on getting link to your 'dataminer-invocation' request. Plese contact the support "+jsonRequest, this.getClass(), helpURI); + }catch (Exception e) { + + 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!"; + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); } - - String dataMinerURL = String.format("%s/%s?%s=%s", analyticsGetResolverURL, vreName, DATAMINER_INVOCATION_MODEL, publicLinkToDMInvFile); - logger.info("Returning Analytics URL: "+dataMinerURL); - return Response.ok(dataMinerURL).header("Location", dataMinerURL).build(); - - }else{ - logger.error("The input scope "+scope+" is not a VRE"); - ExceptionManager.throwBadRequestException(req, "Working in the "+scope+" scope that is not a VRE. Use a token of VRE", this.getClass(), helpURI); + //ALREADY MANAGED AS WebApplicationException + logger.error("Exception:", e); + throw (WebApplicationException) e; } - - return null; } 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 82f80c2..74b2391 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsGetResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsGetResolver.java @@ -10,6 +10,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; @@ -50,13 +51,13 @@ public class AnalyticsGetResolver { */ @GET @Path("/get/{vreName}") - public Response resolveAnalyticsURL(@Context HttpServletRequest req, @PathParam("vreName") String vreName) { + public Response resolveAnalyticsURL(@Context HttpServletRequest req, @PathParam("vreName") String vreName) throws WebApplicationException{ logger.info(this.getClass().getSimpleName()+" GET starts..."); try { if(vreName==null || vreName.isEmpty()){ logger.error("The path parameter 'vreName' not found or empty in the path"); - ExceptionManager.throwBadRequestException(req, "Mandatory path parameter 'vreName' not found or empty", this.getClass(), helpURI); + throw ExceptionManager.badRequestException(req, "Mandatory path parameter 'vreName' not found or empty", this.getClass(), helpURI); } try{ @@ -66,7 +67,7 @@ public class AnalyticsGetResolver { reader = new ApplicationProfileReader(fullScope, APPLICATION_PROFILE, ORG_GCUBE_PORTLETS_USER_DATAMINERMANAGER_SERVER_DATA_MINER_MANAGER_SERVICE_IMPL, false); }catch(Exception e){ logger.error("Error on reading the "+APPLICATION_PROFILE+" with APPID: "+ORG_GCUBE_PORTLETS_USER_DATAMINERMANAGER_SERVER_DATA_MINER_MANAGER_SERVICE_IMPL, e); - ExceptionManager.throwInternalErrorException(req, "Error on reading the Application Profile for the "+ANALYTICS_EXECUTOR_PORTLET_NAME+". Please contact the support", this.getClass(), helpURI); + throw ExceptionManager.internalErrorException(req, "Error on reading the Application Profile for the "+ANALYTICS_EXECUTOR_PORTLET_NAME+". Please contact the support", this.getClass(), helpURI); } //READ THE DATAMINER URL PORTLET FROM APPLICATION PROFRILE IN THE SCOPE fullScope @@ -79,14 +80,20 @@ public class AnalyticsGetResolver { }catch (ExecutionException e) { logger.error("The input VRE Name "+vreName+" not found", e); - ExceptionManager.throwBadRequestException(req, "The input 'VRE Name' "+"+vreName+"+ "not found on Informatiion System. Is it a valid VRE?", this.getClass(), helpURI); + throw ExceptionManager.badRequestException(req, "The input 'VRE Name' "+"+vreName+"+ "not found on Informatiion System. Is it a valid VRE?", this.getClass(), helpURI); + //throw new BadRequestException(req, Status.BAD_REQUEST, "", this.getClass(), null); } - return null; }catch (Exception e) { - logger.error("Error on resolving Anaylitics URL",e); - ExceptionManager.throwInternalErrorException(req, "Error occurred when resolving Anaylitics URL", this.getClass(), helpURI); - return null; + + if(!(e instanceof WebApplicationException)){ + //UNEXPECTED EXCEPTION managing it as WebApplicationException + String error = "Error occurred on resolving the Analytics URL. Please, contact the support!"; + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + } + //ALREADY MANAGED AS WebApplicationException + logger.error("Exception:", e); + throw (WebApplicationException) e; } } 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 6e0fc6d..354d0b7 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java @@ -72,7 +72,7 @@ public class CatalogueResolver { return Response.seeOther(new URL(itemCatalogueURL).toURI()).build(); }catch (Exception e) { logger.error("error resolving catalogue link",e); - ExceptionManager.throwInternalErrorException(req, "Error occurred resolving catalogue link", this.getClass(), helpURI); + ExceptionManager.internalErrorException(req, "Error occurred resolving catalogue link", this.getClass(), helpURI); return null; } } @@ -114,12 +114,12 @@ public class CatalogueResolver { } if(fullScope==null) - ExceptionManager.throwNotFoundException(req, "The scope '"+scope+"' does not matching any scope in the infrastructure. Is it valid?", this.getClass(), helpURI); + ExceptionManager.notFoundException(req, "The scope '"+scope+"' does not matching any scope in the infrastructure. Is it valid?", this.getClass(), helpURI); ResourceCatalogueCodes rc = ResourceCatalogueCodes.valueOfCodeValue(jsonRequest.getEntity_context()); if(rc==null){ logger.error("Entity context is null/malformed"); - ExceptionManager.throwBadRequestException(req, "Entity context is null/malformed", this.getClass(), helpURI); + ExceptionManager.badRequestException(req, "Entity context is null/malformed", this.getClass(), helpURI); //throw new WebApplicationException("Entity context is null/malformed", Status.BAD_REQUEST); } 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 3ec9c4d..aa9a78e 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/GeonetworkResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/GeonetworkResolver.java @@ -129,12 +129,12 @@ public class GeonetworkResolver { if(scope==null || scope.isEmpty()){ logger.error("Path Parameter 'scope' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'scope'", this.getClass(), help); + ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'scope'", this.getClass(), help); } if(mode==null || mode.isEmpty()){ logger.error("Path Parameter 'scope' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'mode'", this.getClass(), help); + ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'mode'", this.getClass(), help); } scope = ScopeUtil.normalizeScope(scope, "|"); @@ -144,12 +144,12 @@ public class GeonetworkResolver { }catch(Exception e){ List toPrint = Arrays.asList(MODE.values()); logger.error("The 'mode' parameter is wrong, Have you pass a valid parameter MODE like "+toPrint+"?"); - ExceptionManager.throwWrongParameterException(req, "The 'mode' parameter must be value of "+toPrint, this.getClass(), help); + ExceptionManager.wrongParameterException(req, "The 'mode' parameter must be value of "+toPrint, this.getClass(), help); } if(visibility==null){ logger.error("Path Parameter 'visibility' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'visibility'", this.getClass(), help); + ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'visibility'", this.getClass(), help); } visibility = visibility.toUpperCase(); @@ -158,7 +158,7 @@ public class GeonetworkResolver { }catch (Exception e) { List toPrint = Arrays.asList(VISIBILITY.values()); logger.error("The 'visibility' parameter is wrong, Have you pass a valid parameter VISIBILITY like "+toPrint+"?"); - ExceptionManager.throwWrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, this.getClass(), help); + ExceptionManager.wrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, this.getClass(), help); } if(resetCache!=null && Boolean.parseBoolean(resetCache)){ @@ -202,17 +202,17 @@ public class GeonetworkResolver { return responseBuilder.build(); case HttpServletResponse.SC_FORBIDDEN: - ExceptionManager.throwForbiddenException(req, "You are not authorized to perform the request "+gnGetlURL, this.getClass(), help); + ExceptionManager.forbiddenException(req, "You are not authorized to perform the request "+gnGetlURL, this.getClass(), help); break; default: - ExceptionManager.throwInternalErrorException(req, "Sorry, an error occurred performing the geonetwork request "+gnGetlURL+" with scope "+scope, this.getClass(), help); + ExceptionManager.internalErrorException(req, "Sorry, an error occurred performing the geonetwork request "+gnGetlURL+" with scope "+scope, this.getClass(), help); } } catch (Exception e) { logger.error("Exception:", e); String error = "Sorry, an error occurred on resolving geonetwork request with scope "+scope+". Please, contact support!"; - ExceptionManager.throwInternalErrorException(req, error, this.getClass(), help); + ExceptionManager.internalErrorException(req, error, this.getClass(), help); } //An error occurred @@ -244,12 +244,12 @@ public class GeonetworkResolver { if(scope==null || scope.isEmpty()){ logger.error("Path Parameter 'scope' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'scope'", this.getClass(), help); + ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'scope'", this.getClass(), help); } if(mode==null || mode.isEmpty()){ logger.error("Path Parameter 'scope' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'mode'", this.getClass(), help); + ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'mode'", this.getClass(), help); } scope = ScopeUtil.normalizeScope(scope, "|"); @@ -259,12 +259,12 @@ public class GeonetworkResolver { }catch(Exception e){ List toPrint = Arrays.asList(MODE.values()); logger.error("The 'mode' parameter is wrong, Have you pass a valid parameter MODE like "+toPrint+"?"); - ExceptionManager.throwWrongParameterException(req, "The 'mode' parameter must be value of "+toPrint, this.getClass(), help); + ExceptionManager.wrongParameterException(req, "The 'mode' parameter must be value of "+toPrint, this.getClass(), help); } if(visibility==null){ logger.error("Path Parameter 'visibility' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory path parameter 'visibility'", this.getClass(), help); + ExceptionManager.badRequestException(req, "Missing mandatory path parameter 'visibility'", this.getClass(), help); } visibility = visibility.toUpperCase(); @@ -273,7 +273,7 @@ public class GeonetworkResolver { }catch (Exception e) { List toPrint = Arrays.asList(VISIBILITY.values()); logger.error("The 'visibility' parameter is wrong, Have you pass a valid parameter VISIBILITY like "+toPrint+"?"); - ExceptionManager.throwWrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, this.getClass(), help); + ExceptionManager.wrongParameterException(req, "The 'visibility' parameter must be value of "+toPrint, this.getClass(), help); } //HOW TO PASS ANY FILTER? @@ -378,7 +378,7 @@ public class GeonetworkResolver { if(in==null){ logger.warn("Input stream returned is null, sending "+HttpServletResponse.SC_NOT_FOUND); - ExceptionManager.throwNotFoundException(req, "Input stream is null to the request "+gnCSWlURL+ " with body: "+byteArray.toString(), this.getClass(), help); + ExceptionManager.notFoundException(req, "Input stream is null to the request "+gnCSWlURL+ " with body: "+byteArray.toString(), this.getClass(), help); } try{ @@ -441,19 +441,19 @@ public class GeonetworkResolver { }catch(Exception e){ logger.error("Error on copy the response to send to client: ", e); - ExceptionManager.throwInternalErrorException(req, "Error on copy the response!", this.getClass(), help); + ExceptionManager.internalErrorException(req, "Error on copy the response!", this.getClass(), help); }finally{ IOUtils.closeQuietly(in); } } catch (IllegalArgumentException e){ logger.error("IllegalArgumentException:", e); - ExceptionManager.throwBadRequestException(req, "Illegal argument to carry out the request!", this.getClass(), help); + ExceptionManager.badRequestException(req, "Illegal argument to carry out the request!", this.getClass(), help); } catch (Exception e) { logger.error("Exception:", e); String error = "Sorry, an error occurred on resolving geonetwork request with scope "+scope+". Please, contact support!"; - ExceptionManager.throwInternalErrorException(req, error, this.getClass(), help); + ExceptionManager.internalErrorException(req, error, this.getClass(), help); } //An error occurred 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 f047f94..6616e0a 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/GisResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/GisResolver.java @@ -69,12 +69,12 @@ public class GisResolver { if(scope==null || scope.isEmpty()){ logger.error("Query Parameter 'scope' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory query parameter 'scope'", this.getClass(), help); + ExceptionManager.badRequestException(req, "Missing mandatory query parameter 'scope'", this.getClass(), help); } if(gisUUID==null || gisUUID.isEmpty()){ logger.error("Path Parameter 'gis-UUID' not found"); - ExceptionManager.throwBadRequestException(req, "Missing mandatory query parameter 'gis-UUID'", this.getClass(), help); + ExceptionManager.badRequestException(req, "Missing mandatory query parameter 'gis-UUID'", this.getClass(), help); }else isGisLink = true; @@ -91,7 +91,7 @@ public class GisResolver { if(!isGisLink && !isGeoExplorerLink){ String err = GIS_UUID+" or "+GEO_EXPLORER_LAYER_UUID+" not found or empty in the query string"; logger.error(err); - ExceptionManager.throwBadRequestException(req, err, this.getClass(), help); + ExceptionManager.badRequestException(req, err, this.getClass(), help); } try { @@ -115,7 +115,7 @@ public class GisResolver { String gisViewerPortletUrl = LoadingGisViewerApplicationURLCache.getCache().get(scope); //CHECKING IF THE GisViewer Portlet URL is valid if(gisViewerPortletUrl==null || gisViewerPortletUrl.isEmpty()) - ExceptionManager.throwNotFoundException(req, "GisViewer Portlet URL not found in the scope: "+scope +". Please contact the support", this.getClass(), help); + ExceptionManager.notFoundException(req, "GisViewer Portlet URL not found in the scope: "+scope +". Please contact the support", this.getClass(), help); logger.info("Gis Viewer Application url is: " + gisViewerPortletUrl); gisViewerPortletUrl+="?rid="+new Random().nextLong() @@ -140,7 +140,7 @@ public class GisResolver { String geoExplorerPortletUrl = LoadingGeoExplorerApplicationURLCache.getCache().get(scope); //CHECKING IF THE GeoExplorer Portlet URL is valid if(geoExplorerPortletUrl==null || geoExplorerPortletUrl.isEmpty()) - ExceptionManager.throwNotFoundException(req, "GeoExplorer Portlet URL not found in the scope: "+scope +". Please contact the support", this.getClass(), help); + ExceptionManager.notFoundException(req, "GeoExplorer Portlet URL not found in the scope: "+scope +". Please contact the support", this.getClass(), help); logger.info("GeoExplorer Application url is: " + geoExplorerPortletUrl); geoExplorerPortletUrl+="?rid="+new Random().nextLong() @@ -153,13 +153,13 @@ public class GisResolver { } } - ExceptionManager.throwBadRequestException(req, GIS_UUID+" or "+GEO_EXPLORER_LAYER_UUID+" not found or empty in the query string", this.getClass(), help); + ExceptionManager.badRequestException(req, GIS_UUID+" or "+GEO_EXPLORER_LAYER_UUID+" not found or empty in the query string", this.getClass(), help); return null; } catch (Exception e) { logger.error("Exception:", e); String error = "Sorry, an error occurred on resolving request with UUID "+gisUUID+" and scope "+scope+". Please, contact support!"; - ExceptionManager.throwInternalErrorException(req, error, this.getClass(), help); + ExceptionManager.internalErrorException(req, error, this.getClass(), help); return null; } 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 502ec14..43ff2bb 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/KnimeCreateResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/KnimeCreateResolver.java @@ -59,7 +59,7 @@ public class KnimeCreateResolver { if(contextToken.compareTo(appToken)==0){ logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"..."); - ExceptionManager.throwUnauthorizedException(req, "You are not authorized. You must pass a token of VRE", this.getClass(), helpURI); + ExceptionManager.unauthorizedException(req, "You are not authorized. You must pass a token of VRE", this.getClass(), helpURI); } ScopeBean scopeBean = new ScopeBean(scope); @@ -78,7 +78,7 @@ public class KnimeCreateResolver { }else{ logger.error("The input scope "+scope+" is not a VRE"); - ExceptionManager.throwBadRequestException(req, "Working in the "+scope+" scope that is not a VRE. Use a token of VRE", this.getClass(), helpURI); + ExceptionManager.badRequestException(req, "Working in the "+scope+" scope that is not a VRE. Use a token of VRE", this.getClass(), helpURI); } return null; 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 de69685..a5c57de 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/KnimeGetResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/KnimeGetResolver.java @@ -55,7 +55,7 @@ public class KnimeGetResolver { try { if(vreName==null || vreName.isEmpty()){ logger.error("The path parameter 'vreName' not found or empty in the path"); - ExceptionManager.throwBadRequestException(req, "Mandatory path parameter 'vreName' not found or empty", this.getClass(), helpURI); + ExceptionManager.badRequestException(req, "Mandatory path parameter 'vreName' not found or empty", this.getClass(), helpURI); } try{ @@ -65,7 +65,7 @@ public class KnimeGetResolver { reader = new ApplicationProfileReader(fullScope, APPLICATION_PROFILE, ORG_GCUBE_PORTLETS_USER_KNIMEMODELSIMULATION_MANAGER_SERVICE_IMPL, false); }catch(Exception e){ logger.error("Error on reading the "+APPLICATION_PROFILE+" with APPID: "+ORG_GCUBE_PORTLETS_USER_KNIMEMODELSIMULATION_MANAGER_SERVICE_IMPL, e); - ExceptionManager.throwInternalErrorException(req, "Error on reading the Application Profile for the "+KNIME_EXECUTOR_APPLICATION+". Please contact the support", this.getClass(), helpURI); + ExceptionManager.internalErrorException(req, "Error on reading the Application Profile for the "+KNIME_EXECUTOR_APPLICATION+". Please contact the support", this.getClass(), helpURI); } //READ THE KNIME URL PORTLET FROM APPLICATION PROFRILE IN THE SCOPE fullScope @@ -78,13 +78,13 @@ public class KnimeGetResolver { }catch (ExecutionException e) { logger.error("The input VRE Name "+vreName+" not found", e); - ExceptionManager.throwBadRequestException(req, "The input 'VRE Name' "+"+vreName+"+ "not found on Informatiion System. Is it a valid VRE?", this.getClass(), helpURI); + ExceptionManager.badRequestException(req, "The input 'VRE Name' "+"+vreName+"+ "not found on Informatiion System. Is it a valid VRE?", this.getClass(), helpURI); } return null; }catch(Exception e){ logger.error("Error on resolving Knime URL",e); - ExceptionManager.throwInternalErrorException(req, "Error occurred when resolving Knime URL", this.getClass(), helpURI); + ExceptionManager.internalErrorException(req, "Error occurred when resolving Knime URL", this.getClass(), helpURI); return null; } } 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 a825d34..d7605d2 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/PartheosRegistryResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/PartheosRegistryResolver.java @@ -14,6 +14,7 @@ import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; 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; @@ -78,24 +79,26 @@ public class PartheosRegistryResolver { }catch (Exception e) { logger.error("Exception:", e); String error = "Error occurred on resolving the path "+remainPathParthenosURL+". Please, contact the support!"; - ExceptionManager.throwInternalErrorException(req, error, this.getClass(), helpURI); + ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); return null; } } + /** * Post catalogue. * * @param req the req * @param jsonRequest the json request * @return the response + * @throws Exception the exception */ @POST @Path("/") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.TEXT_PLAIN) - public Response postCatalogue(@Context HttpServletRequest req, ParthenosRequest jsonRequest) { + public Response postCatalogue(@Context HttpServletRequest req, ParthenosRequest jsonRequest) throws WebApplicationException{ logger.info(this.getClass().getSimpleName()+" POST starts..."); try{ @@ -103,7 +106,7 @@ public class PartheosRegistryResolver { if(entityName==null || entityName.isEmpty()){ logger.error("Entity Name Parameter like 'entity_name' not found or empty"); - ExceptionManager.throwBadRequestException(req, "Mandatory body parameter 'entity_name' not found or empty", this.getClass(), helpURI); + ExceptionManager.badRequestException(req, "Mandatory body parameter 'entity_name' not found or empty", this.getClass(), helpURI); } //REMOVING FIRST '/' IF EXISTS @@ -116,10 +119,15 @@ public class PartheosRegistryResolver { return Response.ok(normalizedEntityName).header("Location", itemCatalogueURLs.getPrivateCataloguePortletURL()).build(); }catch (Exception e) { + + 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); + } + //ALREADY MANAGED AS WebApplicationException logger.error("Exception:", e); - String error = "Error occurred on transforming the "+jsonRequest+". Please, contact the support!"; - ExceptionManager.throwInternalErrorException(req, error, this.getClass(), helpURI); - return null; + throw (WebApplicationException) 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 561c7da..2e73fcd 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/SMPIDResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/SMPIDResolver.java @@ -50,7 +50,7 @@ public class SMPIDResolver { //Checking mandatory parameter smpId if(smpId==null || smpId.isEmpty()){ logger.error(SMP_ID+" not found"); - ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory parameter "+SMP_ID, SMPIDResolver.class, help); + ExceptionManager.badRequestException(httpRequest, "Missing mandatory parameter "+SMP_ID, SMPIDResolver.class, help); } 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 c7d2f5e..43744a7 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/SMPResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/SMPResolver.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; @@ -18,6 +19,7 @@ import org.slf4j.LoggerFactory; /** + * The Class SMPResolver. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * Oct 22, 2018 @@ -28,33 +30,56 @@ public class SMPResolver { /** * */ - private static final String help = "https://wiki.gcube-system.org/gcube/URI_Resolver#SMP_Resolver"; + private static final String helpURI = "https://wiki.gcube-system.org/gcube/URI_Resolver#SMP_Resolver"; private static final String SMP_URI = "smp-uri"; private static Logger logger = LoggerFactory.getLogger(SMPResolver.class); + /** - * Gets the smpid. + * Gets the smpuri. * - * @param smpURI the smp id + * @param req the req + * @param smpURI the smp uri * @param fileName the file name * @param contentType the content type * @param validation the validation - * @return the smpid + * @return the smpuri + * @throws WebApplicationException the web application exception */ @GET @Path("smp") - public Response getSMPURI(@Context HttpServletRequest httpRequest, @QueryParam(SMP_URI) @Nullable String smpURI, @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 getSMPURI(@Context HttpServletRequest req, + @QueryParam(SMP_URI) @Nullable + String smpURI, + @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 smpURI - if(smpURI==null || smpURI.isEmpty()){ - logger.error(SMP_URI+" not found"); - ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory parameter "+SMP_URI, SMPResolver.class, help); - } + try{ - return StorageIDResolver.resolveStorageId(httpRequest, smpURI, fileName, contentType, validation); + //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); + } + + return StorageIDResolver.resolveStorageId(req, smpURI, fileName, contentType, validation); + + }catch (Exception e) { + + 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); + } + //ALREADY MANAGED AS WebApplicationException + logger.error("Exception:", e); + throw (WebApplicationException) 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 fe53285..3d22fea 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/StorageHubResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/StorageHubResolver.java @@ -41,7 +41,7 @@ public class StorageHubResolver { //Checking mandatory parameter id if(id==null || id.isEmpty()){ logger.error("Path Parameter "+STORAGE_HUB_ID+" not found"); - ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help); + ExceptionManager.badRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help); } try{ @@ -53,7 +53,7 @@ public class StorageHubResolver { }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.throwInternalErrorException(httpRequest, errorMsg, StorageHubResolver.class, help); + ExceptionManager.internalErrorException(httpRequest, errorMsg, StorageHubResolver.class, help); return null; } } @@ -68,13 +68,13 @@ public class StorageHubResolver { //Checking mandatory parameter id if(id==null || id.isEmpty()){ logger.error("Path Parameter "+STORAGE_HUB_ID+" not found"); - ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_HUB_ID, StorageHubResolver.class, help); + 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.throwBadRequestException(httpRequest, "Missing mandatory parameter 'version'", StorageHubResolver.class, help); + ExceptionManager.badRequestException(httpRequest, "Missing mandatory parameter 'version'", StorageHubResolver.class, help); } try{ @@ -86,7 +86,7 @@ public class StorageHubResolver { }catch(Exception e){ String errorMsg = "Error on getting versioned file with hub id '"+id+ "' and version '"+version+"'"; logger.error(errorMsg, e); - ExceptionManager.throwInternalErrorException(httpRequest, errorMsg, StorageHubResolver.class, help); + ExceptionManager.internalErrorException(httpRequest, errorMsg, StorageHubResolver.class, help); return null; } } 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 65fbc95..f8a719a 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/StorageIDResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/StorageIDResolver.java @@ -68,7 +68,7 @@ public class StorageIDResolver { //Checking mandatory parameter storageId if(storageId==null || storageId.isEmpty()){ logger.error(STORAGE_ID+" not found"); - ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help); + ExceptionManager.badRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help); } return resolveStorageId(httpRequest, storageId, fileName, contentType, validation); } @@ -91,7 +91,7 @@ public class StorageIDResolver { //Checking mandatory parameter storageId if (storageId == null || storageId.isEmpty()) { logger.error("storageId not found"); - ExceptionManager.throwBadRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help); + ExceptionManager.badRequestException(httpRequest, "Missing mandatory path parameter "+STORAGE_ID, StorageIDResolver.class, help); } StorageClient client = getStorageClientInstance(storageId); @@ -104,12 +104,12 @@ public class StorageIDResolver { logger.info("Decoded ID"+" = "+ toSEID); }catch(Exception e){ logger.error("Storage Client Exception when getting file from storage: ", e); - ExceptionManager.throwNotFoundException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help); + 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.throwNotFoundException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help); + ExceptionManager.notFoundException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help); } long size = iClient.getSize().RFileById(toSEID); @@ -189,7 +189,7 @@ public class StorageIDResolver { //Checking mandatory parameter storageId if (storageId == null || storageId.isEmpty()) { logger.warn("storageId not found"); - ExceptionManager.throwBadRequestException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, this.getClass(), help); + 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 +200,12 @@ public class StorageIDResolver { logger.debug("Decoded ID"+" = "+ toSEID); }catch(Exception e){ logger.error("Storage Client Exception when getting file from storage: ", e); - ExceptionManager.throwInternalErrorException(httpRequest, "Storage Client Exception when getting file from storage with id: "+storageId, StorageIDResolver.class, help); + 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.throwNotFoundException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help); + ExceptionManager.notFoundException(httpRequest, "Error on decrypting the "+STORAGE_ID+ " '"+storageId+"'. Is it a valid id?", StorageIDResolver.class, help); } @@ -223,15 +223,15 @@ public class StorageIDResolver { IOUtils.closeQuietly(streamToWrite); response = Response.status(HttpStatus.SC_OK); }else - ExceptionManager.throwNotFoundException(httpRequest, "The file with id: "+storageId+" is missing in the storage", StorageIDResolver.class, help); + 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.throwInternalErrorException(httpRequest, "Error on validating the file with id: "+storageId, StorageIDResolver.class, help); + ExceptionManager.internalErrorException(httpRequest, "Error on validating the file with id: "+storageId, StorageIDResolver.class, help); } if(response==null) - ExceptionManager.throwInternalErrorException(httpRequest, "Error on validating the file with id: "+storageId, StorageIDResolver.class, help); + ExceptionManager.internalErrorException(httpRequest, "Error on validating the file with id: "+storageId, StorageIDResolver.class, help); return response.build(); diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/error/ExceptionManager.java b/src/main/java/org/gcube/datatransfer/resolver/services/error/ExceptionManager.java index 3985b0d..3225254 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/error/ExceptionManager.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/error/ExceptionManager.java @@ -20,107 +20,111 @@ import org.gcube.datatransfer.resolver.services.exceptions.WrongParameterExcepti /** * The Class ExceptionManager. * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Oct 23, 2018 + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * Dec 14, 2018 */ public class ExceptionManager { - /** - * Throw internal error exception. + * Internal error exception. * * @param httpRequest the http request * @param errorMessage the error message * @param thrownBy the thrown by * @param helpURI the help uri + * @return the internal server exception */ - public static void throwInternalErrorException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){ + public static InternalServerException internalErrorException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){ URI theURI = checkURI(helpURI); - throw new InternalServerException(httpRequest, Status.INTERNAL_SERVER_ERROR, errorMessage, thrownBy, theURI); + return new InternalServerException(httpRequest, Status.INTERNAL_SERVER_ERROR, errorMessage, thrownBy, theURI); } /** - * Throw bad request exception. + * Bad request exception. * * @param httpRequest the http request * @param errorMessage the error message * @param thrownBy the thrown by * @param helpURI the help uri + * @return the bad request exception */ - public static void throwBadRequestException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){ + public static BadRequestException badRequestException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){ URI theURI = checkURI(helpURI); - throw new BadRequestException(httpRequest, Status.BAD_REQUEST, errorMessage, thrownBy, theURI); + return new BadRequestException(httpRequest, Status.BAD_REQUEST, errorMessage, thrownBy, theURI); } /** - * Throw wrong parameter exception. + * Wrong parameter exception. * * @param httpRequest the http request * @param errorMessage the error message * @param thrownBy the thrown by * @param helpURI the help uri + * @return the wrong parameter exception */ - public static void throwWrongParameterException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){ + public static WrongParameterException wrongParameterException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI) { URI theURI = checkURI(helpURI); - throw new WrongParameterException(httpRequest, Status.BAD_REQUEST, errorMessage, thrownBy, theURI); + return new WrongParameterException(httpRequest, Status.BAD_REQUEST, errorMessage, thrownBy, theURI); } /** - * Throw not found exception. + * Not found exception. * * @param httpRequest the http request * @param errorMessage the error message * @param thrownBy the thrown by * @param helpURI the help uri + * @return the not found exception */ - public static void throwNotFoundException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){ + public static NotFoundException notFoundException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI) { URI theURI = checkURI(helpURI); - throw new NotFoundException(httpRequest, Status.NOT_FOUND, errorMessage, thrownBy, theURI); + return new NotFoundException(httpRequest, Status.NOT_FOUND, errorMessage, thrownBy, theURI); } + /** - * Throw unauthorized exception. + * Unauthorized exception. * * @param httpRequest the http request * @param errorMessage the error message * @param thrownBy the thrown by * @param helpURI the help uri + * @return the not authorized request exception */ - public static void throwUnauthorizedException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){ + public static NotAuthorizedRequestException unauthorizedException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI) { URI theURI = checkURI(helpURI); - throw new NotAuthorizedRequestException(httpRequest, Status.UNAUTHORIZED, errorMessage, thrownBy, theURI); + return new NotAuthorizedRequestException(httpRequest, Status.UNAUTHORIZED, errorMessage, thrownBy, theURI); } - /** - * Throw forbidden exception. + * Forbidden exception. * * @param httpRequest the http request * @param errorMessage the error message * @param thrownBy the thrown by * @param helpURI the help uri + * @return the forbidden request exception */ - public static void throwForbiddenException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI){ + public static ForbiddenRequestException forbiddenException(HttpServletRequest httpRequest, String errorMessage, Class thrownBy, String helpURI) { URI theURI = checkURI(helpURI); - throw new ForbiddenRequestException(httpRequest, Status.FORBIDDEN, errorMessage, thrownBy, theURI); + return new ForbiddenRequestException(httpRequest, Status.FORBIDDEN, errorMessage, thrownBy, theURI); } - /** * Check uri. *