diff --git a/src/main/java/org/gcube/datatransfer/resolver/dataminer/DataMinerRequest.java b/src/main/java/org/gcube/datatransfer/resolver/dataminer/DataMinerRequest.java new file mode 100644 index 0000000..a8921d3 --- /dev/null +++ b/src/main/java/org/gcube/datatransfer/resolver/dataminer/DataMinerRequest.java @@ -0,0 +1,31 @@ +/** + * + */ +package org.gcube.datatransfer.resolver.dataminer; + +import java.util.Map; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + + +/** + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Nov 26, 2018 + */ +@AllArgsConstructor +@Getter +@Setter +@NoArgsConstructor +@ToString +public class DataMinerRequest { + + private String scope; + private String operatorId; + private Map parameters; + +} 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 89b1681..7c99a77 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/parthenos/ParthenosRequest.java +++ b/src/main/java/org/gcube/datatransfer/resolver/parthenos/ParthenosRequest.java @@ -3,47 +3,25 @@ */ package org.gcube.datatransfer.resolver.parthenos; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + /** * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * Nov 26, 2018 */ +@NoArgsConstructor +@AllArgsConstructor +@Getter +@Setter +@ToString public class ParthenosRequest { private String entity_name; - /** - * @return the entity_name - */ - public String getEntity_name() { - - return entity_name; - } - - - /** - * @param entity_name the entity_name to set - */ - public void setEntity_name(String entity_name) { - - this.entity_name = entity_name; - } - - - /* (non-Javadoc) - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - - StringBuilder builder = new StringBuilder(); - builder.append("ParthenosRequest [entity_name="); - builder.append(entity_name); - builder.append("]"); - return builder.toString(); - } - - - } 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 81c1676..229c7f2 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/CatalogueResolver.java @@ -23,6 +23,7 @@ import org.gcube.datatransfer.resolver.catalogue.resource.CkanCatalogueConfigura import org.gcube.datatransfer.resolver.catalogue.resource.GatewayCKANCatalogueReference; import org.gcube.datatransfer.resolver.catalogue.resource.GetAllInfrastructureVREs; import org.gcube.datatransfer.resolver.services.error.ExceptionManager; +import org.gcube.datatransfer.resolver.util.Util; import org.gcube.smartgears.utils.InnerMethodName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -101,7 +102,7 @@ public class CatalogueResolver { scope+="/"+scope; } - String serverUrl = getServerURL(req); + String serverUrl = Util.getServerURL(req); final String vreName = scope.substring(scope.lastIndexOf("/")+1, scope.length()); String fullScope = null; @@ -183,32 +184,5 @@ public class CatalogueResolver { } - /** - * Gets the server url. - * - * @param req the req - * @return the server url - */ - private String getServerURL(HttpServletRequest req) { - - String scheme = req.getScheme(); // http - String serverName = req.getServerName(); // hostname.com - int serverPort = req.getServerPort(); // 80 - //String contextPath = req.getContextPath(); // /mywebapp - - // Reconstruct original requesting URL - StringBuffer url = new StringBuffer(); - url.append(scheme).append("://").append(serverName); - - if (serverPort != 80 && serverPort != 443) - url.append(":").append(serverPort); - - // if(contextPath!=null) - // url.append(":").append(contextPath); - - String uToS = url.toString(); - logger.debug("returning servlet context URL: "+uToS); - return uToS; - } } diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/DataMinerResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/DataMinerResolver.java new file mode 100644 index 0000000..b095db5 --- /dev/null +++ b/src/main/java/org/gcube/datatransfer/resolver/services/DataMinerResolver.java @@ -0,0 +1,181 @@ +/** + * + */ +package org.gcube.datatransfer.resolver.services; + +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URLEncoder; +import java.util.Map; +import java.util.concurrent.ExecutionException; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.common.scope.impl.ScopeBean.Type; +import org.gcube.datatransfer.resolver.caches.LoadingVREsScopeCache; +import org.gcube.datatransfer.resolver.dataminer.DataMinerRequest; +import org.gcube.datatransfer.resolver.services.error.ExceptionManager; +import org.gcube.datatransfer.resolver.util.Util; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * The Class DataMinerResolver. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Nov 28, 2018 + */ +@Path("/dataminer") +public class DataMinerResolver { + + + /** + * + */ + private static final String UTF_8 = "UTF-8"; + private static Logger logger = LoggerFactory.getLogger(CatalogueResolver.class); + private static String helpURI = "https://wiki.gcube-system.org/gcube/URI_Resolver"; + + + /** + * Gets the data miner. + * + * @param req the req + * @param provider the provider + * @param path the path + * @param remainPath the remain path + * @return the data miner + */ + @GET + @Path("/{vreName}/{operatorId}") + public Response getDataMiner(@Context HttpServletRequest req, @PathParam("vreName") String vreName, @PathParam("operatorId") String operatorId) { + 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); + } + + + if(operatorId==null || operatorId.isEmpty()){ + logger.error("The path parameter 'operatorId' not found or empty in the JSON object"); + ExceptionManager.throwBadRequestException(req, "Mandatory path parameter 'operatorId' not found or empty", this.getClass(), helpURI); + } + + try{ + String fullScope = LoadingVREsScopeCache.getCache().get(vreName); + + //READ THE DATAMINER URL PORTLET FROM APPLICATION PROFRILE IN THE SCOPE fullScope + String dataminerEndPoint = "https://pre.d4science.org/group/prevre/dataminer-manager"; + + String queryString = "operatorId="+URLEncoder.encode(operatorId, UTF_8); + if(req.getQueryString()!=null && !req.getQueryString().isEmpty()){ + queryString+="&"+req.getQueryString(); + } + + String dataMinerResolveURL = String.format("%s?%s", dataminerEndPoint, queryString); + logger.info("Resolving the request as DataMinerURL: "+dataMinerResolveURL); + return Response.seeOther(new URI(dataMinerResolveURL)).build(); + + }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); + } + + return null; + }catch (Exception e) { + logger.error("error resolving catalogue link",e); + ExceptionManager.throwInternalErrorException(req, "Error occurred resolving catalogue link", this.getClass(), helpURI); + return null; + } + } + + /** + * Post catalogue. + * + * @param req the req + * @param jsonRequest the json request + * @return the response + */ + @POST + @Path("") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.TEXT_PLAIN) + public Response postCatalogue(@Context HttpServletRequest req, DataMinerRequest jsonRequest) { + logger.info(this.getClass().getSimpleName()+" POST starts..."); + + logger.info("The body contains the request: "+jsonRequest.toString()); + + String scope = jsonRequest.getScope(); + 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); + + if(scopeBean.is(Type.VRE)){ + String vreName = scopeBean.name(); + try { + //CHECK IF IT IS A VALID SCOPE + LoadingVREsScopeCache.getCache().get(vreName); + String dataminerResolverURL = String.format("%s/%s", Util.getServerURL(req), "dataminer"); + String theOperator = ""; + String queryString = ""; + try { + theOperator = URLEncoder.encode(operatorID, UTF_8); + Map parameters = jsonRequest.getParameters(); + for (String param : parameters.keySet()) { + String value = parameters.get(param); + if(value!=null) + queryString+=String.format(param+"=%s", URLEncoder.encode(parameters.get(param), UTF_8))+"&"; + } + queryString = Util.removeLastChar(queryString); + + } + catch (UnsupportedEncodingException e) { + logger.error("Encoding error: ",e); + ExceptionManager.throwBadRequestException(req, "Encoding error: "+e.getMessage(), this.getClass(), helpURI); + } + + String dataMinerURL = String.format("%s/%s/%s?%s", dataminerResolverURL, vreName, theOperator, queryString); + logger.info("Returning DataMinerURL: "+dataMinerURL); + return Response.ok(dataMinerURL).header("Location", dataMinerURL).build(); + + } + catch (ExecutionException e) { + logger.error("The input scope "+scope+" not found", e); + ExceptionManager.throwBadRequestException(req, "The input 'scope' "+"+scope+"+ "not found on Informatiion System. Is it a valid VRE?", this.getClass(), helpURI); + } + + return null; + + }else{ + logger.error("The input scope "+scope+" is not a VRE"); + ExceptionManager.throwBadRequestException(req, "The input 'scope' "+"+scope+"+ "is not a VRE", this.getClass(), helpURI); + } + + return null; + } + +} diff --git a/src/main/java/org/gcube/datatransfer/resolver/util/UrlEncoderUtil.java b/src/main/java/org/gcube/datatransfer/resolver/util/UrlEncoderUtil.java index 1ef9e57..5e83228 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/util/UrlEncoderUtil.java +++ b/src/main/java/org/gcube/datatransfer/resolver/util/UrlEncoderUtil.java @@ -74,23 +74,6 @@ public class UrlEncoderUtil { } } - return removeLastChar(query); - } - - /** - * Removes the last char. - * - * @param string the string - * @return the string - */ - public static String removeLastChar(String string){ - - if(string == null) - return null; - - if(string.length()>0) - return string.substring(0, string.length()-1); - - return string; + return Util.removeLastChar(query); } } diff --git a/src/main/java/org/gcube/datatransfer/resolver/util/Util.java b/src/main/java/org/gcube/datatransfer/resolver/util/Util.java index 3d550c5..0cfeb6e 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/util/Util.java +++ b/src/main/java/org/gcube/datatransfer/resolver/util/Util.java @@ -5,15 +5,20 @@ package org.gcube.datatransfer.resolver.util; import javax.servlet.http.HttpServletRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * The Class Util. * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Oct 22, 2018 + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * Nov 30, 2018 */ public class Util { + private static final Logger log = LoggerFactory.getLogger(Util.class); + /** * Gets the full url. * @@ -30,4 +35,64 @@ public class Util { return requestURL.append('?').append(queryString).toString(); } } + + + /** + * Gets the server url. + * + * @param req the req + * @return the server url + */ + public static String getServerURL(HttpServletRequest req) { + + String scheme = req.getScheme(); // http + String serverName = req.getServerName(); // hostname.com + int serverPort = req.getServerPort(); // 80 + //String contextPath = req.getContextPath(); // /mywebapp + + // Reconstruct original requesting URL + StringBuffer url = new StringBuffer(); + url.append(scheme).append("://").append(serverName); + + if (serverPort != 80 && serverPort != 443) + url.append(":").append(serverPort); + + // if(contextPath!=null) + // url.append(":").append(contextPath); + + String uToS = url.toString(); + log.debug("returning servlet context URL: "+uToS); + return uToS; + } + + + /** + * Gets the server url. + * + * @param req the req + * @return the server url + */ + public static String getContextURL(HttpServletRequest req) { + + String serverURL = getServerURL(req); + return String.format("%s/%s",serverURL,req.getContextPath()); + } + + /** + * Removes the last char. + * + * @param string the string + * @return the string + */ + public static String removeLastChar(String string){ + + if(string == null) + return null; + + if(string.length()>0) + return string.substring(0, string.length()-1); + + return string; + } + } diff --git a/src/test/java/GeonetworkQueryTest.java b/src/test/java/GeonetworkQueryTest.java index 6aaafca..cf4ab56 100644 --- a/src/test/java/GeonetworkQueryTest.java +++ b/src/test/java/GeonetworkQueryTest.java @@ -23,7 +23,7 @@ public class GeonetworkQueryTest { private static final String UUID = "8a878105-ef06-4b1f-843f-120fc525b22b"; - private String[] scopesProd = {"/gcube/devsec/devVRE"}; + //private String[] scopesProd = {"/gcube/devsec/devVRE"}; //private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/SIASPA"}; @@ -36,6 +36,7 @@ public class GeonetworkQueryTest { //private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/FAO_TunaAtlas"}; //private String[] scopesProd = {"/d4science.research-infrastructures.eu/D4Research/Blue-Datathon"}; + private String[] scopesProd = {"/d4science.research-infrastructures.eu/gCubeApps/RPrototypingLab"}; private LoginLevel loginLevel = LoginLevel.CKAN;