summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/ftp/FtpDownloadWorker.java
blob: d31573564efdb85c953caccb4bf5d2c9e76b9c5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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 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;
import org.openslx.satellitedaemon.Globals;
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
{
	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 ) {

				DownloadInfos downInfos = ThriftConnection.getDownloadInfos( image );
				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 );
					d.sendToken( downInfos.token );
					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 );
			} catch ( InterruptedException e ) {
				Thread.currentThread().interrupt();
				return;
			}
		}

	}
}