Files
WebServer/src/main/java/org/openautonomousconnection/webserver/runtime/JavaPageDispatcher.java

84 lines
3.2 KiB
Java
Raw Normal View History

2025-12-12 21:16:13 +01:00
package org.openautonomousconnection.webserver.runtime;
2026-01-18 18:41:59 +01:00
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebRequestPacket;
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebResponsePacket;
2026-02-01 19:28:45 +01:00
import org.openautonomousconnection.protocol.side.server.CustomConnectedClient;
2025-12-12 21:16:13 +01:00
import org.openautonomousconnection.protocol.side.web.ProtocolWebServer;
2026-01-19 14:23:53 +01:00
import org.openautonomousconnection.webserver.WebServer;
2025-12-12 21:16:13 +01:00
import org.openautonomousconnection.webserver.api.WebPage;
import org.openautonomousconnection.webserver.api.WebPageContext;
2026-01-19 14:23:53 +01:00
import org.openautonomousconnection.webserver.utils.WebHasher;
import org.openautonomousconnection.webserver.utils.RequestParams;
2025-12-12 21:16:13 +01:00
import java.io.File;
2026-01-19 14:23:53 +01:00
import java.nio.charset.StandardCharsets;
import java.util.Map;
2025-12-12 21:16:13 +01:00
2026-01-19 14:23:53 +01:00
/**
* Dispatches Java WebPages using @Route annotation.
*/
2025-12-12 21:16:13 +01:00
public final class JavaPageDispatcher {
2026-01-19 14:23:53 +01:00
private static final JavaRouteRegistry ROUTES = new JavaRouteRegistry();
2025-12-12 21:16:13 +01:00
private JavaPageDispatcher() {}
public static WebResponsePacket dispatch(
2026-02-01 19:28:45 +01:00
CustomConnectedClient client,
2025-12-12 21:16:13 +01:00
ProtocolWebServer server,
WebRequestPacket request
) throws Exception {
if (request.getPath() == null) return null;
2026-01-19 14:23:53 +01:00
String route = request.getPath();
if (!route.startsWith("/")) route = "/" + route;
2025-12-12 21:16:13 +01:00
2026-01-19 14:23:53 +01:00
File contentRoot = server.getContentFolder();
ROUTES.refreshIfNeeded(contentRoot);
2025-12-12 21:16:13 +01:00
2026-01-19 14:23:53 +01:00
JavaRouteRegistry.RouteLookupResult found = ROUTES.find(route);
// If no @Route match, still detect "requested a .java-backed path by filename"
// (legacy behavior): if a matching *.java exists, compile/load but return error.
if (found == null) {
String p = route.startsWith("/") ? route.substring(1) : route;
File javaFile = new File(contentRoot, p + ".java");
if (javaFile.exists() && javaFile.isFile()) {
// Compile/load but do not serve.
new JavaPageCache().getOrCompile(javaFile);
return error(501, "Java class exists but has no @Route: " + route);
}
return null; // not a Java route -> let file server handle it
}
// If it has @Route but is not a WebPage, compile/load but return error.
if (!found.routable()) {
return error(500, "Class has @Route but is not a WebPage: " + found.fqcn());
}
// Load and execute
Class<?> clazz = Class.forName(found.fqcn());
2025-12-12 21:16:13 +01:00
Object instance = clazz.getDeclaredConstructor().newInstance();
2026-01-19 14:23:53 +01:00
if (!(instance instanceof WebPage page)) {
return error(500, "Routed class is not a WebPage: " + found.fqcn());
}
2025-12-12 21:16:13 +01:00
2026-01-19 14:23:53 +01:00
WebHasher hasher = (server instanceof WebServer ws) ? ws.getHasher() : null;
if (hasher == null) return error(500, "WebHasher missing on server instance.");
WebPageContext ctx = new WebPageContext(client, server, request, new RequestParams(request), hasher);
2025-12-12 21:16:13 +01:00
return page.handle(ctx);
}
2026-01-19 14:23:53 +01:00
private static WebResponsePacket error(int code, String msg) {
return new WebResponsePacket(
code,
"text/plain; charset=utf-8",
Map.of(),
msg.getBytes(StandardCharsets.UTF_8)
);
}
2025-12-12 21:16:13 +01:00
}