summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java
diff options
context:
space:
mode:
authorSimon Rettberg2016-01-14 17:43:02 +0100
committerSimon Rettberg2016-01-14 17:43:02 +0100
commit3578c2cdf4bba9a73d57a64941ebc2e4c931e555 (patch)
tree622e50e698663f288f6b7d9d51a610f23218a831 /dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java
parent[server] Remove stupid debug output (diff)
downloadtutor-module-3578c2cdf4bba9a73d57a64941ebc2e4c931e555.tar.gz
tutor-module-3578c2cdf4bba9a73d57a64941ebc2e4c931e555.tar.xz
tutor-module-3578c2cdf4bba9a73d57a64941ebc2e4c931e555.zip
[server] Add location support/filtering
Diffstat (limited to 'dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java')
-rw-r--r--dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java66
1 files changed, 59 insertions, 7 deletions
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 37a64ee8..5f82bac1 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,6 +3,9 @@ package org.openslx.bwlp.sat.web;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.SQLException;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.log4j.Logger;
@@ -21,23 +24,66 @@ public class WebServer extends NanoHTTPD {
private static final Logger LOGGER = Logger.getLogger(WebServer.class);
- private static final GenericDataCache<byte[]> lectureListCache = new GenericDataCache<byte[]>(15000) {
- Serializer serializer = new Persister();
+ private static final int LIST_CACHE_MS = 15000;
+ private static final int LIST_CACHE_MAX_ENTRIES = 16;
+
+ private static final Map<String, XmlDataCache> lectureListCache = Collections.synchronizedMap(new LinkedHashMap<String, XmlDataCache>() {
+ private static final long serialVersionUID = -8461839969909678055L;
+
+ @Override
+ protected boolean removeEldestEntry(Map.Entry<String, XmlDataCache> eldest) {
+ return size() > LIST_CACHE_MAX_ENTRIES;
+ }
+ });
+
+ private static final Serializer serializer = new Persister();
+
+ private class XmlDataCache extends GenericDataCache<byte[]> {
+
+ private final String locationId;
+
+ public XmlDataCache(String locations) {
+ super(LIST_CACHE_MS);
+ this.locationId = locations;
+ }
@Override
protected byte[] update() throws Exception {
- VmChooserListXml listXml = DbLecture.getUsableListXml(false);
+ VmChooserListXml listXml = DbLecture.getUsableListXml(false, locationId);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
serializer.write(listXml, baos);
return baos.toByteArray();
}
- };
+
+ }
public WebServer(int port) {
super("127.0.0.1", port);
super.maxRequestSize = 65535;
}
+ /**
+ * Extract request source ip address. Honors the x-forwarded-for header.
+ *
+ * @param headers map of headers as supplied by nanohttpd
+ * @return IP address, or empty string if unknown
+ */
+ private String extractIp(Map<String, String> headers) {
+ String ip;
+ ip = headers.get("remote-addr");
+ if (ip != null && !ip.equals("127.0.0.1"))
+ return ip;
+ if (headers == null || headers.isEmpty())
+ return "";
+ ip = headers.get("x-forwarded-for");
+ if (ip == null || ip.isEmpty())
+ return "";
+ final int i = ip.lastIndexOf(',');
+ if (i == -1)
+ return ip.trim();
+ return ip.substring(i + 1).trim();
+ }
+
@Override
public Response serve(IHTTPSession session) {
String uri = session.getUri();
@@ -61,7 +107,7 @@ public class WebServer extends NanoHTTPD {
private Response handle(IHTTPSession session, String uri) {
// Our special stuff
if (uri.startsWith("/vmchooser/list")) {
- return serveVmChooserList();
+ return serveVmChooserList(session.getParms());
}
if (uri.startsWith("/vmchooser/lecture/")) {
return serveLectureStart(uri.substring(19));
@@ -106,9 +152,15 @@ public class WebServer extends NanoHTTPD {
new ByteArrayInputStream(meta.getFilteredDefinitionArray()));
}
- private Response serveVmChooserList() {
+ private Response serveVmChooserList(Map<String, String> params) {
+ String locations = params.get("locations");
+ XmlDataCache cache = lectureListCache.get(locations);
+ if (cache == null) {
+ cache = new XmlDataCache(locations);
+ lectureListCache.put(locations, cache);
+ }
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "text/xml; charset=utf-8",
- new ByteArrayInputStream(lectureListCache.get()));
+ new ByteArrayInputStream(cache.get()));
}
public static Response internalServerError() {