summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/server/ApiServer.java
diff options
context:
space:
mode:
authorNils Schwabe2014-06-30 17:11:03 +0200
committerNils Schwabe2014-06-30 17:11:03 +0200
commit1a3dbab6ca7118f4ca9f61043f416f074ede13bc (patch)
treefad14555be544c3ba2afdf31b8f315364a67e7a6 /src/main/java/org/openslx/imagemaster/server/ApiServer.java
parent[Webinterface] Add "images" tab (diff)
downloadmasterserver-1a3dbab6ca7118f4ca9f61043f416f074ede13bc.tar.gz
masterserver-1a3dbab6ca7118f4ca9f61043f416f074ede13bc.tar.xz
masterserver-1a3dbab6ca7118f4ca9f61043f416f074ede13bc.zip
Add implementation for the new up- and download protocoll
Remove some old stuff that is not needed anymore Fix some small bugs
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/server/ApiServer.java')
-rw-r--r--src/main/java/org/openslx/imagemaster/server/ApiServer.java247
1 files changed, 140 insertions, 107 deletions
diff --git a/src/main/java/org/openslx/imagemaster/server/ApiServer.java b/src/main/java/org/openslx/imagemaster/server/ApiServer.java
index 52783d9..9c3dfb4 100644
--- a/src/main/java/org/openslx/imagemaster/server/ApiServer.java
+++ b/src/main/java/org/openslx/imagemaster/server/ApiServer.java
@@ -1,16 +1,12 @@
package org.openslx.imagemaster.server;
-import java.io.File;
import java.nio.ByteBuffer;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
-import org.openslx.imagemaster.App;
-import org.openslx.imagemaster.Globals;
import org.openslx.imagemaster.db.DbImage;
import org.openslx.imagemaster.db.DbSatellite;
-import org.openslx.imagemaster.ftp.ImageProcessor;
-import org.openslx.imagemaster.ftp.MasterFtpServer;
+import org.openslx.imagemaster.serverconnection.ImageProcessor;
import org.openslx.imagemaster.serversession.ServerAuthenticator;
import org.openslx.imagemaster.serversession.ServerSession;
import org.openslx.imagemaster.serversession.ServerSessionManager;
@@ -23,7 +19,7 @@ import org.openslx.imagemaster.thrift.iface.AuthenticationError;
import org.openslx.imagemaster.thrift.iface.AuthenticationException;
import org.openslx.imagemaster.thrift.iface.AuthorizationError;
import org.openslx.imagemaster.thrift.iface.AuthorizationException;
-import org.openslx.imagemaster.thrift.iface.FtpCredentials;
+import org.openslx.imagemaster.thrift.iface.DownloadInfos;
import org.openslx.imagemaster.thrift.iface.ImageData;
import org.openslx.imagemaster.thrift.iface.ImageDataError;
import org.openslx.imagemaster.thrift.iface.ImageDataException;
@@ -32,7 +28,10 @@ import org.openslx.imagemaster.thrift.iface.ServerAuthenticationError;
import org.openslx.imagemaster.thrift.iface.ServerAuthenticationException;
import org.openslx.imagemaster.thrift.iface.ServerSessionData;
import org.openslx.imagemaster.thrift.iface.SessionData;
+import org.openslx.imagemaster.thrift.iface.UploadException;
+import org.openslx.imagemaster.thrift.iface.UploadInfos;
import org.openslx.imagemaster.thrift.iface.UserInfo;
+import org.openslx.imagemaster.util.RandomString;
/**
* API Server This is where all the requests from the outside arrive. We don't
@@ -88,40 +87,74 @@ public class ApiServer
session.getLastName(), session.getEMail() );
}
- /**
- * Request ftp credentials to upload a new image to the masterserver.
- *
- * @param imageDescription MetaData of the new image
- * @param serverSessionData the session data of the authenticated uni/hs server
- * @return the genereated ftp credentials
- * @throws AuthorizationException if the uni/hs server has no valid session
- * @throws TException
- */
- public static FtpCredentials submitImage( String serverSessionId,
- ImageData imageDescription ) throws AuthorizationException, ImageDataException
+// /**
+// * Request ftp credentials to upload a new image to the masterserver.
+// *
+// * @param imageDescription MetaData of the new image
+// * @param serverSessionData the session data of the authenticated uni/hs server
+// * @return the genereated ftp credentials
+// * @throws AuthorizationException if the uni/hs server has no valid session
+// * @throws TException
+// */
+// public static FtpCredentials submitImage( String serverSessionId,
+// ImageData imageDescription ) throws AuthorizationException, ImageDataException
+// {
+// if ( ServerSessionManager.getSession( serverSessionId ) == null ) {
+// throw new AuthorizationException( AuthorizationError.NOT_AUTHENTICATED, "No valid serverSessionData" );
+// }
+//
+// // create new user
+// FtpCredentials ftpCredentials = App.ftpServer.addUser( serverSessionId , MasterFtpServer.Mode.UPLOADING, "");
+//
+// if ( ftpCredentials == null ) {
+// log.error( "Could not create ftp credentials" );
+// return null;
+// }
+//
+// try {
+// ImageProcessor.addImageDataToProcess( imageDescription, ftpCredentials.username, ftpCredentials.password );
+// } catch (ImageDataException e) {
+// App.ftpServer.removeUser( serverSessionId );
+// throw new ImageDataException( ImageDataError.INVALID_DATA, e.getMessage() );
+// }
+//
+// return ftpCredentials;
+// }
+
+ public static UploadInfos submitImage( String serverSessionId, ImageData imageDescription ) throws AuthorizationException, ImageDataException, UploadException
{
+ // first check session of server
if ( ServerSessionManager.getSession( serverSessionId ) == null ) {
- throw new AuthorizationException( AuthorizationError.NOT_AUTHENTICATED, "No valid serverSessionData" );
- }
-
- // create new user
- FtpCredentials ftpCredentials = App.ftpServer.addUser( serverSessionId , MasterFtpServer.Mode.UPLOADING, "");
-
- if ( ftpCredentials == null ) {
- log.error( "Could not create ftp credentials" );
- return null;
- }
-
- try {
- ImageProcessor.addImageDataToProcess( imageDescription, ftpCredentials.username, ftpCredentials.password );
- } catch (ImageDataException e) {
- App.ftpServer.removeUser( serverSessionId );
- throw new ImageDataException( ImageDataError.INVALID_DATA, e.getMessage() );
+ throw new AuthorizationException( AuthorizationError.NOT_AUTHENTICATED, "No valid serverSessioId" );
+ }
+ // then let the image processor decide what to do
+ return ImageProcessor.getUploadInfos(serverSessionId, imageDescription);
+ }
+
+ public static DownloadInfos getImage( String uuid, String serverSessionId ) throws AuthorizationException, ImageDataException
+ {
+ /*
+ * TODO: done
+ * Check if server has active session
+ * Check if UUID exists
+ * Create token for downloading
+ */
+
+ // first check session of server
+ if ( ServerSessionManager.getSession( serverSessionId ) == null ) {
+ throw new AuthorizationException( AuthorizationError.NOT_AUTHENTICATED, "No valid serverSessionId" );
+ }
+
+ if (!DbImage.exists( uuid )) {
+ throw new ImageDataException( ImageDataError.UNKNOWN_IMAGE, "UUID is not known by this server." );
}
-
- return ftpCredentials;
+
+ String path = DbImage.getImageByUUID( uuid ).imagePath;
+
+ // TODO: get token form downloader class
+ return new DownloadInfos( RandomString.generate( 100, false ) );
}
-
+
/**
* Start the server authentication of a uni/hs satellite server.
*
@@ -169,78 +202,78 @@ public class ApiServer
return ServerSessionManager.addSession( session );
}
- /**
- * Tell the masterserver that the image upload finished.
- *
- * @param ftpUser the user that was used to upload
- * @param imageDescription the description of the uploaded image
- * @return if nothing went wrong
- * @throws ImageDataException if image was not submitted before
- * @throws AuthorizationException if no valid session exists
- */
- public static boolean finishedUpload( String ftpUser, ImageData imageDescription ) throws ImageDataException
- {
- // check if user is valid
- synchronized ( App.ftpServer.users ) {
- if (!App.ftpServer.users.containsKey( ftpUser )) {
- throw new ImageDataException( ImageDataError.UNKNOWN_IMAGE, "Image with this data was not submitted before." );
- } else {
- if ( App.ftpServer.users.get( ftpUser ).getMode() == MasterFtpServer.Mode.DOWNLOADING ) {
- throw new ImageDataException( ImageDataError.UNKNOWN_IMAGE, "Your were downloading and not uploading." );
- }
- }
- }
-
- // process the image
- File userDirectory = new File( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + ftpUser );
- File[] list = userDirectory.listFiles();
-
- if ( list == null || list.length != 1 ) {
- // user uploaded too many files
- return false;
- }
-
- log.info( ftpUser + " is done with upload" );
-
- ImageProcessor.processImageAfterUpload( ftpUser, list[0].getName() );
-
- // remove user that is not needed anymore
- App.ftpServer.removeUser( ftpUser );
- log.info( "Removed user: " + ftpUser );
+// /**
+// * Tell the masterserver that the image upload finished.
+// *
+// * @param ftpUser the user that was used to upload
+// * @param imageDescription the description of the uploaded image
+// * @return if nothing went wrong
+// * @throws ImageDataException if image was not submitted before
+// * @throws AuthorizationException if no valid session exists
+// */
+// public static boolean finishedUpload( String ftpUser, ImageData imageDescription ) throws ImageDataException
+// {
+// // check if user is valid
+// synchronized ( App.ftpServer.users ) {
+// if (!App.ftpServer.users.containsKey( ftpUser )) {
+// throw new ImageDataException( ImageDataError.UNKNOWN_IMAGE, "Image with this data was not submitted before." );
+// } else {
+// if ( App.ftpServer.users.get( ftpUser ).getMode() == MasterFtpServer.Mode.DOWNLOADING ) {
+// throw new ImageDataException( ImageDataError.UNKNOWN_IMAGE, "Your were downloading and not uploading." );
+// }
+// }
+// }
+//
+// // process the image
+// File userDirectory = new File( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + ftpUser );
+// File[] list = userDirectory.listFiles();
+//
+// if ( list == null || list.length != 1 ) {
+// // user uploaded too many files
+// return false;
+// }
+//
+// log.info( ftpUser + " is done with upload" );
+//
+// ImageProcessor.processImageAfterUpload( ftpUser, list[0].getName() );
+//
+// // remove user that is not needed anymore
+// App.ftpServer.removeUser( ftpUser );
+// log.info( "Removed user: " + ftpUser );
+//
+// return true;
+// }
- return true;
- }
-
- public static FtpCredentials getImage( String uuid, String serverSessionId ) throws AuthorizationException, ImageDataException
- {
- if ( ServerSessionManager.getSession( serverSessionId ) == null ) {
- throw new AuthorizationException( AuthorizationError.NOT_AUTHENTICATED, "No valid serverSessionData" );
- }
-
- // check if image exists
- DbImage image = DbImage.getImageByUUID( uuid );
-
- if (image == null) throw new ImageDataException( ImageDataError.UNKNOWN_IMAGE, "No image found with uuid '" + uuid + "'");
-
- FtpCredentials ftpCredentials = App.ftpServer.addUser( serverSessionId, MasterFtpServer.Mode.DOWNLOADING, new File(image.imagePath).getName() );
-
- // TODO: what is happening here?
- if (ftpCredentials == null) return null;
-
- return ftpCredentials;
- }
+// public static FtpCredentials getImage( String uuid, String serverSessionId ) throws AuthorizationException, ImageDataException
+// {
+// if ( ServerSessionManager.getSession( serverSessionId ) == null ) {
+// throw new AuthorizationException( AuthorizationError.NOT_AUTHENTICATED, "No valid serverSessionData" );
+// }
+//
+// // check if image exists
+// DbImage image = DbImage.getImageByUUID( uuid );
+//
+// if (image == null) throw new ImageDataException( ImageDataError.UNKNOWN_IMAGE, "No image found with uuid '" + uuid + "'");
+//
+// FtpCredentials ftpCredentials = App.ftpServer.addUser( serverSessionId, MasterFtpServer.Mode.DOWNLOADING, new File(image.imagePath).getName() );
+//
+// // TODO: what is happening here?
+// if (ftpCredentials == null) return null;
+//
+// return ftpCredentials;
+// }
- public static boolean finishedDownload( String ftpUser )
- {
- if ( !App.ftpServer.users.containsKey( ftpUser ))
- return false;
-
- log.info( "User: '" + ftpUser + "' finished download and gets deleted." );
-
- // download is done, user can be removed now
- App.ftpServer.removeUser( ftpUser );
-
- return true;
- }
+// public static boolean finishedDownload( String ftpUser )
+// {
+// if ( !App.ftpServer.users.containsKey( ftpUser ))
+// return false;
+//
+// log.info( "User: '" + ftpUser + "' finished download and gets deleted." );
+//
+// // download is done, user can be removed now
+// App.ftpServer.removeUser( ftpUser );
+//
+// return true;
+// }
}