summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/imagemaster/thrift/server/MasterServerHandler.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/thrift/server/MasterServerHandler.java')
-rw-r--r--src/main/java/org/openslx/imagemaster/thrift/server/MasterServerHandler.java117
1 files changed, 107 insertions, 10 deletions
diff --git a/src/main/java/org/openslx/imagemaster/thrift/server/MasterServerHandler.java b/src/main/java/org/openslx/imagemaster/thrift/server/MasterServerHandler.java
index ed1db56..60f4ccb 100644
--- a/src/main/java/org/openslx/imagemaster/thrift/server/MasterServerHandler.java
+++ b/src/main/java/org/openslx/imagemaster/thrift/server/MasterServerHandler.java
@@ -1,5 +1,6 @@
package org.openslx.imagemaster.thrift.server;
+import java.io.File;
import java.nio.ByteBuffer;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
@@ -9,6 +10,7 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
+import org.apache.thrift.TException;
import org.openslx.bwlp.thrift.iface.AuthorizationError;
import org.openslx.bwlp.thrift.iface.ClientSessionData;
import org.openslx.bwlp.thrift.iface.ImagePublishData;
@@ -30,12 +32,20 @@ import org.openslx.bwlp.thrift.iface.TransferInformation;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.bwlp.thrift.iface.Virtualizer;
import org.openslx.encryption.AsymKeyHolder;
+import org.openslx.filetransfer.util.FileChunk;
+import org.openslx.imagemaster.Globals;
+import org.openslx.imagemaster.db.Database;
+import org.openslx.imagemaster.db.mappers.DbImage;
+import org.openslx.imagemaster.db.mappers.DbImageBlock;
import org.openslx.imagemaster.db.mappers.DbOrganization;
import org.openslx.imagemaster.db.mappers.DbOsVirt;
import org.openslx.imagemaster.db.mappers.DbPendingSatellite;
import org.openslx.imagemaster.db.mappers.DbSatellite;
import org.openslx.imagemaster.db.mappers.DbUser;
import org.openslx.imagemaster.db.models.LocalSatellite;
+import org.openslx.imagemaster.db.models.LocalUser;
+import org.openslx.imagemaster.serverconnection.ConnectionHandler;
+import org.openslx.imagemaster.serverconnection.IncomingTransfer;
import org.openslx.imagemaster.serversession.ServerAuthenticator;
import org.openslx.imagemaster.serversession.ServerSession;
import org.openslx.imagemaster.serversession.ServerSessionManager;
@@ -95,6 +105,18 @@ public class MasterServerHandler implements MasterServer.Iface
return SessionManager.addSession( session );
}
+ /**
+ * User tells us which satellite they connected to.
+ */
+ @Override
+ public void setUsedSatellite( String sessionId, String satelliteName )
+ {
+ Session session = SessionManager.getSessionFromSessionId( sessionId );
+ if ( session == null )
+ return;
+ //session.setUsedSatellite( satelliteName );
+ }
+
@Override
public List<UserInfo> findUser( String sessionId, String organizationId, String searchTerm )
throws TAuthorizationException, TInvocationException
@@ -197,11 +219,90 @@ public class MasterServerHandler implements MasterServer.Iface
}
@Override
- public TransferInformation submitImage( String serverSessionId, ImagePublishData imageDescription, List<ByteBuffer> blockHashes )
+ public TransferInformation submitImage( String userToken, ImagePublishData img, List<ByteBuffer> blockHashes )
throws TAuthorizationException, TInvocationException, TTransferRejectedException
{
- // TODO Auto-generated method stub
- return null;
+ // Valid submit session?
+ Session session = SessionManager.getSessionFromToken( userToken );
+ if ( session == null )
+ throw new TAuthorizationException( AuthorizationError.INVALID_TOKEN, "Given user token not known to the server" );
+ // check image data
+ if ( Util.isEmpty( img.imageName ) )
+ throw new TInvocationException( InvocationError.INVALID_DATA, "Image name not set" );
+ if ( img.fileSize <= 0 )
+ throw new TInvocationException( InvocationError.INVALID_DATA, "File size is too small" );
+ if ( !Util.isUUID( img.imageBaseId ) )
+ throw new TInvocationException( InvocationError.MISSING_DATA, "ImagePublishData has invalid imageBaseId" );
+ if ( !Util.isUUID( img.imageVersionId ) )
+ throw new TInvocationException( InvocationError.MISSING_DATA, "ImagePublishData has invalid imageVersionId" );
+ if ( img.user == null || img.user.userId == null )
+ throw new TInvocationException( InvocationError.MISSING_DATA, "Missing user id" );
+ // check for complete block hash list
+ boolean listComplete = false;
+ if ( blockHashes != null && blockHashes.size() == FileChunk.fileSizeToChunkCount( img.fileSize ) ) {
+ listComplete = true;
+ for ( ByteBuffer bb : blockHashes ) {
+ if ( bb == null || bb.remaining() != FileChunk.SHA1_LENGTH ) {
+ listComplete = false;
+ break;
+ }
+ }
+ }
+ if ( !listComplete )
+ throw new TInvocationException( InvocationError.INVALID_DATA, "Chunk hash list missing or incomplete" );
+ // Check if an upload is already assigned
+ IncomingTransfer existingUpload = ConnectionHandler.getExistingUpload( img, blockHashes );
+ if ( existingUpload != null ) {
+ return existingUpload.getTransferInfo();
+ }
+ // No existing upload - create new one
+ // checks that hit the db
+ if ( !DbOsVirt.osExists( img.osId ) )
+ throw new TInvocationException( InvocationError.INVALID_DATA, "Content operating system not set" );
+ if ( !DbOsVirt.virtExists( img.virtId ) )
+ throw new TInvocationException( InvocationError.INVALID_DATA, "Content virtualizer system not set" );
+ try {
+ LocalUser user = DbUser.forUserId( img.user.userId );
+ if ( user == null ) {
+ user = DbUser.forUserId( session.getUserInfo().userId );
+ if ( user != null ) {
+ img.user = user.toUserInfo();
+ }
+ }
+ if ( user == null )
+ throw new TInvocationException( InvocationError.UNKNOWN_USER, "Unknown user id " + img.user.userId );
+ if ( user.isAnonymous() )
+ throw new TInvocationException( InvocationError.UNKNOWN_USER, "The owner of the image does not participate in image exchange" );
+ } catch ( SQLException e ) {
+ throw new TInvocationException( InvocationError.INTERNAL_SERVER_ERROR, "Database error" );
+ }
+ // Make sure we have a destination to write to
+ if ( !new File( Globals.getImageDir() ).isDirectory() )
+ throw new TInvocationException( InvocationError.INTERNAL_SERVER_ERROR, "Storage offline" );
+ // Try to register an upload
+ IncomingTransfer transfer = ConnectionHandler.registerUpload( img, blockHashes );
+ try {
+ DbImage.createImageBase( img );
+ } catch ( TException t ) {
+ transfer.cancel();
+ throw t;
+ }
+ try {
+ DbImage.createImageVersion( img, transfer.getRelativePath() );
+ } catch ( SQLException e1 ) {
+ transfer.cancel();
+ if ( Database.isDuplicateKeyException( e1 ) ) {
+ throw new TInvocationException( InvocationError.INVALID_DATA, "The image already exists on the server" );
+ } else {
+ throw new TInvocationException( InvocationError.INTERNAL_SERVER_ERROR, "Database error" );
+ }
+ }
+ try {
+ DbImageBlock.insertChunkList( img.imageVersionId, transfer.getChunks().getAll(), true );
+ } catch ( SQLException e ) {
+ LOGGER.warn( "Could not insert block hashes of image " + img.imageVersionId + " to db" );
+ }
+ return transfer.getTransferInfo();
}
@Override
@@ -218,7 +319,7 @@ public class MasterServerHandler implements MasterServer.Iface
try {
return DbOrganization.getAll();
} catch ( SQLException e ) {
- throw new TInvocationException();
+ throw new TInvocationException( InvocationError.INTERNAL_SERVER_ERROR, "Database error" );
}
}
@@ -228,7 +329,7 @@ public class MasterServerHandler implements MasterServer.Iface
try {
return DbOsVirt.getOsList();
} catch ( SQLException e ) {
- throw new TInvocationException();
+ throw new TInvocationException( InvocationError.INTERNAL_SERVER_ERROR, "Database error" );
}
}
@@ -238,7 +339,7 @@ public class MasterServerHandler implements MasterServer.Iface
try {
return DbOsVirt.getVirtualizerList();
} catch ( SQLException e ) {
- throw new TInvocationException();
+ throw new TInvocationException( InvocationError.INTERNAL_SERVER_ERROR, "Database error" );
}
}
@@ -273,10 +374,6 @@ public class MasterServerHandler implements MasterServer.Iface
LOGGER.warn( "Invalid public key in registerOrganization for " + organizationId + " (By " + session.getLogin() + ")", e );
throw new TInvocationException( InvocationError.INVALID_DATA, "Cannot reconstruct public key" );
}
- if ( newKey == null ) {
- LOGGER.warn( "Uninstantiable public key in registerOrganization for " + organizationId + " (By " + session.getLogin() + ")" );
- throw new TInvocationException( InvocationError.INVALID_DATA, "Cannot reconstruct public key" );
- }
LocalSatellite existing = DbSatellite.get( organizationId, displayName );
if ( existing != null ) {
Key existingKey = existing.getPubkey();