2018-12-13 11:09:08 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
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;
|
2018-12-14 12:34:18 +01:00
|
|
|
import javax.ws.rs.WebApplicationException;
|
2018-12-13 11:09:08 +01:00
|
|
|
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;
|
2019-03-15 12:23:04 +01:00
|
|
|
import org.gcube.datatransfer.resolver.requesthandler.RequestHandler;
|
2018-12-13 11:09:08 +01:00
|
|
|
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
|
|
|
|
*/
|
2019-02-19 15:18:19 +01:00
|
|
|
@Path("knime")
|
2018-12-13 11:09:08 +01:00
|
|
|
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";
|
|
|
|
|
2019-10-04 17:03:24 +02:00
|
|
|
|
2018-12-13 11:09:08 +01:00
|
|
|
/**
|
2019-10-04 17:03:24 +02:00
|
|
|
* Creates the knime URL.
|
2018-12-13 11:09:08 +01:00
|
|
|
*
|
|
|
|
* @param req the req
|
|
|
|
* @return the response
|
2019-10-04 17:03:24 +02:00
|
|
|
* @throws WebApplicationException the web application exception
|
2018-12-13 11:09:08 +01:00
|
|
|
*/
|
|
|
|
@POST
|
|
|
|
@Path("/create")
|
|
|
|
@Consumes(MediaType.TEXT_PLAIN)
|
|
|
|
@Produces(MediaType.TEXT_PLAIN)
|
2018-12-14 12:34:18 +01:00
|
|
|
public Response createKnimeURL(@Context HttpServletRequest req) throws WebApplicationException{
|
|
|
|
|
2018-12-13 11:09:08 +01:00
|
|
|
logger.info(this.getClass().getSimpleName()+" POST starts...");
|
|
|
|
|
2018-12-14 12:34:18 +01:00
|
|
|
try{
|
|
|
|
String contextToken = SecurityTokenProvider.instance.get();
|
|
|
|
String scope = ScopeProvider.instance.get();
|
|
|
|
// logger.info("SecurityTokenProvider contextToken: "+contextToken);
|
|
|
|
logger.info("ScopeProvider has scope: "+scope);
|
2018-12-13 11:09:08 +01:00
|
|
|
|
2019-03-15 12:23:04 +01:00
|
|
|
String appToken = req.getServletContext().getInitParameter(RequestHandler.ROOT_APP_TOKEN);
|
2018-12-13 11:09:08 +01:00
|
|
|
|
2018-12-14 12:34:18 +01:00
|
|
|
if(contextToken.compareTo(appToken)==0){
|
|
|
|
logger.error("Token not passed, SecurityTokenProvider contains the root app token: "+appToken.substring(0,10)+"...");
|
|
|
|
throw ExceptionManager.unauthorizedException(req, "You are not authorized. You must pass a token of VRE", this.getClass(), helpURI);
|
|
|
|
}
|
2018-12-13 11:09:08 +01:00
|
|
|
|
2018-12-14 12:34:18 +01:00
|
|
|
ScopeBean scopeBean = new ScopeBean(scope);
|
2018-12-13 11:09:08 +01:00
|
|
|
|
2018-12-14 12:34:18 +01:00
|
|
|
if(scopeBean.is(Type.VRE)){
|
2018-12-13 11:09:08 +01:00
|
|
|
|
2018-12-14 12:34:18 +01:00
|
|
|
String vreName = scopeBean.name();
|
|
|
|
String knimeGetResolverURL = String.format("%s/%s/%s", Util.getServerURL(req), "knime/get", vreName);
|
|
|
|
String queryString =req.getQueryString()==null?"":req.getQueryString();
|
2018-12-13 11:09:08 +01:00
|
|
|
|
2018-12-14 12:34:18 +01:00
|
|
|
if(req.getQueryString()!=null)
|
|
|
|
knimeGetResolverURL+="?"+queryString;
|
2018-12-13 11:09:08 +01:00
|
|
|
|
2018-12-14 12:34:18 +01:00
|
|
|
logger.info("Returning Knime Resolver URL: "+knimeGetResolverURL);
|
|
|
|
return Response.ok(knimeGetResolverURL).header("Location", knimeGetResolverURL).build();
|
2018-12-13 11:09:08 +01:00
|
|
|
|
2018-12-14 12:34:18 +01:00
|
|
|
}else{
|
|
|
|
logger.error("The input scope "+scope+" is not a VRE");
|
|
|
|
throw ExceptionManager.badRequestException(req, "Working in the "+scope+" scope that is not a VRE. Use a token of VRE", this.getClass(), helpURI);
|
|
|
|
}
|
|
|
|
}catch (Exception e) {
|
2018-12-13 11:09:08 +01:00
|
|
|
|
2018-12-14 12:34:18 +01:00
|
|
|
if(!(e instanceof WebApplicationException)){
|
|
|
|
//UNEXPECTED EXCEPTION managing it as WebApplicationException
|
2018-12-14 14:26:37 +01:00
|
|
|
String error = "Sorry, an error occurred on creating Knime URL. Please, contact the support!";
|
|
|
|
if(e.getCause()!=null)
|
|
|
|
error+="\n\nCaused: "+e.getCause().getMessage();
|
2018-12-14 12:34:18 +01:00
|
|
|
throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI);
|
|
|
|
}
|
|
|
|
//ALREADY MANAGED AS WebApplicationException
|
|
|
|
logger.error("Exception:", e);
|
|
|
|
throw (WebApplicationException) e;
|
2018-12-13 11:09:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|