Analytics-Create-Resolver has been integrated with storage-application-persistence library
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@174933 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
c1b9841b7f
commit
3320431805
6
pom.xml
6
pom.xml
|
@ -73,6 +73,12 @@
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.gcube.data-publishing</groupId>
|
||||||
|
<artifactId>storagehub-application-persistence</artifactId>
|
||||||
|
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.gcube.data.analysis</groupId>
|
<groupId>org.gcube.data.analysis</groupId>
|
||||||
<artifactId>dataminer-invocation-model</artifactId>
|
<artifactId>dataminer-invocation-model</artifactId>
|
||||||
|
|
|
@ -3,12 +3,20 @@
|
||||||
*/
|
*/
|
||||||
package org.gcube.datatransfer.resolver.services;
|
package org.gcube.datatransfer.resolver.services;
|
||||||
|
|
||||||
|
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
||||||
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
||||||
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
|
@ -22,18 +30,28 @@ import javax.ws.rs.core.Response;
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||||
|
import org.gcube.common.authorization.library.provider.ClientInfo;
|
||||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||||
|
import org.gcube.common.encryption.StringEncrypter;
|
||||||
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||||
|
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
||||||
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
import org.gcube.common.scope.impl.ScopeBean;
|
import org.gcube.common.scope.impl.ScopeBean;
|
||||||
import org.gcube.common.scope.impl.ScopeBean.Type;
|
import org.gcube.common.scope.impl.ScopeBean.Type;
|
||||||
import org.gcube.common.storagehub.client.dsl.FileContainer;
|
import org.gcube.common.storagehub.model.Metadata;
|
||||||
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
|
|
||||||
import org.gcube.data.analysis.dminvocation.ActionType;
|
import org.gcube.data.analysis.dminvocation.ActionType;
|
||||||
import org.gcube.data.analysis.dminvocation.DataMinerInvocationManager;
|
import org.gcube.data.analysis.dminvocation.DataMinerInvocationManager;
|
||||||
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
|
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
|
||||||
import org.gcube.datatransfer.resolver.requesthandler.TokenSetter;
|
import org.gcube.datatransfer.resolver.requesthandler.TokenSetter;
|
||||||
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
|
import org.gcube.datatransfer.resolver.services.error.ExceptionManager;
|
||||||
|
import org.gcube.datatransfer.resolver.util.ScopeUtil;
|
||||||
import org.gcube.datatransfer.resolver.util.Util;
|
import org.gcube.datatransfer.resolver.util.Util;
|
||||||
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||||
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||||
|
import org.gcube.storagehub.ApplicationMode;
|
||||||
|
import org.gcube.storagehub.StorageHubManagement;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
|
@ -47,6 +65,14 @@ import org.xml.sax.SAXException;
|
||||||
@Path("/analytics")
|
@Path("/analytics")
|
||||||
public class AnalyticsCreateResolver {
|
public class AnalyticsCreateResolver {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
protected static final String GCUBE_TOKEN = "gcube-token";
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final String ANALYTICS_RESOLVER_SERVICE_ENDPOINT_NAME = "Analytics-Resolver";
|
||||||
private static final String DATAMINER_INVOCATION_MODEL = "dim";
|
private static final String DATAMINER_INVOCATION_MODEL = "dim";
|
||||||
private static Logger logger = LoggerFactory.getLogger(AnalyticsCreateResolver.class);
|
private static Logger logger = LoggerFactory.getLogger(AnalyticsCreateResolver.class);
|
||||||
private static String helpURI = "https://gcube.wiki.gcube-system.org/gcube/URI_Resolver#Analytics_Resolver";
|
private static String helpURI = "https://gcube.wiki.gcube-system.org/gcube/URI_Resolver#Analytics_Resolver";
|
||||||
|
@ -58,6 +84,7 @@ public class AnalyticsCreateResolver {
|
||||||
* @param req the req
|
* @param req the req
|
||||||
* @param body the body
|
* @param body the body
|
||||||
* @return the response
|
* @return the response
|
||||||
|
* @throws WebApplicationException the web application exception
|
||||||
*/
|
*/
|
||||||
@POST
|
@POST
|
||||||
@Path("/create")
|
@Path("/create")
|
||||||
|
@ -123,13 +150,29 @@ public class AnalyticsCreateResolver {
|
||||||
ByteArrayOutputStream xmlByteArray = DataMinerInvocationManager.getInstance().marshaling(jsonRequest, org.gcube.data.analysis.dminvocation.MediaType.ApplicationXML, true);
|
ByteArrayOutputStream xmlByteArray = DataMinerInvocationManager.getInstance().marshaling(jsonRequest, org.gcube.data.analysis.dminvocation.MediaType.ApplicationXML, true);
|
||||||
String uniqueName = createDMInvocationFileName(jsonRequest.getOperatorId());
|
String uniqueName = createDMInvocationFileName(jsonRequest.getOperatorId());
|
||||||
tempInvocationFile = createTempFile(uniqueName, ".xml", xmlByteArray.toByteArray());
|
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());
|
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());
|
String infra = ScopeUtil.getInfrastructureNameFromScope(contextToken);
|
||||||
URL thePublicLink = fileContainer.getPublicLink();
|
String theAppToken = readApplicationTokenFromSE(req, infra);
|
||||||
|
ApplicationMode applicationMode = new ApplicationMode(theAppToken);
|
||||||
|
applicationMode.start();
|
||||||
|
|
||||||
|
StorageHubManagement storageHubManagement = new StorageHubManagement();
|
||||||
|
Metadata metadata = new Metadata();
|
||||||
|
Map<String, String> theMap = new HashMap<String, String>();
|
||||||
|
|
||||||
|
AuthorizationEntry entry = authorizationService().get(contextToken);
|
||||||
|
//retrieve the info of the token owner
|
||||||
|
ClientInfo clientInfo = entry.getClientInfo();
|
||||||
|
String owner = clientInfo.getId(); //IS THIS THE USERNAME?
|
||||||
|
theMap.put("owner", owner);
|
||||||
|
logger.info("Saving dataminer-invocation file for the user: "+owner);
|
||||||
|
URL thePublicLink = storageHubManagement.persistFile(new FileInputStream(tempInvocationFile), tempInvocationFile.getName(), "application/xml", metadata);
|
||||||
|
logger.info("Saved dataminer-invocation file at: "+thePublicLink);
|
||||||
|
|
||||||
|
// 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;
|
publicLinkToDMInvFile = thePublicLink!=null?thePublicLink.toString():null;
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
|
@ -175,6 +218,78 @@ public class AnalyticsCreateResolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the Application Token from Service Endpoint {@link AnalyticsCreateResolver#ANALYTICS_RESOLVER_SERVICE_ENDPOINT_NAME}
|
||||||
|
*
|
||||||
|
* @param req the req
|
||||||
|
* @param scope the scope
|
||||||
|
* @return the string
|
||||||
|
*/
|
||||||
|
private static String readApplicationTokenFromSE(HttpServletRequest req, String scope){
|
||||||
|
|
||||||
|
String origalScope = null;
|
||||||
|
String gCubeAppToken = null;
|
||||||
|
try{
|
||||||
|
origalScope = ScopeProvider.instance.get();
|
||||||
|
ScopeProvider.instance.set(scope);
|
||||||
|
logger.info("Searching SE "+ANALYTICS_RESOLVER_SERVICE_ENDPOINT_NAME+" configurations in the scope: "+ScopeProvider.instance.get());
|
||||||
|
|
||||||
|
SimpleQuery query = queryFor(ServiceEndpoint.class);
|
||||||
|
query.addCondition("$resource/Profile/Name/text() eq '"+ ANALYTICS_RESOLVER_SERVICE_ENDPOINT_NAME +"'");
|
||||||
|
query.addCondition("$resource/Profile/Category/text() eq 'Service'");
|
||||||
|
|
||||||
|
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
|
||||||
|
List<ServiceEndpoint> toReturn = client.submit(query);
|
||||||
|
|
||||||
|
logger.info("The query returned "+toReturn.size()+ " ServiceEndpoint/s");
|
||||||
|
|
||||||
|
if(toReturn.size()==0){
|
||||||
|
String errorMessage = "No "+ANALYTICS_RESOLVER_SERVICE_ENDPOINT_NAME+" registered in the scope: "+ScopeProvider.instance.get();
|
||||||
|
logger.error(errorMessage);
|
||||||
|
throw ExceptionManager.internalErrorException(req, errorMessage, AnalyticsCreateResolver.class, helpURI);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ServiceEndpoint se = toReturn.get(0);
|
||||||
|
Collection<AccessPoint> theAccessPoints = se.profile().accessPoints().asCollection();
|
||||||
|
for (AccessPoint accessPoint : theAccessPoints) {
|
||||||
|
Collection<Property> properties = accessPoint.properties().asCollection();
|
||||||
|
for (Property property : properties) {
|
||||||
|
if(property.name().equalsIgnoreCase(GCUBE_TOKEN)){
|
||||||
|
logger.info("gcube-token as property was found, returning it");
|
||||||
|
gCubeAppToken = property.value();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(gCubeAppToken!=null)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(gCubeAppToken!=null){
|
||||||
|
String decryptedPassword = StringEncrypter.getEncrypter().decrypt(gCubeAppToken);
|
||||||
|
logger.info("Returning decrypted Application Token registered into "+ANALYTICS_RESOLVER_SERVICE_ENDPOINT_NAME +": "+decryptedPassword.substring(0,decryptedPassword.length()/2)+"....");
|
||||||
|
return gCubeAppToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
String errorMessage = "No "+GCUBE_TOKEN+" as Property saved in the "+ANALYTICS_RESOLVER_SERVICE_ENDPOINT_NAME+" SE registered in the scope: "+ScopeProvider.instance.get();
|
||||||
|
logger.error(errorMessage);
|
||||||
|
throw ExceptionManager.internalErrorException(req, errorMessage, AnalyticsCreateResolver.class, helpURI);
|
||||||
|
|
||||||
|
|
||||||
|
}catch(Exception e){
|
||||||
|
String errorMessage = "Error occurred on reading the "+ANALYTICS_RESOLVER_SERVICE_ENDPOINT_NAME+" SE registered in the scope: "+ScopeProvider.instance.get();
|
||||||
|
logger.error(errorMessage, e);
|
||||||
|
throw ExceptionManager.internalErrorException(req, errorMessage, AnalyticsCreateResolver.class, helpURI);
|
||||||
|
|
||||||
|
}finally{
|
||||||
|
if(origalScope!=null)
|
||||||
|
ScopeProvider.instance.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates the temp file.
|
* Creates the temp file.
|
||||||
|
@ -212,4 +327,11 @@ public class AnalyticsCreateResolver {
|
||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
System.out.println(readApplicationTokenFromSE(null, "/gcube"));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue