diff options
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/server/ApiServer.java')
-rw-r--r-- | src/main/java/org/openslx/imagemaster/server/ApiServer.java | 152 |
1 files changed, 82 insertions, 70 deletions
diff --git a/src/main/java/org/openslx/imagemaster/server/ApiServer.java b/src/main/java/org/openslx/imagemaster/server/ApiServer.java index f4511fe..30fb0a8 100644 --- a/src/main/java/org/openslx/imagemaster/server/ApiServer.java +++ b/src/main/java/org/openslx/imagemaster/server/ApiServer.java @@ -39,8 +39,9 @@ import org.openslx.imagemaster.thrift.iface.UserInfo; * This will be accessed from multiple threads, so use synchronization when * needed (or in doubt) */ -public class ApiServer { - private static Logger log = Logger.getLogger(ApiServer.class); +public class ApiServer +{ + private static Logger log = Logger.getLogger( ApiServer.class ); /** * Request for authentication @@ -50,17 +51,18 @@ public class ApiServer { * @return SessionData struct with session id/token iff login successful * @throws AuthenticationException if login not successful */ - public static SessionData authenticate(String login, String password) - throws AuthenticationException { - if (login == null || password == null) { + public static SessionData authenticate( String login, String password ) + throws AuthenticationException + { + if ( login == null || password == null ) { throw new AuthenticationException( AuthenticationError.INVALID_CREDENTIALS, - "Empty username or password!"); + "Empty username or password!" ); } - final User user = Authenticator.authenticate(login, password); + final User user = Authenticator.authenticate( login, password ); - final Session session = new Session(user); - return SessionManager.addSession(session); + final Session session = new Session( user ); + return SessionManager.addSession( session ); } /** @@ -70,121 +72,131 @@ public class ApiServer { * @return UserInfo struct for given token's user * @throws InvalidTokenException if no user matches the given token */ - public static UserInfo getUserFromToken(String token) - throws InvalidTokenException { - final Session session = SessionManager.getSession(token); - if (session == null) + public static UserInfo getUserFromToken( String token ) + throws InvalidTokenException + { + final Session session = SessionManager.getSession( token ); + if ( session == null ) throw new InvalidTokenException(); - return new UserInfo(session.getUserId(), session.getFirstName(), - session.getLastName(), session.getEMail()); + return new UserInfo( session.getUserId(), session.getFirstName(), + 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, - TException { - if (ServerSessionManager.getSession(serverSessionId) == null) { - throw new AuthorizationException(AuthorizationError.NOT_AUTHENTICATED, "No valid serverSessionData"); + public static FtpCredentials submitImage( String serverSessionId, + ImageData imageDescription ) throws AuthorizationException, + TException + { + if ( ServerSessionManager.getSession( serverSessionId ) == null ) { + throw new AuthorizationException( AuthorizationError.NOT_AUTHENTICATED, "No valid serverSessionData" ); } - + // create new user - FtpCredentials ftpCredentials = Globals.ftpServer.addUser(serverSessionId); - - if (ftpCredentials == null) { - log.error("Could not create ftp credentials"); + FtpCredentials ftpCredentials = Globals.ftpServer.addUser( serverSessionId ); + + if ( ftpCredentials == null ) { + log.error( "Could not create ftp credentials" ); return null; } - - if (!ImageProcessor.addImageDataToProcess(imageDescription, ftpCredentials.username)) { - Globals.ftpServer.removeUser(serverSessionId); - throw new TException("ImageData is not valid."); + + if ( !ImageProcessor.addImageDataToProcess( imageDescription, ftpCredentials.username ) ) { + Globals.ftpServer.removeUser( serverSessionId ); + throw new TException( "ImageData is not valid." ); } - + return ftpCredentials; } /** * Start the server authentication of a uni/hs satellite server. + * * @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 + * key of the requesting satellite server * @throws ServerAuthenticationException when organization is invalid/unknown */ - public static String startServerAuthentication(String organization) - throws ServerAuthenticationException { - if (organization == null || organization == "") { - throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_ORGANIZATION, "Empty organization"); + public static String startServerAuthentication( String organization ) + throws ServerAuthenticationException + { + if ( organization == null || organization == "" ) { + throw new ServerAuthenticationException( ServerAuthenticationError.INVALID_ORGANIZATION, "Empty organization" ); } - if (DbSatellite.fromOrganization(organization) == null) { - throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_ORGANIZATION, "Unknown organization"); + if ( DbSatellite.fromOrganization( organization ) == null ) { + throw new ServerAuthenticationException( ServerAuthenticationError.INVALID_ORGANIZATION, "Unknown organization" ); } - return ServerAuthenticator.startServerAuthentication(organization); + return ServerAuthenticator.startServerAuthentication( organization ); } /** * Authenticate the uni/hs satellite server with the encrypted string. + * * @param organization the organization that the server belongs to * @param challengeResponse the encrypted string * @return session data iff the authentication was successful * @throws AuthenticationException * @throws TException */ - public static ServerSessionData serverAuthenticate(String organization, - String challengeResponse) throws AuthenticationException, - TException { - if (organization == null || challengeResponse == null) { - throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_ORGANIZATION, "Empty organization or challengeResponse"); + public static ServerSessionData serverAuthenticate( String organization, + String challengeResponse ) throws AuthenticationException, + TException + { + if ( organization == null || challengeResponse == null ) { + throw new ServerAuthenticationException( ServerAuthenticationError.INVALID_ORGANIZATION, "Empty organization or challengeResponse" ); } - DbSatellite satellite = DbSatellite.fromOrganization(organization); - if (satellite == null) { - throw new ServerAuthenticationException(ServerAuthenticationError.INVALID_ORGANIZATION, "Unknown organization"); + DbSatellite satellite = DbSatellite.fromOrganization( organization ); + if ( satellite == null ) { + throw new ServerAuthenticationException( ServerAuthenticationError.INVALID_ORGANIZATION, "Unknown organization" ); } final ServerUser serverUser = ServerAuthenticator.serverAuthenticate( - organization, satellite.getAddress(), challengeResponse); - - final ServerSession session = new ServerSession(serverUser); - return ServerSessionManager.addSession(session); + organization, satellite.getAddress(), challengeResponse ); + + final ServerSession session = new ServerSession( serverUser ); + 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 { + 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"); + 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.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + username); + String username = Globals.ftpServer.getCredentialsFromSessionId( serverSessionId ).username; + + File userDirectory = new File( Globals.getPropertyString( Globals.PropString.FTPBASEDIR ) + "/" + username ); File[] list = userDirectory.listFiles(); - - if (list.length != 1) return false; - - log.info(username + " is done with upload"); - + + 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); - + Globals.ftpServer.removeUser( username ); + log.info( "Removed user: " + username ); + + ImageProcessor.processImageAfterUpload( username, list[0].getName() ); + + Globals.ftpServer.removeUser( serverSessionId ); + return true; } |