blob: be6807994fae1d2a1edba34160cf260f5b331ca7 (
plain) (
tree)
|
|
package org.openslx.bwlp.sat.web;
import java.io.ByteArrayInputStream;
import java.sql.SQLException;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.openslx.bwlp.sat.database.mappers.DbLecture;
import org.openslx.bwlp.thrift.iface.TNotFoundException;
import org.openslx.util.GenericDataCache;
import org.openslx.util.vm.VmMetaData;
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("127.0.0.1", port);
}
@Override
public Response serve(IHTTPSession session) {
String uri = session.getUri();
if (uri == null || uri.length() == 0) {
return internalServerError();
}
// Our special stuff
if (uri.startsWith("/vmchooser/list")) {
return serveVmChooserList();
}
if (uri.startsWith("/vmchooser/lecture/")) {
return serveLectureStart(uri.substring(19));
}
return notFound();
}
private Response notFound() {
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.NOT_FOUND, "text/plain", "Nicht gefunden!");
}
private Response serveLectureStart(String lectureId) {
VmMetaData meta;
try {
meta = DbLecture.getClientLaunchData(lectureId);
} catch (TNotFoundException e) {
return notFound();
} catch (SQLException e) {
return internalServerError();
}
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "text/plain; charset=utf-8",
new ByteArrayInputStream(meta.getFilteredDefinitionArray()));
}
private Response serveVmChooserList() {
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "text/xml; charset=utf-8",
new ByteArrayInputStream(lectureListCache.get()));
}
private Response internalServerError() {
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.INTERNAL_ERROR, "text/plain",
"Internal Server Error");
}
}
|