package org.openslx.bwlp.sat.thrift;
import java.nio.ByteBuffer;
import java.sql.SQLException;
import java.util.ArrayList;
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.fileserv.ActiveDownload;
import org.openslx.bwlp.sat.fileserv.ActiveUpload;
import org.openslx.bwlp.sat.fileserv.FileServer;
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.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.ImageDataError;
import org.openslx.bwlp.thrift.iface.ImageDetailsRead;
import org.openslx.bwlp.thrift.iface.ImagePermissions;
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.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.ShareMode;
import org.openslx.bwlp.thrift.iface.TAuthorizationException;
import org.openslx.bwlp.thrift.iface.TImageDataException;
import org.openslx.bwlp.thrift.iface.TInternalServerError;
import org.openslx.bwlp.thrift.iface.TInvalidDateParam;
import org.openslx.bwlp.thrift.iface.TInvalidTokenException;
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;
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
*/
private List<byte[]> unwrapHashes(List<ByteBuffer> blockHashes) {
if (blockHashes == null || blockHashes.isEmpty())
return null;
List<byte[]> hashList = new ArrayList<>(blockHashes.size());
for (ByteBuffer hash : blockHashes) {
byte[] buffer = new byte[hash.remaining()];
hash.get(buffer);
hashList.add(buffer);
}
return hashList;
}
@Override
public TransferInformation requestImageVersionUpload(String userToken, String imageBaseId, long fileSize,
List<ByteBuffer> blockHashes, ByteBuffer machineDescription) throws TTransferRejectedException,
TAuthorizationException, TInternalServerError, TNotFoundException, TException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canEditBaseImageOrFail(user, imageBaseId);
ImageDetailsRead image;
try {
image = DbImage.getImageDetails(user, imageBaseId);
} catch (SQLException e) {
throw new TInternalServerError();
}
// Unwrap machine description
byte[] mDesc = null;
if (machineDescription != null) {
mDesc = new byte[machineDescription.remaining()];
machineDescription.get(mDesc);
}
// Unwrap sha1sum list
List<byte[]> hashList = unwrapHashes(blockHashes);
ActiveUpload 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 {
ActiveUpload upload = fileServer.getUploadByToken(uploadToken);
if (upload == null)
throw new TInvalidTokenException();
List<byte[]> hashList = unwrapHashes(blockHashes);
upload.updateBlockHashList(hashList);
}
@Override
public ByteBuffer getMachineDescription(String userToken, String imageVersionId)
throws TAuthorizationException, TInternalServerError, TNotFoundException, TException {
// TODO Auto-generated method stub
return null;
}
@Override
public void cancelUpload(String uploadToken) {
ActiveUpload upload = fileServer.getUploadByToken(uploadToken);
if (upload != null)
upload.cancel();
}
@Override
public TransferStatus queryUploadStatus(String uploadToken) throws TInvalidTokenException {
ActiveUpload upload = fileServer.getUploadByToken(uploadToken);
if (upload == null)
throw new TInvalidTokenException();
return upload.getStatus();
}
@Override
public TransferInformation requestDownload(String userToken, String imageVersionId)
throws TAuthorizationException, TInternalServerError, TNotFoundException,
TTransferRejectedException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canDownloadImageVersionOrFail(user, imageVersionId);
ActiveDownload transfer;
try {
transfer = fileServer.createNewUserDownload(DbImage.getLocalImageData(imageVersionId));
} catch (SQLException e) {
throw new TInternalServerError();
}
return new TransferInformation(transfer.getId(), fileServer.getPlainPort(), fileServer.getSslPort());
}
@Override
public void cancelDownload(String downloadToken) {
ActiveDownload download = fileServer.getDownloadByToken(downloadToken);
if (download != null)
download.cancel();
}
/*
* Authentication/Validation
*/
@Override
public void isAuthenticated(String userToken) throws TAuthorizationException, TInternalServerError {
SessionManager.ensureAuthenticated(userToken);
}
@Override
public WhoamiInfo whoami(String userToken) throws TAuthorizationException, TInternalServerError {
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();
}
/*
* Everything below required at least a valid session
*/
@Override
public List<ImageSummaryRead> getImageList(String userToken, List<String> tagSearch, int page)
throws TAuthorizationException, TInternalServerError {
UserInfo user = SessionManager.getOrFail(userToken);
User.canListImagesOrFail(user);
try {
return DbImage.getAllVisible(user, tagSearch, page);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public ImageDetailsRead getImageDetails(String userToken, String imageBaseId)
throws TAuthorizationException, TNotFoundException, TInternalServerError {
UserInfo user = SessionManager.getOrFail(userToken);
User.canSeeImageDetailsOrFail(user);
try {
return DbImage.getImageDetails(user, imageBaseId);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public String createImage(String userToken, String imageName) throws TAuthorizationException,
TImageDataException, TInternalServerError {
UserInfo user = SessionManager.getOrFail(userToken);
User.canCreateImageOrFail(user);
if (!Util.isPrintable(imageName) || Util.isEmptyString(imageName))
throw new TImageDataException(ImageDataError.INVALID_DATA, "Invalid or empty name");
try {
return DbImage.createImage(user, imageName);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public void updateImageBase(String userToken, String imageBaseId, ImageBaseWrite newData)
throws TAuthorizationException, TInternalServerError, TNotFoundException, TImageDataException {
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 TImageDataException(ImageDataError.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 TImageDataException(ImageDataError.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 TImageDataException(ImageDataError.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
DbImage.updateImageMetadata(user, imageBaseId, newData);
} catch (SQLException e1) {
throw new TInternalServerError();
}
}
@Override
public void updateImageVersion(String userToken, String imageVersionId, ImageVersionWrite image)
throws TAuthorizationException, TInternalServerError, TNotFoundException {
UserInfo user = SessionManager.getOrFail(userToken);
// Special case: Version is still being uploaded, so there's no entry yet - remember for later
ActiveUpload 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 TInternalServerError();
}
}
@Override
public void deleteImageVersion(String userToken, String imageVersionId) throws TAuthorizationException,
TNotFoundException, TInternalServerError {
UserInfo user = SessionManager.getOrFail(userToken);
User.canDeleteImageVersionOrFail(user, imageVersionId);
try {
DbImage.markForDeletion(imageVersionId);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public void deleteImageBase(String userToken, String imageBaseId) throws TAuthorizationException,
TNotFoundException, TInternalServerError {
UserInfo user = SessionManager.getOrFail(userToken);
ImageDetailsRead imageDetails;
try {
imageDetails = DbImage.getImageDetails(user, imageBaseId);
} catch (SQLException e) {
throw new TInternalServerError();
}
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,
TInternalServerError {
UserInfo user = SessionManager.getOrFail(userToken);
User.canEditImagePermissionsOrFail(user, imageBaseId);
try {
DbImagePermissions.writeForImageBase(imageBaseId, permissions);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public Map<String, ImagePermissions> getImagePermissions(String userToken, String imageBaseId)
throws TAuthorizationException, TNotFoundException, TInternalServerError {
UserInfo user = SessionManager.getOrFail(userToken);
boolean adminOnly = !User.canEditImagePermissions(user, imageBaseId);
try {
return DbImagePermissions.getForImageBase(imageBaseId, adminOnly);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public void setImageOwner(String userToken, String imageBaseId, String newOwnerId)
throws TAuthorizationException, TNotFoundException, TInternalServerError, 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 TInternalServerError();
}
}
@Override
public void setImageVersionExpiry(String userToken, String imageBaseId, long expireTime)
throws TAuthorizationException, TNotFoundException, TInternalServerError, TInvalidDateParam,
TException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canChangeImageExpireDateOrFail(user);
}
@Override
public String createLecture(String userToken, LectureWrite lecture) throws TAuthorizationException,
TInternalServerError, TInvalidDateParam {
if (lecture == null || lecture.defaultPermissions == null)
throw new TInternalServerError(); // TODO Own exception for this
UserInfo user = SessionManager.getOrFail(userToken);
User.canCreateLectureOrFail(user);
Sanitizer.handleLectureDates(lecture);
try {
return DbLecture.create(user, lecture);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public void updateLecture(String userToken, String lectureId, LectureWrite lecture)
throws TAuthorizationException, TNotFoundException, TInternalServerError, TInvalidDateParam {
UserInfo user = SessionManager.getOrFail(userToken);
User.canEditLectureOrFail(user, lectureId);
Sanitizer.handleLectureDates(lecture);
try {
DbLecture.update(user, lectureId, lecture);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public List<LectureSummary> getLectureList(String userToken, int page) throws TAuthorizationException,
TInternalServerError {
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 TInternalServerError();
}
}
@Override
public LectureRead getLectureDetails(String userToken, String lectureId) throws TAuthorizationException,
TNotFoundException, TInternalServerError {
UserInfo user = SessionManager.getOrFail(userToken);
User.canSeeLectureDetailsOrFail(user);
try {
return DbLecture.getLectureDetails(user, lectureId);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public void deleteLecture(String userToken, String lectureId) throws TAuthorizationException,
TNotFoundException, TInternalServerError {
UserInfo user = SessionManager.getOrFail(userToken);
User.canDeleteLectureOrFail(user, lectureId);
try {
if (!DbLecture.delete(lectureId))
throw new TNotFoundException();
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public void writeLecturePermissions(String userToken, String lectureId,
Map<String, LecturePermissions> permissions) throws TAuthorizationException, TNotFoundException,
TInternalServerError {
UserInfo user = SessionManager.getOrFail(userToken);
User.canEditLecturePermissionsOrFail(user, lectureId);
try {
DbLecturePermissions.writeForLecture(lectureId, permissions);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public Map<String, LecturePermissions> getLecturePermissions(String userToken, String lectureId)
throws TAuthorizationException, TNotFoundException, TInternalServerError {
UserInfo user = SessionManager.getOrFail(userToken);
boolean adminOnly = !User.canEditLecturePermissions(user, lectureId);
try {
return DbLecturePermissions.getForLecture(lectureId, adminOnly);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public void setLectureOwner(String userToken, String lectureId, String newOwnerId)
throws TAuthorizationException, TNotFoundException, TInternalServerError, TException {
UserInfo user = SessionManager.getOrFail(userToken);
User.canChangeLectureOwnerOrFail(user, lectureId);
try {
DbLecture.setOwner(user, lectureId, newOwnerId);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public List<UserInfo> getUserList(String userToken, int page) throws TAuthorizationException,
TInternalServerError {
try {
return DbUser.getAll(page);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
}