From d375c9189012b74353746fdf08604590e57830f3 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Fri, 15 Mar 2019 11:23:04 +0000 Subject: [PATCH] Removed unused import Performed a Refactor from TokenSetter to RequestHandler git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@178597 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../resolver/ConstantsResolver.java | 23 +-- .../datatransfer/resolver/UriResolver.java | 8 +- .../resolver/UriResolverServices.java | 4 + .../gis/geonetwork/FilterGetRecords.java | 8 +- .../resolver/parthenos/ParthenosRequest.java | 4 +- .../requesthandler/RequestHandler.java | 131 ++++++++++++++++++ .../resolver/requesthandler/TokenSetter.java | 95 ------------- .../services/AnalyticsCreateResolver.java | 4 +- .../resolver/services/CR7Huevos.java | 48 +++++++ .../services/KnimeCreateResolver.java | 4 +- .../resolver/services/UriResolverIndex.java | 2 +- .../util/GetResponseRecordFilter.java | 12 +- src/main/webapp/WEB-INF/jsp/cr7huevos.jsp | 21 +++ 13 files changed, 228 insertions(+), 136 deletions(-) create mode 100644 src/main/java/org/gcube/datatransfer/resolver/requesthandler/RequestHandler.java delete mode 100644 src/main/java/org/gcube/datatransfer/resolver/requesthandler/TokenSetter.java create mode 100644 src/main/java/org/gcube/datatransfer/resolver/services/CR7Huevos.java create mode 100644 src/main/webapp/WEB-INF/jsp/cr7huevos.jsp diff --git a/src/main/java/org/gcube/datatransfer/resolver/ConstantsResolver.java b/src/main/java/org/gcube/datatransfer/resolver/ConstantsResolver.java index b8f7be5..3a25806 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/ConstantsResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/ConstantsResolver.java @@ -3,8 +3,6 @@ */ package org.gcube.datatransfer.resolver; - - /** * The Class ConstantsResolver. * @@ -24,21 +22,10 @@ public class ConstantsResolver { public static final String QUERY_PARAM_VALIDATION = "validation"; public static final String QUERY_PARAM_CONTENT_TYPE = "contentType"; public static final String QUERY_PARAM_FILE_NAME = "fileName"; - - - /*static { - Map aMap = new HashMap(); - aMap.put("smp", "smp"); - aMap.put("id", "id"); - aMap.put("gis", "gis"); - aMap.put("storage", "storage"); - aMap.put("ctlg", "ctlg"); - aMap.put("catalogue", "catalogue"); - aMap.put("geonetwork", "geonetwork"); - aMap.put("shub", "shub"); - aMap.put("parthenos_registry", "parthenos_registry"); - aMap.put("knime", "knime"); - aMap = Collections.unmodifiableMap(aMap); - }*/ + + public static final String[] resourcesHardCoded = {"ctlg"}; + + //The default resource candidate to manage the input requests not matching any resolver services + public static final String defaultServiceToRedirect= "storage"; } diff --git a/src/main/java/org/gcube/datatransfer/resolver/UriResolver.java b/src/main/java/org/gcube/datatransfer/resolver/UriResolver.java index c90e4ba..76cd2a4 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/UriResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/UriResolver.java @@ -1,12 +1,10 @@ package org.gcube.datatransfer.resolver; -import java.util.List; - import javax.ws.rs.Path; import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation; import org.gcube.datatransfer.resolver.init.UriResolverSmartGearManagerInit; -import org.gcube.datatransfer.resolver.requesthandler.TokenSetter; +import org.gcube.datatransfer.resolver.requesthandler.RequestHandler; import org.gcube.datatransfer.resolver.services.CatalogueResolver; import org.gcube.smartgears.annotations.ManagedBy; import org.glassfish.jersey.server.ResourceConfig; @@ -15,11 +13,9 @@ import org.glassfish.jersey.server.ResourceConfig; @ManagedBy(UriResolverSmartGearManagerInit.class) public class UriResolver extends ResourceConfig { - private static List listOfResources; - public UriResolver() { // Register all resources present under the package. - packages(CatalogueResolver.class.getPackage().getName(), TokenSetter.class.getPackage().getName()); + packages(CatalogueResolver.class.getPackage().getName(), RequestHandler.class.getPackage().getName()); packages(DataMinerInvocation.class.getPackage().getName()); } } diff --git a/src/main/java/org/gcube/datatransfer/resolver/UriResolverServices.java b/src/main/java/org/gcube/datatransfer/resolver/UriResolverServices.java index 9a29f88..ee0439b 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/UriResolverServices.java +++ b/src/main/java/org/gcube/datatransfer/resolver/UriResolverServices.java @@ -5,6 +5,7 @@ package org.gcube.datatransfer.resolver; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Set; @@ -189,7 +190,10 @@ public class UriResolverServices { log.trace("The Application Classes are {}", applicationClasses); if(listResourcePath.isEmpty()){ + log.info("Reference to the List of Resources/Services is empty, creating it..."); readResources(applicationClasses); + log.info("Hard-Coding the resource/s: "); + listResourcePath.addAll(Arrays.asList(ConstantsResolver.resourcesHardCoded)); } return listResourcePath; diff --git a/src/main/java/org/gcube/datatransfer/resolver/gis/geonetwork/FilterGetRecords.java b/src/main/java/org/gcube/datatransfer/resolver/gis/geonetwork/FilterGetRecords.java index 794e786..ad2a494 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/gis/geonetwork/FilterGetRecords.java +++ b/src/main/java/org/gcube/datatransfer/resolver/gis/geonetwork/FilterGetRecords.java @@ -3,10 +3,6 @@ */ package org.gcube.datatransfer.resolver.gis.geonetwork; -import it.geosolutions.geonetwork.util.GNSearchRequest; -import it.geosolutions.geonetwork.util.GNSearchResponse; -import it.geosolutions.geonetwork.util.GNSearchResponse.GNMetadata; - import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -16,6 +12,10 @@ import org.gcube.spatial.data.geonetwork.GeoNetworkReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import it.geosolutions.geonetwork.util.GNSearchRequest; +import it.geosolutions.geonetwork.util.GNSearchResponse; +import it.geosolutions.geonetwork.util.GNSearchResponse.GNMetadata; + /** * The Class FilterGetRecords. diff --git a/src/main/java/org/gcube/datatransfer/resolver/parthenos/ParthenosRequest.java b/src/main/java/org/gcube/datatransfer/resolver/parthenos/ParthenosRequest.java index fd184f9..1e407e8 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/parthenos/ParthenosRequest.java +++ b/src/main/java/org/gcube/datatransfer/resolver/parthenos/ParthenosRequest.java @@ -3,14 +3,14 @@ */ package org.gcube.datatransfer.resolver.parthenos; +import com.fasterxml.jackson.annotation.JsonProperty; + import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; -import com.fasterxml.jackson.annotation.JsonProperty; - /** * diff --git a/src/main/java/org/gcube/datatransfer/resolver/requesthandler/RequestHandler.java b/src/main/java/org/gcube/datatransfer/resolver/requesthandler/RequestHandler.java new file mode 100644 index 0000000..cdd7bf8 --- /dev/null +++ b/src/main/java/org/gcube/datatransfer/resolver/requesthandler/RequestHandler.java @@ -0,0 +1,131 @@ +package org.gcube.datatransfer.resolver.requesthandler; + +import java.io.IOException; +import java.net.URI; +import java.util.List; + +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.container.ContainerRequestContext; +import javax.ws.rs.container.ContainerRequestFilter; +import javax.ws.rs.container.ContainerResponseContext; +import javax.ws.rs.container.ContainerResponseFilter; +import javax.ws.rs.container.PreMatching; +import javax.ws.rs.container.ResourceContext; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.Context; +import javax.ws.rs.ext.Provider; + +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.datatransfer.resolver.ConstantsResolver; +import org.gcube.datatransfer.resolver.UriResolverServices; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * The Class RequestHandler. + * + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * + * Mar 15, 2019 + */ +@Provider +@PreMatching +public class RequestHandler implements ContainerRequestFilter, ContainerResponseFilter { + + public static final String ROOT_SCOPE = "root-scope"; + + public static final String ROOT_APP_TOKEN = "root-app-token"; + + private static final Logger log = LoggerFactory.getLogger(RequestHandler.class); + + @Context ServletContext context; + + @Context + HttpServletRequest webRequest; + + @Context + Application application; + + @Context + ResourceContext resourceContext; + + /* (non-Javadoc) + * @see javax.ws.rs.container.ContainerRequestFilter#filter(javax.ws.rs.container.ContainerRequestContext) + */ + @Override + public void filter(ContainerRequestContext reqContext) throws IOException { + log.info(RequestHandler.class.getSimpleName() +" Request called"); + + if(SecurityTokenProvider.instance.get()==null) + SecurityTokenProvider.instance.set(context.getInitParameter(ROOT_APP_TOKEN)); + + if(ScopeProvider.instance.get()==null) + ScopeProvider.instance.set(context.getInitParameter(ROOT_SCOPE)); + + log.info("Token and Scope Provider set called"); + + List listOfPath = UriResolverServices.getInstance().getListOfResourcePath(application.getClasses()); + log.debug("The resources are: {}", listOfPath); + + String path = reqContext.getUriInfo().getPath(); + log.debug("The path is: {}", path); + + if(path==null || path.isEmpty()) { + log.debug("The path is null or empty, redirecting to /index"); + URI newRequestURI = reqContext.getUriInfo().getBaseUriBuilder().path("/index").build(); + reqContext.setRequestUri(newRequestURI); + return; + } + + String[] splittePath = null; + boolean resourceToRedirectFound = false; + String candidateResource = ""; + try { + splittePath = path.split("/"); + if(splittePath!=null && splittePath.length>0) { + String resourceName = splittePath[0]; + if(resourceName!=null && !resourceName.isEmpty()) { + for (String resource : listOfPath) { + if(resource.startsWith(resourceName)) { + candidateResource = resourceName; + resourceToRedirectFound = true; + break; + } + } + }else + log.warn("It was not possible to get the resource name from the splitted path {}. No action performed", path); + }else { + log.warn("It was not possible to split the path {}. No action performed", path); + } + }catch (Exception e) { + log.error("Error trying to retrieve the service able to manage the request. No action performed", e); + } + + if(resourceToRedirectFound) { + log.debug("The input request '{}' can be managed by the service '{}'. No redirect performed", path, candidateResource); + }else { + log.info("No resource/service found to manage the input request '{}'", path); + String newPath = String.format("/%s/%s", ConstantsResolver.defaultServiceToRedirect,path); + //log.debug("The path to redirect is '{}'", newPath); + URI newRequestURI = reqContext.getUriInfo().getBaseUriBuilder().path(newPath).build(); + log.info("Redirect to URI path '{}'", newRequestURI.toString()); + reqContext.setRequestUri(newRequestURI); + } + } + + /* (non-Javadoc) + * @see javax.ws.rs.container.ContainerResponseFilter#filter(javax.ws.rs.container.ContainerRequestContext, javax.ws.rs.container.ContainerResponseContext) + */ + @Override + public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) + throws IOException { + log.info(RequestHandler.class.getSimpleName() +" Response called"); + SecurityTokenProvider.instance.reset(); + ScopeProvider.instance.reset(); + log.info("Token and Scope Provider reset called"); + + } +} \ No newline at end of file diff --git a/src/main/java/org/gcube/datatransfer/resolver/requesthandler/TokenSetter.java b/src/main/java/org/gcube/datatransfer/resolver/requesthandler/TokenSetter.java deleted file mode 100644 index d654a27..0000000 --- a/src/main/java/org/gcube/datatransfer/resolver/requesthandler/TokenSetter.java +++ /dev/null @@ -1,95 +0,0 @@ -package org.gcube.datatransfer.resolver.requesthandler; - -import java.io.IOException; -import java.net.URI; -import java.util.List; - -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.container.ContainerRequestContext; -import javax.ws.rs.container.ContainerRequestFilter; -import javax.ws.rs.container.ContainerResponseContext; -import javax.ws.rs.container.ContainerResponseFilter; -import javax.ws.rs.container.ResourceContext; -import javax.ws.rs.core.Application; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.UriInfo; -import javax.ws.rs.ext.Provider; - -import org.gcube.common.authorization.library.provider.SecurityTokenProvider; -import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.datatransfer.resolver.MultiReadHttpServletRequest; -import org.gcube.datatransfer.resolver.UriResolverServices; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -/** - * The Class TokenSetter. - * - * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) - * Feb 19, 2019 - */ -@Provider -public class TokenSetter implements ContainerRequestFilter, ContainerResponseFilter { - - public static final String ROOT_SCOPE = "root-scope"; - - public static final String ROOT_APP_TOKEN = "root-app-token"; - - private static final Logger log = LoggerFactory.getLogger(TokenSetter.class); - - @Context ServletContext context; - - @Context - HttpServletRequest webRequest; - - @Context - Application application; - - @Context - ResourceContext resourceContext; - - /* (non-Javadoc) - * @see javax.ws.rs.container.ContainerRequestFilter#filter(javax.ws.rs.container.ContainerRequestContext) - */ - @Override - public void filter(ContainerRequestContext reqContext) throws IOException { - log.info("TokenSetter Request called"); - - if(SecurityTokenProvider.instance.get()==null) - SecurityTokenProvider.instance.set(context.getInitParameter(ROOT_APP_TOKEN)); - - if(ScopeProvider.instance.get()==null) - ScopeProvider.instance.set(context.getInitParameter(ROOT_SCOPE)); - - - MultiReadHttpServletRequest multiReadRequest = new MultiReadHttpServletRequest(webRequest); - String requestURI = multiReadRequest.getRequestURI(); - String queryString = multiReadRequest.getQueryString(); - log.debug("Request URI: " + requestURI + ", QueryString: " +queryString+ ", Path Info: "+multiReadRequest.getPathInfo()); - List listOfPath = UriResolverServices.getInstance().getListOfResourcePath(application.getClasses()); - log.info("The resources are: {}", listOfPath); - - UriInfo uriInfo = reqContext.getUriInfo(); - String prefix = "/redirect"; - String path = uriInfo.getRequestUri().getPath(); - log.info("The path is: {}", path); - if(requestURI.startsWith("qqqqq")){ - URI newRequestURI = uriInfo.getBaseUriBuilder().path("new").build(); - reqContext.setRequestUri(newRequestURI); - } - } - - /* (non-Javadoc) - * @see javax.ws.rs.container.ContainerResponseFilter#filter(javax.ws.rs.container.ContainerRequestContext, javax.ws.rs.container.ContainerResponseContext) - */ - @Override - public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) - throws IOException { - log.info("TokenSetter Response called"); - SecurityTokenProvider.instance.reset(); - ScopeProvider.instance.reset(); - - } -} \ No newline at end of file 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 199c113..c20b674 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsCreateResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsCreateResolver.java @@ -43,7 +43,7 @@ import org.gcube.common.storagehub.model.Metadata; import org.gcube.data.analysis.dminvocation.ActionType; import org.gcube.data.analysis.dminvocation.DataMinerInvocationManager; import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation; -import org.gcube.datatransfer.resolver.requesthandler.TokenSetter; +import org.gcube.datatransfer.resolver.requesthandler.RequestHandler; import org.gcube.datatransfer.resolver.services.error.ExceptionManager; import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException; import org.gcube.datatransfer.resolver.util.ScopeUtil; @@ -113,7 +113,7 @@ public class AnalyticsCreateResolver { // 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(RequestHandler.ROOT_APP_TOKEN); if(contextToken.compareTo(appToken)==0){ logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"..."); diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/CR7Huevos.java b/src/main/java/org/gcube/datatransfer/resolver/services/CR7Huevos.java new file mode 100644 index 0000000..b6456ff --- /dev/null +++ b/src/main/java/org/gcube/datatransfer/resolver/services/CR7Huevos.java @@ -0,0 +1,48 @@ +package org.gcube.datatransfer.resolver.services; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; + +import org.gcube.datatransfer.resolver.services.error.ExceptionManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Path("cr7huevos") +public class CR7Huevos { + + private static Logger logger = LoggerFactory.getLogger(UriResolverIndex.class); + + @GET + @Produces({MediaType.TEXT_HTML}) + @Path("") + public InputStream index(@Context HttpServletRequest req) throws WebApplicationException{ + + String indexFile = "/WEB-INF/jsp/cr7huevos.jsp"; + + try{ + logger.info(CR7Huevos.class.getSimpleName() +" called"); + String realPath = req.getServletContext().getRealPath(indexFile); + return new FileInputStream(new File(realPath)); + }catch (Exception e) { + + if(!(e instanceof WebApplicationException)){ + //UNEXPECTED EXCEPTION managing it as WebApplicationException + String error = "Index.jsp not found. Please, contact the support!"; + throw ExceptionManager.internalErrorException(req, error, this.getClass(), null); + } + //ALREADY MANAGED AS WebApplicationException + logger.error("Exception:", e); + throw (WebApplicationException) e; + } + } + +} 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 6d21ae4..716d5fe 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/KnimeCreateResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/KnimeCreateResolver.java @@ -17,7 +17,7 @@ import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean.Type; -import org.gcube.datatransfer.resolver.requesthandler.TokenSetter; +import org.gcube.datatransfer.resolver.requesthandler.RequestHandler; import org.gcube.datatransfer.resolver.services.error.ExceptionManager; import org.gcube.datatransfer.resolver.util.Util; import org.slf4j.Logger; @@ -57,7 +57,7 @@ public class KnimeCreateResolver { // 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(RequestHandler.ROOT_APP_TOKEN); if(contextToken.compareTo(appToken)==0){ logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"..."); diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/UriResolverIndex.java b/src/main/java/org/gcube/datatransfer/resolver/services/UriResolverIndex.java index b5835e2..fd86ca3 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/UriResolverIndex.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/UriResolverIndex.java @@ -37,7 +37,7 @@ public class UriResolverIndex { String indexFile = "/WEB-INF/jsp/index.jsp"; try{ - logger.info("UriResolverIndex called"); + logger.info(UriResolverIndex.class.getSimpleName() +" called"); String realPath = req.getServletContext().getRealPath(indexFile); return new FileInputStream(new File(realPath)); }catch (Exception e) { diff --git a/src/main/java/org/gcube/datatransfer/resolver/util/GetResponseRecordFilter.java b/src/main/java/org/gcube/datatransfer/resolver/util/GetResponseRecordFilter.java index 92b0a52..e93ddba 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/util/GetResponseRecordFilter.java +++ b/src/main/java/org/gcube/datatransfer/resolver/util/GetResponseRecordFilter.java @@ -4,12 +4,6 @@ package org.gcube.datatransfer.resolver.util; -import it.geosolutions.geonetwork.exception.GNLibException; -import it.geosolutions.geonetwork.exception.GNServerException; -import it.geosolutions.geonetwork.util.GNSearchRequest; -import it.geosolutions.geonetwork.util.GNSearchResponse; -import it.geosolutions.geonetwork.util.GNSearchResponse.GNMetadata; - import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -45,6 +39,12 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import it.geosolutions.geonetwork.exception.GNLibException; +import it.geosolutions.geonetwork.exception.GNServerException; +import it.geosolutions.geonetwork.util.GNSearchRequest; +import it.geosolutions.geonetwork.util.GNSearchResponse; +import it.geosolutions.geonetwork.util.GNSearchResponse.GNMetadata; + /** * The Class GetResponseRecordFilter. diff --git a/src/main/webapp/WEB-INF/jsp/cr7huevos.jsp b/src/main/webapp/WEB-INF/jsp/cr7huevos.jsp new file mode 100644 index 0000000..e634682 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/cr7huevos.jsp @@ -0,0 +1,21 @@ + + + + + + + +
+ +
+ + \ No newline at end of file