summaryrefslogtreecommitdiffstats
path: root/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/thrift/JsonHttpListener.java
blob: 55c097568796d91be500a2c48900ded617984319 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package org.openslx.bwlp.sat.thrift;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.thrift.protocol.TJSONProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TMemoryBuffer;
import org.openslx.bwlp.thrift.iface.SatelliteServer;
import org.openslx.util.Util;

import fi.iki.elonen.NanoHTTPD;

public class JsonHttpListener extends NanoHTTPD {

	private static final Logger LOGGER = LogManager.getLogger(JsonHttpListener.class);

	private final SatelliteServer.Processor<ServerHandler> processor = new SatelliteServer.Processor<ServerHandler>(
			new ServerHandler());

	public JsonHttpListener(int port, ExecutorService es) throws IOException {
		super("127.0.0.1", port, es);
		this.maxRequestSize = 1_000_000;
	}

	@Override
	public Response serve(IHTTPSession session) {
		Response res = serveInternal(session);
		if (res != null) {
			addCorsHeaders(res);
		}
		return res;
	}

	private Response serveInternal(IHTTPSession session) {
		Method method = session.getMethod();
		if (Method.OPTIONS.equals(method))
			return new Response(Response.Status.NO_CONTENT, "application/json", "");
		if (!Method.PUT.equals(method) && !Method.POST.equals(method))
			return new Response(Response.Status.BAD_REQUEST, "text/plain; charset=UTF-8",
					"Method not supported");

		try {
			//Input
			String str = session.getHeaders().get("content-length");
			int len = 0;
			if (str != null) {
				len = Util.parseInt(str, 0);
			}
			if (len <= 0) {
				len = session.getInputStream().available();
			}
			if (len <= 0)
				return new Response(Response.Status.BAD_REQUEST, "text/plain; charset=UTF-8",
						"No Content-Length provided");

			byte[] buffer = session.getInputStream().readNBytes(len);
			TMemoryBuffer inbuffer = new TMemoryBuffer(buffer.length);
			inbuffer.write(buffer);
			TProtocol inprotocol = new TJSONProtocol(inbuffer);

			//Output
			TMemoryBuffer outbuffer = new TMemoryBuffer(900);
			TProtocol outprotocol = new TJSONProtocol(outbuffer);

			processor.process(inprotocol, outprotocol);

			buffer = Arrays.copyOf(outbuffer.getArray(), outbuffer.length());

			return new Response(Response.Status.OK, "application/json", buffer);
		} catch (Throwable t) {
			if (!t.getMessage().contains("Remote side has closed")) {
				LOGGER.warn("Error handling HTTP thrift", t);
			}
			return new Response(Response.Status.INTERNAL_ERROR, "text/plain; charset=UTF-8", t.getMessage());
		}
	}

	@Override
	public void serverStopped() {
		System.exit(1);
	}

	private static void addCorsHeaders(Response response) {
		response.addHeader("Allow", "OPTIONS, GET, HEAD, POST, PUT");
		response.addHeader("Access-Control-Allow-Methods", "*");
		response.addHeader("Access-Control-Allow-Origin", "*");
		response.addHeader("Access-Control-Allow-Headers", "*, Content-Type");
		response.addHeader("Access-Control-Max-Age", "86400");
	}

}