summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/satellitedaemon/ftp/FtpUploadWorker.java
blob: e4e4b7c5974e04ecc0fd8e6defa4a4f94cfa55da (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
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();
			// 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 );
				// TODO: Handle ftpc == null

				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() ); // TODO: define relative path, eg. "config/masterserver_cert.jks"
					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." ); // TODO: Should not throw exception, otherwise we'd exit the run() method
						}
						//						System.out.println( "Connected to " + nilsIp + ":" + ftpPort
						//								+ ". Reply code: " + ftpClient.getReplyCode() );

						// 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( "/path/to/File" );
						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();
			}
		}

	}

}