updated comment and optimized the code

starting work on Knime Resolver

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@174837 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-12-13 10:09:08 +00:00
parent 3b0f7f5e45
commit 6334b99520
5 changed files with 229 additions and 60 deletions

View File

@ -50,8 +50,9 @@ public class AnalyticsCreateResolver {
private static Logger logger = LoggerFactory.getLogger(AnalyticsCreateResolver.class);
private static String helpURI = "https://wiki.gcube-system.org/gcube/URI_Resolver#Analitycs_Resolver";
/**
* Post catalogue.
* Creates the analytics url.
*
* @param req the req
* @param body the body
@ -61,7 +62,7 @@ public class AnalyticsCreateResolver {
@Path("/create")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public Response postCatalogue(@Context HttpServletRequest req, String body) {
public Response createAnalyticsURL(@Context HttpServletRequest req, String body) {
logger.info(this.getClass().getSimpleName()+" POST starts...");
logger.info("body is: "+body);
@ -107,53 +108,48 @@ public class AnalyticsCreateResolver {
if(scopeBean.is(Type.VRE)){
String vreName = scopeBean.name();
// try {
String dataminerResolverURL = String.format("%s/%s", Util.getServerURL(req), "analytics/get");
String analyticsGetResolverURL = String.format("%s/%s", Util.getServerURL(req), "analytics/get");
//Creating DM invocation file
if(jsonRequest.getActionType()==null)
jsonRequest.setActionType(ActionType.RUN);
//Creating DM invocation file
if(jsonRequest.getActionType()==null)
jsonRequest.setActionType(ActionType.RUN);
File tempInvocationFile = null;
try {
File tempInvocationFile = null;
try {
ByteArrayOutputStream xmlByteArray = DataMinerInvocationManager.getInstance().marshaling(jsonRequest, org.gcube.data.analysis.dminvocation.MediaType.ApplicationXML, true);
String uniqueName = createDMInvocationFileName(jsonRequest.getOperatorId());
tempInvocationFile = createTempFile(uniqueName, ".xml", xmlByteArray.toByteArray());
ByteArrayOutputStream xmlByteArray = DataMinerInvocationManager.getInstance().marshaling(jsonRequest, org.gcube.data.analysis.dminvocation.MediaType.ApplicationXML, true);
String uniqueName = createDMInvocationFileName(jsonRequest.getOperatorId());
tempInvocationFile = createTempFile(uniqueName, ".xml", xmlByteArray.toByteArray());
//CREATE THE FILE ON STORAGE HUB
StorageHubClient shc = new StorageHubClient();
logger.info("Created StorageHubClient Instance, uploading file: "+tempInvocationFile.getName());
FileContainer fileContainer = shc.getWSRoot().uploadFile(new FileInputStream(tempInvocationFile), tempInvocationFile.getName(), "DataMinerInvocation Request created by "+this.getClass().getSimpleName());
logger.info("UPLOADED FILE at: "+fileContainer.getPublicLink());
URL thePublicLink = fileContainer.getPublicLink();
publicLinkToDMInvFile = thePublicLink!=null?thePublicLink.toString():null;
}
catch (Exception e) {
logger.error("Error on creating 'dataminer-invocation:", e);
ExceptionManager.throwBadRequestException(req, "Error on creating your 'dataminer-invocation' request with "+jsonRequest+". \nPlease contact the support", this.getClass(), helpURI);
}finally{
//DELETING THE TEMP FILE
// if(tempInvocationFile!=null && tempInvocationFile.exists())
// tempInvocationFile.delete();
//CREATE THE FILE ON STORAGE HUB
StorageHubClient shc = new StorageHubClient();
logger.info("Created StorageHubClient Instance, uploading file: "+tempInvocationFile.getName());
FileContainer fileContainer = shc.getWSRoot().uploadFile(new FileInputStream(tempInvocationFile), tempInvocationFile.getName(), "DataMinerInvocation Request created by "+this.getClass().getSimpleName());
logger.info("UPLOADED FILE at: "+fileContainer.getPublicLink());
URL thePublicLink = fileContainer.getPublicLink();
publicLinkToDMInvFile = thePublicLink!=null?thePublicLink.toString():null;
}
catch (Exception e) {
logger.error("Error on creating 'dataminer-invocation:", e);
ExceptionManager.throwBadRequestException(req, "Error on creating your 'dataminer-invocation' request with "+jsonRequest+". \nPlease contact the support", this.getClass(), helpURI);
}finally{
try{
//DELETING THE TEMP FILE
if(tempInvocationFile!=null && tempInvocationFile.exists())
tempInvocationFile.delete();
}catch(Exception e){
//silent
}
}
if(publicLinkToDMInvFile==null){
logger.error("Error on creating the public link to file");
ExceptionManager.throwBadRequestException(req, "Error on getting link to your 'dataminer-invocation' request. Plese contact the support "+jsonRequest, this.getClass(), helpURI);
}
if(publicLinkToDMInvFile==null){
logger.error("Error on creating the public link to file");
ExceptionManager.throwBadRequestException(req, "Error on getting link to your 'dataminer-invocation' request. Plese contact the support "+jsonRequest, this.getClass(), helpURI);
}
String dataMinerURL = String.format("%s/%s?%s=%s", dataminerResolverURL, vreName, DATAMINER_INVOCATION_MODEL, publicLinkToDMInvFile);
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;
String dataMinerURL = String.format("%s/%s?%s=%s", analyticsGetResolverURL, vreName, DATAMINER_INVOCATION_MODEL, publicLinkToDMInvFile);
logger.info("Returning Analytics URL: "+dataMinerURL);
return Response.ok(dataMinerURL).header("Location", dataMinerURL).build();
}else{
logger.error("The input scope "+scope+" is not a VRE");

View File

@ -21,10 +21,10 @@ import org.slf4j.LoggerFactory;
/**
* The Class DataMinerResolver.
* The Class AnalyticsGetResolver.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Nov 28, 2018
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
* Dec 13, 2018
*/
@Path("/analytics")
public class AnalyticsGetResolver {
@ -42,15 +42,15 @@ public class AnalyticsGetResolver {
/**
* Gets the data miner.
* Resolve analytics url.
*
* @param req the req
* @param vreName the vre name
* @return the data miner
* @return the response
*/
@GET
@Path("/get/{vreName}")
public Response getDataMiner(@Context HttpServletRequest req, @PathParam("vreName") String vreName) {
public Response resolveAnalyticsURL(@Context HttpServletRequest req, @PathParam("vreName") String vreName) {
logger.info(this.getClass().getSimpleName()+" GET starts...");
try {
@ -71,15 +71,11 @@ public class AnalyticsGetResolver {
//READ THE DATAMINER URL PORTLET FROM APPLICATION PROFRILE IN THE SCOPE fullScope
String analyticsExecutorEndPoint = reader.getApplicationProfile().getUrl();
String queryString = "";
if(req.getQueryString()!=null && !req.getQueryString().isEmpty()){
queryString+=req.getQueryString();
}
String analExecutorURL = String.format("%s?%s", analyticsExecutorEndPoint, queryString);
logger.info("Resolving the request with the "+ANALYTICS_EXECUTOR_PORTLET_NAME+" URL: "+analExecutorURL);
return Response.seeOther(new URI(analExecutorURL)).build();
//CHECKING THE QUERY STRING
String queryString = req.getQueryString()!=null?req.getQueryString():"";
String analitycsExecutorURL = String.format("%s?%s", analyticsExecutorEndPoint, queryString);
logger.info("Resolving the request with the "+ANALYTICS_EXECUTOR_PORTLET_NAME+" URL: "+analitycsExecutorURL);
return Response.seeOther(new URI(analitycsExecutorURL)).build();
}catch (ExecutionException e) {
logger.error("The input VRE Name "+vreName+" not found", e);
@ -88,8 +84,8 @@ public class AnalyticsGetResolver {
return null;
}catch (Exception e) {
logger.error("error resolving catalogue link",e);
ExceptionManager.throwInternalErrorException(req, "Error occurred resolving catalogue link", this.getClass(), helpURI);
logger.error("Error on resolving Anaylitics URL",e);
ExceptionManager.throwInternalErrorException(req, "Error occurred when resolving Anaylitics URL", this.getClass(), helpURI);
return null;
}
}

View File

@ -157,7 +157,6 @@ public class CatalogueResolver {
logger.info("For scope "+fullScope+" loaded end points: "+ckanCatalogueReference);
//IS THE PRODUCT PLUBLIC OR PRIVATE?
String datasetName = entityName;
boolean isPublicItem = false;
if(ckanCatalogueReference.getCkanURL()!=null){

View File

@ -0,0 +1,86 @@
/**
*
*/
package org.gcube.datatransfer.resolver.services;
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 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.services.error.ExceptionManager;
import org.gcube.datatransfer.resolver.util.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Dec 13, 2018
*/
@Path("/knime")
public class KnimeCreateResolver {
private static Logger logger = LoggerFactory.getLogger(KnimeCreateResolver.class);
private static String helpURI = "https://gcube.wiki.gcube-system.org/gcube/URI_Resolver#KNIME_Resolver";
/**
* Post catalogue.
*
* @param req the req
* @param body the body
* @return the response
*/
@POST
@Path("/create")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public Response createKnimeURL(@Context HttpServletRequest req) {
logger.info(this.getClass().getSimpleName()+" POST starts...");
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);
if(contextToken.compareTo(appToken)==0){
logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"...");
ExceptionManager.throwUnauthorizedException(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");
ExceptionManager.throwBadRequestException(req, "Working in the "+scope+" scope that is not a VRE. Use a token of VRE", this.getClass(), helpURI);
}
return null;
}
}

View File

@ -0,0 +1,92 @@
/**
*
*/
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.applicationprofile.ApplicationProfileReader;
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 KnimeGetResolver.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Dec 13, 2018
*/
@Path("/knime")
public class KnimeGetResolver {
private static Logger logger = LoggerFactory.getLogger(KnimeGetResolver.class);
private static String helpURI = "https://gcube.wiki.gcube-system.org/gcube/URI_Resolver#KNIME_Resolver";
private static final String ORG_GCUBE_PORTLETS_USER_DATAMINERMANAGER_SERVER_DATA_MINER_MANAGER_SERVICE_IMPL =
"org.gcube.portlets.user.dataminermanager.server.DataMinerManagerServiceImpl";
private static final String APPLICATION_PROFILE = "ApplicationProfile";
private static final String KNIME_EXECUTOR_APPLICATION = null;
/**
* Resolve knime url.
*
* @param req the req
* @param vreName the vre name
* @return the response
*/
@GET
@Path("/get/{vreName}")
public Response resolveKnimeURL(@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);
ApplicationProfileReader reader = null;
try{
reader = new ApplicationProfileReader(fullScope, APPLICATION_PROFILE, ORG_GCUBE_PORTLETS_USER_DATAMINERMANAGER_SERVER_DATA_MINER_MANAGER_SERVICE_IMPL, false);
}catch(Exception e){
logger.error("Error on reading the "+APPLICATION_PROFILE+" with APPID: "+ORG_GCUBE_PORTLETS_USER_DATAMINERMANAGER_SERVER_DATA_MINER_MANAGER_SERVICE_IMPL, e);
ExceptionManager.throwInternalErrorException(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
String knimeExecutorEndPoint = reader.getApplicationProfile().getUrl();
//CHECKING THE QUERY STRING
String queryString = req.getQueryString()!=null?req.getQueryString():"";
String knimeExecutorURL = String.format("%s?%s", knimeExecutorEndPoint, queryString);
logger.info("Resolving the Knime URL with: "+knimeExecutorURL);
return Response.seeOther(new URI(knimeExecutorURL)).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 on resolving Knime URL",e);
ExceptionManager.throwInternalErrorException(req, "Error occurred when resolving Knime URL", this.getClass(), helpURI);
return null;
}
}
}