diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index cfa6374..6952109 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -4,6 +4,9 @@ + + uses + diff --git a/pom.xml b/pom.xml index 5df753c..be449db 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,12 @@ + + org.gcube.data.analysis + dataminer-invocation-model + [0.0.1-SNAPSHOT,1.0.0-SNAPSHOT) + + org.gcube.core common-encryption diff --git a/src/main/java/org/gcube/datatransfer/resolver/dataminer/DataMinerRequest.java b/src/main/java/org/gcube/datatransfer/resolver/dataminer/DataMinerRequest.java index a8921d3..d917d20 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/dataminer/DataMinerRequest.java +++ b/src/main/java/org/gcube/datatransfer/resolver/dataminer/DataMinerRequest.java @@ -24,7 +24,7 @@ import lombok.ToString; @ToString public class DataMinerRequest { - private String scope; + //private String scope; private String operatorId; private Map parameters; diff --git a/src/main/java/org/gcube/datatransfer/resolver/requesthandler/TokenSetter.java b/src/main/java/org/gcube/datatransfer/resolver/requesthandler/TokenSetter.java index 2a6077f..d962ffb 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/requesthandler/TokenSetter.java +++ b/src/main/java/org/gcube/datatransfer/resolver/requesthandler/TokenSetter.java @@ -18,16 +18,20 @@ import org.slf4j.LoggerFactory; @Provider public class TokenSetter implements ContainerRequestFilter, ContainerResponseFilter { - + private static final Logger log = LoggerFactory.getLogger(TokenSetter.class); - + @Context ServletContext context; - + @Override public void filter(ContainerRequestContext ctx) throws IOException { log.info("TokenSetter Request called"); - SecurityTokenProvider.instance.set(context.getInitParameter("root-app-token")); - ScopeProvider.instance.set(context.getInitParameter("root-scope")); + + 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")); } @Override @@ -36,6 +40,6 @@ public class TokenSetter implements ContainerRequestFilter, ContainerResponseFil 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 new file mode 100644 index 0000000..c6e1ebf --- /dev/null +++ b/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsCreateResolver.java @@ -0,0 +1,172 @@ +/** + * + */ +package org.gcube.datatransfer.resolver.services; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.xml.bind.JAXBException; + +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.data.analysis.dminvocation.ActionType; +import org.gcube.data.analysis.dminvocation.DataMinerInvocationManager; +import org.gcube.data.analysis.dminvocation.model.DataMinerInputParams; +import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation; +import org.gcube.data.analysis.dminvocation.model.DataMinerParam; +import org.gcube.data.analysis.dminvocation.model.DataMinerParameters; +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("/analytics") +public class AnalyticsCreateResolver { + + + /** + * + */ + private static final String UTF_8 = "UTF-8"; + private static Logger logger = LoggerFactory.getLogger(AnalyticsCreateResolver.class); + private static String helpURI = "https://wiki.gcube-system.org/gcube/URI_Resolver"; + + /** + * Post catalogue. + * + * @param req the req + * @param jsonRequest the json request + * @return the response + */ + @POST + @Path("/create") + @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 = ScopeProvider.instance.get(); + 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(); + try { + + String dataminerResolverURL = String.format("%s/%s", Util.getServerURL(req), "dataminer/get"); + + //Creating DM invocation file + DataMinerInvocation dmInvocation = new DataMinerInvocation(); + dmInvocation.setOperatorId(operatorID); + dmInvocation.setActionType(ActionType.RUN); + + DataMinerInputParams inputParams = new DataMinerInputParams(); + List listParam = new ArrayList(); + Map parameters = jsonRequest.getParameters(); + for (String param : parameters.keySet()) { + listParam.add(new DataMinerParam(param, parameters.get(param))); + } + + inputParams.setListParam(listParam); + dmInvocation.setParameters(new DataMinerParameters(inputParams, null)); + File tempInvocationFile = null; + try { + + ByteArrayOutputStream xmlByteArray = DataMinerInvocationManager.marshaling(dmInvocation, org.gcube.data.analysis.dminvocation.MediaType.ApplicationXML); + tempInvocationFile = createTempFile(operatorID+UUID.randomUUID(), ".xml", xmlByteArray.toByteArray()); + + //CREATE THE FILE ON STORAGE HUB + + } + catch (IOException | JAXBException e) { + logger.error("Error on creating you request with "+dmInvocation+"", e); + ExceptionManager.throwBadRequestException(req, "Error on creating you request with "+dmInvocation, this.getClass(), helpURI); + }finally{ + //DELETING THE TEMP FILE +// if(tempInvocationFile!=null && tempInvocationFile.exists()) +// tempInvocationFile.delete(); + } + + + String dataMinerURL = String.format("%s/%s?%s=%s", dataminerResolverURL, vreName, "di", URLEncoder.encode(publicLinkToDMInvFile, UTF_8)); + logger.info("Returning Analytics URL: "+dataMinerURL); + return Response.ok(dataMinerURL).header("Location", dataMinerURL).build(); + + } + catch (UnsupportedEncodingException e) { + logger.error("Encoding error for "+publicLinkToDMInvFile+"", e); + ExceptionManager.throwBadRequestException(req, "Error on encoding the public link "+publicLinkToDMInvFile, this.getClass(), helpURI); + } + + return null; + + }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); + } + + return null; + } + + + /** + * Creates the temp file. + * + * @param fileName the file name + * @param extension the extension + * @param data the data + * @return the file + * @throws IOException Signals that an I/O exception has occurred. + */ + private static File createTempFile(String fileName, String extension, byte[] data) throws IOException { + // Since Java 1.7 Files and Path API simplify operations on files + java.nio.file.Path path = Files.createTempFile(fileName, extension); + File file = path.toFile(); + // writing sample data + Files.write(path, data); + logger.info("Created the temfile: "+file.getAbsolutePath()); + return file; + } + +} diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsGetResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsGetResolver.java new file mode 100644 index 0000000..32506e5 --- /dev/null +++ b/src/main/java/org/gcube/datatransfer/resolver/services/AnalyticsGetResolver.java @@ -0,0 +1,88 @@ +/** + * + */ +package org.gcube.datatransfer.resolver.services; + +import java.net.URI; +import java.util.concurrent.ExecutionException; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; + +import org.gcube.datatransfer.resolver.caches.LoadingVREsScopeCache; +import org.gcube.datatransfer.resolver.services.error.ExceptionManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * The Class DataMinerResolver. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Nov 28, 2018 + */ +@Path("/analytics") +public class AnalyticsGetResolver { + + + /** + * + */ + private static final String UTF_8 = "UTF-8"; + private static Logger logger = LoggerFactory.getLogger(AnalyticsGetResolver.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("/get/{vreName}") + public Response getDataMiner(@Context HttpServletRequest req, @PathParam("vreName") String vreName) { + 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); + } + + 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 = ""; + 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; + } + } + +} diff --git a/src/main/java/org/gcube/datatransfer/resolver/services/DataMinerResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/DataMinerResolver.java deleted file mode 100644 index e5a848d..0000000 --- a/src/main/java/org/gcube/datatransfer/resolver/services/DataMinerResolver.java +++ /dev/null @@ -1,181 +0,0 @@ -/** - * - */ -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/services/StorageIDResolver.java b/src/main/java/org/gcube/datatransfer/resolver/services/StorageIDResolver.java index c8d8357..65fbc95 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/services/StorageIDResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/services/StorageIDResolver.java @@ -37,7 +37,7 @@ import org.slf4j.LoggerFactory; * * Oct 19, 2018 */ -@Path("/") +@Path("storage") public class StorageIDResolver { /** diff --git a/src/main/webapp/WEB-INF/gcube-app.xml b/src/main/webapp/WEB-INF/gcube-app.xml index 6098a2a..1a45af6 100644 --- a/src/main/webapp/WEB-INF/gcube-app.xml +++ b/src/main/webapp/WEB-INF/gcube-app.xml @@ -1,7 +1,18 @@ - URIResolver - DataTransfer - 1.0.0-SNAPSHOT - URIResolver RESTful - /* + URIResolver + DataTransfer + 1.0.0-SNAPSHOT + URIResolver RESTful + /smp + /id + /gis + /storage* + /ctlg* + /catalogue + /geonetwork + /shub + /parthenos_registry + /analytics/get + /knime/get + \ No newline at end of file diff --git a/src/test/resources/DataMinerInvocation.xml b/src/test/resources/DataMinerInvocation.xml new file mode 100644 index 0000000..b177e59 --- /dev/null +++ b/src/test/resources/DataMinerInvocation.xml @@ -0,0 +1,17 @@ + + + THE_OPERATOR_ID + + + + key1 + value1 + + + key2 + value2 + + + + + \ No newline at end of file