diff options
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/db')
6 files changed, 175 insertions, 142 deletions
diff --git a/src/main/java/org/openslx/imagemaster/db/DbImage.java b/src/main/java/org/openslx/imagemaster/db/DbImage.java index d3e8a94..bf643bb 100644 --- a/src/main/java/org/openslx/imagemaster/db/DbImage.java +++ b/src/main/java/org/openslx/imagemaster/db/DbImage.java @@ -5,10 +5,13 @@ import java.util.Date; import org.openslx.imagemaster.thrift.iface.ImageData; -public class DbImage { +public class DbImage +{ + private String UUID; - public DbImage(String UUID) { + public DbImage(String UUID) + { this.UUID = UUID; } @@ -18,37 +21,42 @@ public class DbImage { * @param imageData * @return */ - public static boolean exists(ImageData imageData) { - if (MySQL.findUniqueOrNull(DbImage.class, + public static boolean exists( ImageData imageData ) + { + if ( MySQL.findUniqueOrNull( DbImage.class, "SELECT images.UUID FROM images WHERE images.UUID = ?", - imageData.uuid) == null) { + imageData.uuid ) == null ) { return false; } else { return true; } } - public static int insert(ImageData imageData) { - Date createTime = new Date(imageData.imageCreateTime); - Date updateTime = new Date(imageData.imageUpdateTime); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - - int ownerId = DbUser.getUserIdByName(imageData.imageOwner); - - return MySQL.update( - "INSERT INTO images (UUID, image_version, image_name, image_path, image_createTime, image_updateTime, image_owner, content_operatingSystem, status_isValid, status_isDeleted, image_shortDescription, image_longDescription) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", - imageData.uuid, imageData.imageVersion, imageData.imageName, "!uploading!", - sdf.format(createTime), sdf.format(updateTime), ownerId, - imageData.conentOperatingSystem, imageData.statusIsValid, - imageData.statusIsDeleted, imageData.imageShortDescription, - imageData.imageLongDescription); + public static int insert( ImageData imageData ) + { + Date createTime = new Date( imageData.imageCreateTime ); + Date updateTime = new Date( imageData.imageUpdateTime ); + SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); + + int ownerId = DbUser.getUserIdByName( imageData.imageOwner ); + + return MySQL + .update( + "INSERT INTO images (UUID, image_version, image_name, image_path, image_createTime, image_updateTime, image_owner, content_operatingSystem, status_isValid, status_isDeleted, image_shortDescription, image_longDescription) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + imageData.uuid, imageData.imageVersion, imageData.imageName, "!uploading!", + sdf.format( createTime ), sdf.format( updateTime ), ownerId, + imageData.conentOperatingSystem, imageData.statusIsValid, + imageData.statusIsDeleted, imageData.imageShortDescription, + imageData.imageLongDescription ); } - public String getUUID() { + public String getUUID() + { return this.UUID; } - public static int update(ImageData imageData, String location) { - return MySQL.update("UPDATE images SET images.image_path = ? WHERE images.UUID = ?", location, imageData.uuid); + public static int update( ImageData imageData, String location ) + { + return MySQL.update( "UPDATE images SET images.image_path = ? WHERE images.UUID = ?", location, imageData.uuid ); } } diff --git a/src/main/java/org/openslx/imagemaster/db/DbSatellite.java b/src/main/java/org/openslx/imagemaster/db/DbSatellite.java index 7e9ef54..9f3cd01 100644 --- a/src/main/java/org/openslx/imagemaster/db/DbSatellite.java +++ b/src/main/java/org/openslx/imagemaster/db/DbSatellite.java @@ -1,32 +1,39 @@ package org.openslx.imagemaster.db; -public class DbSatellite { +public class DbSatellite +{ + private String organization, address, name; // needs to be public in order to be found by MySQL - public DbSatellite(String organization, String address, String name) { + public DbSatellite(String organization, String address, String name) + { this.organization = organization; this.address = address; this.name = name; } - public static DbSatellite fromOrganization(String organization) { + public static DbSatellite fromOrganization( String organization ) + { return MySQL .findUniqueOrNull( DbSatellite.class, "SELECT satellite.organization, satellite.address, satellite.name FROM satellite WHERE satellite.organization = ? LIMIT 1", - organization); + organization ); } - public String getAddress() { + public String getAddress() + { return address; } - public String getName() { + public String getName() + { return name; } - public String getOrganization() { + public String getOrganization() + { return organization; } -}
\ No newline at end of file +} diff --git a/src/main/java/org/openslx/imagemaster/db/DbUser.java b/src/main/java/org/openslx/imagemaster/db/DbUser.java index 774917d..45cbb2a 100644 --- a/src/main/java/org/openslx/imagemaster/db/DbUser.java +++ b/src/main/java/org/openslx/imagemaster/db/DbUser.java @@ -3,27 +3,31 @@ package org.openslx.imagemaster.db; import org.apache.log4j.Logger; import org.openslx.imagemaster.session.User; -public class DbUser extends User { +public class DbUser extends User +{ + private static Logger log = Logger.getLogger( DbUser.class ); - + public DbUser(int userId, String username, String password, String organization, String firstName, String lastName, String eMail, - String satelliteAddress) { - super(userId, username, password, organization, firstName, lastName, eMail, - satelliteAddress); + String satelliteAddress) + { + super( userId, username, password, organization, firstName, lastName, eMail, + satelliteAddress ); } /** * Query database for user with given login * * @param login - * (user@organization) + * (user@organization) * @return instance of DbUser for matching entry from DB, or null if not * found */ - public static DbUser forLogin(final String login) { - final String[] parts = login.split("@"); - if (parts.length != 2) + public static DbUser forLogin( final String login ) + { + final String[] parts = login.split( "@" ); + if ( parts.length != 2 ) return null; return MySQL .findUniqueOrNull( @@ -31,24 +35,27 @@ public class DbUser extends User { "SELECT user.userid, user.username, user.password, user.organization, user.firstname, user.lastname, user.email, satellite.address FROM user" + " LEFT JOIN satellite USING (organization)" + " WHERE user.username = ? AND user.organization = ? LIMIT 1", - parts[0], parts[1]); + parts[0], parts[1] ); } - public static boolean insertOrUpdate(User user) { - log.debug("Inserted user '" + user.username + "' into db."); - MySQL.update("INSERT INTO user (username, password, organization, firstname, lastname, email) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE password=VALUES(password), organization=VALUES(organization), firstname=VALUES(firstname), lastname=VALUES(lastname), email=VALUES(email)", - user.username, user.password, user.organization, user.firstName, user.lastName, user.eMail); + public static boolean insertOrUpdate( User user ) + { + log.debug( "Inserted user '" + user.username + "' into db." ); + MySQL.update( + "INSERT INTO user (username, password, organization, firstname, lastname, email) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE password=VALUES(password), organization=VALUES(organization), firstname=VALUES(firstname), lastname=VALUES(lastname), email=VALUES(email)", + user.username, user.password, user.organization, user.firstName, user.lastName, user.eMail ); return false; } - - public static int getUserIdByName(String username) { - DbUser user = MySQL + + public static int getUserIdByName( String username ) + { + DbUser user = MySQL .findUniqueOrNull( DbUser.class, "SELECT user.userid, user.username, user.password, user.organization, user.firstname, user.lastname, user.email, satellite.address FROM user" + " LEFT JOIN satellite USING (organization)" - + " WHERE user.username = ? LIMIT 1", username); - if (user != null) { + + " WHERE user.username = ? LIMIT 1", username ); + if ( user != null ) { return user.userId; } else { return 0; diff --git a/src/main/java/org/openslx/imagemaster/db/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/db/ImageProcessor.java index de545ae..d0ac5c6 100644 --- a/src/main/java/org/openslx/imagemaster/db/ImageProcessor.java +++ b/src/main/java/org/openslx/imagemaster/db/ImageProcessor.java @@ -7,49 +7,51 @@ import org.apache.log4j.Logger; import org.openslx.imagemaster.Globals; import org.openslx.imagemaster.thrift.iface.ImageData; -public class ImageProcessor { +public class ImageProcessor +{ - private static Logger log = Logger.getLogger(ImageProcessor.class); + private static Logger log = Logger.getLogger( ImageProcessor.class ); private static HashMap<String, ImageData> images = new HashMap<>(); - /** * Processes an image after upload + * * @param username the user that uploaded the file * @param filename the name of the file that was uploaded (_no_ absolute path) * @return */ - public static boolean processImageAfterUpload(String username, String filename) { - if (!images.containsKey(username)) { + public static boolean processImageAfterUpload( String username, String filename ) + { + if ( !images.containsKey( username ) ) { return false; } - - log.info("Will now process '" + filename + "' from user '" + username - + "'"); + + log.info( "Will now process '" + filename + "' from user '" + username + + "'" ); // move image to right location String oldFileName = Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + username + "/" + filename; - String newFileName = Globals.getPropertyString( Globals.PropString.IMAGEDIR ) + "/" + images.get(username).uuid; - - File imageFile = new File(oldFileName); - - if (!imageFile.exists()) { + String newFileName = Globals.getPropertyString( Globals.PropString.IMAGEDIR ) + "/" + images.get( username ).uuid; + + File imageFile = new File( oldFileName ); + + if ( !imageFile.exists() ) { // image file does not exist return false; } - - imageFile.renameTo( new File(newFileName) ); - - log.info("Moved file from " + oldFileName + " to " + newFileName ); - - File tempUserDir = new File (Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + username); + + imageFile.renameTo( new File( newFileName ) ); + + log.info( "Moved file from " + oldFileName + " to " + newFileName ); + + File tempUserDir = new File( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + username ); tempUserDir.delete(); - + // update database - DbImage.update(images.get(username), newFileName); - log.info("Updated db: " + images.get(username).uuid); - - images.remove(username); + DbImage.update( images.get( username ), newFileName ); + log.info( "Updated db: " + images.get( username ).uuid ); + + images.remove( username ); return true; } @@ -57,31 +59,31 @@ public class ImageProcessor { * Try to add imageData to database. * * @param imageData - * the data for the image to add + * the data for the image to add * @return false if submit fails */ - public static boolean addImageDataToProcess(ImageData imageData, - String username) { - log.info("Adding image to process list: " + imageData.imageName + ", submitted by " + username); - - if (imageData.uuid.isEmpty() || imageData.imageName.isEmpty() + public static boolean addImageDataToProcess( ImageData imageData, String username ) + { + log.info( "Adding image to process list: " + imageData.imageName + ", submitted by " + username ); + + if ( imageData.uuid.isEmpty() || imageData.imageName.isEmpty() || imageData.imageOwner.isEmpty() || imageData.conentOperatingSystem.isEmpty() || imageData.imageShortDescription.isEmpty() - || imageData.imageLongDescription.isEmpty()) { + || imageData.imageLongDescription.isEmpty() ) { return false; } - + // TODO: check some regex? - - if (DbImage.exists(imageData)) { + + if ( DbImage.exists( imageData ) ) { return false; } - + // if everything went fine, add image to db - DbImage.insert(imageData); - + DbImage.insert( imageData ); + // and to processinglist - images.put(username, imageData); + images.put( username, imageData ); return true; } } diff --git a/src/main/java/org/openslx/imagemaster/db/LdapUser.java b/src/main/java/org/openslx/imagemaster/db/LdapUser.java index 0a18565..08eb127 100644 --- a/src/main/java/org/openslx/imagemaster/db/LdapUser.java +++ b/src/main/java/org/openslx/imagemaster/db/LdapUser.java @@ -28,21 +28,27 @@ import org.openslx.imagemaster.util.Sha512Crypt; * actually verify the cert, or we could just stop using ssl * altogether. */ -class MyTrustManager implements X509TrustManager { +class MyTrustManager implements X509TrustManager +{ @Override - public void checkClientTrusted(X509Certificate[] arg0, String arg1) - throws CertificateException {} + public void checkClientTrusted( X509Certificate[] arg0, String arg1 ) + throws CertificateException + { + } @Override - public void checkServerTrusted(X509Certificate[] arg0, String arg1) - throws CertificateException {} + public void checkServerTrusted( X509Certificate[] arg0, String arg1 ) + throws CertificateException + { + } @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; + public X509Certificate[] getAcceptedIssuers() + { + return new X509Certificate[ 0 ]; } - + } /** @@ -52,101 +58,104 @@ class MyTrustManager implements X509TrustManager { */ public class LdapUser extends User { + private static final Logger log = Logger.getLogger( LdapUser.class ); - - protected LdapUser(int userId, String username, String password, String organization, - String firstName, String lastName, String eMail, - String satelliteAddress) { - super(userId, username, password, organization, firstName, lastName, eMail, - satelliteAddress); + + protected LdapUser(int userId, String username, String password, String organization, String firstName, String lastName, String eMail, String satelliteAddress) + { + super( userId, username, password, organization, firstName, lastName, eMail, + satelliteAddress ); } - + /** * Query LDAP for user with given login - * @param login Login of user in the form "user@organization.com" + * + * @param login Login of user in the form "user@organization.com" * @return instance of LDAPUser for matching entry from LDAP, or null if not found */ - public static LdapUser forLogin( final String login, final String password ) throws AuthenticationException { + public static LdapUser forLogin( final String login, final String password ) throws AuthenticationException + { String username, organization, firstName, lastName, eMail, satelliteAddress; - - String[] temp = login.split("@"); - if (temp.length != 2) throw new AuthenticationException( AuthenticationError.GENERIC_ERROR, "Login must be in form user@organization.com"); + + String[] temp = login.split( "@" ); + if ( temp.length != 2 ) + throw new AuthenticationException( AuthenticationError.GENERIC_ERROR, "Login must be in form user@organization.com" ); username = temp[0]; - + LdapConnectionConfig ldapConfig = new LdapConnectionConfig(); - ldapConfig.setTrustManagers(new MyTrustManager()); - ldapConfig.setLdapPort(Globals.getPropertyInt( Globals.PropInt.LDAPPORT )); - ldapConfig.setLdapHost(Globals.getPropertyString( Globals.PropString.LDAPHOST )); - ldapConfig.setUseSsl(Globals.getPropertyBool( PropBool.LDAPSSL )); - + ldapConfig.setTrustManagers( new MyTrustManager() ); + ldapConfig.setLdapPort( Globals.getPropertyInt( Globals.PropInt.LDAPPORT ) ); + ldapConfig.setLdapHost( Globals.getPropertyString( Globals.PropString.LDAPHOST ) ); + ldapConfig.setUseSsl( Globals.getPropertyBool( PropBool.LDAPSSL ) ); + LdapNetworkConnection connection = new LdapNetworkConnection( ldapConfig ); - + // bind connection try { if ( connection.connect() ) { - String name = Globals.getPropertyString( PropString.LDAPBINDQUERY ).replace("%", username); - connection.bind(name, password); + String name = Globals.getPropertyString( PropString.LDAPBINDQUERY ).replace( "%", username ); + connection.bind( name, password ); } - } catch (LdapException e1) { + } catch ( LdapException e1 ) { log.warn( "Connection to LDAP failed: " + e1.getMessage() ); } - + if ( !connection.isConnected() ) { try { connection.unBind(); connection.close(); - } catch (LdapException | IOException e) { + } catch ( LdapException | IOException e ) { // Not doing anything here, as ldap already failed... } throw new AuthenticationException( AuthenticationError.GENERIC_ERROR, "Could not connect to LDAP server." ); } - + // test authorization if ( !connection.isAuthenticated() ) { try { connection.unBind(); connection.close(); - } catch (LdapException | IOException e) { + } catch ( LdapException | IOException e ) { // Failing disconnect... Can't do much about it, just go on } throw new AuthenticationException( AuthenticationError.INVALID_CREDENTIALS, "Could not authenticate to LDAP server. Invalid credentials?" ); } - + // make search query try { - EntryCursor cursor = connection.search(Globals.getPropertyString( Globals.PropString.LDAPSEARCHBASEDN ), - Globals.getPropertyString( Globals.PropString.LDAPSEARCHFILTER ).replace("%", username), SearchScope.SUBTREE); + EntryCursor cursor = connection.search( Globals.getPropertyString( Globals.PropString.LDAPSEARCHBASEDN ), + Globals.getPropertyString( Globals.PropString.LDAPSEARCHFILTER ).replace( "%", username ), SearchScope.SUBTREE ); // only use the first result cursor.next(); Entry entry = cursor.get(); - username = entry.get("uid").getString(); + username = entry.get( "uid" ).getString(); organization = "Test Organization"; // will be filled with bwIDM LDAP server - firstName = entry.get("givenName").getString(); - lastName = entry.get("sn").getString(); - eMail = entry.get("rufPreferredMail").getString(); + firstName = entry.get( "givenName" ).getString(); + lastName = entry.get( "sn" ).getString(); + eMail = entry.get( "rufPreferredMail" ).getString(); // get the satellite address from db - DbSatellite dbSatellite = DbSatellite.fromOrganization(organization); - if (dbSatellite != null) { + DbSatellite dbSatellite = DbSatellite.fromOrganization( organization ); + if ( dbSatellite != null ) { satelliteAddress = dbSatellite.getAddress(); } else { // TODO: Organization is not known.. Handle this satelliteAddress = "addressNotKown"; } - } catch (LdapException | CursorException e1) { + } catch ( LdapException | CursorException e1 ) { return null; } finally { // close connection try { connection.unBind(); - } catch (LdapException e) { + } catch ( LdapException e ) { return null; } try { connection.close(); - } catch (IOException e) { + } catch ( IOException e ) { return null; } } - return new LdapUser(0, username, Sha512Crypt.Sha512_crypt(password, null, 0), organization, firstName, lastName, eMail, satelliteAddress); + return new LdapUser( 0, username, Sha512Crypt.Sha512_crypt( password, null, 0 ), organization, firstName, lastName, eMail, satelliteAddress ); } } diff --git a/src/main/java/org/openslx/imagemaster/db/MySQL.java b/src/main/java/org/openslx/imagemaster/db/MySQL.java index ebc44c7..121ef1b 100644 --- a/src/main/java/org/openslx/imagemaster/db/MySQL.java +++ b/src/main/java/org/openslx/imagemaster/db/MySQL.java @@ -25,7 +25,7 @@ class MySQL /** * Static initializer for setting up the database connection. - * This gets called implicitly as soon as the clas loader loads + * This gets called implicitly as soon as the class loader loads * the class. In most cases that happens when the class is being * accessed for the first time during run time. */ @@ -52,7 +52,7 @@ class MySQL final String dbname = properties.getProperty( "db" ); final String user = properties.getProperty( "user" ); final String password = properties.getProperty( "password" ); - + Util.notNullFatal( host, "host not set in mysql properties" ); Util.notNullFatal( dbname, "db not set in mysql properties" ); Util.notNullFatal( user, "user not set in mysql properties" ); @@ -77,7 +77,7 @@ class MySQL * Get a list of objects of the given class from the database. * The class needs a matching constructor for the query you pass in, i.e. number of * arguments has to be equal to number of columns returned by query. - * + * * @param clazz The class to instanciate for the result(s) * @param sql The sql query to run * @param args Any number of arguments to the query (using the '?' placeholder) @@ -92,7 +92,7 @@ class MySQL * Run a query on the database that will return at most one result. * If the query returns a row, it will be used to instanciate the given class. If * it doesn't return a row, null will be returned. - * + * * @param clazz The class to instanciate for the result (if any) * @param sql The sql query to run * @param args Any number of arguments to the query (using the '?' placeholder) @@ -102,16 +102,16 @@ class MySQL { return db.findUniqueOrNull( clazz, sql, args ); } - + /** * Run an update on the database, return number of rows affected. - * + * * @param sql The update/insert query to run * @param args Any number of arguments to the query (using the '?' placeholder) * @return Number of rows affected by query */ - protected static int update( String sql, Object... args) { + protected static int update( String sql, Object... args ) + { return db.update( sql, args ); } } - |
