From f918a80aa668899912ca37cf82d3794728b05c14 Mon Sep 17 00:00:00 2001 From: Michael Petretti Date: Tue, 20 May 2014 14:22:43 +0200 Subject: Finished UploadWorker and added some comments. --- src/main/java/org/openslx/satellitedaemon/App.java | 30 ++++---- .../satellitedaemon/ftp/FtpUploadWorker.java | 10 +-- .../satellitedaemon/ftp/ThriftConnection.java | 89 +++++++++++++--------- 3 files changed, 69 insertions(+), 60 deletions(-) diff --git a/src/main/java/org/openslx/satellitedaemon/App.java b/src/main/java/org/openslx/satellitedaemon/App.java index a0de29a..7faf365 100644 --- a/src/main/java/org/openslx/satellitedaemon/App.java +++ b/src/main/java/org/openslx/satellitedaemon/App.java @@ -8,14 +8,9 @@ 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.ThriftConnection; -import org.openslx.satellitedaemon.util.Util; +import org.openslx.satellitedaemon.ftp.FtpUploadWorker; /** * Main class for uploading images from the HS-Server to the Satellite Server. @@ -26,16 +21,17 @@ public class App 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. - 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" ); - ftpIU.connectTest(); - +// // TODO: A Thread that starts the call for new credentials and the upload +// // whenever a new image was sceduled in the db. +// 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" ); +// ftpIU.connectTest(); + Thread uploadWorker = new Thread(new FtpUploadWorker()); + uploadWorker.start(); } } diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java index 4f90467..d019e53 100644 --- a/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java +++ b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java @@ -57,16 +57,16 @@ public class FtpUploadWorker implements Runnable if ( !ftpClient.login( ftpc.username, ftpc.password ) ) { throw new ConnectException( "Could not login." ); } -// System.out.println( "Connected to " + nilsIp + ":" + ftpPort -// + ". Reply code: " + ftpClient.getReplyCode() ); - + // 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(); } diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java b/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java index 810a496..054eb62 100644 --- a/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java +++ b/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java @@ -35,6 +35,57 @@ public class ThriftConnection static String nilsIp = "132.230.4.23"; static int thriftPort = 9090; + /** + * The method calls getConnection() to check if the connection is ok, + * if so, it returns ftpCredential. + * + * @return returns 'null' if there is a problem. + */ + public static FtpCredentials getFtpCredentials( ImageData imDat ) + { + try { + client = getConnection(); + Util.notNullFatal( client, "Client is null. Maybe a Network error." ); + + return client.submitImage( sSD.sessionId, imDat ); + } 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, + * newClient() establishes one. Also it does the Authentication if not done + * yet. + * + * @return returns the client if successful. + */ private static ImageServer.Client getConnection() throws ServerAuthenticationException, TException, UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, KeyStoreException, IOException, InvalidKeyException, SignatureException @@ -82,42 +133,4 @@ public class ThriftConnection return newClient; } - public static FtpCredentials getFtpCredentials(ImageData imDat) - - { - try { - client = getConnection(); - Util.notNullFatal( client, "Client is null. Maybe a Network error." ); - - return client.submitImage( sSD.sessionId, imDat ); - } 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; - } } -- cgit v1.2.3-55-g7522