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
This commit is contained in:
Francesco Mangiacrapa 2019-03-15 11:23:04 +00:00
parent c66d6fbb69
commit d375c91890
13 changed files with 228 additions and 136 deletions

View File

@ -3,8 +3,6 @@
*/ */
package org.gcube.datatransfer.resolver; package org.gcube.datatransfer.resolver;
/** /**
* The Class ConstantsResolver. * The Class ConstantsResolver.
* *
@ -24,21 +22,10 @@ public class ConstantsResolver {
public static final String QUERY_PARAM_VALIDATION = "validation"; public static final String QUERY_PARAM_VALIDATION = "validation";
public static final String QUERY_PARAM_CONTENT_TYPE = "contentType"; public static final String QUERY_PARAM_CONTENT_TYPE = "contentType";
public static final String QUERY_PARAM_FILE_NAME = "fileName"; public static final String QUERY_PARAM_FILE_NAME = "fileName";
public static final String[] resourcesHardCoded = {"ctlg"};
/*static {
Map<String, String> aMap = new HashMap<String, String>(); //The default resource candidate to manage the input requests not matching any resolver services
aMap.put("smp", "smp"); public static final String defaultServiceToRedirect= "storage";
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);
}*/
} }

View File

@ -1,12 +1,10 @@
package org.gcube.datatransfer.resolver; package org.gcube.datatransfer.resolver;
import java.util.List;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation; import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
import org.gcube.datatransfer.resolver.init.UriResolverSmartGearManagerInit; 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.datatransfer.resolver.services.CatalogueResolver;
import org.gcube.smartgears.annotations.ManagedBy; import org.gcube.smartgears.annotations.ManagedBy;
import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.ResourceConfig;
@ -15,11 +13,9 @@ import org.glassfish.jersey.server.ResourceConfig;
@ManagedBy(UriResolverSmartGearManagerInit.class) @ManagedBy(UriResolverSmartGearManagerInit.class)
public class UriResolver extends ResourceConfig { public class UriResolver extends ResourceConfig {
private static List<String> listOfResources;
public UriResolver() { public UriResolver() {
// Register all resources present under the package. // 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()); packages(DataMinerInvocation.class.getPackage().getName());
} }
} }

View File

@ -5,6 +5,7 @@
package org.gcube.datatransfer.resolver; package org.gcube.datatransfer.resolver;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -189,7 +190,10 @@ public class UriResolverServices {
log.trace("The Application Classes are {}", applicationClasses); log.trace("The Application Classes are {}", applicationClasses);
if(listResourcePath.isEmpty()){ if(listResourcePath.isEmpty()){
log.info("Reference to the List of Resources/Services is empty, creating it...");
readResources(applicationClasses); readResources(applicationClasses);
log.info("Hard-Coding the resource/s: ");
listResourcePath.addAll(Arrays.asList(ConstantsResolver.resourcesHardCoded));
} }
return listResourcePath; return listResourcePath;

View File

@ -3,10 +3,6 @@
*/ */
package org.gcube.datatransfer.resolver.gis.geonetwork; 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.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -16,6 +12,10 @@ import org.gcube.spatial.data.geonetwork.GeoNetworkReader;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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. * The Class FilterGetRecords.

View File

@ -3,14 +3,14 @@
*/ */
package org.gcube.datatransfer.resolver.parthenos; package org.gcube.datatransfer.resolver.parthenos;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import com.fasterxml.jackson.annotation.JsonProperty;
/** /**
* *

View File

@ -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<String> 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");
}
}

View File

@ -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<String> 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();
}
}

View File

@ -43,7 +43,7 @@ import org.gcube.common.storagehub.model.Metadata;
import org.gcube.data.analysis.dminvocation.ActionType; import org.gcube.data.analysis.dminvocation.ActionType;
import org.gcube.data.analysis.dminvocation.DataMinerInvocationManager; import org.gcube.data.analysis.dminvocation.DataMinerInvocationManager;
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation; 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.error.ExceptionManager;
import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException; import org.gcube.datatransfer.resolver.services.exceptions.InternalServerException;
import org.gcube.datatransfer.resolver.util.ScopeUtil; import org.gcube.datatransfer.resolver.util.ScopeUtil;
@ -113,7 +113,7 @@ public class AnalyticsCreateResolver {
// logger.info("SecurityTokenProvider contextToken: "+contextToken); // logger.info("SecurityTokenProvider contextToken: "+contextToken);
logger.info("ScopeProvider has scope: "+scope); 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){ if(contextToken.compareTo(appToken)==0){
logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"..."); logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"...");

View File

@ -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;
}
}
}

View File

@ -17,7 +17,7 @@ import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type; 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.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.util.Util; import org.gcube.datatransfer.resolver.util.Util;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -57,7 +57,7 @@ public class KnimeCreateResolver {
// logger.info("SecurityTokenProvider contextToken: "+contextToken); // logger.info("SecurityTokenProvider contextToken: "+contextToken);
logger.info("ScopeProvider has scope: "+scope); 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){ if(contextToken.compareTo(appToken)==0){
logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"..."); logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"...");

View File

@ -37,7 +37,7 @@ public class UriResolverIndex {
String indexFile = "/WEB-INF/jsp/index.jsp"; String indexFile = "/WEB-INF/jsp/index.jsp";
try{ try{
logger.info("UriResolverIndex called"); logger.info(UriResolverIndex.class.getSimpleName() +" 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) {

View File

@ -4,12 +4,6 @@
package org.gcube.datatransfer.resolver.util; 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.BufferedInputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -45,6 +39,12 @@ import org.w3c.dom.Element;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; 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. * The Class GetResponseRecordFilter.

View File

@ -0,0 +1,21 @@
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<style type="text/css">
body {
background-color: #f8f6f2;
color: #225f97;
font-family: 'Droid Sans', arial, sans-serif;
font-size: 16px;
}
</style>
</head>
<body>
<div>
<img style="-webkit-user-select: none; cursor: zoom-out;"
src="https://www.sportmediaset.mediaset.it/binary/26.$plit/C_29_fotogallery_1024745__ImageGallery__imageGalleryItem_4_image.jpg"
width="2598" height="2074">
</div>
</body>
</html>