summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver
diff options
context:
space:
mode:
authorSimon Rettberg2016-07-26 17:21:26 +0200
committerSimon Rettberg2016-07-26 17:21:26 +0200
commit36e9590bbbe6507bd994cbcb337da1f8bae3a6aa (patch)
treeac1e3c87e7e46a8d82ef1bdee7277fcc0764cc73 /dozentenmodulserver
parent[server] Register NetRule thrift class with Json helper (diff)
downloadtutor-module-36e9590bbbe6507bd994cbcb337da1f8bae3a6aa.tar.gz
tutor-module-36e9590bbbe6507bd994cbcb337da1f8bae3a6aa.tar.xz
tutor-module-36e9590bbbe6507bd994cbcb337da1f8bae3a6aa.zip
[server] Supply netrules and runscript via webrpc
Diffstat (limited to 'dozentenmodulserver')
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbLecture.java11
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebRpc.java6
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java100
3 files changed, 102 insertions, 15 deletions
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 76eaac2d..d9135bb1 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
@@ -36,7 +36,7 @@ import com.google.gson.JsonParseException;
public class DbLecture {
private static final Logger LOGGER = Logger.getLogger(DbLecture.class);
-
+
static {
Json.registerThriftClass(NetRule.class);
}
@@ -276,7 +276,7 @@ public class DbLecture {
+ " LEFT JOIN imageversion i USING (imageversionid)"
+ " LEFT JOIN lecturepermission p ON (l.lectureid = p.lectureid AND p.userid = :userid)"
+ " WHERE l.lectureid = :lectureid LIMIT 1");
- stmt.setString("userid", user.userId);
+ stmt.setString("userid", user == null ? "" : user.userId);
stmt.setString("lectureid", lectureId);
ResultSet rs = stmt.executeQuery();
if (!rs.next())
@@ -441,7 +441,8 @@ public class DbLecture {
}
}
- public static VmChooserListXml getUsableListXml(boolean exams, String locationsString) throws SQLException {
+ public static VmChooserListXml getUsableListXml(boolean exams, String locationsString)
+ throws SQLException {
// Sanitize and clean locations string
// Input is in the form of "1 2 3 4" or "1" or " 1 4 5"
// We want "1,2,3,4" or "1" or "1,4,5"
@@ -481,7 +482,9 @@ public class DbLecture {
+ " INNER JOIN virtualizer v USING (virtid)"
+ " LEFT JOIN os_x_virt ov USING (osid, virtid)"
+ " LEFT JOIN ("
- + " SELECT DISTINCT lectureid FROM lecture_x_location WHERE locationid IN (" + cleanLocations + ")"
+ + " SELECT DISTINCT lectureid FROM lecture_x_location WHERE locationid IN ("
+ + cleanLocations
+ + ")"
+ " ) loc USING (lectureid)"
+ " 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");
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebRpc.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebRpc.java
index a8d769cb..19cb787c 100644
--- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebRpc.java
+++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebRpc.java
@@ -46,6 +46,9 @@ public class WebRpc {
"Image queued for checking");
}
+ /**
+ * Delete all image versions marked as WANT_DELETE.
+ */
private static Response deleteImages() {
StringBuilder res = DeleteOldImages.hardDeleteImages();
if (res == null)
@@ -54,6 +57,9 @@ public class WebRpc {
res.toString());
}
+ /**
+ * Send test mail to given SMTP config.
+ */
private static Response mailTest(Map<String, String> params) {
SmtpMailer smtpc;
String recipient = params.get("recipient");
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 fe1f559d..c0edc706 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
@@ -3,13 +3,14 @@ package org.openslx.bwlp.sat.web;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.SQLException;
-
import java.util.Map;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.log4j.Logger;
import org.openslx.bwlp.sat.database.mappers.DbLecture;
import org.openslx.bwlp.sat.fileserv.FileServer;
+import org.openslx.bwlp.thrift.iface.LectureRead;
+import org.openslx.bwlp.thrift.iface.NetRule;
import org.openslx.bwlp.thrift.iface.TNotFoundException;
import org.openslx.util.Json;
import org.openslx.util.vm.VmMetaData;
@@ -74,16 +75,28 @@ public class WebServer extends NanoHTTPD {
private Response handle(IHTTPSession session, String uri) {
// Our special stuff
- if (uri.startsWith("/vmchooser/list")) {
- try {
- return serveVmChooserList(session.getParms());
- } catch (Exception e) {
- LOGGER.debug("problem while retrieving the vmChooserList", e);
- return internalServerError();
+ String[] parts = uri.replaceFirst("^/+", "").split("/+");
+ // /vmchooser/*
+ if (parts.length > 1 && parts[0].equals("vmchooser")) {
+ if (parts[1].equals("list")) {
+ try {
+ return serveVmChooserList(session.getParms());
+ } catch (Exception e) {
+ LOGGER.debug("problem while retrieving the vmChooserList", e);
+ return internalServerError();
+ }
}
- }
- if (uri.startsWith("/vmchooser/lecture/")) {
- return serveLectureStart(uri.substring(19));
+ if (parts[1].equals("lecture")) {
+ if (parts.length < 3)
+ return badRequest("Bad Request");
+ if (parts.length < 4 || parts[3].equals("vmx"))
+ return serveLectureStart(parts[2]);
+ if (parts[3].equals("netrules"))
+ return serveLectureNetRules(parts[2]);
+ if (parts[3].equals("runscript"))
+ return serveLectureScript(parts[2]);
+ }
+ return notFound();
}
if (uri.startsWith("/status/fileserver")) {
return serveStatus();
@@ -125,6 +138,61 @@ public class WebServer extends NanoHTTPD {
new ByteArrayInputStream(meta.getFilteredDefinitionArray()));
}
+ private Response serveLectureNetRules(String lectureId) {
+ LectureRead lecture;
+ try {
+ lecture = DbLecture.getLectureDetails(null, lectureId);
+ } catch (TNotFoundException e) {
+ return notFound();
+ } catch (SQLException e) {
+ return internalServerError();
+ }
+ StringBuilder sb = new StringBuilder();
+ if (lecture.networkExceptions != null) {
+ for (NetRule rule : lecture.networkExceptions) {
+ sb.append(rule.direction.name());
+ sb.append(' ');
+ sb.append(rule.host);
+ sb.append(' ');
+ sb.append(rule.port);
+ sb.append(' ');
+ sb.append(lecture.hasInternetAccess ? "REJECT" : "ACCEPT");
+ sb.append('\n');
+ }
+ }
+ if (lecture.hasInternetAccess) {
+ sb.append("IN * 0 ACCEPT\n");
+ sb.append("OUT * 0 ACCEPT\n");
+ } else {
+ sb.append("IN * 0 REJECT\n");
+ sb.append("OUT * 0 REJECT\n");
+ }
+ return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "text/plain; charset=utf-8",
+ sb.toString());
+ }
+
+ private Response serveLectureScript(String lectureId) {
+ LectureRead lecture;
+ try {
+ lecture = DbLecture.getLectureDetails(null, lectureId);
+ } catch (TNotFoundException e) {
+ return notFound();
+ } catch (SQLException e) {
+ return internalServerError();
+ }
+ if (lecture.runscript == null) {
+ lecture.runscript = "";
+ }
+ return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "text/plain; charset=utf-8",
+ lecture.runscript);
+ }
+
+ /**
+ * Return full list of lectures matching given location(s).
+ *
+ * @return
+ * @throws Exception
+ */
private Response serveVmChooserList(Map<String, String> params) throws Exception {
String locations = params.get("locations");
boolean exams = params.containsKey("exams");
@@ -136,14 +204,24 @@ public class WebServer extends NanoHTTPD {
new ByteArrayInputStream(baos.toByteArray()));
}
+ /**
+ * Helper for returning "Internal Server Error" Status
+ */
public static Response internalServerError() {
- return new NanoHTTPD.Response(NanoHTTPD.Response.Status.INTERNAL_ERROR, "text/plain", "Internal Server Error");
+ return new NanoHTTPD.Response(NanoHTTPD.Response.Status.INTERNAL_ERROR, "text/plain",
+ "Internal Server Error");
}
+ /**
+ * Helper for returning "404 Not Found" Status
+ */
public static Response notFound() {
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.NOT_FOUND, "text/plain", "Nicht gefunden!");
}
+ /**
+ * Helper for returning "Bad Request" Status
+ */
public static Response badRequest(String message) {
if (message == null) {
message = "Schlechte Anfrage!";