Skip to content

Commit 77f4e3d

Browse files
committed
Add Windows HTTP frontend
1 parent 9b8a059 commit 77f4e3d

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package windows;
2+
3+
import com.sun.net.httpserver.HttpExchange;
4+
import com.sun.net.httpserver.HttpHandler;
5+
6+
import java.io.IOException;
7+
import java.nio.charset.StandardCharsets;
8+
import java.util.Objects;
9+
10+
/** Minimal HTTP boundary for the Windows execution model. */
11+
public final class WindowsHttpFrontend implements HttpHandler {
12+
private final WindowsFrontendAdapter adapter;
13+
14+
public WindowsHttpFrontend(WindowsFrontendAdapter adapter) {
15+
this.adapter = Objects.requireNonNull(adapter, "adapter");
16+
}
17+
18+
@Override
19+
public void handle(HttpExchange exchange) throws IOException {
20+
if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) {
21+
exchange.sendResponseHeaders(405, -1);
22+
return;
23+
}
24+
25+
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
26+
WindowsCall call = WindowsCall.parse(body);
27+
WindowsExecution.Result result = adapter.accept(call).toCompletableFuture().join();
28+
byte[] response = result.toJson().getBytes(StandardCharsets.UTF_8);
29+
exchange.getResponseHeaders().set("Content-Type", "application/json; charset=utf-8");
30+
exchange.sendResponseHeaders(result.exitCode(), response.length);
31+
try (var output = exchange.getResponseBody()) {
32+
output.write(response);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)