summaryrefslogblamecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/web/WebServer.java
blob: a7a1330588d1c067e0a6f48678731c2e1ed1159e (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                                 







                                                          



                                          











                                                                                                              






                                                     
 












                                                                                                                    

                                                                                                             







                                                                                                     
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);
	}

	@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();
		}

		return new NanoHTTPD.Response(NanoHTTPD.Response.Status.NOT_FOUND, "text/plain", "Nicht gefunden!");
	}

	private Response serveVmChooserList() {
		return new NanoHTTPD.Response(NanoHTTPD.Response.Status.NOT_FOUND, "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");
	}

}