package org.openslx.bwlp.sat.thrift;
import java.nio.ByteBuffer;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.openslx.bwlp.sat.RuntimeConfig;
import org.openslx.bwlp.sat.database.mappers.DbImage;
import org.openslx.bwlp.sat.database.mappers.DbImagePermissions;
import org.openslx.bwlp.sat.database.mappers.DbLecture;
import org.openslx.bwlp.sat.database.mappers.DbLecturePermissions;
import org.openslx.bwlp.sat.database.mappers.DbUser;
import org.openslx.bwlp.sat.database.models.ImageVersionMeta;
import org.openslx.bwlp.sat.database.models.LocalUser;
import org.openslx.bwlp.sat.fileserv.FileServer;
import org.openslx.bwlp.sat.fileserv.IncomingDataTransfer;
import org.openslx.bwlp.sat.fileserv.OutgoingDataTransfer;
import org.openslx.bwlp.sat.fileserv.SyncTransferHandler;
import org.openslx.bwlp.sat.permissions.User;
import org.openslx.bwlp.sat.thrift.cache.OperatingSystemList;
import org.openslx.bwlp.sat.thrift.cache.OrganizationList;
import org.openslx.bwlp.sat.thrift.cache.VirtualizerList;
import org.openslx.bwlp.sat.util.FileSystem;
import org.openslx.bwlp.sat.util.Sanitizer;
import org.openslx.bwlp.sat.util.Util;
import org.openslx.bwlp.thrift.iface.AuthorizationError;
import org.openslx.bwlp.thrift.iface.ImageBaseWrite;
import org.openslx.bwlp.thrift.iface.ImageDetailsRead;
import org.openslx.bwlp.thrift.iface.ImagePermissions;
import org.openslx.bwlp.thrift.iface.ImagePublishData;
import org.openslx.bwlp.thrift.iface.ImageSummaryRead;
import org.openslx.bwlp.thrift.iface.ImageVersionDetails;
import org.openslx.bwlp.thrift.iface.ImageVersionWrite;
import org.openslx.bwlp.thrift.iface.InvocationError;
import org.openslx.bwlp.thrift.iface.LecturePermissions;
import org.openslx.bwlp.thrift.iface.LectureRead;
import org.openslx.bwlp.thrift.iface.LectureSummary;
import org.openslx.bwlp.thrift.iface.LectureWrite;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.Organization;
import org.openslx.bwlp.thrift.iface.SatelliteConfig;
import org.openslx.bwlp.thrift.iface.SatelliteServer;
import org.openslx.bwlp.thrift.iface.SatelliteStatus;
import org.openslx.bwlp.thrift.iface.SatelliteUserConfig;
import org.openslx.bwlp.thrift.iface.ShareMode;
import org.openslx.bwlp.thrift.iface.TAuthorizationException;
import org.openslx.bwlp.thrift.iface.TInvalidDateParam;
import org.openslx.bwlp.thrift.iface.TInvalidTokenException;
import org.openslx.bwlp.thrift.iface.TInvocationException;
import org.openslx.bwlp.thrift.iface.TNotFoundException;
import org.openslx.bwlp.thrift.iface.TTransferRejectedException;
import org.openslx.bwlp.thrift.iface.TransferInformation;
import org.openslx.bwlp.thrift.iface.TransferStatus;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.bwlp.thrift.iface.Virtualizer;
import org.openslx.bwlp.thrift.iface.WhoamiInfo;
import org.openslx.sat.thrift.version.Version;
import org.openslx.thrifthelper.ThriftManager;
public class ServerHandler implements SatelliteServer.Iface {
private static final Logger LOGGER = Logger.getLogger(ServerHandler.class);
private static final FileServer fileServer = FileServer.instance();
@Override
public long getVersion() {
return Version.VERSION;
}
@Override
public SatelliteConfig getConfiguration() {
return RuntimeConfig.get();
}
/*
* File Transfer
*/
@Override
public TransferInformation requestImageVersionUpload(String userToken, String imageBaseId, long fileSize,
List<ByteBuffer> blockHashes, ByteBuffer machineDescription) throws TTransferRejectedException,
TAuthorizationException, TInvocationException, TNotFoundException, TException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canEditBaseImageOrFail(user, imageBaseId);
ImageDetailsRead image;
try {
image = DbImage.getImageDetails(user, imageBaseId);
} catch (SQLException e) {
throw new TInvocationException();
}
if (image.shareMode != ShareMode.LOCAL && image.shareMode != ShareMode.PUBLISH)
throw new TAuthorizationException(AuthorizationError.NO_PERMISSION,
"Cannot upload new versions of a replicated image");
// Unwrap machine description
byte[] mDesc = ThriftUtil.unwrapByteBuffer(machineDescription);
// Unwrap sha1sum list
List<byte[]> hashList = ThriftUtil.unwrapByteBufferList(blockHashes);
IncomingDataTransfer transfer = fileServer.createNewUserUpload(user, image, fileSize, hashList, mDesc);
return new TransferInformation(transfer.getId(), fileServer.getPlainPort(), fileServer.getSslPort());
}
@Override
public void updateBlockHashes(String uploadToken, List<ByteBuffer> blockHashes)
throws TInvalidTokenException {
IncomingDataTransfer upload = fileServer.getUploadByToken(uploadToken);
if (upload == null)
throw new TInvalidTokenException();
List<byte[]> hashList = ThriftUtil.unwrapByteBufferList(blockHashes);
upload.updateBlockHashList(hashList);
}
@Override
public void cancelUpload(String uploadToken) {
IncomingDataTransfer upload = fileServer.getUploadByToken(uploadToken);
if (upload != null)
upload.cancel();
}
@Override
public TransferStatus queryUploadStatus(String uploadToken) throws TInvalidTokenException {
IncomingDataTransfer upload = fileServer.getUploadByToken(uploadToken);
if (upload == null)
throw new TInvalidTokenException();
return upload.getStatus();
}
@Override
public TransferInformation requestDownload(String userToken, String imageVersionId)
throws TAuthorizationException, TInvocationException, TNotFoundException,
TTransferRejectedException {
UserInfo user = SessionManager.getOrFail(userToken);
ImageVersionMeta imageVersion;
try {
imageVersion = DbImage.getVersionDetails(imageVersionId);
} catch (SQLException e) {
throw new TInvocationException();
}
User.canDownloadImageVersionOrFail(user, imageVersion.imageBaseId, imageVersionId);
OutgoingDataTransfer transfer;
try {
transfer = fileServer.createNewUserDownload(DbImage.getLocalImageData(imageVersionId));
} catch (SQLException e) {
throw new TInvocationException();
}
TransferInformation ti = new TransferInformation(transfer.getId(), fileServer.getPlainPort(),
fileServer.getSslPort());
ti.setBlockHashes(imageVersion.sha1sums);
ti.setMachineDescription(imageVersion.machineDescription);
return ti;
}
@Override
public void cancelDownload(String downloadToken) {
OutgoingDataTransfer download = fileServer.getDownloadByToken(downloadToken);
if (download != null)
download.cancel();
}
/*
* Authentication/Validation
*/
@Override
public void isAuthenticated(String userToken) throws TAuthorizationException, TInvocationException {
SessionManager.ensureAuthenticated(userToken);
}
@Override
public WhoamiInfo whoami(String userToken) throws TAuthorizationException, TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
return new WhoamiInfo(user, User.isSuperUser(user), User.canListImages(user));
}
@Override
public void invalidateSession(String userToken) {
SessionManager.remove(userToken);
}
/*
* Query basic information which doesn't require authentication
*/
@Override
public List<OperatingSystem> getOperatingSystems() {
return OperatingSystemList.get();
}
@Override
public List<Virtualizer> getVirtualizers() {
return VirtualizerList.get();
}
@Override
public List<Organization> getAllOrganizations() {
return OrganizationList.get();
}
@Override
public SatelliteStatus getStatus() {
return new SatelliteStatus(FileSystem.getAvailableStorageBytes(), Util.unixTime());
}
/*
* Everything below required at least a valid session
*/
@Override
public SatelliteUserConfig getUserConfig(String userToken) throws TAuthorizationException,
TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
LocalUser localData;
try {
localData = DbUser.getLocalData(user);
} catch (SQLException e) {
throw new TInvocationException();
}
return new SatelliteUserConfig(localData != null && localData.emailNotifications);
}
@Override
public void setUserConfig(String userToken, SatelliteUserConfig config) throws TAuthorizationException,
TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
try {
DbUser.writeUserConfig(user, config);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public List<ImageSummaryRead> getImageList(String userToken, List<String> tagSearch, int page)
throws TAuthorizationException, TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canListImagesOrFail(user);
try {
return DbImage.getAllVisible(user, tagSearch, page);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public ImageDetailsRead getImageDetails(String userToken, String imageBaseId)
throws TAuthorizationException, TNotFoundException, TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canSeeImageDetailsOrFail(user);
try {
return DbImage.getImageDetails(user, imageBaseId);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public String createImage(String userToken, String imageName) throws TAuthorizationException,
TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canCreateImageOrFail(user);
if (!Util.isPrintable(imageName) || Util.isEmptyString(imageName))
throw new TInvocationException(InvocationError.INVALID_DATA, "Invalid or empty name");
try {
return DbImage.createImage(user, imageName);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public void updateImageBase(String userToken, String imageBaseId, ImageBaseWrite newData)
throws TAuthorizationException, TInvocationException, TNotFoundException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canEditBaseImageOrFail(user, imageBaseId);
// Check image name for invalid characters
if (!Util.isPrintable(newData.imageName) || Util.isEmptyString(newData.imageName))
throw new TInvocationException(InvocationError.INVALID_DATA, "Invalid or empty name");
// Check if image is marked for replication. If so, only allow changing the syncmode to FROZEN/DOWNLOAD
try {
ImageSummaryRead imageSummary = DbImage.getImageSummary(user, imageBaseId);
if (imageSummary.shareMode == ShareMode.DOWNLOAD || imageSummary.shareMode == ShareMode.FROZEN) {
if (newData.shareMode != ShareMode.DOWNLOAD && newData.shareMode != ShareMode.FROZEN) {
throw new TInvocationException(InvocationError.INVALID_SHARE_MODE,
"Cannot change share mode from remote to local");
} else {
// Share mode is valid and changed, but ignore all other fields
DbImage.setShareMode(imageBaseId, newData);
return;
}
} else {
// Likewise, if share mode is local or publish, don't allow changing to FROZEN/DOWNLOAD
if (newData.shareMode != ShareMode.LOCAL && newData.shareMode != ShareMode.PUBLISH) {
throw new TInvocationException(InvocationError.INVALID_SHARE_MODE,
"Cannot change share mode from local to remote");
}
}
// TODO: Should other fields be validated? Most fields should be protected by fk constraints,
// but the user would only get a generic error, with no hint at the actual problem.
// The update routine will make sure only the super user can change the template flag
newData.defaultPermissions = Sanitizer.handleImagePermissions(newData.defaultPermissions);
DbImage.updateImageMetadata(user, imageBaseId, newData);
} catch (SQLException e1) {
throw new TInvocationException();
}
}
@Override
public void updateImageVersion(String userToken, String imageVersionId, ImageVersionWrite image)
throws TAuthorizationException, TInvocationException, TNotFoundException {
UserInfo user = SessionManager.getOrFail(userToken);
// Special case: Version is still being uploaded, so there's no entry yet - remember for later
IncomingDataTransfer upload = fileServer.getUploadByToken(imageVersionId);
if (upload != null && upload.setVersionData(user, image)) {
return;
}
// Normal case - version already exists
User.canEditImageVersionOrFail(user, imageVersionId);
try {
// Do not allow editing remote images
ImageSummaryRead imageSummary = DbImage.getImageSummary(user,
DbImage.getBaseIdForVersionId(imageVersionId));
if (imageSummary.shareMode == ShareMode.DOWNLOAD || imageSummary.shareMode == ShareMode.FROZEN) {
throw new TAuthorizationException(AuthorizationError.NO_PERMISSION,
"Cannot edit image coming from master server");
}
DbImage.updateImageVersion(user, imageVersionId, image);
} catch (SQLException e1) {
throw new TInvocationException();
}
}
@Override
public void deleteImageVersion(String userToken, String imageVersionId) throws TAuthorizationException,
TNotFoundException, TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canDeleteImageVersionOrFail(user, imageVersionId);
try {
DbImage.markForDeletion(imageVersionId);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public void deleteImageBase(String userToken, String imageBaseId) throws TAuthorizationException,
TNotFoundException, TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
ImageDetailsRead imageDetails;
try {
imageDetails = DbImage.getImageDetails(user, imageBaseId);
} catch (SQLException e) {
throw new TInvocationException();
}
User.canDeleteImageOrFail(imageDetails);
String[] ids = new String[imageDetails.versions.size()];
int index = 0;
for (ImageVersionDetails version : imageDetails.versions) {
ids[index++] = version.versionId;
}
try {
DbImage.markForDeletion(ids);
} catch (Exception e) {
LOGGER.warn("Could not delete version when trying to delete base image", e);
}
}
@Override
public void writeImagePermissions(String userToken, String imageBaseId,
Map<String, ImagePermissions> permissions) throws TAuthorizationException, TNotFoundException,
TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canEditImagePermissionsOrFail(user, imageBaseId);
try {
DbImagePermissions.writeForImageBase(imageBaseId, permissions);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public Map<String, ImagePermissions> getImagePermissions(String userToken, String imageBaseId)
throws TAuthorizationException, TNotFoundException, TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
boolean adminOnly = !User.canEditImagePermissions(user, imageBaseId);
try {
return DbImagePermissions.getForImageBase(imageBaseId, adminOnly);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public void setImageOwner(String userToken, String imageBaseId, String newOwnerId)
throws TAuthorizationException, TNotFoundException, TInvocationException, TException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canChangeImageOwnerOrFail(user, imageBaseId);
try {
ImageSummaryRead imageSummary = DbImage.getImageSummary(user, imageBaseId);
if (imageSummary.shareMode == ShareMode.DOWNLOAD || imageSummary.shareMode == ShareMode.FROZEN) {
throw new TAuthorizationException(AuthorizationError.NO_PERMISSION,
"Cannot change owner of image that gets downloaded from master server");
}
DbImage.setImageOwner(imageBaseId, newOwnerId, user);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public void setImageVersionExpiry(String userToken, String imageBaseId, long expireTime)
throws TAuthorizationException, TNotFoundException, TInvocationException, TInvalidDateParam,
TException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canChangeImageExpireDateOrFail(user);
// TODO Manually-generated method stub
}
@Override
public String requestImageReplication(String userToken, String imageVersionId)
throws TAuthorizationException, TNotFoundException, TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canTriggerReplicationOrFail(user, imageVersionId);
// Query master server
ImagePublishData imagePublishData;
try {
imagePublishData = ThriftManager.getMasterClient().getImageData(userToken, imageVersionId);
} catch (TException e) {
LOGGER.error(
"Could not query image data from master server for an image that a client wants to replicate",
e);
throw new TInvocationException(InvocationError.INTERNAL_SERVER_ERROR,
"Cannot query master server for image information");
}
// Known by master server; now update/write to DB
try {
DbImage.writeBaseImage(user, imagePublishData);
} catch (SQLException e) {
throw new TInvocationException(InvocationError.INTERNAL_SERVER_ERROR,
"Could not write to local DB");
}
return SyncTransferHandler.requestImageDownload(imagePublishData);
}
@Override
public String createLecture(String userToken, LectureWrite lecture) throws TAuthorizationException,
TInvocationException, TInvalidDateParam, TNotFoundException {
if (lecture == null || lecture.defaultPermissions == null)
throw new TInvocationException(); // TODO Own exception for this
UserInfo user = SessionManager.getOrFail(userToken);
User.canCreateLectureOrFail(user);
User.canLinkToImageOrFail(user, lecture.imageVersionId);
Sanitizer.handleLectureDates(lecture);
try {
return DbLecture.create(user, lecture);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public void updateLecture(String userToken, String lectureId, LectureWrite newLectureData)
throws TAuthorizationException, TNotFoundException, TInvocationException, TInvalidDateParam {
UserInfo user = SessionManager.getOrFail(userToken);
LectureSummary oldLecture;
try {
oldLecture = DbLecture.getLectureSummary(user, lectureId);
} catch (SQLException e1) {
throw new TInvocationException();
}
User.canEditLectureOrFail(user, oldLecture);
// TODO Copy empty fields in new from old
if (oldLecture.imageVersionId == null
|| !oldLecture.imageVersionId.equals(newLectureData.imageVersionId)) {
User.canLinkToImageOrFail(user, newLectureData.imageVersionId);
}
Sanitizer.handleLectureDates(newLectureData);
try {
DbLecture.update(user, lectureId, newLectureData);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public List<LectureSummary> getLectureList(String userToken, int page) throws TAuthorizationException,
TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
try {
// If user is student, getAll() will only return lectures where the current linked image is not restricted
return DbLecture.getAll(user, page);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public LectureRead getLectureDetails(String userToken, String lectureId) throws TAuthorizationException,
TNotFoundException, TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canSeeLectureDetailsOrFail(user);
try {
return DbLecture.getLectureDetails(user, lectureId);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public void deleteLecture(String userToken, String lectureId) throws TAuthorizationException,
TNotFoundException, TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canDeleteLectureOrFail(user, lectureId);
try {
if (!DbLecture.delete(lectureId))
throw new TNotFoundException();
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public void writeLecturePermissions(String userToken, String lectureId,
Map<String, LecturePermissions> permissions) throws TAuthorizationException, TNotFoundException,
TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canEditLecturePermissionsOrFail(user, lectureId);
try {
DbLecturePermissions.writeForLecture(lectureId, permissions);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public Map<String, LecturePermissions> getLecturePermissions(String userToken, String lectureId)
throws TAuthorizationException, TNotFoundException, TInvocationException {
UserInfo user = SessionManager.getOrFail(userToken);
boolean adminOnly = !User.canEditLecturePermissions(user, lectureId);
try {
return DbLecturePermissions.getForLecture(lectureId, adminOnly);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public void setLectureOwner(String userToken, String lectureId, String newOwnerId)
throws TAuthorizationException, TNotFoundException, TInvocationException, TException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canChangeLectureOwnerOrFail(user, lectureId);
try {
DbLecture.setOwner(user, lectureId, newOwnerId);
} catch (SQLException e) {
throw new TInvocationException();
}
}
@Override
public List<UserInfo> getUserList(String userToken, int page) throws TAuthorizationException,
TInvocationException {
SessionManager.getOrFail(userToken);
try {
return DbUser.getAll(page);
} catch (SQLException e) {
throw new TInvocationException();
}
}
}