blob: 765587e955b05a0f152964c21fbc42048838e2a1 (
plain) (
tree)
|
|
package org.openslx.bwlp.sat.web;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.SQLException;
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.TNotFoundException;
import org.openslx.util.GenericDataCache;
import org.openslx.util.Json;
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 Logger LOGGER = Logger.getLogger(WebServer.class);
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);
maxRequestSize = 65535;
}
@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));
}
if (uri.startsWith("/status/fileserver")) {
return serveStatus();
}
if (session.getMethod() == Method.POST && uri.startsWith("/do/")) {
try {
session.parseBody(null);
} catch (IOException | ResponseException e) {
LOGGER.debug("could not parse request body", e);
return internalServerError();
}
return WebRpc.handle(uri.substring(4), session.getParms());
}
return notFound();
}
private Response serveStatus() {
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, "application/json; charset=utf-8",
Json.serialize(FileServer.instance().getStatus()));
}
/**
* Return meta data (eg. *.vmx) required to start the given lecture.
*
* @param lectureId
* @return
*/
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()));
}
public static Response internalServerError() {
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.INTERNAL_ERROR, "text/plain",
"Internal Server Error");
}
public static Response notFound() {
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.NOT_FOUND, "text/plain", "Nicht gefunden!");
}
public static Response badRequest(String message) {
if (message == null) {
message = "Schlechte Anfrage!";
}
return new NanoHTTPD.Response(NanoHTTPD.Response.Status.BAD_REQUEST, "text/plain", message);
}
}
|