summaryrefslogblamecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java
blob: 73ec7051f969b6b303d8ce5a23a8270fc517a946 (plain) (tree)


















                                               
                               

                                                           


                                                      



                                                
                                                                              






                                                                    
                                                                                       







                                                                                                                                      
                                                     

                                                                                   

                                     
                                                                                                              

                                                                                                                
                                                                                                                                         
                                                                                     

                                                                                                                                




                                                                                                              
                                                                                                                                                                    
                                                                                                         
                                                                                                     
                                                 
                                                                                                                                                                                                                                                 
                                                                                                
                                                                                                  
 
                                                                                                                                                                                                                                                            
                                                                                                           
 















                                                                                                                                 
 
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.apache.log4j.Logger;
import org.openslx.imagemaster.thrift.iface.FtpCredentials;
import org.openslx.imagemaster.thrift.iface.ImageData;
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 FtpUploadWorker implements Runnable
{
	private static Logger log = Logger.getLogger( FtpUploadWorker.class );

	@Override
	public void run()
	{
		List<DbImage> imageList = new ArrayList<DbImage>();
		while ( true ) {
			imageList = DbImage.getAllMarkedForUpload();
			// TODO: Maybe use iterator ( for (DbImage image : imageList) )
			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 );
				if ( ftpc == null ) {
					log.error( "The FtpCredentials are null" );
				}

				try {
					// ToDo: Add everything with the keyStores to config/global.properties
					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!" );
						}
						// TODO: Where do I find the path to the db-image? <-- in DbImage. It's relative, base path should come from config/???.properties (global static config class, see masterserver's Globals class)
						File file = new File( imageList.get( 0 ).path );
						FileInputStream fis = new FileInputStream( file );

						// TODO: What is the path where it should be stored? <-- Should be set in the FtpCredentials you get via Thrift, but doesn't really matter, Server can decide to ignore the name and pick own (talk to Nils)
						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();
			}
		}

	}
}