summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/server/ApiServer.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/server/ApiServer.java')
-rw-r--r--src/main/java/org/openslx/imagemaster/server/ApiServer.java81
1 files changed, 56 insertions, 25 deletions
diff --git a/src/main/java/org/openslx/imagemaster/server/ApiServer.java b/src/main/java/org/openslx/imagemaster/server/ApiServer.java
index eda48eb..fc1d9b0 100644
--- a/src/main/java/org/openslx/imagemaster/server/ApiServer.java
+++ b/src/main/java/org/openslx/imagemaster/server/ApiServer.java
@@ -1,7 +1,9 @@
package org.openslx.imagemaster.server;
import java.io.File;
+import java.util.HashMap;
+import org.apache.ftpserver.ftplet.FtpException;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.openslx.imagemaster.Globals;
@@ -17,10 +19,13 @@ import org.openslx.imagemaster.session.SessionManager;
import org.openslx.imagemaster.session.User;
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.ImageData;
import org.openslx.imagemaster.thrift.iface.InvalidTokenException;
+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.UserInfo;
@@ -85,29 +90,22 @@ public class ApiServer {
* @throws AuthorizationException if the uni/hs server has no valid session
* @throws TException
*/
- public static FtpCredentials submitImage(ImageData imageDescription,
- ServerSessionData serverSessionData) throws AuthorizationException,
+ public static FtpCredentials submitImage(String serverSessionId,
+ ImageData imageDescription) throws AuthorizationException,
TException {
- if (ServerSessionManager.getSession(serverSessionData.sessionId) == null) {
- throw new AuthenticationException(AuthenticationError.GENERIC_ERROR, "No valid serverSessionData");
+ if (ServerSessionManager.getSession(serverSessionId) == null) {
+ throw new AuthorizationException(AuthorizationError.NOT_AUTHENTICATED, "No valid serverSessionData");
}
- String generatedUser = RandomString.generate(10, false);
- String generatedPass = RandomString.generate(16, true);
+ // create new user
+ FtpCredentials ftpCredentials = Globals.ftpServer.addUser(serverSessionId);
- if (!ImageProcessor.addImageDataToProcess(imageDescription, generatedUser)) {
+ if (!ImageProcessor.addImageDataToProcess(imageDescription, ftpCredentials.username)) {
+ Globals.ftpServer.removeUser(serverSessionId);
throw new TException("ImageData is not valid.");
}
-
- String dir = Globals.properties.getProperty("ftp_base_dir") + "/"
- + generatedUser + "/";
- if (new File(dir).mkdir()) {
- Globals.ftpServer.addUser(generatedUser, generatedPass, dir, true);
- log.info("Generated user/pass: " + generatedUser + "\t"
- + generatedPass + "\n with home dir: " + dir);
- }
-
- return new FtpCredentials(generatedUser, generatedPass);
+
+ return ftpCredentials;
}
/**
@@ -115,16 +113,15 @@ public class ApiServer {
* @param organization the organization that the server belongs to
* @return a random string that needs to be encrypted with the private
* key of the requesting satellite server
- * @throws TException
+ * @throws ServerAuthenticationException when organization is invalid/unknown
*/
public static String startServerAuthentication(String organization)
- throws TException {
- // TODO: Proper exceptions
+ throws ServerAuthenticationException {
if (organization == null || organization == "") {
- throw new TException("Empty organization");
+ throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_ORGANIZATION, "Empty organization");
}
if (DbSatellite.fromOrganization(organization) == null) {
- throw new TException("Unkown organization");
+ throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_ORGANIZATION, "Unknown organization");
}
return ServerAuthenticator.startServerAuthentication(organization);
}
@@ -140,13 +137,12 @@ public class ApiServer {
public static ServerSessionData serverAuthenticate(String organization,
String challengeResponse) throws AuthenticationException,
TException {
- // TODO: Proper exceptions
if (organization == null || challengeResponse == null) {
- throw new TException("Empty organization org challengeResponse");
+ throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_ORGANIZATION, "Empty organization or challengeResponse");
}
DbSatellite satellite = DbSatellite.fromOrganization(organization);
if (satellite == null) {
- throw new TException("Unkown organization");
+ throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_ORGANIZATION, "Unknown organization");
}
final ServerUser serverUser = ServerAuthenticator.serverAuthenticate(
organization, satellite.getAddress(), challengeResponse);
@@ -155,4 +151,39 @@ public class ApiServer {
return ServerSessionManager.addSession(session);
}
+ /**
+ * Tell the masterserver that the image upload finished.
+ * @param serverSessionId The session id of the hs/uni server
+ * @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 {
+ // check if valid session exists
+ if (ServerSessionManager.getSession(serverSessionId) == null) {
+ throw new AuthorizationException(AuthorizationError.NOT_AUTHENTICATED, "No valid serverSessionData");
+ }
+
+ // process the image
+ String username = Globals.ftpServer.getCredentialsFromSessionId(serverSessionId).username;
+
+ File userDirectory = new File(Globals.properties.getProperty(Globals.ftpBaseDir) + "/" + username);
+ File[] list = userDirectory.listFiles();
+
+ if (list.length != 1) return false;
+
+ log.info(username + " is done with upload");
+
+ // remove user that is not needed anymore
+ Globals.ftpServer.removeUser(username);
+ log.info("Removed user: " + username);
+
+ ImageProcessor.processImageAfterUpload(username, list[0].getName());
+
+ Globals.ftpServer.removeUser(serverSessionId);
+
+ return true;
+ }
+
}