diff options
-rw-r--r-- | src/main/java/org/openslx/satellitedaemon/App.java | 14 | ||||
-rw-r--r-- | src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java | 87 | ||||
-rw-r--r-- | src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java (renamed from src/main/java/org/openslx/satellitedaemon/ftp/FtpConnection.java) | 41 |
3 files changed, 118 insertions, 24 deletions
diff --git a/src/main/java/org/openslx/satellitedaemon/App.java b/src/main/java/org/openslx/satellitedaemon/App.java index 073e2e8..a0de29a 100644 --- a/src/main/java/org/openslx/satellitedaemon/App.java +++ b/src/main/java/org/openslx/satellitedaemon/App.java @@ -2,14 +2,19 @@ package org.openslx.satellitedaemon; import java.io.FileNotFoundException; import java.io.IOException; +import java.security.InvalidKeyException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; +import java.security.SignatureException; +import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; +import java.util.UUID; import org.openslx.imagemaster.thrift.iface.FtpCredentials; +import org.openslx.imagemaster.thrift.iface.ImageData; import org.openslx.imagemaster.thrift.iface.ServerAuthenticationException; import org.openslx.satellitedaemon.ftp.FtpImageUploader; -import org.openslx.satellitedaemon.ftp.FtpConnection; +import org.openslx.satellitedaemon.ftp.ThriftConnection; import org.openslx.satellitedaemon.util.Util; /** @@ -18,12 +23,15 @@ import org.openslx.satellitedaemon.util.Util; */ public class App { - public static void main( String[] args ) throws NoSuchAlgorithmException, KeyStoreException, CertificateException, FileNotFoundException, IOException, ServerAuthenticationException + public static void main( String[] args ) throws NoSuchAlgorithmException, KeyStoreException, CertificateException, FileNotFoundException, IOException, ServerAuthenticationException, UnrecoverableKeyException, InvalidKeyException, SignatureException { // TODO: A Thread that starts the call for new credentials and the upload // whenever a new image was sceduled in the db. - FtpCredentials ftpc = FtpConnection.getFtpCredentials(); + ImageData imDat = new ImageData( UUID.randomUUID().toString(), 113, + "TestImage", System.currentTimeMillis(), System.currentTimeMillis(), "me", "anyThing", + true, false, "best", "theVeryBest", 1024 ); + FtpCredentials ftpc = ThriftConnection.getFtpCredentials(imDat); Util.notNullFatal( ftpc, "ftpc was null" ); FtpImageUploader ftpIU = new FtpImageUploader( ftpc ); Util.notNullFatal( ftpIU, "ftpIU was null" ); diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java new file mode 100644 index 0000000..4f90467 --- /dev/null +++ b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java @@ -0,0 +1,87 @@ +package org.openslx.satellitedaemon.ftp; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.ConnectException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.CertificateException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; + +import org.apache.commons.net.ftp.FTPSClient; +import org.openslx.imagemaster.thrift.iface.FtpCredentials; +import org.openslx.imagemaster.thrift.iface.ImageData; +import org.openslx.satellitedaemon.db.DbImage; + +public class FtpUploadWorker implements Runnable +{ + static String nilsIp = "132.230.4.23"; + static int ftpPort = 2221; + + @Override + public void run() + { + List<DbImage> imageList = new ArrayList<DbImage>(); + while ( true ) { + imageList = DbImage.getAllMarkedForUpload(); + while ( !imageList.isEmpty() ) { + // TODO: imDat should be filled by the first entry of imageList. + // imageList.get(0); + ImageData imDat = new ImageData( UUID.randomUUID().toString(), 113, + "TestImage", System.currentTimeMillis(), System.currentTimeMillis(), "me", "anyThing", + true, false, "best", "theVeryBest", 1024 ); + + FtpCredentials ftpc = ThriftConnection.getFtpCredentials( imDat ); + + try { + TrustManagerFactory trustManagerFactory = TrustManagerFactory + .getInstance( KeyManagerFactory.getDefaultAlgorithm() ); + KeyStore keystore = KeyStore.getInstance( "JKS" ); + keystore.load( new FileInputStream( new File( + "/home/michael/satellite-daemon/config/ftpsid.jks" ) ), + "password".toCharArray() ); + trustManagerFactory.init( keystore ); + TrustManager trustManager = trustManagerFactory.getTrustManagers()[0]; + FTPSClient ftpClient = new FTPSClient( "SSL", true ); + ftpClient.setTrustManager( trustManager ); + try { + ftpClient.connect( nilsIp, ftpPort ); + if ( !ftpClient.login( ftpc.username, ftpc.password ) ) { + throw new ConnectException( "Could not login." ); + } +// System.out.println( "Connected to " + nilsIp + ":" + ftpPort +// + ". Reply code: " + ftpClient.getReplyCode() ); + + // TODO: Where do I find the path to the db-image? + File file = new File( "/path/to/File" ); + FileInputStream fis = new FileInputStream( file ); + + // TODO: What is the path where it should be stored? + ftpClient.storeFile( "/path/where/theImage/belongs", fis ); + + } finally { + ftpClient.disconnect(); + } + } catch ( NoSuchAlgorithmException | KeyStoreException | CertificateException | IOException e ) { + } + imageList.remove( 0 ); + } + try { + Thread.sleep( 5 * 60 * 1000 ); + } catch ( InterruptedException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + } + +} diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/FtpConnection.java b/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java index fa0b040..810a496 100644 --- a/src/main/java/org/openslx/satellitedaemon/ftp/FtpConnection.java +++ b/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java @@ -9,7 +9,6 @@ import java.security.NoSuchAlgorithmException; import java.security.SignatureException; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; -import java.util.UUID; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; @@ -28,14 +27,17 @@ import org.openslx.satellitedaemon.util.Util; * Handles the authentication with the Satellite Server and sends the FtpCredentials, which * are necessary for the upload of the image. */ -public class FtpConnection +public class ThriftConnection { private static ImageServer.Client client = null; + private static ServerSessionData sSD = null; // TODO: All of the Strings and int's should not fall from sky. static String nilsIp = "132.230.4.23"; static int thriftPort = 9090; private static ImageServer.Client getConnection() + throws ServerAuthenticationException, TException, UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, KeyStoreException, + IOException, InvalidKeyException, SignatureException { ImageServer.Client theClient = null; if ( client == null ) { @@ -48,12 +50,18 @@ public class FtpConnection isAuthenticated = theClient.ping(); } catch ( TException x ) { theClient = newClient(); - if (theClient==null){ + if ( theClient == null ) { return null; } } if ( !isAuthenticated ) { -// TODO: a server authentication. + String toEncrypt = client.startServerAuthentication( "uni-freiburg.de" ); + // System.out.println( toEncrypt ); + EncryptWithServerIdPublicKey rse = new EncryptWithServerIdPublicKey( "serverid", "password", + "/home/michael/satellite-daemon/config/serverid.jks" ); + byte[] byteArray = rse.encryptString( toEncrypt ); + sSD = client.serverAuthenticate( + "uni-freiburg.de", ByteBuffer.wrap( byteArray ) ); } return theClient; } @@ -74,33 +82,24 @@ public class FtpConnection return newClient; } - public static FtpCredentials getFtpCredentials() throws ServerAuthenticationException + public static FtpCredentials getFtpCredentials(ImageData imDat) + { try { client = getConnection(); Util.notNullFatal( client, "Client is null. Maybe a Network error." ); - String toEncrypt = client.startServerAuthentication( "uni-freiburg.de" ); - // System.out.println( toEncrypt ); - EncryptWithServerIdPublicKey rse = new EncryptWithServerIdPublicKey( "serverid", "password", - "/home/michael/satellite-daemon/config/serverid.jks" ); - byte[] byteArray = rse.encryptString( toEncrypt ); - ServerSessionData sSD = client.serverAuthenticate( - "uni-freiburg.de", ByteBuffer.wrap( byteArray ) ); - ImageData imDat = new ImageData( UUID.randomUUID().toString(), 113, - "TestImage", System.currentTimeMillis(), System.currentTimeMillis(), "me", "anyThing", - true, false, "best", "theVeryBest", 1024 ); return client.submitImage( sSD.sessionId, imDat ); - } catch ( InvalidKeyException e ) { + } catch ( TException e ) { // TODO Auto-generated catch block e.printStackTrace(); - } catch ( NoSuchAlgorithmException e ) { + } catch ( UnrecoverableKeyException e ) { // TODO Auto-generated catch block e.printStackTrace(); - } catch ( SignatureException e ) { + } catch ( InvalidKeyException e ) { // TODO Auto-generated catch block e.printStackTrace(); - } catch ( UnrecoverableKeyException e ) { + } catch ( NoSuchAlgorithmException e ) { // TODO Auto-generated catch block e.printStackTrace(); } catch ( CertificateException e ) { @@ -112,10 +111,10 @@ public class FtpConnection } catch ( KeyStoreException e ) { // TODO Auto-generated catch block e.printStackTrace(); - } catch ( IOException e ) { + } catch ( SignatureException e ) { // TODO Auto-generated catch block e.printStackTrace(); - } catch ( TException e ) { + } catch ( IOException e ) { // TODO Auto-generated catch block e.printStackTrace(); } |