diff options
author | Michael Petretti | 2014-07-10 16:48:49 +0200 |
---|---|---|
committer | Michael Petretti | 2014-07-10 16:48:49 +0200 |
commit | c4b51e6fd2b7e0fc3531cb43064f64e9d0d0b74e (patch) | |
tree | f1ca31217db6ef1521f07f9c132e5a6c4844b09f /src/main/java/org/openslx/satellitedaemon/ftp | |
parent | Merge branch 'master' of git.openslx.org:bwlp/satellite-daemon (diff) | |
download | satellite-daemon-c4b51e6fd2b7e0fc3531cb43064f64e9d0d0b74e.tar.gz satellite-daemon-c4b51e6fd2b7e0fc3531cb43064f64e9d0d0b74e.tar.xz satellite-daemon-c4b51e6fd2b7e0fc3531cb43064f64e9d0d0b74e.zip |
Fixed a lot of TODOs and upload works.
Diffstat (limited to 'src/main/java/org/openslx/satellitedaemon/ftp')
3 files changed, 44 insertions, 175 deletions
diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/FtpDownloadWorker.java b/src/main/java/org/openslx/satellitedaemon/ftp/FtpDownloadWorker.java index d315735..73e0b03 100644 --- a/src/main/java/org/openslx/satellitedaemon/ftp/FtpDownloadWorker.java +++ b/src/main/java/org/openslx/satellitedaemon/ftp/FtpDownloadWorker.java @@ -1,19 +1,8 @@ package org.openslx.satellitedaemon.ftp; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.security.KeyManagementException; -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 javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.TrustManagerFactory; - import org.apache.log4j.Logger; import org.openslx.filetransfer.Downloader; import org.openslx.imagemaster.thrift.iface.DownloadInfos; @@ -22,7 +11,6 @@ import org.openslx.satellitedaemon.Globals.PropInt; import org.openslx.satellitedaemon.Globals.PropString; import org.openslx.satellitedaemon.db.DbImage; -// TODO: Pretty much the same todos as in the uploader apply here public class FtpDownloadWorker implements Runnable { @@ -36,86 +24,21 @@ public class FtpDownloadWorker implements Runnable log.info( "FtpDownloadWorker: imageList Contains " + imageList.size() + " items." ); for ( DbImage image : imageList ) { - DownloadInfos downInfos = ThriftConnection.getDownloadInfos( image ); + List<Integer> range = new ArrayList<Integer>(); + for (long i = 0; i < (image.fileSize / Globals.getPropertyInt( PropInt.BLOCKSIZE )); i++) { + range.add( (int) i ); + } + DownloadInfos downInfos = ThriftConnection.getDownloadInfos( image, range ); if ( downInfos == null ) { log.error( "The DownloadInfos returned by ThriftConnection class are null" ); } - char[] passphrase = Globals.getPropertyString( PropString.FTPSKEYSTOREPWD ).toCharArray(); - KeyStore keystore; - try { - keystore = KeyStore.getInstance( "JKS" ); - keystore.load( new FileInputStream( Globals.getPropertyString( PropString.FTPSKEYSTOREPATH ) ), passphrase ); - TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() ); - tmf.init( keystore ); - SSLContext context = SSLContext.getInstance( "SSLv3" ); - TrustManager[] trustManagers = tmf.getTrustManagers(); - context.init( null, trustManagers, null ); - - Downloader d = new Downloader( Globals.getPropertyString( PropString.FTPSERVERIP ), Globals.getPropertyInt( PropInt.FTPPORT ), context ); + Downloader d = new Downloader( Globals.getPropertyString( PropString.FTPSERVERIP ), downInfos.port, Globals.getMasterServerSslContext() ); d.sendToken( downInfos.token ); + d.setOutputFilename( "/home/michael/Downloads/tescht.whatever" ); while ( d.readMetaData() ) d.readBinary(); - } 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 ( IOException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch ( KeyStoreException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch ( KeyManagementException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - // 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 ); diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java index aa750f2..4445a2e 100644 --- a/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java +++ b/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java @@ -1,25 +1,14 @@ package org.openslx.satellitedaemon.ftp; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.security.KeyManagementException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.cert.CertificateException; import java.util.List; import java.util.UUID; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.TrustManagerFactory; - import org.apache.log4j.Logger; import org.openslx.filetransfer.Uploader; import org.openslx.imagemaster.thrift.iface.ImageData; import org.openslx.imagemaster.thrift.iface.UploadInfos; import org.openslx.satellitedaemon.Globals; +import org.openslx.satellitedaemon.Globals.PropInt; import org.openslx.satellitedaemon.Globals.PropString; import org.openslx.satellitedaemon.db.DbImage; @@ -47,20 +36,6 @@ public class FtpUploadWorker implements Runnable ImageData imDat = new ImageData( UUID.randomUUID().toString(), image.rid, image.name, System.currentTimeMillis(), System.currentTimeMillis(), image.creator, "anyThing", true, false, "best", "theVeryBest", image.fileSize ); - char[] passphrase = Globals.getPropertyString( PropString.FTPSKEYSTOREPWD ).toCharArray(); - KeyStore keystore; - try { - - // All the necessary KeyStore handling for the "context"-item. - // TODO: Don't do this again and again for every image, but once before the loop (or in the constructor) - // Or better yet in a central spot, as the downloadworker needs the context too. Maybe something like Globals.getMasterServerSslContext() - keystore = KeyStore.getInstance( "JKS" ); - keystore.load( new FileInputStream( Globals.getPropertyString( PropString.FTPSKEYSTOREPATH ) ), passphrase ); - TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() ); - tmf.init( keystore ); - SSLContext context = SSLContext.getInstance( "SSLv3" ); - TrustManager[] trustManagers = tmf.getTrustManagers(); - context.init( null, trustManagers, null ); // uploadInfo and ThriftAuthentication String crcPath = image.path.concat( ".crc" ); @@ -76,61 +51,28 @@ public class FtpUploadWorker implements Runnable log.info( "Got upInfos. Trying to create Uploader with token: " + upInfos.token ); // creating the uploader with the "context"-item. - Uploader u = new Uploader( Globals.getPropertyString( PropString.FTPSERVERIP ), upInfos.port, context ); + Uploader u = new Uploader( Globals.getPropertyString( PropString.FTPSERVERIP ), upInfos.port, Globals.getMasterServerSslContext() ); u.sendToken( upInfos.token ); // continue sending Blocks until getMissingBlocks is empty. - - - while ( !upInfos.getMissingBlocks().isEmpty() ) { // Send all Blocks from upInfos.getMissingBlocks() in ranges. List<Integer> blocks = upInfos.getMissingBlocks(); int start = 0; for ( int i = 0; i < blocks.size() - 1; i++ ) { - if ( blocks.get( i ) != ( blocks.get( i + 1 ) - 1 ) ) { // TODO: !? u.sendRange expects byte values, upInfos contains block indexes (16MiB) - u.sendRange( start, i ); + if ( blocks.get( i ) != ( blocks.get( i + 1 ) - 1 ) ) { + u.sendRange( start * Globals.getPropertyInt( PropInt.BLOCKSIZE ), i * Globals.getPropertyInt( PropInt.BLOCKSIZE )); u.sendFile( image.path ); start = i + 1; } if ( i == blocks.size() - 2 ) { // TODO: != - u.sendRange( start, blocks.size() - 1 ); + u.sendRange( start * Globals.getPropertyInt( PropInt.BLOCKSIZE ), (blocks.size() - 1) * Globals.getPropertyInt( PropInt.BLOCKSIZE ) ); u.sendFile( image.path ); } } + upInfos = ThriftConnection.getUploadInfos( imDat ); } - // FIXME: Should be inside loop, otherwise you'll only ever upload the first 20 blocks - upInfos = ThriftConnection.getUploadInfos( imDat ); - - - // TODO: Use more local try/catch-blocks, not a big one around everything, so you know what exactly caused the exception - // and can handle it properly. There are roughly 3 types of error/exception you can encounter: - // 1) Expected exception: Something you expect to go wrong, so you handle it in the catch block and let the code flow continue - // 2) Minor exception: Something went wrong and you can't continue trying to upload that image. Try to clean things up that - // you already did (like opening a file, opening a network connection) and then continue with the next image - // 3) Major messup: Something went wrong that will prevent the whole application from continuing to work. Try to finish or cancel - // any operation going on in your application and then exit (and don't forget to log meaningful information about the error) - // Basically all those empty catch blocks below should be moved up and filled with logic. - } 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 ( IOException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch ( KeyStoreException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch ( KeyManagementException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - + u.close(); } try { Thread.sleep( 5 * 60 * 1000 ); diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java b/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java index cacd6c8..5d67635 100644 --- a/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java +++ b/src/main/java/org/openslx/satellitedaemon/ftp/ThriftConnection.java @@ -9,6 +9,7 @@ import java.security.NoSuchAlgorithmException; import java.security.SignatureException; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; +import java.util.List; import org.apache.log4j.Logger; import org.apache.thrift.TException; @@ -31,7 +32,7 @@ import org.openslx.satellitedaemon.util.EncryptWithServerIdPublicKey; // TODO: Handle all the auto-generated catch blocks in a meaningful way -/** +/*********************************************************************************************** * Handles the authentication with the Satellite Server and sends the FtpCredentials, which * are necessary for the upload of the image. */ @@ -42,14 +43,14 @@ public class ThriftConnection private static Logger log = Logger.getLogger( ThriftConnection.class ); private static CRCFile crc = null; - /** - * Method for getting UploadeInfos + /*********************************************************************************************** + * Method for getting UploadeInfos * - * !! on the first Call !! + * !! on the first Call !! * * when the CRCsum need to be transfered. - * The method calls getConnection() to check if the connection is ok - * and to get the ServerSessionData. If connection is ok, it calls + * The method calls getConnection() to check if the connection is ok + * and to get the ServerSessionData. If connection is ok, it calls * submitImage with CRCsum in List<Integer>. * * @return returns 'null' if there is a problem. @@ -66,7 +67,7 @@ public class ThriftConnection // .submitImage needs the List<Integer> from CRCFile.getCRCs() only // on the first time called. null afterwards. log.info( "First call of submitImage following..." ); - crc = new CRCFile(filename); + crc = new CRCFile( filename ); log.info( "Made CRCFile from " + filename ); return theClient.submitImage( sSD.sessionId, imDat, crc.getCrcSums() ); } catch ( TException e ) { @@ -99,12 +100,13 @@ public class ThriftConnection } return null; } - - /** + + /*********************************************************************************************** * Method for getting UploadeInfos when CRCsum was already transfered on first call. - * The method calls getConnection() to check if the connection is ok - * and to get the ServerSessionData. If connection is ok, it calls + * The method calls getConnection() to check if the connection is ok + * and to get the ServerSessionData. If connection is ok, it calls * submitImage with sSD.sessionId, imDat and !!null!! + * * @return returns 'null' if there is a problem. */ public static UploadInfos getUploadInfos( ImageData imDat ) @@ -118,7 +120,7 @@ public class ThriftConnection } // .submitImage needs the List<Integer> from CRCFile.getCRCs() only // on the first time called. null afterwards. - return theClient.submitImage( sSD.sessionId, imDat, crc.getCrcSums()); + return theClient.submitImage( sSD.sessionId, imDat, crc.getCrcSums() ); } catch ( TException e ) { // TODO Auto-generated catch block e.printStackTrace(); @@ -149,14 +151,14 @@ public class ThriftConnection } return null; } - - /** - * The method calls getConnection() to check if the connection is ok - * and to get the DownloadeInfos. If connection is ok, it returns ftpCredential. + + /*********************************************************************************************** + * Method for getting DonwloadInfos. Calls getConnection if client was null. + * You need to spezifie all Blocks you want to have in an List. * * @return returns 'null' if there is a problem. */ - public static DownloadInfos getDownloadInfos( DbImage imDat ) + public static DownloadInfos getDownloadInfos( DbImage imDat, List<Integer> range ) { ImageServer.Client theClient = null; try { @@ -166,7 +168,7 @@ public class ThriftConnection return null; } - return theClient.getImage( imDat.guid, sSD.sessionId ); + return theClient.getImage( imDat.guid, sSD.sessionId, range ); } catch ( TException e ) { // TODO Auto-generated catch block e.printStackTrace(); @@ -198,9 +200,7 @@ public class ThriftConnection 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. @@ -209,7 +209,7 @@ public class ThriftConnection */ private static ImageServer.Client getConnection() throws UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException, - FileNotFoundException, KeyStoreException, IOException, InvalidKeyException, SignatureException + FileNotFoundException, KeyStoreException, IOException, InvalidKeyException, SignatureException { ImageServer.Client theClient = null; boolean isAuthenticated = false; @@ -241,14 +241,14 @@ public class ThriftConnection return null; } try { - toEncrypt = theClient.startServerAuthentication( Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME) ); + toEncrypt = theClient.startServerAuthentication( Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME ) ); log.info( "The random String we want to encrypt: " + toEncrypt ); EncryptWithServerIdPublicKey rse = new EncryptWithServerIdPublicKey( Globals.getPropertyString( PropString.RNDSTRINGENCRYPTALIAS ), Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPASSWORD ), Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPATH ) ); byte[] byteArray = rse.encryptString( toEncrypt ); sSD = theClient.serverAuthenticate( - Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME), ByteBuffer.wrap( byteArray ) ); + Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME ), ByteBuffer.wrap( byteArray ) ); } catch ( ServerAuthenticationException e ) { log.error( "ThriftConnection: ServerAuthenticationException: Server Authetication was not sucessful." ); e.printStackTrace(); @@ -265,11 +265,15 @@ public class ThriftConnection return theClient; } + /*********************************************************************************************** + * + * @return + */ private static ImageServer.Client newClient() { ImageServer.Client newClient = null; try { - TTransport transport; + TTransport transport; transport = new TSocket( Globals.getPropertyString( PropString.FTPSERVERIP ), Globals.getPropertyInt( PropInt.THRIFTPORT ) ); transport.open(); TProtocol protocol = new TBinaryProtocol( transport ); |