summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNils Schwabe2014-04-28 18:18:59 +0200
committerNils Schwabe2014-04-28 18:18:59 +0200
commit287ad92152c530b400a5405a4672a41d92e4d368 (patch)
tree42dcb2c698672ae371d70a81ea5c49f3280db9c6
parentAdd remove image from process list (when user gets deleted after timeout) (diff)
downloadmasterserver-287ad92152c530b400a5405a4672a41d92e4d368.tar.gz
masterserver-287ad92152c530b400a5405a4672a41d92e4d368.tar.xz
masterserver-287ad92152c530b400a5405a4672a41d92e4d368.zip
Add ftp users and images in processing list are remembered
-rw-r--r--pom.xml2
-rw-r--r--src/main/java/org/openslx/imagemaster/App.java6
-rw-r--r--src/main/java/org/openslx/imagemaster/db/DbImage.java77
-rw-r--r--src/main/java/org/openslx/imagemaster/db/DbUser.java19
-rw-r--r--src/main/java/org/openslx/imagemaster/db/LdapUser.java8
-rw-r--r--src/main/java/org/openslx/imagemaster/ftp/FtpCredentialsScheduler.java61
-rw-r--r--src/main/java/org/openslx/imagemaster/ftp/ImageProcessor.java48
-rw-r--r--src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java43
-rw-r--r--src/main/java/org/openslx/imagemaster/ftp/MasterFtplet.java4
-rw-r--r--src/main/java/org/openslx/imagemaster/server/ApiServer.java25
-rw-r--r--src/main/java/org/openslx/imagemaster/serversession/ServerAuthenticator.java1
-rw-r--r--src/main/java/org/openslx/imagemaster/thrift/server/ImageServerHandler.java6
-rw-r--r--src/main/thrift/imagemaster.thrift5
-rw-r--r--src/test/java/org/openslx/imagemaster/ServerTest.java51
14 files changed, 233 insertions, 123 deletions
diff --git a/pom.xml b/pom.xml
index 4e6974a..2683c31 100644
--- a/pom.xml
+++ b/pom.xml
@@ -106,7 +106,7 @@
<dependency>
<groupId>org.apache.directory.api</groupId>
<artifactId>api-all</artifactId>
- <version>1.0.0-M21</version>
+ <version>1.0.0-M22</version>
<scope>compile</scope>
</dependency>
<dependency>
diff --git a/src/main/java/org/openslx/imagemaster/App.java b/src/main/java/org/openslx/imagemaster/App.java
index a73e1ef..e40dae3 100644
--- a/src/main/java/org/openslx/imagemaster/App.java
+++ b/src/main/java/org/openslx/imagemaster/App.java
@@ -7,6 +7,7 @@ import java.util.List;
import org.apache.log4j.Logger;
import org.openslx.imagemaster.Globals.PropInt;
import org.openslx.imagemaster.ftp.FtpCredentialsScheduler;
+import org.openslx.imagemaster.ftp.ImageProcessor;
import org.openslx.imagemaster.ftp.MasterFtpServer;
import org.openslx.imagemaster.thrift.server.BinaryListener;
@@ -36,6 +37,9 @@ public class App
System.exit( 1 );
}
log.info( "Loaded config file" );
+
+ log.info( "Checking !uploading! db entries." );
+ ImageProcessor.checkUploading();
// Create binary listener
Thread t;
@@ -44,7 +48,7 @@ public class App
t.start();
// Create Ftp Server
- ftpServer.init( Globals.getPropertyInt( PropInt.FTPPORT ) );
+ ftpServer.init( Globals.getPropertyInt( PropInt.FTPPORT ) ); // this init needs to be done, because we have to wait for the config file
Thread f;
f = new Thread( ftpServer, "FtpServer" );
servers.add( f );
diff --git a/src/main/java/org/openslx/imagemaster/db/DbImage.java b/src/main/java/org/openslx/imagemaster/db/DbImage.java
index f5b8ee4..5ab2c4b 100644
--- a/src/main/java/org/openslx/imagemaster/db/DbImage.java
+++ b/src/main/java/org/openslx/imagemaster/db/DbImage.java
@@ -1,18 +1,71 @@
package org.openslx.imagemaster.db;
+import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
+import java.util.List;
import org.openslx.imagemaster.thrift.iface.ImageData;
public class DbImage
{
- private String UUID;
+ public final String UUID;
+ public final int imageVersion;
+ public final String imageName;
+ public final String imagePath;
+ public final Timestamp imageCreateTime;
+ public final Timestamp imageUpdateTime;
+ public final String imageOwner;
+ public final String contentOperatingSystem;
+ public final boolean isValid;
+ public final boolean isDeleted;
+ public final String shortDescription;
+ public final String longDescription;
+ public final Timestamp timestamp;
+ public final String ftpUser;
+ public final long fileSize;
+
public DbImage(String UUID)
{
this.UUID = UUID;
+ this.imageVersion = 0;
+ this.imageName = null;
+ this.imagePath = null;
+ this.imageCreateTime = null;
+ this.imageUpdateTime = null;
+ this.imageOwner = null;
+ this.contentOperatingSystem = null;
+ this.isValid = false;
+ this.isDeleted = false;
+ this.shortDescription = null;
+ this.longDescription = null;
+ this.timestamp = new Timestamp( 0 );
+ this.ftpUser = null;
+ this.fileSize = 0;
+ }
+
+ public DbImage(String UUID, int imageVersion, String imageName, String imagePath,
+ Timestamp imageCreateTime, Timestamp imageUpdateTime, int imageOwner, String contentOperatingSystem,
+ boolean isValid, boolean isDeleted, String shortDescription, String longDescription,
+ Timestamp timestamp, String ftpUser, long fileSize)
+ {
+ this.UUID = UUID;
+ this.imageVersion = imageVersion;
+ this.imageName = imageName;
+ this.imagePath = imagePath;
+ this.imageCreateTime = imageCreateTime;
+ this.imageUpdateTime = imageUpdateTime;
+ this.imageOwner = DbUser.getUserNameById( imageOwner );
+ this.contentOperatingSystem = contentOperatingSystem;
+ this.isValid = isValid;
+ this.isDeleted = isDeleted;
+ this.shortDescription = shortDescription;
+ this.longDescription = longDescription;
+ this.timestamp = timestamp;
+ this.ftpUser = ftpUser;
+ this.fileSize = fileSize;
}
/**
@@ -32,22 +85,29 @@ public class DbImage
}
}
- public static int insert( ImageData imageData )
+ /**
+ * Insert a new image into database
+ * @param imageData The metadata of the image
+ * @param ts The timestamp of inserting
+ * @return Affected rows
+ */
+ public static int insert( ImageData imageData, long ts, String ftpUser )
{
Date createTime = new Date( imageData.imageCreateTime );
Date updateTime = new Date( imageData.imageUpdateTime );
+ Date timestamp = new Date( ts );
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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+ "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, timestamp, ftpUser, fileSize) 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 );
+ imageData.imageLongDescription, sdf.format( timestamp ), ftpUser, imageData.fileSize );
}
public String getUUID()
@@ -60,8 +120,13 @@ public class DbImage
return MySQL.update( "UPDATE images SET images.image_path = ? WHERE images.UUID = ?", location, imageData.uuid );
}
- public static int delete( ImageData imageData )
+ public static int delete( String UUID )
+ {
+ return MySQL.update( "DELETE FROM images WHERE images.UUID=?", UUID );
+ }
+
+ public static List<DbImage> getUploadingImages()
{
- return MySQL.update( "DELETE FROM images WHERE images.UUID=?", imageData.uuid );
+ return MySQL.findAll( DbImage.class, "SELECT images.UUID, images.image_version, images.image_name, images.image_path, images.image_createTime, images.image_updateTime, images.image_owner, images.content_operatingSystem, images.status_isValid, images.status_isDeleted, images.image_shortDescription, images.image_longDescription, images.timestamp, images.ftpUser, images.fileSize FROM images WHERE image_path = ?", "!uploading!" );
}
}
diff --git a/src/main/java/org/openslx/imagemaster/db/DbUser.java b/src/main/java/org/openslx/imagemaster/db/DbUser.java
index 45cbb2a..8cf4b55 100644
--- a/src/main/java/org/openslx/imagemaster/db/DbUser.java
+++ b/src/main/java/org/openslx/imagemaster/db/DbUser.java
@@ -55,11 +55,20 @@ 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 = ? LIMIT 1", username );
- if ( user != null ) {
- return user.userId;
- } else {
- return 0;
- }
+ if ( user == null ) return 0;
+ return user.userId;
+ }
+
+ public static String getUserNameById( int id )
+ {
+ 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.userid = ? LIMIT 1", id );
+ if (user == null) return "";
+ return user.username;
}
}
diff --git a/src/main/java/org/openslx/imagemaster/db/LdapUser.java b/src/main/java/org/openslx/imagemaster/db/LdapUser.java
index 08eb127..deb30f3 100644
--- a/src/main/java/org/openslx/imagemaster/db/LdapUser.java
+++ b/src/main/java/org/openslx/imagemaster/db/LdapUser.java
@@ -80,7 +80,6 @@ public class LdapUser extends User
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() );
@@ -93,7 +92,8 @@ public class LdapUser extends User
// bind connection
try {
if ( connection.connect() ) {
- String name = Globals.getPropertyString( PropString.LDAPBINDQUERY ).replace( "%", username );
+ String name = Globals.getPropertyString( PropString.LDAPBINDQUERY ).replace( "%", login ).replace( "@", "\\40" );
+ log.info( "Bind with: " + name );
connection.bind( name, password );
}
} catch ( LdapException e1 ) {
@@ -124,11 +124,11 @@ public class LdapUser extends User
// make search query
try {
EntryCursor cursor = connection.search( Globals.getPropertyString( Globals.PropString.LDAPSEARCHBASEDN ),
- Globals.getPropertyString( Globals.PropString.LDAPSEARCHFILTER ).replace( "%", username ), SearchScope.SUBTREE );
+ Globals.getPropertyString( Globals.PropString.LDAPSEARCHFILTER ).replace( "%", login ), SearchScope.SUBTREE );
// only use the first result
cursor.next();
Entry entry = cursor.get();
- username = entry.get( "uid" ).getString();
+ username = entry.get( "cn" ).getString();
organization = "Test Organization"; // will be filled with bwIDM LDAP server
firstName = entry.get( "givenName" ).getString();
lastName = entry.get( "sn" ).getString();
diff --git a/src/main/java/org/openslx/imagemaster/ftp/FtpCredentialsScheduler.java b/src/main/java/org/openslx/imagemaster/ftp/FtpCredentialsScheduler.java
index 62d16dc..8cd72df 100644
--- a/src/main/java/org/openslx/imagemaster/ftp/FtpCredentialsScheduler.java
+++ b/src/main/java/org/openslx/imagemaster/ftp/FtpCredentialsScheduler.java
@@ -9,50 +9,49 @@ import java.util.TimerTask;
import org.apache.log4j.Logger;
import org.openslx.imagemaster.App;
import org.openslx.imagemaster.Globals;
-import org.openslx.imagemaster.thrift.iface.FtpCredentials;
import org.openslx.imagemaster.util.Util;
public class FtpCredentialsScheduler extends TimerTask
{
+
private static Logger log = Logger.getLogger( FtpCredentialsScheduler.class );
public static final long timeout = Long.valueOf( Globals.getPropertyInt( Globals.PropInt.FTPTIMEOUT ) ) * 60L * 1000L; // timeout in ms
-
- static {
- log.info( "timeout is:" + timeout );
- }
@Override
public void run()
{
- // check all folders
- for ( Map.Entry<String, FtpCredentials> entry : App.ftpServer.users.entrySet() ) {
- if (entry == null) continue;
- String sessionId = entry.getKey();
- String username = entry.getValue().username;
- File dir = new File( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + username );
- if ( !dir.exists() )
- continue;
- File[] list = dir.listFiles();
- if ( list.length == 1 ) {
- if ( ( new Date().getTime() - list[0].lastModified() ) >= timeout ) {
- log.info( username + "'s files are too old. Deleting him and his folder." );
- Util.deleteFolder( dir );
- App.ftpServer.removeUser( sessionId );
- ImageProcessor.removeImageFromProcessList( username );
- }
- } else if ( list.length > 1 ) {
- log.info( username + " uploaded too many files. Deleting his account and his folder." );
- Util.deleteFolder( dir );
- App.ftpServer.removeUser( sessionId );
- ImageProcessor.removeImageFromProcessList( username );
- } else {
- // check the creation time of the user
- if ( ( System.currentTimeMillis() - App.ftpServer.timeouts.get( username ) ) >= timeout ) {
- log.info( username + " did nothing for too long. Deleting him and his folder" );
+ synchronized ( App.ftpServer.users ) {
+ // check all folders
+ for ( Map.Entry<String, Long> entry : App.ftpServer.users.entrySet() ) {
+ if ( entry == null )
+ continue;
+ String username = entry.getKey();
+ File dir = new File( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + username );
+ if ( !dir.exists() )
+ continue;
+ File[] list = dir.listFiles();
+ if ( list.length == 1 ) {
+ if ( ( new Date().getTime() - list[0].lastModified() ) >= timeout ) {
+ // TODO: test if his file is complete by checking the file size
+ log.info( username + "'s files are too old. Deleting him and his folder." );
+ Util.deleteFolder( dir );
+ App.ftpServer.removeUser( username );
+ ImageProcessor.removeImageFromProcessList( username );
+ }
+ } else if ( list.length > 1 ) {
+ log.info( "User '" + username + "' uploaded too many files. Deleting his account and his folder." );
Util.deleteFolder( dir );
- App.ftpServer.removeUser( sessionId );
+ App.ftpServer.removeUser( username );
ImageProcessor.removeImageFromProcessList( username );
+ } else {
+ // check the creation time of the user
+ if ( ( System.currentTimeMillis() - App.ftpServer.users.get( username ) ) >= timeout ) {
+ log.info( username + " did nothing for too long. Deleting him and his folder" );
+ Util.deleteFolder( dir );
+ App.ftpServer.removeUser( username );
+ ImageProcessor.removeImageFromProcessList( username );
+ }
}
}
}
diff --git a/src/main/java/org/openslx/imagemaster/ftp/ImageProcessor.java b/src/main/java/org/openslx/imagemaster/ftp/ImageProcessor.java
index 3169aec..b7e96a4 100644
--- a/src/main/java/org/openslx/imagemaster/ftp/ImageProcessor.java
+++ b/src/main/java/org/openslx/imagemaster/ftp/ImageProcessor.java
@@ -2,9 +2,13 @@ package org.openslx.imagemaster.ftp;
import java.io.File;
import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
import org.apache.log4j.Logger;
+import org.openslx.imagemaster.App;
import org.openslx.imagemaster.Globals;
+import org.openslx.imagemaster.Globals.PropInt;
import org.openslx.imagemaster.db.DbImage;
import org.openslx.imagemaster.thrift.iface.ImageData;
@@ -12,10 +16,43 @@ public class ImageProcessor
{
private static Logger log = Logger.getLogger( ImageProcessor.class );
+ // key: ftpUser, value: imageData
private static HashMap<String, ImageData> images = new HashMap<>();
+
+ /**
+ * After (re)starting server: check dead !uploading! images in database
+ */
+ public static void checkUploading() {
+ long timeout = Globals.getPropertyInt( PropInt.FTPTIMEOUT ) * 60L * 1000L;
+ List<DbImage> uploadingImages = DbImage.getUploadingImages();
+
+ log.info( uploadingImages.size() + " image(s) are uploading" );
+
+ if (uploadingImages.isEmpty()) return;
+
+ Iterator<DbImage> iter = uploadingImages.iterator();
+ while (iter.hasNext()) {
+ DbImage dbImage = iter.next();
+ if (System.currentTimeMillis() - dbImage.timestamp.getTime() >= timeout) {
+ DbImage.delete( dbImage.UUID );
+ log.info( "Deleted dbimage from db: " + dbImage.UUID + " due to timeout");
+ } else {
+ // add ftpUser to list
+ synchronized ( App.ftpServer.users ) {
+ App.ftpServer.users.put( dbImage.ftpUser, dbImage.timestamp.getTime() );
+ }
+ log.info( "Added user '" + dbImage.ftpUser + "' to list again." );
+
+ // add image to process list again
+ ImageData imageData = new ImageData( dbImage.UUID, dbImage.imageVersion, dbImage.imageName, dbImage.imageCreateTime.getTime(), dbImage.imageUpdateTime.getTime(), dbImage.imageOwner, dbImage.contentOperatingSystem, dbImage.isValid, dbImage.isDeleted, dbImage.shortDescription, dbImage.longDescription, dbImage.fileSize );
+ images.put( dbImage.ftpUser, imageData );
+ log.info( "Added image to processlist again: " + dbImage.UUID );
+ }
+ }
+ }
/**
- * Processes an image after upload
+ * Process 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)
@@ -32,7 +69,7 @@ public class ImageProcessor
// 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;
+ String newFileName = Globals.getPropertyString( Globals.PropString.IMAGEDIR ) + "/" + images.get( username ).uuid + ".vmdk";
File imageFile = new File( oldFileName );
@@ -70,7 +107,8 @@ public class ImageProcessor
if ( imageData.uuid.isEmpty() || imageData.imageName.isEmpty()
|| imageData.imageOwner.isEmpty() || imageData.conentOperatingSystem.isEmpty()
|| imageData.imageShortDescription.isEmpty()
- || imageData.imageLongDescription.isEmpty() ) {
+ || imageData.imageLongDescription.isEmpty()
+ || imageData.fileSize == 0) {
return false;
}
@@ -81,7 +119,7 @@ public class ImageProcessor
}
// if everything went fine, add image to db
- DbImage.insert( imageData );
+ DbImage.insert( imageData, System.currentTimeMillis(), username);
// and to processinglist
images.put( username, imageData );
@@ -99,7 +137,7 @@ public class ImageProcessor
return false;
}
- DbImage.delete( images.get( username ));
+ DbImage.delete( images.get( username ).uuid );
images.remove( username );
log.info( "Deleted image from process list and db. (User: " + username + ")" );
diff --git a/src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java b/src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java
index 19671e3..e3514f6 100644
--- a/src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java
+++ b/src/main/java/org/openslx/imagemaster/ftp/MasterFtpServer.java
@@ -1,8 +1,6 @@
package org.openslx.imagemaster.ftp;
import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@@ -34,10 +32,8 @@ public class MasterFtpServer implements Runnable
private FtpServer server;
private UserManager userManager;
private Listener listener;
- // key: serverSessionId, value: FtpCredentials
- public final HashMap<String, FtpCredentials> users = new HashMap<>();
// key: ftpUsername, value: createTime
- public final HashMap<String, Long> timeouts = new HashMap<>();
+ public final HashMap<String, Long> users = new HashMap<>();
private boolean ini = false;
public void init( int port )
@@ -66,15 +62,6 @@ public class MasterFtpServer implements Runnable
// create user manager
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
File userFile = new File( "src/main/properties/ftp.properties" );
- // clear user file to avoid dead users
- PrintWriter writer = null;
- try {
- writer = new PrintWriter(userFile);
- } catch ( FileNotFoundException e ) {
- } finally {
- writer.print("");
- writer.close();
- }
userManagerFactory.setFile( userFile );
userManagerFactory.setPasswordEncryptor( new SaltedPasswordEncryptor() );
userManager = userManagerFactory.createUserManager();
@@ -116,8 +103,9 @@ public class MasterFtpServer implements Runnable
try {
userManager.save( user );
ftpCredentials = new FtpCredentials( generatedUser, generatedPass );
- users.put( serverSessionId, ftpCredentials );
- timeouts.put( ftpCredentials.username, System.currentTimeMillis());
+ synchronized ( users ) {
+ users.put( ftpCredentials.username, System.currentTimeMillis());
+ }
} catch ( FtpException e ) {
}
@@ -127,43 +115,38 @@ public class MasterFtpServer implements Runnable
return ftpCredentials;
}
- public boolean removeUser( final String serverSessionId )
+ public boolean removeUser( final String username )
{
- if ( !users.containsKey( serverSessionId ) )
+ if ( !users.containsKey( username ) )
return false;
try {
- log.info( "Closing session" );
// first find active session and close it
Iterator<FtpIoSession> iter = listener.getActiveSessions().iterator();
while (iter.hasNext()) {
FtpIoSession session = (FtpIoSession) iter.next();
if (session.getUser() == null) continue;
- if (session.getUser().getName() == users.get( serverSessionId ).username) {
+ if (session.getUser().getName() == username) {
session.close();
}
}
- log.info( "Deleting user" );
// afterwards delete user
- userManager.delete( users.get( serverSessionId ).username );
- log.info( "removing user from timeout list" );
- // remove user from both maps
- timeouts.remove( users.remove( serverSessionId ).username );
+ userManager.delete( username );
+ // remove user from map (cache)
+ synchronized ( users ) {
+ users.remove( username );
+ }
return true;
} catch ( FtpException e ) {
return false;
}
}
- public FtpCredentials getCredentialsFromSessionId( String serverSessionId )
- {
- return users.get( serverSessionId );
- }
-
@Override
public void run()
{
try {
+ //if (!ini) throw new Exception("FTP server needs to be initalized.");
log.info( "Starting FTP Sever" );
server.start();
} catch ( FtpException e1 ) {
diff --git a/src/main/java/org/openslx/imagemaster/ftp/MasterFtplet.java b/src/main/java/org/openslx/imagemaster/ftp/MasterFtplet.java
index 0e113fe..5e12628 100644
--- a/src/main/java/org/openslx/imagemaster/ftp/MasterFtplet.java
+++ b/src/main/java/org/openslx/imagemaster/ftp/MasterFtplet.java
@@ -49,7 +49,9 @@ public class MasterFtplet implements Ftplet
public FtpletResult onConnect( FtpSession session ) throws FtpException,
IOException
{
- log.info( session.getUser().getName() + " connected" );
+ if (session.getUser() != null) {
+ log.info( session.getUser().getName() + " connected" );
+ }
return null;
}
diff --git a/src/main/java/org/openslx/imagemaster/server/ApiServer.java b/src/main/java/org/openslx/imagemaster/server/ApiServer.java
index 31bb712..4080f75 100644
--- a/src/main/java/org/openslx/imagemaster/server/ApiServer.java
+++ b/src/main/java/org/openslx/imagemaster/server/ApiServer.java
@@ -167,35 +167,32 @@ public class ApiServer
/**
* Tell the masterserver that the image upload finished.
*
- * @param serverSessionId The session id of the hs/uni server
+ * @param ftpUser the user that was used to upload
* @param imageDescription the description of the uploaded image
* @return if nothing went wrong
* @throws AuthorizationException if no valid session exists
*/
- public static boolean finishedUpload( String serverSessionId,
- ImageData imageDescription ) throws AuthorizationException
+ public static boolean finishedUpload( String ftpUser, ImageData imageDescription )
{
- // check if valid session exists
- if ( ServerSessionManager.getSession( serverSessionId ) == null ) {
- throw new AuthorizationException( AuthorizationError.NOT_AUTHENTICATED, "No valid serverSessionData" );
+ // check if user is valid
+ synchronized ( App.ftpServer.users ) {
+ if (!App.ftpServer.users.containsKey( ftpUser )) return false;
}
-
+
// process the image
- String username = App.ftpServer.getCredentialsFromSessionId( serverSessionId ).username;
-
- File userDirectory = new File( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + username );
+ File userDirectory = new File( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + ftpUser );
File[] list = userDirectory.listFiles();
if ( list.length != 1 )
return false;
- log.info( username + " is done with upload" );
+ log.info( ftpUser + " is done with upload" );
- ImageProcessor.processImageAfterUpload( username, list[0].getName() );
+ ImageProcessor.processImageAfterUpload( ftpUser, list[0].getName() );
// remove user that is not needed anymore
- App.ftpServer.removeUser( serverSessionId );
- log.info( "Removed user: " + username );
+ App.ftpServer.removeUser( ftpUser );
+ log.info( "Removed user: " + ftpUser );
return true;
}
diff --git a/src/main/java/org/openslx/imagemaster/serversession/ServerAuthenticator.java b/src/main/java/org/openslx/imagemaster/serversession/ServerAuthenticator.java
index 4f67415..8a8e426 100644
--- a/src/main/java/org/openslx/imagemaster/serversession/ServerAuthenticator.java
+++ b/src/main/java/org/openslx/imagemaster/serversession/ServerAuthenticator.java
@@ -68,7 +68,6 @@ public class ServerAuthenticator
throws AuthenticationException, TException
{
byte[] bytes = challengeResponse.array();
- log.info( "Response was: " + challengeResponse + " with length: " + bytes.length );
boolean result = false;
diff --git a/src/main/java/org/openslx/imagemaster/thrift/server/ImageServerHandler.java b/src/main/java/org/openslx/imagemaster/thrift/server/ImageServerHandler.java
index e468d46..d6bd59e 100644
--- a/src/main/java/org/openslx/imagemaster/thrift/server/ImageServerHandler.java
+++ b/src/main/java/org/openslx/imagemaster/thrift/server/ImageServerHandler.java
@@ -17,6 +17,7 @@ import org.openslx.imagemaster.thrift.iface.UserInfo;
public class ImageServerHandler implements ImageServer.Iface
{
+
private static Logger log = Logger.getLogger( ImageServerHandler.class );
@Override
@@ -65,10 +66,9 @@ public class ImageServerHandler implements ImageServer.Iface
}
@Override
- public boolean finshedUpload( String serverSessionId,
- ImageData imageDescription ) throws AuthorizationException
+ public boolean finshedUpload( String ftpUser, ImageData imageDescription )
{
- return ApiServer.finishedUpload( serverSessionId, imageDescription );
+ return ApiServer.finishedUpload( ftpUser, imageDescription );
}
}
diff --git a/src/main/thrift/imagemaster.thrift b/src/main/thrift/imagemaster.thrift
index 5db7b05..3171416 100644
--- a/src/main/thrift/imagemaster.thrift
+++ b/src/main/thrift/imagemaster.thrift
@@ -80,7 +80,8 @@ struct ImageData {
8: bool statusIsValid,
9: bool statusIsDeleted,
10: string imageShortDescription,
- 11: string imageLongDescription
+ 11: string imageLongDescription,
+ 12: i64 fileSize
}
service ImageServer {
@@ -97,7 +98,7 @@ service ImageServer {
FtpCredentials submitImage(1:string serverSessionId, 2:ImageData imageDescription) throws (1:AuthorizationException failure),
- bool finshedUpload(1:string serverSessionId, 2:ImageData imageDescription) throws (1:AuthorizationException failure)
+ bool finshedUpload(1:string ftpUser, 2:ImageData imageDescription)
}
diff --git a/src/test/java/org/openslx/imagemaster/ServerTest.java b/src/test/java/org/openslx/imagemaster/ServerTest.java
index bb81432..de3bc04 100644
--- a/src/test/java/org/openslx/imagemaster/ServerTest.java
+++ b/src/test/java/org/openslx/imagemaster/ServerTest.java
@@ -1,12 +1,15 @@
package org.openslx.imagemaster;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.SocketException;
import java.nio.ByteBuffer;
+import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyStore;
import java.security.KeyStoreException;
@@ -14,18 +17,18 @@ import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
-import java.security.cert.X509Certificate;
-import java.util.Date;
import java.util.UUID;
+import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
-import javax.net.ssl.X509TrustManager;
+
+import junit.framework.TestCase;
import org.apache.commons.net.ftp.FTP;
-import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
+import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
@@ -33,17 +36,17 @@ 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.Client;
import org.openslx.imagemaster.thrift.iface.ServerSessionData;
import org.openslx.imagemaster.thrift.iface.SessionData;
import org.openslx.imagemaster.thrift.iface.UserInfo;
-import org.openslx.imagemaster.thrift.iface.ImageServer.Client;
import org.openslx.imagemaster.util.AsymMessageSign;
-import junit.framework.TestCase;
-
public class ServerTest extends TestCase
{
+ private static Logger log = Logger.getLogger( ServerTest.class );
+
@Override
public void setUp() throws Exception {
// start the server
@@ -63,8 +66,9 @@ public class ServerTest extends TestCase
* Test the authentication
*
* @throws TException
+ * @throws IOException
*/
- public void testAuthentication() throws TException
+ public void testAuthentication() throws TException, IOException
{
TTransport transport = new TSocket( "localhost", 9090 );
transport.open();
@@ -73,8 +77,14 @@ public class ServerTest extends TestCase
Client client = new Client( protocol );
assertTrue( "Could not ping server", client.ping() );
+
+ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
+ System.out.print("Enter username: ");
+ String username = reader.readLine();
+ System.out.print("Enter password: ");
+ String password = reader.readLine();
- SessionData sessionData = client.authenticate( "ns202", "xxxxxxxxxxxx" );
+ SessionData sessionData = client.authenticate( username, password );
UserInfo userInfo = client.getUserFromToken( sessionData.getAuthToken() );
System.out.println( "User info: " + userInfo );
System.out.println( "Server address from MySQL: " + sessionData.serverAddress );
@@ -92,8 +102,9 @@ public class ServerTest extends TestCase
* @throws UnrecoverableKeyException
* @throws SignatureException
* @throws InvalidKeyException
+ * @throws InvalidAlgorithmParameterException
*/
- public void testServerAuthAndFtpUpload() throws TException, SocketException, IOException, UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException, KeyStoreException, InvalidKeyException, SignatureException
+ public void testServerAuthAndFtpUpload() throws TException, SocketException, IOException, UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException, KeyStoreException, InvalidKeyException, SignatureException, InvalidAlgorithmParameterException
{
TTransport transport = new TSocket( "localhost", 9090 );
transport.open();
@@ -127,19 +138,21 @@ public class ServerTest extends TestCase
boolean statusIsDeleted = false;
String imageShortDescrption = "EIN SUPER TOLLES IMAGE!";
String imageLongDescription = "Lorem ipsum dolor sit amet.";
+
+ String fileName = "/home/nils/file_to_upload.bin";
ImageData imageData = new ImageData( uuid.toString(), version, imageName,
imageCreateTime, imageUpdateTime, imageOwner, contentOperatingSystem,
- statusIsValid, statusIsDeleted, imageShortDescrption, imageLongDescription );
+ statusIsValid, statusIsDeleted, imageShortDescrption, imageLongDescription, new File(fileName).getTotalSpace() );
System.out.println( "Created imageData..." );
FtpCredentials ftpCredentials = client.submitImage( data.sessionId, imageData );
System.out.println( "Got FTP credentials. User: " + ftpCredentials.username + ", password: " + ftpCredentials.password );
- FTPSClient FtpClient = new FTPSClient( "SSL" );
+ FTPSClient FtpClient = new FTPSClient( "SSL", true );
System.out.println("Created new ftpsclient...");
- TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( "JKS" );
+ TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm() );
KeyStore keystore = KeyStore.getInstance( "JKS" );
keystore.load( new FileInputStream( new File( "./config/keystore.jks" ) ), "password".toCharArray() );
System.out.println("Loaded keystore..");
@@ -153,18 +166,15 @@ public class ServerTest extends TestCase
int port = 2221;
String user = ftpCredentials.username;
String password = ftpCredentials.password;
- String fileName = "/home/nils/file_to_upload.bin";
try {
FtpClient.connect( host, port );
System.out.println( "Connected to " + host + ":" + port + ". Reply code: " + FtpClient.getReplyCode() );
if ( !FTPReply.isPositiveCompletion( FtpClient.getReplyCode() ) ) {
- ConnectException ce = new ConnectException( "No positive reply code." );
- throw ce;
+ throw new ConnectException( "No positive reply code." );
}
if ( !FtpClient.login( user, password ) ) {
- ConnectException ce = new ConnectException( "Could not login." );
- throw ce;
+ throw new ConnectException( "Could not login." );
}
System.out.println( "Logged in with user: " + user );
FtpClient.setFileType( FTP.BINARY_FILE_TYPE );
@@ -175,12 +185,15 @@ public class ServerTest extends TestCase
FtpClient.storeFile( "xcvb.vmdk", input );
System.out.println( "done." );
FtpClient.noop();
- client.finshedUpload( data.sessionId, imageData );
+ } catch (Exception e) {
+ e.printStackTrace();
} finally {
if ( FtpClient.isConnected() ) {
try {
FtpClient.logout();
FtpClient.disconnect();
+ boolean result = client.finshedUpload( ftpCredentials.username, imageData );
+ System.out.println("Telling server that upload finished: " + result);
} catch ( IOException e ) {
e.printStackTrace();
}