summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openslx/satellitedaemon')
-rw-r--r--src/main/java/org/openslx/satellitedaemon/App.java3
-rw-r--r--src/main/java/org/openslx/satellitedaemon/db/DbImage.java11
-rw-r--r--src/main/java/org/openslx/satellitedaemon/ftp/FtpDownloadWorker.java93
-rw-r--r--src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java62
4 files changed, 168 insertions, 1 deletions
diff --git a/src/main/java/org/openslx/satellitedaemon/App.java b/src/main/java/org/openslx/satellitedaemon/App.java
index 30382db..7210c47 100644
--- a/src/main/java/org/openslx/satellitedaemon/App.java
+++ b/src/main/java/org/openslx/satellitedaemon/App.java
@@ -12,6 +12,7 @@ import java.security.cert.CertificateException;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.openslx.imagemaster.thrift.iface.ServerAuthenticationException;
+import org.openslx.satellitedaemon.ftp.FtpDownloadWorker;
import org.openslx.satellitedaemon.ftp.FtpUploadWorker;
/**
@@ -39,5 +40,7 @@ public class App
}
Thread uploadWorker = new Thread( new FtpUploadWorker() );
uploadWorker.start();
+ Thread downloadWorker = new Thread( new FtpDownloadWorker() );
+ downloadWorker.start();
}
}
diff --git a/src/main/java/org/openslx/satellitedaemon/db/DbImage.java b/src/main/java/org/openslx/satellitedaemon/db/DbImage.java
index 0ed8efd..5169173 100644
--- a/src/main/java/org/openslx/satellitedaemon/db/DbImage.java
+++ b/src/main/java/org/openslx/satellitedaemon/db/DbImage.java
@@ -44,5 +44,16 @@ public class DbImage
" INNER JOIN m_institution institution ON (institution.institutionID = user.institution)" +
" WHERE image_syncMode = 'to_be_published'" );
}
+
+
+ public static List<DbImage> getAllMarkedForDownload()
+ {
+ return MySQL.findAll( DbImage.class, "SELECT image.GUID_imageID, image.image_name, image.imageVersion, image.image_path," +
+ " Concat(user.loginName, '@', institution.name) AS userID, image.image_filesize" +
+ " FROM m_VLData_imageInfo image" +
+ " INNER JOIN m_user user ON (image.image_owner = user.userID)" +
+ " INNER JOIN m_institution institution ON (institution.institutionID = user.institution)" +
+ " WHERE image_syncMode = 'to_be_decentralized'" );
+ }
}
diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/FtpDownloadWorker.java b/src/main/java/org/openslx/satellitedaemon/ftp/FtpDownloadWorker.java
new file mode 100644
index 0000000..bcf217f
--- /dev/null
+++ b/src/main/java/org/openslx/satellitedaemon/ftp/FtpDownloadWorker.java
@@ -0,0 +1,93 @@
+package org.openslx.satellitedaemon.ftp;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateException;
+import java.util.List;
+
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.TrustManagerFactory;
+
+import org.apache.commons.net.ftp.FTP;
+import org.apache.commons.net.ftp.FTPSClient;
+import org.apache.log4j.Logger;
+import org.openslx.imagemaster.thrift.iface.FtpCredentials;
+import org.openslx.satellitedaemon.Globals;
+import org.openslx.satellitedaemon.Globals.PropInt;
+import org.openslx.satellitedaemon.Globals.PropString;
+import org.openslx.satellitedaemon.db.DbImage;
+
+public class FtpDownloadWorker implements Runnable
+{
+ private static Logger log = Logger.getLogger( FtpUploadWorker.class );
+
+ @Override
+ public void run()
+ {
+ while ( true ) {
+ List<DbImage> imageList = DbImage.getAllMarkedForDownload();
+ log.info( "FtpDownloadWorker: imageList Contains " + imageList.size() + " items." );
+ for ( DbImage image : imageList ) {
+
+ FtpCredentials ftpc = ThriftConnection.getFtpCredentials( image.guid );
+ if ( ftpc == null ) {
+ log.error( "The FtpCredentials are null" );
+ }
+
+ try {
+ TrustManagerFactory trustManagerFactory = TrustManagerFactory
+ .getInstance( KeyManagerFactory.getDefaultAlgorithm() );
+ KeyStore keystore = KeyStore.getInstance( Globals.getPropertyString( PropString.KEYSTORETYPE ) );
+ keystore.load( new FileInputStream( new File(
+ Globals.getPropertyString( PropString.FTPSKEYSTOREPATH ) ) ),
+ Globals.getPropertyString( PropString.FTPSKEYSTOREPWD ).toCharArray() );
+ trustManagerFactory.init( keystore );
+ TrustManager trustManager = trustManagerFactory.getTrustManagers()[0];
+ FTPSClient ftpClient = new FTPSClient( "SSL", true );
+ ftpClient.setTrustManager( trustManager );
+ try {
+ ftpClient.connect( Globals.getPropertyString( PropString.FTPSERVERIP ), Globals.getPropertyInt( PropInt.FTPPORT ) );
+ if ( !ftpClient.login( ftpc.username, ftpc.password ) ) {
+ log.error( "FTP problem. Coundn't log in!" );
+ }
+ File file = new File( "/tmp/" + image.guid + ".vmdk");
+ ftpClient.setFileType( FTP.BINARY_FILE_TYPE );
+ log.info( "FtpDownloadWorker: ftpc.filename: " + ftpc.filename );
+ InputStream is = ftpClient.retrieveFileStream( ftpc.filename );
+ FileOutputStream fos = new FileOutputStream( file );
+ int b;
+ while ((b = is.read()) != -1) {
+ fos.write( b );
+ }
+ is.close();
+ fos.close();
+ ThriftConnection.finishedDownload( ftpc.username);
+
+ } catch (IOException e) {
+ log.error("FtpDownloadWorker: Error creating the FileInputStream");
+ }
+ finally {
+ ftpClient.disconnect();
+ log.info( "FtpDownloadWorker: ftpClient disconnected" );
+ }
+ } catch ( NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException e ) {
+ log.debug( "FtpDownloadWorker: Problem with Keystore ore FtpsClient creation." );
+ }
+ }
+ try {
+ Thread.sleep( 5 * 60 * 1000 );
+ } catch ( InterruptedException e ) {
+ log.error( "FtpUploadWorker: Sleep interrupted" );
+ e.printStackTrace();
+ }
+ }
+
+ }
+}
diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java b/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java
index a2c990f..251e2b3 100644
--- a/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java
+++ b/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java
@@ -84,6 +84,54 @@ public class ThriftConnection
}
return null;
}
+
+ /**
+ * The method calls getConnection() to check if the connection is ok
+ * and to get the ServerSessionData. If connection is ok, it returns ftpCredential.
+ *
+ * @return returns 'null' if there is a problem.
+ */
+ public static FtpCredentials getFtpCredentials( String uUID )
+ {
+ ImageServer.Client theClient = null;
+ try {
+ theClient = getConnection();
+ if ( theClient == null ) {
+ log.error( "Client was null!" );
+ return null;
+ }
+
+ return theClient.getImage( uUID, sSD.sessionId );
+ } catch ( TException e ) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch ( UnrecoverableKeyException e ) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch ( InvalidKeyException e ) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch ( NoSuchAlgorithmException e ) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch ( CertificateException e ) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch ( FileNotFoundException e ) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch ( KeyStoreException e ) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch ( SignatureException e ) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch ( IOException e ) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return null;
+ }
/**
* This method checks if there is already a working connection. If not,
@@ -170,7 +218,19 @@ public class ThriftConnection
public static void finishedUpload(String ftpUser, ImageData imageDescription) {
try {
- client.finshedUpload( ftpUser, imageDescription );
+ client.finishedUpload( ftpUser, imageDescription );
+ } catch ( ImageDataException e ) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch ( TException e ) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public static void finishedDownload(String ftpUser) {
+ try {
+ client.finishedDownload( ftpUser );
} catch ( ImageDataException e ) {
// TODO Auto-generated catch block
e.printStackTrace();