2016-04-01 17:31:06 +02:00
package org.gcube.dataanalysis.executor.nodes.transducers ;
import java.io.File ;
import java.util.List ;
import java.util.UUID ;
import org.apache.commons.io.FileUtils ;
import org.gcube.dataanalysis.ecoengine.datatypes.PrimitiveType ;
import org.gcube.dataanalysis.ecoengine.datatypes.ServiceType ;
import org.gcube.dataanalysis.ecoengine.datatypes.enumtypes.PrimitiveTypes ;
import org.gcube.dataanalysis.ecoengine.datatypes.enumtypes.ServiceParameters ;
2016-09-27 16:27:28 +02:00
import org.gcube.dataanalysis.ecoengine.interfaces.StandardLocalInfraAlgorithm ;
2016-04-01 17:31:06 +02:00
import org.gcube.dataanalysis.executor.util.DataTransferer ;
import org.gcube.dataanalysis.executor.util.InfraRetrieval ;
2017-05-24 18:04:56 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2016-04-01 17:31:06 +02:00
2016-09-27 16:27:28 +02:00
public class WebApplicationPublisher extends StandardLocalInfraAlgorithm {
2017-06-09 12:01:03 +02:00
//private static String MainPageParam = "MainPage";
private static final String DATA_TRANSFER_PERSISTENCE_ID = " data-transfer-service " ;
2016-04-01 17:31:06 +02:00
private static String FileParam = " ZipFile " ;
private String transferServiceAddress = " " ;
private int transferServicePort = 0 ;
2017-06-09 12:01:03 +02:00
2017-05-24 18:04:56 +02:00
private Logger LOGGER = LoggerFactory . getLogger ( WebApplicationPublisher . class ) ;
2017-06-09 12:01:03 +02:00
2016-04-01 17:31:06 +02:00
@Override
public String getDescription ( ) {
return " This algorithm publishes a zip file containing a Web site, based on html and javascript in the e-Infrastructure. It generates a public URL to the application that can be shared. " ;
}
@Override
public void init ( ) throws Exception {
}
@Override
protected void process ( ) throws Exception {
String uuid = " webpub_ " + UUID . randomUUID ( ) ;
File folder = new File ( config . getConfigPath ( ) , uuid ) ;
try
{
status = 10 ;
2017-06-09 12:01:03 +02:00
String scope = config . getGcubeScope ( ) ;
String username = config . getParam ( " ServiceUserName " ) ;
String fileAbsolutePath = config . getParam ( FileParam ) ;
// String mainPage = config.getParam(MainPageParam);
LOGGER . debug ( " scope: {} " , scope ) ;
LOGGER . debug ( " username: {} " , username ) ;
LOGGER . debug ( " fileAbsolutePath: {} " , fileAbsolutePath ) ;
// LOGGER.debug("layerTitle: "+mainPage);
if ( scope = = null | | username = = null )
throw new Exception ( " Service parameters are not set - please contact the Administrators " ) ;
if ( fileAbsolutePath = = null | | fileAbsolutePath . trim ( ) . length ( ) = = 0 )
throw new Exception ( " No file has been provided to the process " ) ;
getTransferInfo ( config . getGcubeScope ( ) ) ;
DataTransferer . transferFileToService ( scope , username , transferServiceAddress , transferServicePort , fileAbsolutePath , " " , DATA_TRANSFER_PERSISTENCE_ID , true , uuid ) ;
String producedPage = " http:// " + transferServiceAddress + " / " + uuid + " / " ;
LOGGER . debug ( " Entry point of the page " + producedPage ) ;
//get URL
addOutputString ( " Generated Website - Main URL " , producedPage ) ;
} catch ( Exception e ) {
2017-05-24 18:04:56 +02:00
LOGGER . error ( " an error occurred! " , e ) ;
2016-04-01 17:31:06 +02:00
throw e ;
} finally {
2017-06-09 12:01:03 +02:00
//clean everything
2016-04-01 17:31:06 +02:00
if ( folder . exists ( ) ) {
2017-05-24 18:04:56 +02:00
LOGGER . debug ( " Cleaning folder " + folder ) ;
2016-04-01 17:31:06 +02:00
FileUtils . cleanDirectory ( folder ) ;
FileUtils . deleteDirectory ( folder ) ;
}
2017-06-09 12:01:03 +02:00
}
2017-05-24 18:04:56 +02:00
LOGGER . debug ( " Process finished " ) ;
2016-04-01 17:31:06 +02:00
status = 100 ;
}
2017-06-09 12:01:03 +02:00
/ * private Map < String , String > getFilesPaths ( File [ ] toexplore , String prefix ) {
2016-04-01 17:31:06 +02:00
Map < String , String > toreturn = new HashMap < String , String > ( ) ;
for ( File toex : toexplore ) {
if ( toex . isDirectory ( ) ) {
toreturn . putAll ( getFilesPaths ( toex . listFiles ( ) , prefix + toex . getName ( ) + " / " ) ) ;
}
else
toreturn . put ( toex . getAbsolutePath ( ) , prefix + toex . getName ( ) ) ;
}
2017-06-09 12:01:03 +02:00
2016-04-01 17:31:06 +02:00
return toreturn ;
2017-06-09 12:01:03 +02:00
} * /
2016-04-01 17:31:06 +02:00
@Override
protected void setInputParameters ( ) {
2017-06-09 12:01:03 +02:00
inputs . add ( new PrimitiveType ( File . class . getName ( ) , null , PrimitiveTypes . FILE , FileParam , " Zip file containing the Web site " ) ) ;
inputs . add ( new ServiceType ( ServiceParameters . USERNAME , " ServiceUserName " , " The final user Name " ) ) ;
//inputs.add(MainPageParam, "Main page of the website", "index.html");
}
2016-04-01 17:31:06 +02:00
@Override
public void shutdown ( ) {
2017-05-24 18:04:56 +02:00
LOGGER . debug ( " WebApplicationPublisher - shutdown " ) ;
2016-04-01 17:31:06 +02:00
}
2017-06-09 12:01:03 +02:00
2016-04-01 17:31:06 +02:00
public void getTransferInfo ( String scope ) throws Exception {
2017-06-09 12:01:03 +02:00
2016-04-01 17:31:06 +02:00
List < String > apacheAddress = InfraRetrieval . retrieveServiceAddress ( " Application " , " Apache Server " , scope , " Transect " ) ;
2017-06-09 12:01:03 +02:00
2016-04-01 17:31:06 +02:00
if ( apacheAddress . size ( ) = = 0 )
throw new Exception ( " Apache Server resource is not available in scope " + scope ) ;
2017-06-09 12:01:03 +02:00
2016-04-01 17:31:06 +02:00
String apacheServiceAddress = apacheAddress . get ( 0 ) ;
apacheServiceAddress = apacheServiceAddress . substring ( apacheServiceAddress . indexOf ( " http:// " ) + 7 ) ;
2017-06-09 12:01:03 +02:00
2017-05-24 18:04:56 +02:00
LOGGER . debug ( " Found {} services " , apacheAddress . size ( ) ) ;
LOGGER . debug ( " Apache address: {} " , apacheServiceAddress ) ;
2017-06-09 12:01:03 +02:00
List < String > dataTransferAddress = InfraRetrieval . retrieveService ( " data-transfer-service " , scope ) ;
2016-04-01 17:31:06 +02:00
if ( dataTransferAddress . size ( ) = = 0 )
throw new Exception ( " Data Transfer services are not available in scope " + scope ) ;
2017-06-09 12:01:03 +02:00
2017-05-24 18:04:56 +02:00
LOGGER . debug ( " Found {} transfer services " , dataTransferAddress . size ( ) ) ;
2017-06-09 12:01:03 +02:00
2016-04-01 17:31:06 +02:00
int apacheDTPort = 9090 ;
boolean found = false ;
for ( String datatransferservice : dataTransferAddress ) {
2017-05-24 18:04:56 +02:00
LOGGER . debug ( " Transfer service found " ) ;
2016-04-01 17:31:06 +02:00
datatransferservice = datatransferservice . substring ( datatransferservice . indexOf ( " http:// " ) + 7 ) ;
String servicehost = datatransferservice . substring ( 0 , datatransferservice . indexOf ( " : " ) ) ;
String serviceport = datatransferservice . substring ( datatransferservice . indexOf ( " : " ) + 1 , datatransferservice . indexOf ( " / " ) ) ;
2017-05-24 18:04:56 +02:00
LOGGER . debug ( " Transfer service: {} , {} " , servicehost , serviceport ) ;
2016-04-01 17:31:06 +02:00
if ( apacheServiceAddress . equals ( servicehost ) ) {
apacheDTPort = Integer . parseInt ( serviceport ) ;
found = true ;
break ;
}
}
2017-06-09 12:01:03 +02:00
2016-04-01 17:31:06 +02:00
if ( ! found )
throw new Exception ( " Apache data transfer has not been found in the same scope of the catalog: " + scope ) ;
else
2017-05-24 18:04:56 +02:00
LOGGER . debug ( " Transfer service found at address {} : {} " , apacheServiceAddress , apacheDTPort ) ;
2017-06-09 12:01:03 +02:00
2016-04-01 17:31:06 +02:00
transferServiceAddress = apacheServiceAddress ;
transferServicePort = apacheDTPort ;
}
2017-06-09 12:01:03 +02:00
2016-04-01 17:31:06 +02:00
}