summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver
diff options
context:
space:
mode:
authorSimon Rettberg2015-09-02 12:09:23 +0200
committerSimon Rettberg2015-09-02 12:09:23 +0200
commit8e444863bdb91f7b8109ff7c15c29673fe67d4d6 (patch)
tree41b8a00754f6b67b5e6392edcf5762ca5b9477b1 /dozentenmodulserver
parent.....aaaaand it's gone (diff)
downloadtutor-module-8e444863bdb91f7b8109ff7c15c29673fe67d4d6.tar.gz
tutor-module-8e444863bdb91f7b8109ff7c15c29673fe67d4d6.tar.xz
tutor-module-8e444863bdb91f7b8109ff7c15c29673fe67d4d6.zip
[server] Serve vmchooser-compatible lecture list via http
Diffstat (limited to 'dozentenmodulserver')
-rw-r--r--dozentenmodulserver/setup/sat-01-schema.sql8
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java31
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserEntryXml.java45
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserListXml.java13
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserParamXml.java14
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java25
6 files changed, 107 insertions, 29 deletions
diff --git a/dozentenmodulserver/setup/sat-01-schema.sql b/dozentenmodulserver/setup/sat-01-schema.sql
index f030298a..d0585db2 100644
--- a/dozentenmodulserver/setup/sat-01-schema.sql
+++ b/dozentenmodulserver/setup/sat-01-schema.sql
@@ -100,8 +100,9 @@ CREATE TABLE IF NOT EXISTS `lecture` (
`displayname` varchar(100) NOT NULL,
`description` text NOT NULL,
`imageversionid` char(36) CHARACTER SET ascii COLLATE ascii_bin NOT NULL COMMENT 'We reference a specific image version here, not the base image.\nOn update of an image, we update the lecture table for all matching lectures that used the current image version.\nThis way, a tutor can explicitly switch back to an older version of an image.',
- `autoupdate` tinyint(1) NOT NULL,
- `isenabled` tinyint(1) NOT NULL,
+ `autoupdate` tinyint(1) UNSIGNED NOT NULL DEFAULT '1',
+ `isenabled` tinyint(1) UNSIGNED NOT NULL DEFAULT '1',
+ `isprivate` tinyint(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Only users from the lectureuser table can start this lecture',
`starttime` bigint(20) NOT NULL,
`endtime` bigint(20) NOT NULL,
`lastused` bigint(20) NOT NULL DEFAULT '0',
@@ -120,7 +121,8 @@ CREATE TABLE IF NOT EXISTS `lecture` (
PRIMARY KEY (`lectureid`),
KEY `fk_lecture_1_idx` (`imageversionid`),
KEY `fk_lecture_2_idx` (`ownerid`),
- KEY `fk_lecture_3_idx` (`updaterid`)
+ KEY `fk_lecture_3_idx` (`updaterid`),
+ KEY `list_lookup` (`isenabled`,`isexam`,`isprivate`,`endtime`,`starttime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `lecturepermission` (
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java
index 8492dd58..1afcdb93 100644
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java
@@ -17,6 +17,8 @@ import org.openslx.bwlp.sat.mail.MailGenerator;
import org.openslx.bwlp.sat.permissions.User;
import org.openslx.bwlp.sat.util.Json;
import org.openslx.bwlp.sat.util.Util;
+import org.openslx.bwlp.sat.web.VmChooserEntryXml;
+import org.openslx.bwlp.sat.web.VmChooserListXml;
import org.openslx.bwlp.thrift.iface.LectureRead;
import org.openslx.bwlp.thrift.iface.LectureSummary;
import org.openslx.bwlp.thrift.iface.LectureWrite;
@@ -360,7 +362,6 @@ public class DbLecture {
List<LectureSummary> lectures = getAllUsingImageVersion(connection, oldVersion.imageVersionId);
if (lectures.isEmpty())
return;
- // TODO: If there is no new candidate to switch to, send a warning via mail, otherwise, inform about switch
MysqlStatement stmt;
if (newVersion == null) {
stmt = connection.prepareStatement("UPDATE lecture SET isenabled = 0 WHERE imageversionid = :oldversionid");
@@ -401,4 +402,32 @@ public class DbLecture {
}
}
+ public static VmChooserListXml getUsableListXml(boolean exams) throws SQLException {
+ try (MysqlConnection connection = Database.getConnection()) {
+ MysqlStatement stmt = connection.prepareStatement("SELECT"
+ + " l.lectureid, l.displayname AS lecturename, l.description,"
+ + " l.endtime, l.usecount, o.displayname AS osname, v.virtname, b.istemplate, i.filepath"
+ + " FROM lecture l "
+ + " INNER JOIN imageversion i USING (imageversionid)"
+ + " INNER JOIN imagebase b USING (imagebaseid)"
+ + " INNER JOIN operatingsystem o USING (osid)"
+ + " INNER JOIN virtualizer v USING (virtid)"
+ + " WHERE l.isenabled = 1 AND l.isprivate = 0 AND l.isexam = :isexam"
+ + " AND l.starttime < UNIX_TIMESTAMP() AND l.endtime > UNIX_TIMESTAMP() AND i.isvalid = 1");
+ stmt.setBoolean("isexam", exams);
+ ResultSet rs = stmt.executeQuery();
+ VmChooserListXml list = new VmChooserListXml(true);
+ while (rs.next()) {
+ String prio = rs.getBoolean("istemplate") ? "10" : "100";
+ list.add(new VmChooserEntryXml(prio, rs.getString("filepath"), "-",
+ rs.getString("lecturename"), rs.getString("description"), rs.getString("lectureid"),
+ rs.getString("virtname"), rs.getString("osname"), ""));
+ }
+ return list;
+ } catch (SQLException e) {
+ LOGGER.error("Query failed in DbLecture.getUsableList()", e);
+ throw e;
+ }
+ }
+
}
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserEntryXml.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserEntryXml.java
index b1571e74..9f973bb9 100644
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserEntryXml.java
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserEntryXml.java
@@ -1,24 +1,53 @@
package org.openslx.bwlp.sat.web;
import org.simpleframework.xml.Attribute;
+import org.simpleframework.xml.Element;
+import org.simpleframework.xml.Root;
+@Root(name = "eintrag")
public class VmChooserEntryXml {
- @Attribute
+ @Element
private VmChooserParamXml priority;
- @Attribute
+ @Element
private VmChooserParamXml image_name;
- @Attribute
+ @Element
private VmChooserParamXml creator;
- @Attribute
+ @Element
private VmChooserParamXml short_description;
- @Attribute
+ @Element
private VmChooserParamXml long_description;
- @Attribute
+ @Element
private VmChooserParamXml uuid;
- @Attribute
+ @Element
private VmChooserParamXml virtualmachine;
- @Attribute
+ @Element
+ private VmChooserParamXml os;
+ @Element
private VmChooserParamXml icon;
+ public VmChooserEntryXml(String priority, String image_name, String creator, String short_description,
+ String long_description, String uuid, String virtualmachine, String os, String icon) {
+ this.priority = new VmChooserParamXml(priority);
+ this.image_name = new VmChooserParamXml(image_name);
+ this.creator = new VmChooserParamXml(creator);
+ this.short_description = new VmChooserParamXml(short_description);
+ this.long_description = new VmChooserParamXml(long_description);
+ this.uuid = new VmChooserParamXml(uuid);
+ this.virtualmachine = new VmChooserParamXml(virtualmachine);
+ this.os = new VmChooserParamXml(os);
+ this.icon = new VmChooserParamXml(icon);
+ }
+
+ private static class VmChooserParamXml {
+
+ @Attribute
+ private String param;
+
+ public VmChooserParamXml(String value) {
+ this.param = value;
+ }
+
+ }
+
}
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserListXml.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserListXml.java
index f9cfa94d..beebce1a 100644
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserListXml.java
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserListXml.java
@@ -1,5 +1,6 @@
package org.openslx.bwlp.sat.web;
+import java.util.ArrayList;
import java.util.List;
import org.simpleframework.xml.ElementList;
@@ -9,6 +10,16 @@ import org.simpleframework.xml.Root;
public class VmChooserListXml {
@ElementList(inline = true, name = "eintrag")
- private List<VmChooserEntryXml> entries;
+ public List<VmChooserEntryXml> entries;
+
+ public VmChooserListXml(boolean createEmptyList) {
+ if (createEmptyList) {
+ entries = new ArrayList<>();
+ }
+ }
+
+ public void add(VmChooserEntryXml vmChooserEntryXml) {
+ entries.add(vmChooserEntryXml);
+ }
}
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserParamXml.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserParamXml.java
deleted file mode 100644
index a1f0425a..00000000
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/VmChooserParamXml.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package org.openslx.bwlp.sat.web;
-
-import org.simpleframework.xml.Attribute;
-
-public class VmChooserParamXml {
-
- @Attribute
- private String param;
-
- public VmChooserParamXml(String value) {
- this.param = value;
- }
-
-}
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java
index 02172616..a7a13305 100644
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java
@@ -1,9 +1,29 @@
package org.openslx.bwlp.sat.web;
+import java.io.ByteArrayInputStream;
+
+import org.apache.commons.io.output.ByteArrayOutputStream;
+import org.openslx.bwlp.sat.database.mappers.DbLecture;
+import org.openslx.util.GenericDataCache;
+import org.simpleframework.xml.Serializer;
+import org.simpleframework.xml.core.Persister;
+
import fi.iki.elonen.NanoHTTPD;
public class WebServer extends NanoHTTPD {
+ private static final GenericDataCache<byte[]> lectureListCache = new GenericDataCache<byte[]>(15000) {
+ Serializer serializer = new Persister();
+
+ @Override
+ protected byte[] update() throws Exception {
+ VmChooserListXml listXml = DbLecture.getUsableListXml(false);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ serializer.write(listXml, baos);
+ return baos.toByteArray();
+ }
+ };
+
public WebServer(int port) {
super(port);
}
@@ -11,7 +31,7 @@ public class WebServer extends NanoHTTPD {
@Override
public Response serve(IHTTPSession session) {
String uri = session.getUri();
-
+
if (uri == null || uri.length() == 0) {
return internalServerError();
}
@@ -25,7 +45,8 @@ public class WebServer extends NanoHTTPD {
}
private Response serveVmChooserList() {
- return new NanoHTTPD.Response(NanoHTTPD.Response.Status.NOT_FOUND, "text/plain", "BLA");
+ return new NanoHTTPD.Response(NanoHTTPD.Response.Status.NOT_FOUND, "text/xml; charset=utf-8",
+ new ByteArrayInputStream(lectureListCache.get()));
}
private Response internalServerError() {