From dd7102e83552f2ba6029bca9bd8dc32e235fd143 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Fri, 14 Dec 2018 11:34:18 +0000 Subject: [PATCH] continue to manage the exceptions git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@174891 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../services/KnimeCreateResolver.java | 71 +++++++++++-------- .../resolver/services/KnimeGetResolver.java | 22 +++--- .../services/PartheosRegistryResolver.java | 34 ++++++--- 3 files changed, 78 insertions(+), 49 deletions(-) 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 43ff2bb..c95c9e2 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/KnimeCreateResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/KnimeCreateResolver.java @@ -8,6 +8,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; @@ -46,41 +47,51 @@ public class KnimeCreateResolver { @Path("/create") @Consumes(MediaType.TEXT_PLAIN) @Produces(MediaType.TEXT_PLAIN) - public Response createKnimeURL(@Context HttpServletRequest req) { + public Response createKnimeURL(@Context HttpServletRequest req) throws WebApplicationException{ + logger.info(this.getClass().getSimpleName()+" POST starts..."); + try{ + String contextToken = SecurityTokenProvider.instance.get(); + String scope = ScopeProvider.instance.get(); + // logger.info("SecurityTokenProvider contextToken: "+contextToken); + logger.info("ScopeProvider has scope: "+scope); - 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); - 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); + } - if(contextToken.compareTo(appToken)==0){ - logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"..."); - ExceptionManager.unauthorizedException(req, "You are not authorized. You must pass a token of VRE", this.getClass(), helpURI); + ScopeBean scopeBean = new ScopeBean(scope); + + if(scopeBean.is(Type.VRE)){ + + String vreName = scopeBean.name(); + String knimeGetResolverURL = String.format("%s/%s/%s", Util.getServerURL(req), "knime/get", vreName); + String queryString =req.getQueryString()==null?"":req.getQueryString(); + + if(req.getQueryString()!=null) + knimeGetResolverURL+="?"+queryString; + + logger.info("Returning Knime Resolver URL: "+knimeGetResolverURL); + return Response.ok(knimeGetResolverURL).header("Location", knimeGetResolverURL).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); + } + }catch (Exception e) { + + 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(); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + } + //ALREADY MANAGED AS WebApplicationException + logger.error("Exception:", e); + throw (WebApplicationException) e; } - - ScopeBean scopeBean = new ScopeBean(scope); - - if(scopeBean.is(Type.VRE)){ - - String vreName = scopeBean.name(); - String knimeGetResolverURL = String.format("%s/%s/%s", Util.getServerURL(req), "knime/get", vreName); - String queryString =req.getQueryString()==null?"":req.getQueryString(); - - if(req.getQueryString()!=null) - knimeGetResolverURL+="?"+queryString; - - logger.info("Returning Knime Resolver URL: "+knimeGetResolverURL); - return Response.ok(knimeGetResolverURL).header("Location", knimeGetResolverURL).build(); - - }else{ - logger.error("The input scope "+scope+" is not a VRE"); - 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 a5c57de..3ba6450 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/KnimeGetResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/KnimeGetResolver.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; @@ -55,7 +56,7 @@ public class KnimeGetResolver { try { if(vreName==null || vreName.isEmpty()){ logger.error("The path parameter 'vreName' not found or empty in the path"); - ExceptionManager.badRequestException(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{ @@ -65,7 +66,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.internalErrorException(req, "Error on reading the Application Profile for the "+KNIME_EXECUTOR_APPLICATION+". Please contact the support", this.getClass(), helpURI); + throw 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,14 +79,19 @@ public class KnimeGetResolver { }catch (ExecutionException e) { logger.error("The input VRE Name "+vreName+" not found", e); - ExceptionManager.badRequestException(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); } - return null; - }catch(Exception e){ - logger.error("Error on resolving Knime URL",e); - ExceptionManager.internalErrorException(req, "Error occurred when resolving Knime URL", this.getClass(), helpURI); - return null; + }catch (Exception e) { + + 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(); + 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/PartheosRegistryResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/PartheosRegistryResolver.java index 133d544..5cfe3c3 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/PartheosRegistryResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/PartheosRegistryResolver.java @@ -52,10 +52,14 @@ public class PartheosRegistryResolver { */ @GET @Path("/{provider}/{path}{remainPath:(/[^?$]+)?}") - public Response resolveParthenosURL(@Context HttpServletRequest req,@PathParam("provider") String provider, @PathParam("path") String path, @PathParam("remainPath") String remainPath) { - logger.info(this.getClass().getSimpleName()+" GET starts..."); + public Response resolveParthenosURL(@Context HttpServletRequest req, + @PathParam("provider") String provider, + @PathParam("path") String path, + @PathParam("remainPath") String remainPath) throws WebApplicationException { + logger.info(this.getClass().getSimpleName()+" GET starts..."); String remainPathParthenosURL = null; + try { logger.debug("provider is: "+provider); @@ -76,10 +80,17 @@ public class PartheosRegistryResolver { logger.info("Trying to resolve with Catalogue EntityName: "+normalizedEntityName); ItemCatalogueURLs itemCatalogueURLs = CatalogueResolver.getItemCatalogueURLs(UriResolverSmartGearManagerInit.getParthenosVREName(), ResourceCatalogueCodes.CTLGD.getId(), normalizedEntityName); return Response.seeOther(new URL(itemCatalogueURLs.getPrivateCataloguePortletURL()).toURI()).build(); + }catch (Exception e) { + + 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(); + throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + } + //ALREADY MANAGED AS WebApplicationException logger.error("Exception:", e); - String error = "Error occurred on resolving the path "+remainPathParthenosURL+". Please, contact the support!"; - throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI); + throw (WebApplicationException) e; } } @@ -105,7 +116,7 @@ public class PartheosRegistryResolver { if(entityName==null || entityName.isEmpty()){ logger.error("Entity Name Parameter like 'entity_name' not found or empty"); - ExceptionManager.badRequestException(req, "Mandatory body parameter 'entity_name' not found or empty", this.getClass(), helpURI); + throw ExceptionManager.badRequestException(req, "Mandatory body parameter 'entity_name' not found or empty", this.getClass(), helpURI); } //REMOVING FIRST '/' IF EXISTS @@ -145,10 +156,11 @@ public class PartheosRegistryResolver { return name.toLowerCase(); } -// public static void main(String[] args) throws UnsupportedEncodingException { -// -// String remainPathParthenosURL = "Culturalitalia/unknown/Dataset/oai%3Aculturaitalia.it%3Aoai%3Aculturaitalia.it%3Amuseiditalia-mus_11953"; -// System.out.println(URLDecoder.decode(remainPathParthenosURL, "UTF-8")); -// -// } + //TO TEST + // public static void main(String[] args) throws UnsupportedEncodingException { + // + // String remainPathParthenosURL = "Culturalitalia/unknown/Dataset/oai%3Aculturaitalia.it%3Aoai%3Aculturaitalia.it%3Amuseiditalia-mus_11953"; + // System.out.println(URLDecoder.decode(remainPathParthenosURL, "UTF-8")); + // + // } }