-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGateway.java
More file actions
48 lines (43 loc) · 1.54 KB
/
Gateway.java
File metadata and controls
48 lines (43 loc) · 1.54 KB
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
package httpsrv;
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.concurrent.Executors;
public class Gateway {
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("usage: gateway <target>");
System.exit(1);
}
var target = args[0];
var executor = Executors.newVirtualThreadPerTaskExecutor();
var client = HttpClient.newBuilder()
.executor(executor)
.connectTimeout(Duration.ofSeconds(5))
.build();
var request = HttpRequest.newBuilder()
.GET()
.uri(URI.create(target))
.build();
var server = HttpServer.create(new InetSocketAddress(8080), 0);
server.setExecutor(executor);
server.createContext("/").setHandler(exchange -> {
try {
var body = client.send(request, HttpResponse.BodyHandlers.ofByteArray()).body();
exchange.sendResponseHeaders(200, body.length);
try (var os = exchange.getResponseBody()) {
os.write(body);
}
} catch (InterruptedException e) {
throw new IOException(e);
}
});
server.start();
System.out.println("ready");
}
}