summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNils Schwabe2014-08-18 15:34:18 +0200
committerNils Schwabe2014-08-18 15:34:18 +0200
commit859c07eba2b19a11340e473340e408c0a24380de (patch)
treec4eb7806cd4dbe3fec767490d2b03cc5b3e14dbb
parentChange TSocket to TNonBlockingServerTransport (diff)
downloadsatellite-daemon-859c07eba2b19a11340e473340e408c0a24380de.tar.gz
satellite-daemon-859c07eba2b19a11340e473340e408c0a24380de.tar.xz
satellite-daemon-859c07eba2b19a11340e473340e408c0a24380de.zip
Change implementation of the configuration in Global.java
Add sample config file
-rw-r--r--.gitignore2
-rw-r--r--src/main/java/org/openslx/satellitedaemon/App.java13
-rw-r--r--src/main/java/org/openslx/satellitedaemon/Globals.java248
-rw-r--r--src/main/java/org/openslx/satellitedaemon/filetransfer/FileDownloadWorker.java58
-rw-r--r--src/main/java/org/openslx/satellitedaemon/filetransfer/FileUploadWorker.java93
-rw-r--r--src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java359
6 files changed, 360 insertions, 413 deletions
diff --git a/.gitignore b/.gitignore
index e507bc7..0587ac8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,4 @@
/.project
/.classpath
/.settings/
-
+/config/global.config
diff --git a/src/main/java/org/openslx/satellitedaemon/App.java b/src/main/java/org/openslx/satellitedaemon/App.java
index 1805d5a..cdbab6c 100644
--- a/src/main/java/org/openslx/satellitedaemon/App.java
+++ b/src/main/java/org/openslx/satellitedaemon/App.java
@@ -1,7 +1,5 @@
package org.openslx.satellitedaemon;
-import java.io.IOException;
-
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.openslx.satellitedaemon.filetransfer.FileDownloadWorker;
@@ -19,16 +17,7 @@ public class App
public static void main( String[] args )
{
BasicConfigurator.configure();
- try {
- Globals.loadProperties();
- if ( !Globals.propertiesValid() ) {
- log.error( "Config file contains errors." );
- System.exit( 1 );
- }
- } catch ( IOException e ) {
- log.error( "Could not load config file. Quitting." );
- System.exit( 1 );
- }
+ Globals.init();
if (!Globals.masterServerSslContextInit()){
log.error( "Problem with initializing the SSLContext" );
System.exit( 1 );
diff --git a/src/main/java/org/openslx/satellitedaemon/Globals.java b/src/main/java/org/openslx/satellitedaemon/Globals.java
index a62ee9e..d33be1d 100644
--- a/src/main/java/org/openslx/satellitedaemon/Globals.java
+++ b/src/main/java/org/openslx/satellitedaemon/Globals.java
@@ -17,156 +17,77 @@ import javax.net.ssl.TrustManagerFactory;
import org.apache.log4j.Logger;
-public class Globals
-{
- private static Logger log = Logger.getLogger( Globals.class );
+public class Globals {
+ private static Logger log = Logger.getLogger(Globals.class);
private static final Properties properties = new Properties();
- private static boolean loadedProperties = false;
private static SSLContext context = null;
- /***********************************************************************************************/
- /**
- * If there are more ints or Strings which should be added to config/global.properties,
- * add to suiting enum, add a 'case' to getPropertyInt/String() and add checks to
- * propertiesValid().
- *
- * As an Example, if you want the value of the FILETRANSFERSERVERIP you have to call
- * Globals.getPropertyString( PropString.FILETRANSFERSERVERIP ) which returns a string.
- */
- public static enum PropInt
- {
- FILETRANSFERPORT, THRIFTPORT, BLOCKSIZE // More int's? Add them separated with ","
+ public static final int BLOCKSIZE = 16 * 1024 * 1024; // 16 MB blocksize
+
+ public static void init() {
}
- public static enum PropString
- { // More strings's? Add them separated with ","
- FILETRANSFERSERVERIP, KEYSTORETYPE, FILETRANSFERSKEYSTOREPATH, FILETRANSFERSKEYSTOREPWD, THRIFTORGANIZATIONNAME,
- RNDSTRINGENCRYPTALIAS, RNDSTRINGENCRYPTPASSWORD, RNDSTRINGENCRYPTPATH
+ // * Properties *//
+
+ // Strings //
+ public static String getMasterserverHost() {
+ return properties.getProperty("MASTERSERVER_HOST");
}
- /***********************************************************************************************/
- /**
- *
- * @return
- * @throws IOException
- */
- public static boolean loadProperties() throws IOException
- {
- if ( loadedProperties )
- return false;
+ public static String getKeystoreType() {
+ return properties.getProperty("KEYSTORE_TYPE");
+ }
- // Load all entries of the config file into properties
- BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "config/global.properties" ) );
- properties.load( stream );
- stream.close();
+ public static String getFiletransferKeystorePath() {
+ return properties.getProperty("FILETRANSFER_KEYSTORE_PATH");
+ }
- return true;
+ public static String getFiletransferKeystorePassword() {
+ return properties.getProperty("FILETRANSFER_KEYSTORE_PASSWORD");
}
- /***********************************************************************************************/
- /**
- *
- * @param props
- * @return
- */
- public static int getPropertyInt( Globals.PropInt props )
- {
- String result = null;
-
- switch ( props ) {
- case FILETRANSFERPORT:
- result = properties.getProperty( "filetransfer_port" );
- break;
- case BLOCKSIZE:
- result = properties.getProperty( "blockSize" );
- break;
- case THRIFTPORT:
- result = properties.getProperty( "ThriftPort" );
- break;
- default:
- result = "0";
- break;
- }
- if ( result == null )
- return 0;
+ public static String getOrganizationName() {
+ return properties.getProperty("ORGANIZATION_NAME");
+ }
- return Integer.valueOf( result );
+ public static String getThriftKeystoreAlias() {
+ return properties.getProperty("THRIFT_KEYSTORE_ALIAS");
}
- /***********************************************************************************************/
- /**
- *
- * @param props
- * @return
- */
- public static String getPropertyString( Globals.PropString props )
- {
- String result = null;
-
- switch ( props ) {
- case FILETRANSFERSERVERIP:
- result = properties.getProperty( "filetransfer_server_ip" );
- break;
- case KEYSTORETYPE:
- result = properties.getProperty( "keyStore_type" );
- break;
- case FILETRANSFERSKEYSTOREPATH:
- result = properties.getProperty( "path_to_filetransfer_keyStore" );
- break;
- case FILETRANSFERSKEYSTOREPWD:
- result = properties.getProperty( "filetransfer_keyStore_password" );
- break;
- case THRIFTORGANIZATIONNAME:
- result = properties.getProperty( "organization_name" );
- break;
- case RNDSTRINGENCRYPTALIAS:
- result = properties.getProperty( "RndStringEncrypt_alias" );
- break;
- case RNDSTRINGENCRYPTPASSWORD:
- result = properties.getProperty( "RndStringEncrypt_password" );
- break;
- case RNDSTRINGENCRYPTPATH:
- result = properties.getProperty( "RndStringEncrypt_path" );
- break;
- default:
- result = "";
- break;
- }
- return result;
+ public static String getThriftKeystorePassword() {
+ return properties.getProperty("THRIFT_KEYSTORE_PASSWORD");
+ }
+
+ public static String getThriftKeystorePath() {
+ return properties.getProperty("THRIFT_KEYSTORE_PATH");
+ }
+
+ // Integers //
+ public static int getFiletransferPort() {
+ return tryToParseInt(properties.getProperty("FILETRANSFER_PORT"));
+ }
+
+ public static int getThriftPort() {
+ return tryToParseInt(properties.getProperty("THRIFT_PORT"));
}
- // TODO: add real checks
- /***********************************************************************************************/
/**
- *
- * @return
+ * Load properties
*/
- public static boolean propertiesValid()
- {
- if ( Globals.getPropertyInt( PropInt.FILETRANSFERPORT ) == 0
- || Globals.getPropertyInt( PropInt.BLOCKSIZE ) == 0
- || Globals.getPropertyInt( PropInt.THRIFTPORT ) == 0
- || Globals.getPropertyString( PropString.FILETRANSFERSERVERIP ).isEmpty()
- || Globals.getPropertyString( PropString.FILETRANSFERSERVERIP ) == null
- || Globals.getPropertyString( PropString.KEYSTORETYPE ).isEmpty()
- || Globals.getPropertyString( PropString.KEYSTORETYPE ) == null
- || Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPATH ).isEmpty()
- || Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPATH ) == null
- || Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPWD ).isEmpty()
- || Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPWD ) == null
- || Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME ).isEmpty()
- || Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME ) == null
- || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTALIAS ).isEmpty()
- || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTALIAS ) == null
- || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPASSWORD ).isEmpty()
- || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPASSWORD ) == null
- || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPATH ).isEmpty()
- || Globals.getPropertyString( PropString.RNDSTRINGENCRYPTPATH ) == null ) {
- return false;
- }
- else {
- return true;
+ static {
+ try {
+ // Load all entries of the config file into properties
+ BufferedInputStream stream = new BufferedInputStream(
+ new FileInputStream("config/global.properties"));
+ properties.load(stream);
+ stream.close();
+ } catch (IOException e) {
+ log.error("Could not load properties. Exiting.");
+ System.exit(2);
}
+
+ notNullOrEmptyFatal(getMasterserverHost(), "Masterserver Host must not be empty!");
+ // TODO: check properties
}
/***********************************************************************************************/
@@ -174,47 +95,66 @@ public class Globals
*
* @return
*/
- public static boolean masterServerSslContextInit()
- {
- char[] passphrase = Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPWD ).toCharArray();
+ public static boolean masterServerSslContextInit() {
+ char[] passphrase = getFiletransferKeystorePassword().toCharArray();
KeyStore keystore;
try {
- keystore = KeyStore.getInstance( "JKS" );
- keystore.load( new FileInputStream( Globals.getPropertyString( PropString.FILETRANSFERSKEYSTOREPATH ) ), passphrase );
- TrustManagerFactory tmf = TrustManagerFactory.getInstance( TrustManagerFactory.getDefaultAlgorithm() );
- tmf.init( keystore );
- context = SSLContext.getInstance( "SSLv3" );
+ keystore = KeyStore.getInstance("JKS");
+ keystore.load(new FileInputStream(getFiletransferKeystorePath()),
+ passphrase);
+ TrustManagerFactory tmf = TrustManagerFactory
+ .getInstance(TrustManagerFactory.getDefaultAlgorithm());
+ tmf.init(keystore);
+ context = SSLContext.getInstance("SSLv3");
TrustManager[] trustManagers = tmf.getTrustManagers();
- context.init( null, trustManagers, null );
- } catch ( KeyStoreException e ) {
+ context.init(null, trustManagers, null);
+ } catch (KeyStoreException e) {
log.error("KeyStoreException");
return false;
- } catch ( NoSuchAlgorithmException e ) {
+ } catch (NoSuchAlgorithmException e) {
log.error("NoSuchAlgorithmException");
return false;
- } catch ( CertificateException e ) {
+ } catch (CertificateException e) {
log.error("CertificateException");
return false;
- } catch ( FileNotFoundException e ) {
- log.error("FileNotFoundException");
+ } catch (FileNotFoundException e) {
+ log.error("Could not find the keystore for the filetransfer. Path was '" + getFiletransferKeystorePath() + "'");
return false;
- } catch ( IOException e ) {
+ } catch (IOException e) {
log.error("IOException");
return false;
- } catch ( KeyManagementException e ) {
+ } catch (KeyManagementException e) {
log.error("KeyManagementException");
return false;
}
return true;
}
-
-
- /***********************************************************************************************/
+
+ public static SSLContext getMasterServerSslContext() {
+ return Globals.context;
+ }
+
/**
+ * Tries to parse an int. Returns 0 on error.
*
- * @return
+ * @param s
+ * The strig to parse
+ * @return The parsed int or 0 on error
*/
- public static SSLContext getMasterServerSslContext(){
- return Globals.context;
+ public static int tryToParseInt(String s) {
+ try {
+ return Integer.parseInt(s);
+ } catch (NumberFormatException e) {
+ return 0;
+ }
+ }
+
+ public static void notNullOrEmptyFatal(String something, String message) {
+ if (something == null || something.isEmpty()) {
+ if (message != null)
+ log.fatal("[NOTNULL] " + message);
+ log.warn(Thread.currentThread().getStackTrace().toString());
+ System.exit(2);
+ }
}
-}
+} \ No newline at end of file
diff --git a/src/main/java/org/openslx/satellitedaemon/filetransfer/FileDownloadWorker.java b/src/main/java/org/openslx/satellitedaemon/filetransfer/FileDownloadWorker.java
index a6431f5..f15cbb5 100644
--- a/src/main/java/org/openslx/satellitedaemon/filetransfer/FileDownloadWorker.java
+++ b/src/main/java/org/openslx/satellitedaemon/filetransfer/FileDownloadWorker.java
@@ -8,49 +8,47 @@ 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;
-
-public class FileDownloadWorker implements Runnable
-{
- private static Logger log = Logger.getLogger( FileUploadWorker.class );
+public class FileDownloadWorker implements Runnable {
+ private static Logger log = Logger.getLogger(FileUploadWorker.class);
@Override
- public void run()
- {
- while ( true ) {
+ public void run() {
+ while (true) {
List<DbImage> imageList = DbImage.getAllMarkedForDownload();
- log.info( "imageList Contains " + imageList.size() + " items." );
- for ( DbImage image : imageList ) {
+ log.info("imageList Contains " + imageList.size() + " items.");
+ for (DbImage image : imageList) {
List<Integer> range = new ArrayList<Integer>();
- for (long i = 0; i < (image.fileSize / Globals.getPropertyInt( PropInt.BLOCKSIZE )); i++) {
- range.add( (int) i );
+ for (long i = 0; i < (image.fileSize / Globals.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" );
+ DownloadInfos downInfos = ThriftConnection.getDownloadInfos(
+ image, range);
+ if (downInfos == null) {
+ log.error("The DownloadInfos returned by ThriftConnection class are null");
continue;
}
- Downloader d;
- try {
- d = new Downloader( Globals.getPropertyString( PropString.FILETRANSFERSERVERIP ), downInfos.port, Globals.getMasterServerSslContext() );
- } catch ( IOException e ) {
- log.warn( "Could not connect for download: " + e.toString() );
- e.printStackTrace();
- continue;
- }
- d.sendToken( downInfos.token );
- d.setOutputFilename( "/home/michael/Downloads/tescht.whatever" );
- while ( d.readMetaData() ) // TODO: Request range...
- d.receiveBinary();
+ Downloader d;
+ try {
+ d = new Downloader(Globals.getMasterserverHost(),
+ downInfos.port, Globals.getMasterServerSslContext());
+ } catch (IOException e) {
+ log.warn("Could not connect for download: " + e.toString());
+ e.printStackTrace();
+ continue;
+ }
+ d.sendToken(downInfos.token);
+ d.setOutputFilename("/home/michael/Downloads/tescht.whatever");
+ while (d.readMetaData())
+ // TODO: Request range...
+ d.receiveBinary();
}
try {
- Thread.sleep( 5 * 60 * 1000 );
- } catch ( InterruptedException e ) {
+ Thread.sleep(5 * 60 * 1000);
+ } catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
diff --git a/src/main/java/org/openslx/satellitedaemon/filetransfer/FileUploadWorker.java b/src/main/java/org/openslx/satellitedaemon/filetransfer/FileUploadWorker.java
index 48e467b..3515085 100644
--- a/src/main/java/org/openslx/satellitedaemon/filetransfer/FileUploadWorker.java
+++ b/src/main/java/org/openslx/satellitedaemon/filetransfer/FileUploadWorker.java
@@ -9,77 +9,84 @@ 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;
-public class FileUploadWorker implements Runnable
-{
- private static Logger log = Logger.getLogger( FileUploadWorker.class );
+public class FileUploadWorker implements Runnable {
+ private static Logger log = Logger.getLogger(FileUploadWorker.class);
@Override
- public void run()
- {
- while ( true ) {
- // This List contains all Images in the Database that should be uploaded.
+ public void run() {
+ while (true) {
+ // This List contains all Images in the Database that should be
+ // uploaded.
List<DbImage> imageList = DbImage.getAllMarkedForUpload();
- log.info( "imageList Contains " + imageList.size() + " items." );
+ log.info("imageList Contains " + imageList.size() + " items.");
// Upload one Image after the other.
- for ( DbImage image : imageList ) {
-// TODO: still some fields for ImageData, which i can't fill with info from DbImage.
- ImageData imDat = new ImageData( image.guid, image.rid,
- image.name, System.currentTimeMillis(), System.currentTimeMillis(), image.creator, "anyThing",
- true, false, "best", "theVeryBest", image.fileSize );
+ for (DbImage image : imageList) {
+ // TODO: still some fields for ImageData, which i can't fill
+ // with info from DbImage.
+ ImageData imDat = new ImageData(image.guid, image.rid,
+ image.name, System.currentTimeMillis(),
+ System.currentTimeMillis(), image.creator, "anyThing",
+ true, false, "best", "theVeryBest", image.fileSize);
-// ImageData imDat = new ImageData( UUID.randomUUID().toString(), image.rid,
-// image.name, System.currentTimeMillis(), System.currentTimeMillis(), image.creator, "anyThing",
-// true, false, "best", "theVeryBest", image.fileSize );
+ // ImageData imDat = new ImageData(
+ // UUID.randomUUID().toString(), image.rid,
+ // image.name, System.currentTimeMillis(),
+ // System.currentTimeMillis(), image.creator, "anyThing",
+ // true, false, "best", "theVeryBest", image.fileSize );
// uploadInfo and ThriftAuthentication
- String crcPath = image.path.concat( ".crc" );
- UploadInfos upInfos = ThriftConnection.getUploadInfos( imDat, crcPath );
- if ( upInfos == null ) {
- log.error( "The UploadInfos returned by ThriftConnection Class are null" );
+ String crcPath = image.path.concat(".crc");
+ UploadInfos upInfos = ThriftConnection.getUploadInfos(imDat,
+ crcPath);
+ if (upInfos == null) {
+ log.error("The UploadInfos returned by ThriftConnection Class are null");
continue;
}
- log.info( "Got upInfos. Trying to create Uploader with token: " + upInfos.token );
+ log.info("Got upInfos. Trying to create Uploader with token: "
+ + upInfos.token);
// creating the uploader with the "context"-item.
Uploader u;
try {
- u = new Uploader( Globals.getPropertyString( PropString.FILETRANSFERSERVERIP ), upInfos.port, Globals.getMasterServerSslContext() );
- } catch ( IOException e ) {
- log.warn( "Could not connect for uploading an image: " + e.toString() );
+ u = new Uploader(Globals.getMasterserverHost(),
+ upInfos.port, Globals.getMasterServerSslContext());
+ } catch (IOException e) {
+ log.warn("Could not connect for uploading an image: "
+ + e.toString());
continue;
}
- u.sendToken( upInfos.token );
+ u.sendToken(upInfos.token);
- log.info( "upInfos.getMissingBlocks().size() = " + upInfos.getMissingBlocks().size() );
+ log.info("upInfos.getMissingBlocks().size() = "
+ + upInfos.getMissingBlocks().size());
long fileSize = new File(image.path).length();
// continue sending Blocks until getMissingBlocks is empty.
- while ( !upInfos.getMissingBlocks().isEmpty() ) {
+ while (!upInfos.getMissingBlocks().isEmpty()) {
List<Integer> blocks = upInfos.getMissingBlocks();
- log.info( "Anzahl angeforderter Blöcke : " + blocks.size() );
- log.info( blocks );
- for ( int i = 0; i < blocks.size(); i++ ) {
- int startOffset = blocks.get( i ) * Globals.getPropertyInt( PropInt.BLOCKSIZE ); // TODO: long
- int endOffset = startOffset + Globals.getPropertyInt( PropInt.BLOCKSIZE );
- if (endOffset >fileSize )
- endOffset = (int)fileSize; // TODO: Long
- u.prepareSendRange( startOffset, endOffset );
- u.sendFile( image.path );
- log.info( "Block number " + blocks.get(i) + " uploaded.");
+ log.info("Anzahl angeforderter Blöcke : " + blocks.size());
+ log.info(blocks);
+ for (int i = 0; i < blocks.size(); i++) {
+ int startOffset = blocks.get(i) * Globals.BLOCKSIZE; // TODO:
+ // long
+ int endOffset = startOffset + Globals.BLOCKSIZE;
+ if (endOffset > fileSize)
+ endOffset = (int) fileSize; // TODO: Long
+ u.prepareSendRange(startOffset, endOffset);
+ u.sendFile(image.path);
+ log.info("Block number " + blocks.get(i) + " uploaded.");
}
- upInfos = ThriftConnection.getUploadInfos( imDat );
+ upInfos = ThriftConnection.getUploadInfos(imDat);
}
u.close();
}
try {
- Thread.sleep( 1 * 60 * 1000 );
- // Thread.sleep( 1000 );
- } catch ( InterruptedException e ) {
+ Thread.sleep(1 * 60 * 1000);
+ // Thread.sleep( 1000 );
+ } catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
diff --git a/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java b/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java
index 4b95f9b..b813f15 100644
--- a/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java
+++ b/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java
@@ -31,8 +31,6 @@ import org.openslx.imagemaster.thrift.iface.UploadError;
import org.openslx.imagemaster.thrift.iface.UploadException;
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;
import org.openslx.satellitedaemon.util.EncryptWithServerIdPublicKey;
@@ -40,14 +38,13 @@ import org.openslx.satellitedaemon.util.EncryptWithServerIdPublicKey;
/***********************************************************************************************/
/**
- * Handles the authentication with the Satellite Server and sends the FILTRANSFERCredentials, which
- * are necessary for the upload of the image.
+ * Handles the authentication with the Satellite Server and sends the
+ * FILTRANSFERCredentials, which are necessary for the upload of the image.
*/
-public class ThriftConnection
-{
+public class ThriftConnection {
private static ImageServer.Client client = null;
private static ServerSessionData sSD = null;
- private static Logger log = Logger.getLogger( ThriftConnection.class );
+ private static Logger log = Logger.getLogger(ThriftConnection.class);
private static CrcFile crc = null;
/***********************************************************************************************/
@@ -56,93 +53,96 @@ public class ThriftConnection
*
* !! 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
- * submitImage with CRCsum in List<Integer>.
+ * 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 submitImage with CRCsum in List<Integer>.
*
* @return returns 'null' if there is a problem.
*/
- public static UploadInfos getUploadInfos( ImageData imDat, String filename )
- {
+ public static UploadInfos getUploadInfos(ImageData imDat, String filename) {
ImageServer.Client theClient = null;
try {
theClient = getConnection();
- if ( theClient == null ) {
- log.error( "Client was null!" );
+ if (theClient == null) {
+ log.error("Client was null!");
return null;
}
- // .submitImage needs the List<Integer> from CRCFile.getCRCs() only
+ // .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 );
- log.info( "Made CRCFile from " + filename );
- log.info( "crc.getCrcSums( ).size = " + crc.getCrcSums().size() );
- // log.info( "crc.getMasterSum() : " + crc.getMasterSum() );
- // for ( int i = 0; i < crc.getCrcSums().size() - 1; i++ ) {
- // log.info( "crc.getCRCSum() : " + crc.getCRCSum( i ) );
- // }
- return theClient.submitImage( sSD.sessionId, imDat, crc.getCrcSums() );
- } catch ( ImageDataException e ) {
- if ( e.isSetNumber() && e.getNumber().equals( ImageDataError.INVALID_DATA ) ) {
- // Data in the db is not valid
+ log.info("First call of submitImage following...");
+ crc = new CrcFile(filename);
+ log.info("Made CRCFile from " + filename);
+ log.info("crc.getCrcSums( ).size = " + crc.getCrcSums().size());
+ // log.info( "crc.getMasterSum() : " + crc.getMasterSum() );
+ // for ( int i = 0; i < crc.getCrcSums().size() - 1; i++ ) {
+ // log.info( "crc.getCRCSum() : " + crc.getCRCSum( i ) );
+ // }
+ return theClient
+ .submitImage(sSD.sessionId, imDat, crc.getCrcSums());
+ } catch (ImageDataException e) {
+ if (e.isSetNumber()
+ && e.getNumber().equals(ImageDataError.INVALID_DATA)) {
+ // Data in the db is not valid
// TODO: add e.message into DB;
} else {
e.printStackTrace();
}
- } catch ( UploadException e ) {
- if ( e.isSetNumber() && e.getNumber().equals( UploadError.BROKEN_BLOCK ) ) {
+ } catch (UploadException e) {
+ if (e.isSetNumber()
+ && e.getNumber().equals(UploadError.BROKEN_BLOCK)) {
// A Block was transmitted 20 times unsuccessfully.
// TODO: Mark the Image as corrupted.
- } else if ( e.getNumber().equals( UploadError.INVALID_CRC ) ) {
+ } else if (e.getNumber().equals(UploadError.INVALID_CRC)) {
// The CRC sum contained errors
- crc = new CrcFile( filename );
+ crc = new CrcFile(filename);
try {
- if ( !crc.isValid() ) {
- //TODO: Mark CRC-file as corrupted.
+ if (!crc.isValid()) {
+ // TODO: Mark CRC-file as corrupted.
}
- } catch ( IOException e1 ) {
- log.error( "IOERROR, while reading from CRC File" + filename );
+ } catch (IOException e1) {
+ log.error("IOERROR, while reading from CRC File" + filename);
}
} else {
e.printStackTrace();
}
- } catch ( AuthorizationException e ) {
- if ( e.isSetNumber() && e.getNumber().equals( AuthorizationError.NOT_AUTHENTICATED ) ) {
+ } catch (AuthorizationException e) {
+ if (e.isSetNumber()
+ && e.getNumber().equals(
+ AuthorizationError.NOT_AUTHENTICATED)) {
// SessionID is not valid
// TODO: Code for new SSID
- } else if ( e.getNumber().equals( AuthorizationError.NO_PERMISSION ) ) {
- //Gibts noch gar nicht
+ } else if (e.getNumber().equals(AuthorizationError.NO_PERMISSION)) {
+ // Gibts noch gar nicht
} else {
e.printStackTrace();
}
- } catch ( UnrecoverableKeyException e ) {
- log.error( "UnrecoverableKeyException" );
+ } catch (UnrecoverableKeyException e) {
+ log.error("UnrecoverableKeyException");
e.printStackTrace();
- } catch ( InvalidKeyException e ) {
- log.error( "InvalidKeyException" );
+ } catch (InvalidKeyException e) {
+ log.error("InvalidKeyException");
e.printStackTrace();
- } catch ( NoSuchAlgorithmException e ) {
- log.error( "NoSuchAlgorithmException" );
+ } catch (NoSuchAlgorithmException e) {
+ log.error("NoSuchAlgorithmException");
e.printStackTrace();
- } catch ( CertificateException e ) {
- log.error( "CertificateException" );
+ } catch (CertificateException e) {
+ log.error("CertificateException");
e.printStackTrace();
- } catch ( FileNotFoundException e ) {
- log.error( "FileNotFoundException" );
+ } catch (FileNotFoundException e) {
+ log.error("FileNotFoundException");
e.printStackTrace();
- } catch ( KeyStoreException e ) {
- log.error( "KeyStoreException" );
+ } catch (KeyStoreException e) {
+ log.error("KeyStoreException");
e.printStackTrace();
- } catch ( SignatureException e ) {
- log.error( "SignatureException" );
+ } catch (SignatureException e) {
+ log.error("SignatureException");
e.printStackTrace();
- } catch ( IOException e ) {
- log.error( "IOException" );
+ } catch (IOException e) {
+ log.error("IOException");
e.printStackTrace();
- } catch ( TException e ) {
- log.error( "TException" );
+ } catch (TException e) {
+ log.error("TException");
e.printStackTrace();
}
return null;
@@ -150,83 +150,88 @@ public class ThriftConnection
/***********************************************************************************************/
/**
- * 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
+ * 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
* submitImage with sSD.sessionId, imDat and !!null!!
*
* @return returns 'null' if there is a problem.
*/
- public static UploadInfos getUploadInfos( ImageData imDat )
- {
+ public static UploadInfos getUploadInfos(ImageData imDat) {
ImageServer.Client theClient = null;
try {
theClient = getConnection();
- if ( theClient == null ) {
- log.error( "Client was null!" );
+ if (theClient == null) {
+ log.error("Client was null!");
return null;
}
- // .submitImage needs the List<Integer> from CRCFile.getCRCs() only
- // on the first time called. null afterwards. --Was the plan. So far not working I guess.
- return theClient.submitImage( sSD.sessionId, imDat, crc.getCrcSums() );
- } catch ( ImageDataException e ) {
- if ( e.isSetNumber() && e.getNumber().equals( ImageDataError.INVALID_DATA ) ) {
- // Data in the db is not valid
- //TODO: add e.message into DB;
+ // .submitImage needs the List<Integer> from CRCFile.getCRCs() only
+ // on the first time called. null afterwards. --Was the plan. So far
+ // not working I guess.
+ return theClient
+ .submitImage(sSD.sessionId, imDat, crc.getCrcSums());
+ } catch (ImageDataException e) {
+ if (e.isSetNumber()
+ && e.getNumber().equals(ImageDataError.INVALID_DATA)) {
+ // Data in the db is not valid
+ // TODO: add e.message into DB;
} else {
e.printStackTrace();
}
- } catch ( UploadException e ) {
- if ( e.isSetNumber() && e.getNumber().equals( UploadError.BROKEN_BLOCK ) ) {
+ } catch (UploadException e) {
+ if (e.isSetNumber()
+ && e.getNumber().equals(UploadError.BROKEN_BLOCK)) {
// A Block was transmitted 20 times unsuccessfully.
// TODO: Mark the Image as corrupted.
- } else if ( e.getNumber().equals( UploadError.INVALID_CRC ) ) {
+ } else if (e.getNumber().equals(UploadError.INVALID_CRC)) {
// The CRC sum contained errors
try {
- if ( !crc.isValid() ) {
- //TODO: Mark CRC-file as corrupted.
+ if (!crc.isValid()) {
+ // TODO: Mark CRC-file as corrupted.
}
- } catch ( IOException e1 ) {
- log.error( "IOERROR, while reading from CRC File" );
+ } catch (IOException e1) {
+ log.error("IOERROR, while reading from CRC File");
}
} else {
e.printStackTrace();
}
- } catch ( AuthorizationException e ) {
- if ( e.isSetNumber() && e.getNumber().equals( AuthorizationError.NOT_AUTHENTICATED ) ) {
+ } catch (AuthorizationException e) {
+ if (e.isSetNumber()
+ && e.getNumber().equals(
+ AuthorizationError.NOT_AUTHENTICATED)) {
// SessionID is not valid
// TODO: Code for new SSID
- } else if ( e.getNumber().equals( AuthorizationError.NO_PERMISSION ) ) {
+ } else if (e.getNumber().equals(AuthorizationError.NO_PERMISSION)) {
} else {
e.printStackTrace();
}
- } catch ( UnrecoverableKeyException e ) {
- log.error( "UnrecoverableKeyException" );
+ } catch (UnrecoverableKeyException e) {
+ log.error("UnrecoverableKeyException");
e.printStackTrace();
- } catch ( InvalidKeyException e ) {
- log.error( "InvalidKeyException" );
+ } catch (InvalidKeyException e) {
+ log.error("InvalidKeyException");
e.printStackTrace();
- } catch ( NoSuchAlgorithmException e ) {
- log.error( "NoSuchAlgorithmException" );
+ } catch (NoSuchAlgorithmException e) {
+ log.error("NoSuchAlgorithmException");
e.printStackTrace();
- } catch ( CertificateException e ) {
- log.error( "CertificateException" );
+ } catch (CertificateException e) {
+ log.error("CertificateException");
e.printStackTrace();
- } catch ( FileNotFoundException e ) {
- log.error( "FileNotFoundException" );
+ } catch (FileNotFoundException e) {
+ log.error("FileNotFoundException");
e.printStackTrace();
- } catch ( KeyStoreException e ) {
- log.error( "KeyStoreException" );
+ } catch (KeyStoreException e) {
+ log.error("KeyStoreException");
e.printStackTrace();
- } catch ( SignatureException e ) {
- log.error( "SignatureException" );
+ } catch (SignatureException e) {
+ log.error("SignatureException");
e.printStackTrace();
- } catch ( IOException e ) {
- log.error( "IOException" );
+ } catch (IOException e) {
+ log.error("IOException");
e.printStackTrace();
- } catch ( TException e ) {
- log.error( "TException" );
+ } catch (TException e) {
+ log.error("TException");
e.printStackTrace();
}
return null;
@@ -239,63 +244,67 @@ public class ThriftConnection
*
* @return returns 'null' if there is a problem.
*/
- public static DownloadInfos getDownloadInfos( DbImage imDat, List<Integer> range )
- {
+ public static DownloadInfos getDownloadInfos(DbImage imDat,
+ List<Integer> range) {
ImageServer.Client theClient = null;
try {
theClient = getConnection();
- if ( theClient == null ) {
- log.error( "Client was null!" );
+ if (theClient == null) {
+ log.error("Client was null!");
return null;
}
- return theClient.getImage( imDat.guid, sSD.sessionId, range );
- } catch ( ImageDataException e ) {
- if ( e.isSetNumber() && e.getNumber().equals( ImageDataError.INVALID_DATA ) ) {
- // Data in the db is not valid
- //TODO: add e.message into DB;
- } else if ( e.getNumber().equals( ImageDataError.UNKNOWN_IMAGE ) ) {
+ return theClient.getImage(imDat.guid, sSD.sessionId, range);
+ } catch (ImageDataException e) {
+ if (e.isSetNumber()
+ && e.getNumber().equals(ImageDataError.INVALID_DATA)) {
+ // Data in the db is not valid
+ // TODO: add e.message into DB;
+ } else if (e.getNumber().equals(ImageDataError.UNKNOWN_IMAGE)) {
// The image requested is not known.
- //TODO: change field image_syncMode, so the image is not asked for again.
+ // TODO: change field image_syncMode, so the image is not asked
+ // for again.
// Plus add a note in some way to mark as unknown by Server
} else {
e.printStackTrace();
}
- } catch ( AuthorizationException e ) {
- if ( e.isSetNumber() && e.getNumber().equals( AuthorizationError.NOT_AUTHENTICATED ) ) {
+ } catch (AuthorizationException e) {
+ if (e.isSetNumber()
+ && e.getNumber().equals(
+ AuthorizationError.NOT_AUTHENTICATED)) {
// SessionID is not valid
// TODO: Code for new SSID
- } else if ( e.getNumber().equals( AuthorizationError.NO_PERMISSION ) ) {
+ } else if (e.getNumber().equals(AuthorizationError.NO_PERMISSION)) {
} else {
e.printStackTrace();
}
- } catch ( UnrecoverableKeyException e ) {
- log.error( "UnrecoverableKeyException" );
+ } catch (UnrecoverableKeyException e) {
+ log.error("UnrecoverableKeyException");
e.printStackTrace();
- } catch ( InvalidKeyException e ) {
- log.error( "InvalidKeyException" );
+ } catch (InvalidKeyException e) {
+ log.error("InvalidKeyException");
e.printStackTrace();
- } catch ( NoSuchAlgorithmException e ) {
- log.error( "NoSuchAlgorithmException" );
+ } catch (NoSuchAlgorithmException e) {
+ log.error("NoSuchAlgorithmException");
e.printStackTrace();
- } catch ( CertificateException e ) {
- log.error( "CertificateException" );
+ } catch (CertificateException e) {
+ log.error("CertificateException");
e.printStackTrace();
- } catch ( FileNotFoundException e ) {
- log.error( "FileNotFoundException" );
+ } catch (FileNotFoundException e) {
+ log.error("FileNotFoundException");
e.printStackTrace();
- } catch ( KeyStoreException e ) {
- log.error( "KeyStoreException" );
+ } catch (KeyStoreException e) {
+ log.error("KeyStoreException");
e.printStackTrace();
- } catch ( SignatureException e ) {
- log.error( "SignatureException" );
+ } catch (SignatureException e) {
+ log.error("SignatureException");
e.printStackTrace();
- } catch ( IOException e ) {
- log.error( "IOException" );
+ } catch (IOException e) {
+ log.error("IOException");
e.printStackTrace();
- } catch ( TException e ) {
- log.error( "TException" );
+ } catch (TException e) {
+ log.error("TException");
e.printStackTrace();
}
return null;
@@ -310,57 +319,60 @@ public class ThriftConnection
* @return returns the client if successful.
*/
private static ImageServer.Client getConnection()
- throws UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException,
- FileNotFoundException, KeyStoreException, IOException, InvalidKeyException, SignatureException
- {
+ throws UnrecoverableKeyException, NoSuchAlgorithmException,
+ CertificateException, FileNotFoundException, KeyStoreException,
+ IOException, InvalidKeyException, SignatureException {
ImageServer.Client theClient = null;
boolean isAuthenticated = false;
- if ( client == null ) {
- log.info( "The global client was null. Making a new client ..." );
+ if (client == null) {
+ log.info("The global client was null. Making a new client ...");
theClient = newClient();
- if ( theClient == null ) {
- log.debug( "ThriftConnection: The client was null after newClient()" );
+ if (theClient == null) {
+ log.debug("ThriftConnection: The client was null after newClient()");
return null;
}
} else {
- log.info( "The global Client was already used. Setting isAuthenticated = true." );
+ log.info("The global Client was already used. Setting isAuthenticated = true.");
theClient = client;
isAuthenticated = true;
}
- // try {
- // isAuthenticated = theClient.ping();
- // } catch ( TException x ) {
- // theClient = newClient();
- // if ( theClient == null ) {
- // return null;
- // }
- // }
- if ( !isAuthenticated ) {
- log.info( "ThriftConnection: Client not yet Authenticated. Trying..." );
+ // try {
+ // isAuthenticated = theClient.ping();
+ // } catch ( TException x ) {
+ // theClient = newClient();
+ // if ( theClient == null ) {
+ // return null;
+ // }
+ // }
+ if (!isAuthenticated) {
+ log.info("ThriftConnection: Client not yet Authenticated. Trying...");
String toEncrypt;
- if ( theClient == null ) {
- log.debug( "The client was null" );
+ if (theClient == null) {
+ log.debug("The client was null");
return null;
}
try {
- 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 );
+ toEncrypt = theClient.startServerAuthentication(Globals
+ .getOrganizationName());
+ log.info("The random String we want to encrypt: " + toEncrypt);
+ EncryptWithServerIdPublicKey rse = new EncryptWithServerIdPublicKey(
+ Globals.getThriftKeystoreAlias(),
+ Globals.getThriftKeystorePassword(),
+ Globals.getThriftKeystorePath());
+ byte[] byteArray = rse.encryptString(toEncrypt);
sSD = theClient.serverAuthenticate(
- Globals.getPropertyString( PropString.THRIFTORGANIZATIONNAME ), ByteBuffer.wrap( byteArray ) );
- } catch ( ServerAuthenticationException e ) {
- log.error( "ThriftConnection: ServerAuthenticationException: Server Authetication was not sucessful." );
+ Globals.getOrganizationName(),
+ ByteBuffer.wrap(byteArray));
+ } catch (ServerAuthenticationException e) {
+ log.error("ThriftConnection: ServerAuthenticationException: Server Authetication was not sucessful.");
e.printStackTrace();
return null;
- } catch ( TException e ) {
- log.error( "ThriftConnection: TException: Server Authetication was not sucessful." );
+ } catch (TException e) {
+ log.error("ThriftConnection: TException: Server Authetication was not sucessful.");
e.printStackTrace();
return null;
}
- log.info( "is Authenticated." );
+ log.info("is Authenticated.");
}
client = theClient;
@@ -371,20 +383,21 @@ public class ThriftConnection
/**
*
* @return
- * @throws IOException
+ * @throws IOException
*/
- private static ImageServer.Client newClient() throws IOException
- {
+ private static ImageServer.Client newClient() throws IOException {
ImageServer.Client newClient = null;
try {
TTransport transport;
- transport = new TNonblockingSocket( Globals.getPropertyString( PropString.FILETRANSFERSERVERIP ), Globals.getPropertyInt( PropInt.THRIFTPORT ) );
+ transport = new TNonblockingSocket(
+ Globals.getMasterserverHost(),
+ Globals.getThriftPort());
transport.open();
- TProtocol protocol = new TBinaryProtocol( transport );
- newClient = new ImageServer.Client( protocol );
- log.debug( "ThriftConnection: Made a new Client" );
- } catch ( TException x ) {
- log.error( "ThriftConnection coudn't create new client." );
+ TProtocol protocol = new TBinaryProtocol(transport);
+ newClient = new ImageServer.Client(protocol);
+ log.debug("ThriftConnection: Made a new Client");
+ } catch (TException x) {
+ log.error("ThriftConnection coudn't create new client.");
x.printStackTrace();
return null;
}