1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
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.database.Paginator;
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.DbUser;
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.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.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.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.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.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 int getPageSize() throws TException {
return Paginator.PER_PAGE;
}
/*
* File Transfer
*/
@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();
}
byte[] mDesc = null;
if (machineDescription != null) {
mDesc = new byte[machineDescription.remaining()];
machineDescription.get(mDesc);
}
ActiveUpload transfer = fileServer.createNewUserUpload(user, image, fileSize, blockHashes, mDesc);
return new TransferInformation(transfer.getId(), fileServer.getPlainPort(), fileServer.getSslPort());
}
@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 {
// TODO Auto-generated method stub
return null;
}
@Override
public void cancelDownload(String downloadToken) {
// TODO Auto-generated method stub
}
/*
* Authentication/Validation
*/
@Override
public void isAuthenticated(String userToken) throws TAuthorizationException, TInternalServerError {
SessionManager.ensureAuthenticated(userToken);
}
@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.
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 {
// Do not allow deleting remote images if share mode is set to "auto download" and
// the version to delete is the latest
ImageSummaryRead imageSummary = DbImage.getImageSummary(user,
DbImage.getBaseIdForVersionId(imageVersionId));
if (imageSummary.shareMode == ShareMode.DOWNLOAD
&& imageSummary.latestVersionId.equals(imageVersionId)) {
throw new TAuthorizationException(AuthorizationError.NO_PERMISSION,
"Cannot delete latest version of image if auto-download is enabled");
}
DbImage.markForDeletion(imageVersionId);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@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);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
@Override
public String createLecture(String userToken, LectureWrite lecture) throws TAuthorizationException,
TInternalServerError {
UserInfo user = SessionManager.getOrFail(userToken);
User.canCreateLectureOrFail(user);
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 {
UserInfo user = SessionManager.getOrFail(userToken);
User.canEditLectureOrFail(user, lectureId);
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 {
// TODO Auto-generated method stub
}
@Override
public Map<String, LecturePermissions> getLecturePermissions(String userToken, String lectureId)
throws TAuthorizationException, TNotFoundException {
// TODO Auto-generated method stub
return null;
}
@Override
public void setLectureOwner(String userToken, String lectureId, String newOwnerId)
throws TAuthorizationException, TNotFoundException, TInternalServerError, TException {
// TODO Auto-generated method stub
}
@Override
public List<UserInfo> getUserList(String userToken, int page) throws TAuthorizationException,
TInternalServerError {
try {
return DbUser.getAll(page);
} catch (SQLException e) {
throw new TInternalServerError();
}
}
}
|