From 6884f6dbc3b513dd7ac947d90ffbbfc3635d6cba Mon Sep 17 00:00:00 2001 From: Björn Hagemeister Date: Wed, 1 Oct 2014 15:16:01 +0200 Subject: Removed unneseccary try - catch - blocks. --- .../java/org/openslx/satellitedaemon/db/MySQL.java | 8 +- .../filetransfer/ThriftConnection.java | 247 +++++++++------------ 2 files changed, 111 insertions(+), 144 deletions(-) diff --git a/src/main/java/org/openslx/satellitedaemon/db/MySQL.java b/src/main/java/org/openslx/satellitedaemon/db/MySQL.java index e225d54..230f455 100644 --- a/src/main/java/org/openslx/satellitedaemon/db/MySQL.java +++ b/src/main/java/org/openslx/satellitedaemon/db/MySQL.java @@ -34,16 +34,16 @@ class MySQL { // Load connection info from class (TODO: Make pretty) Properties properties = new Properties(); + String propertiesPath = "config/mysql.properties"; try { - // using "mysql.properties.example" for testing - final BufferedInputStream stream = new BufferedInputStream( new FileInputStream( "config/mysql.properties" ) ); + final BufferedInputStream stream = new BufferedInputStream( new FileInputStream( propertiesPath ) ); properties.load( stream ); stream.close(); } catch ( FileNotFoundException e ) { - log.fatal( "config/mysql.properties not found!" ); + log.fatal( propertiesPath + " not found!" ); System.exit( 1 ); } catch ( IOException e ) { - log.fatal( "Error reading from config/mysql.properties: " + e.getMessage() ); + log.fatal( "Error reading from " + propertiesPath + ": " + e.getMessage() ); System.exit( 1 ); } catch ( Exception e ) { log.fatal( "Generic error loading mysql properties file." ); diff --git a/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java b/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java index e88019c..cd1054a 100644 --- a/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java +++ b/src/main/java/org/openslx/satellitedaemon/filetransfer/ThriftConnection.java @@ -45,12 +45,12 @@ 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. */ -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; - /***********************************************************************************************/ /** @@ -62,92 +62,74 @@ public class ThriftConnection { * * @return returns 'null' if there is a problem. */ - public static UploadData getUploadInfos(ImageData imDat, String path) { + public static UploadData getUploadInfos( ImageData imDat, String path ) + { ImageServer.Client theClient = null; - String crcPath = path.concat(".crc"); - try { - theClient = getConnection(); - if (theClient == null) { - log.error("Client was null!"); - return null; - } - } catch (UnrecoverableKeyException e) { - log.error("UnrecoverableKeyException", e); - } catch (InvalidKeyException e) { - log.error("InvalidKeyException", e); - } catch (NoSuchAlgorithmException e) { - log.error("NoSuchAlgorithmException", e); - } catch (CertificateException e) { - log.error("CertificateException", e); - } catch (FileNotFoundException e) { - log.error("FileNotFoundException", e); - } catch (KeyStoreException e) { - log.error("KeyStoreException", e); - } catch (SignatureException e) { - log.error("SignatureException", e); - } catch (IOException e) { - log.error("IOException", e); + String crcPath = path.concat( ".crc" ); + theClient = getConnection(); + if ( theClient == null ) { + log.error( "Client was null!" ); + return null; } List crcSums = null; try { - crc = new CrcFile(crcPath); - log.info("Made CRCFile from " + crcPath); - log.info("crc.getCrcSums( ).size = " + crc.getCrcSums().size()); + crc = new CrcFile( crcPath ); + log.info( "Made CRCFile from " + crcPath ); + 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 ) ); // } crcSums = crc.getCrcSums(); - } catch (FileNotFoundException e) { - log.debug("crcFile " + crcPath + " not found."); - } catch (IOException e) { - log.error("IOException", e); + } catch ( FileNotFoundException e ) { + log.debug( "crcFile " + crcPath + " not found." ); + } catch ( IOException e ) { + log.error( "IOException", e ); } try { - return theClient - .submitImage(sSD.sessionId, imDat, crcSums); - } catch (AuthorizationException e) { - if (e.isSetNumber() + return theClient.submitImage( sSD.sessionId, imDat, crcSums ); + } catch ( AuthorizationException e ) { + if ( e.isSetNumber() && e.getNumber().equals( - AuthorizationError.NOT_AUTHENTICATED)) { + 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 ) ) { // not yet implemented. } else { e.printStackTrace(); } - } catch (ImageDataException e) { - if (e.isSetNumber() - && e.getNumber().equals(ImageDataError.INVALID_DATA)) { + } catch ( ImageDataException e ) { + if ( e.isSetNumber() + && e.getNumber().equals( ImageDataError.INVALID_DATA ) ) { e.printStackTrace(); // 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 { - crc = new CrcFile(crcPath); - } catch (IOException e1) { + crc = new CrcFile( crcPath ); + } catch ( IOException e1 ) { // TODO Auto-generated catch block e1.printStackTrace(); } - if (!crc.isValid()) { + if ( !crc.isValid() ) { // TODO: Mark CRC-file as corrupted. } } else { e.printStackTrace(); } - } catch (TException e) { - log.error("TException", e); + } catch ( TException e ) { + log.error( "TException", e ); } return null; } @@ -159,66 +141,51 @@ public class ThriftConnection { * * @return returns 'null' if there is a problem. */ - public static DownloadData getDownloadInfos(DbImage imDat) { + public static DownloadData getDownloadInfos( DbImage imDat ) + { ImageServer.Client theClient = null; - try { - theClient = getConnection(); - if (theClient == null) { - log.error("Client was null!"); - return null; - } - } catch (UnrecoverableKeyException e) { - log.error("UnrecoverableKeyException", e); - } catch (InvalidKeyException e) { - log.error("InvalidKeyException", e); - } catch (NoSuchAlgorithmException e) { - log.error("NoSuchAlgorithmException", e); - } catch (CertificateException e) { - log.error("CertificateException", e); - } catch (FileNotFoundException e) { - log.error("FileNotFoundException", e); - } catch (KeyStoreException e) { - log.error("KeyStoreException", e); - } catch (SignatureException e) { - log.error("SignatureException", e); - } catch (IOException e) { - log.error("IOException", e); + + theClient = getConnection(); + if ( theClient == null ) { + log.error( "Client was null!" ); + return null; } - + try { - return theClient.getImage(sSD.sessionId, imDat.guid); - } catch (ImageDataException e) { - log.debug("In catch - blog of thrift connection"); - if (e.isSetNumber() - && e.getNumber().equals(ImageDataError.INVALID_DATA)) { + return theClient.getImage( sSD.sessionId, imDat.guid ); + } catch ( ImageDataException e ) { + log.debug( "In catch - blog of thrift connection" ); + 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)) { + } 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. - // For now just changed status of image. Currently no possibility + // For now just changed status of image. Currently no + // possibility // for creating new useful state in DB. (Offenburg) - log.info("Image not known. For skipping next time, mark as only_local."); - imDat.updateStatus(Status.only_local); + log.info( "Image not known. For skipping next time, mark as only_local." ); + imDat.updateStatus( Status.only_local ); // Plus add a note in some way to mark as unknown by Server } else { e.printStackTrace(); } - } catch (AuthorizationException e) { - if (e.isSetNumber() + } catch ( AuthorizationException e ) { + if ( e.isSetNumber() && e.getNumber().equals( - AuthorizationError.NOT_AUTHENTICATED)) { - log.error("Not authenticated. SessionID is not valid.", e); + AuthorizationError.NOT_AUTHENTICATED ) ) { + log.error( "Not authenticated. SessionID is not valid.", e ); // SessionID is not valid // TODO: Code for new SSID - } else if (e.getNumber().equals(AuthorizationError.NO_PERMISSION)) { - log.error("No permission error.", e); + } else if ( e.getNumber().equals( AuthorizationError.NO_PERMISSION ) ) { + log.error( "No permission error.", e ); } else { e.printStackTrace(); } - } catch (TException e) { - log.error("TException", e); + } catch ( TException e ) { + log.error( "TException", e ); } return null; } @@ -232,65 +199,64 @@ public class ThriftConnection { * @return returns the client if successful. */ private static ImageServer.Client getConnection() - 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; } - // here the client was already used so we are just assuming that the client is still + // here the client was already used so we are just assuming that the + // client is still // authenticated. Should be checked with the ping() method. - // try { - // isAuthenticated = theClient.ping(); - // } catch ( TException x ) { - // theClient = newClient(); - // if ( theClient == null ) { - // return null; - // } - // } - if (!isAuthenticated) { - log.info("ThriftConnection: Client not yet Authenticated. Trying..."); - if (theClient == null) { - log.debug("The client was null"); + try { + isAuthenticated = theClient.isServerAuthenticated( sSD.sessionId ); + } catch ( TException x ) { + theClient = newClient(); + if ( theClient == null ) { return null; } + } + if ( !isAuthenticated ) { + log.info( "ThriftConnection: Client not yet Authenticated. Trying..." ); try { - ByteBuffer tmpBuffer = theClient.startServerAuthentication(Globals - .getOrganizationName()); - byte[] toEncrypt = new byte[tmpBuffer.remaining()]; - tmpBuffer.get(toEncrypt); - log.info("The random String we want to encrypt: " + toEncrypt); - log.info("Length of the random String : " + toEncrypt.length); - AsymEncryptionHandler aeh = new AsymEncryptionHandler(Globals.getPrivateKey()); - - byte[] byteArray = aeh.encryptMessage(toEncrypt); - log.info( "Length of the byteArray of the random string after encryption :" + byteArray.length ); + ByteBuffer tmpBuffer = theClient + .startServerAuthentication( Globals + .getOrganizationName() ); + byte[] toEncrypt = new byte[ tmpBuffer.remaining() ]; + tmpBuffer.get( toEncrypt ); + log.info( "The random String we want to encrypt: " + toEncrypt ); + log.info( "Length of the random String : " + toEncrypt.length ); + AsymEncryptionHandler aeh = new AsymEncryptionHandler( + Globals.getPrivateKey() ); + + byte[] byteArray = aeh.encryptMessage( toEncrypt ); + log.info( "Length of the byteArray of the random string after encryption :" + + byteArray.length ); ByteBuffer b = ByteBuffer.wrap( byteArray ); - log.info( "Length of the byteBuffer after encryption :" + b.remaining() ); + log.info( "Length of the byteBuffer after encryption :" + + b.remaining() ); sSD = theClient.serverAuthenticate( Globals.getOrganizationName(), - ByteBuffer.wrap(byteArray)); - } catch (ServerAuthenticationException e) { - log.error("ThriftConnection: ServerAuthenticationException: Server Authetication was not sucessful."); + 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; @@ -303,19 +269,20 @@ public class ThriftConnection { * * @throws IOException */ - private static ImageServer.Client newClient() throws IOException { + private static ImageServer.Client newClient() + { ImageServer.Client newClient = null; try { TTransport transport = new TFramedTransport( new TSocket( - Globals.getMasterserverHost(), - Globals.getThriftPort())); + Globals.getMasterserverHost(), Globals.getThriftPort() ) ); transport.open(); - TProtocol protocol = new TBinaryProtocol(transport); - newClient = new ImageServer.Client(protocol); - log.debug("ThriftConnection: Made a new Client"); - } catch (TTransportException tte) { - log.error("Transport could not be opened. Couldn't create new client."); - tte.printStackTrace(); + TProtocol protocol = new TBinaryProtocol( transport ); + newClient = new ImageServer.Client( protocol ); + log.debug( "ThriftConnection: Made a new Client" ); + } catch ( TTransportException e ) { + log.error( + "Transport could not be opened. Couldn't create new client.", + e ); return null; } return newClient; -- cgit v1.2.3-55-g7522