From 3265140340c2131dc3ffe18b0f9483d391053c51 Mon Sep 17 00:00:00 2001 From: Michael Petretti Date: Mon, 12 May 2014 15:24:04 +0200 Subject: Improvement of FtpConnection Class. --- .../openslx/satellitedaemon/ftp/FtpConnection.java | 124 +++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/main/java/org/openslx/satellitedaemon/ftp/FtpConnection.java (limited to 'src/main/java/org/openslx/satellitedaemon/ftp/FtpConnection.java') diff --git a/src/main/java/org/openslx/satellitedaemon/ftp/FtpConnection.java b/src/main/java/org/openslx/satellitedaemon/ftp/FtpConnection.java new file mode 100644 index 0000000..fa0b040 --- /dev/null +++ b/src/main/java/org/openslx/satellitedaemon/ftp/FtpConnection.java @@ -0,0 +1,124 @@ +package org.openslx.satellitedaemon.ftp; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.security.InvalidKeyException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.SignatureException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; +import java.util.UUID; + +import org.apache.thrift.TException; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.transport.TSocket; +import org.apache.thrift.transport.TTransport; +import org.openslx.imagemaster.thrift.iface.FtpCredentials; +import org.openslx.imagemaster.thrift.iface.ImageData; +import org.openslx.imagemaster.thrift.iface.ImageServer; +import org.openslx.imagemaster.thrift.iface.ServerAuthenticationException; +import org.openslx.imagemaster.thrift.iface.ServerSessionData; +import org.openslx.satellitedaemon.util.EncryptWithServerIdPublicKey; +import org.openslx.satellitedaemon.util.Util; + +/** + * Handles the authentication with the Satellite Server and sends the FtpCredentials, which + * are necessary for the upload of the image. + */ +public class FtpConnection +{ + private static ImageServer.Client client = null; + // TODO: All of the Strings and int's should not fall from sky. + static String nilsIp = "132.230.4.23"; + static int thriftPort = 9090; + + private static ImageServer.Client getConnection() + { + ImageServer.Client theClient = null; + if ( client == null ) { + theClient = newClient(); + } else { + theClient = client; + } + boolean isAuthenticated = false; + try { + isAuthenticated = theClient.ping(); + } catch ( TException x ) { + theClient = newClient(); + if (theClient==null){ + return null; + } + } + if ( !isAuthenticated ) { +// TODO: a server authentication. + } + return theClient; + } + + private static ImageServer.Client newClient() + { + ImageServer.Client newClient = null; + try { + TTransport transport; + transport = new TSocket( nilsIp, thriftPort ); // Nils IP + transport.open(); + TProtocol protocol = new TBinaryProtocol( transport ); + newClient = new ImageServer.Client( protocol ); + } catch ( TException x ) { + x.printStackTrace(); + return null; + } + return newClient; + } + + public static FtpCredentials getFtpCredentials() throws ServerAuthenticationException + { + try { + client = getConnection(); + Util.notNullFatal( client, "Client is null. Maybe a Network error." ); + String toEncrypt = client.startServerAuthentication( "uni-freiburg.de" ); + // System.out.println( toEncrypt ); + EncryptWithServerIdPublicKey rse = new EncryptWithServerIdPublicKey( "serverid", "password", + "/home/michael/satellite-daemon/config/serverid.jks" ); + byte[] byteArray = rse.encryptString( toEncrypt ); + ServerSessionData sSD = client.serverAuthenticate( + "uni-freiburg.de", ByteBuffer.wrap( byteArray ) ); + ImageData imDat = new ImageData( UUID.randomUUID().toString(), 113, + "TestImage", System.currentTimeMillis(), System.currentTimeMillis(), "me", "anyThing", + true, false, "best", "theVeryBest", 1024 ); + + return client.submitImage( sSD.sessionId, imDat ); + } catch ( InvalidKeyException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( NoSuchAlgorithmException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( SignatureException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( UnrecoverableKeyException 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 ( IOException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch ( TException e ) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } +} -- cgit v1.2.3-55-g7522