214 lines
8.4 KiB
Java
214 lines
8.4 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.datatransfer.resolver.services;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.net.URL;
|
|
import java.nio.file.Files;
|
|
|
|
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.WebApplicationException;
|
|
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.apache.commons.io.IOUtils;
|
|
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.common.storagehub.client.dsl.FileContainer;
|
|
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
|
|
import org.gcube.data.analysis.dminvocation.ActionType;
|
|
import org.gcube.data.analysis.dminvocation.DataMinerInvocationManager;
|
|
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
|
|
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;
|
|
import org.xml.sax.SAXException;
|
|
|
|
/**
|
|
* The Class AnalyticsCreateResolver.
|
|
*
|
|
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
|
* Dec 12, 2018
|
|
*/
|
|
@Path("/analytics")
|
|
public class AnalyticsCreateResolver {
|
|
|
|
private static final String DATAMINER_INVOCATION_MODEL = "dim";
|
|
private static Logger logger = LoggerFactory.getLogger(AnalyticsCreateResolver.class);
|
|
private static String helpURI = "https://gcube.wiki.gcube-system.org/gcube/URI_Resolver#Analytics_Resolver";
|
|
|
|
|
|
/**
|
|
* Creates the analytics url.
|
|
*
|
|
* @param req the req
|
|
* @param body the body
|
|
* @return the response
|
|
*/
|
|
@POST
|
|
@Path("/create")
|
|
@Consumes(MediaType.TEXT_PLAIN)
|
|
@Produces(MediaType.TEXT_PLAIN)
|
|
public Response createAnalyticsURL(@Context HttpServletRequest req, String body) throws WebApplicationException{
|
|
logger.info(this.getClass().getSimpleName()+" POST starts...");
|
|
|
|
try{
|
|
logger.info("body is: "+body);
|
|
|
|
DataMinerInvocation jsonRequest = null;
|
|
try {
|
|
|
|
jsonRequest = DataMinerInvocationManager.getInstance().unmarshaling(IOUtils.toInputStream(body), org.gcube.data.analysis.dminvocation.MediaType.ApplicationJSON, true);
|
|
}
|
|
catch (IOException | JAXBException | SAXException e1) {
|
|
logger.error("The body is not a valid DataMinerInvocation JSON request",e1);
|
|
throw ExceptionManager.badRequestException(req, "Bad 'dataminer-invocation' JSON request: \n"+e1.getCause().getMessage(), this.getClass(), helpURI);
|
|
}
|
|
|
|
logger.debug("The body contains the request: "+jsonRequest.toString());
|
|
|
|
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)+"...");
|
|
throw ExceptionManager.unauthorizedException(req, "You are not authorized. You must pass a token of VRE", this.getClass(), helpURI);
|
|
}
|
|
|
|
String operatorID = jsonRequest.getOperatorId();
|
|
|
|
if(scope==null || scope.isEmpty()){
|
|
logger.error("The parameter 'scope' not found or empty in the JSON object");
|
|
throw ExceptionManager.badRequestException(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");
|
|
throw ExceptionManager.badRequestException(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();
|
|
|
|
String analyticsGetResolverURL = String.format("%s/%s", Util.getServerURL(req), "analytics/get");
|
|
//Creating DM invocation file
|
|
if(jsonRequest.getActionType()==null)
|
|
jsonRequest.setActionType(ActionType.RUN);
|
|
|
|
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());
|
|
|
|
//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);
|
|
throw ExceptionManager.badRequestException(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");
|
|
throw ExceptionManager.badRequestException(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", 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");
|
|
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) {
|
|
|
|
if(!(e instanceof WebApplicationException)){
|
|
//UNEXPECTED EXCEPTION managing it as WebApplicationException
|
|
String error = "Error occurred on creating the Analytics for the request "+body+". Please, contact the support!"+"\nCause: "+e.getCause().getMessage();
|
|
throw ExceptionManager.internalErrorException(req, error, this.getClass(), helpURI);
|
|
}
|
|
//ALREADY MANAGED AS WebApplicationException
|
|
logger.error("Exception:", e);
|
|
throw (WebApplicationException) e;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 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 Temp File: "+file.getAbsolutePath());
|
|
return file;
|
|
}
|
|
|
|
|
|
/**
|
|
* Creates the dm invocation file name.
|
|
*
|
|
* @param operatorId the operator id
|
|
* @return the string
|
|
*/
|
|
private static String createDMInvocationFileName(String operatorId){
|
|
String fileName = "dim";
|
|
int index = operatorId.lastIndexOf(".");
|
|
if(index>0 && index<operatorId.length()){
|
|
fileName+="-"+operatorId.substring(index+1,operatorId.length());
|
|
}
|
|
fileName+="-"+System.currentTimeMillis();
|
|
return fileName;
|
|
}
|
|
|
|
}
|