Changed Web Routing and added WebHash
This commit is contained in:
@@ -4,14 +4,22 @@ import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebRequestP
|
||||
import org.openautonomousconnection.protocol.packets.v1_0_0.beta.web.WebResponsePacket;
|
||||
import org.openautonomousconnection.protocol.side.web.ConnectedWebClient;
|
||||
import org.openautonomousconnection.protocol.side.web.ProtocolWebServer;
|
||||
import org.openautonomousconnection.webserver.WebServer;
|
||||
import org.openautonomousconnection.webserver.api.WebPage;
|
||||
import org.openautonomousconnection.webserver.api.WebPageContext;
|
||||
import org.openautonomousconnection.webserver.utils.WebHasher;
|
||||
import org.openautonomousconnection.webserver.utils.RequestParams;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Dispatches Java WebPages using @Route annotation.
|
||||
*/
|
||||
public final class JavaPageDispatcher {
|
||||
|
||||
private static final JavaPageCache CACHE = new JavaPageCache();
|
||||
private static final JavaRouteRegistry ROUTES = new JavaRouteRegistry();
|
||||
|
||||
private JavaPageDispatcher() {}
|
||||
|
||||
@@ -23,18 +31,53 @@ public final class JavaPageDispatcher {
|
||||
|
||||
if (request.getPath() == null) return null;
|
||||
|
||||
String p = request.getPath().startsWith("/") ? request.getPath().substring(1) : request.getPath();
|
||||
File javaFile = new File(server.getContentFolder(), p + ".java");
|
||||
String route = request.getPath();
|
||||
if (!route.startsWith("/")) route = "/" + route;
|
||||
|
||||
if (!javaFile.exists() || !javaFile.isFile()) return null;
|
||||
File contentRoot = server.getContentFolder();
|
||||
ROUTES.refreshIfNeeded(contentRoot);
|
||||
|
||||
Class<?> clazz = CACHE.getOrCompile(javaFile);
|
||||
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());
|
||||
Object instance = clazz.getDeclaredConstructor().newInstance();
|
||||
|
||||
if (!(instance instanceof WebPage page))
|
||||
throw new IllegalStateException("Java page must implement WebPage");
|
||||
if (!(instance instanceof WebPage page)) {
|
||||
return error(500, "Routed class is not a WebPage: " + found.fqcn());
|
||||
}
|
||||
|
||||
WebPageContext ctx = new WebPageContext(client, server, request);
|
||||
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);
|
||||
return page.handle(ctx);
|
||||
}
|
||||
|
||||
private static WebResponsePacket error(int code, String msg) {
|
||||
return new WebResponsePacket(
|
||||
code,
|
||||
"text/plain; charset=utf-8",
|
||||
Map.of(),
|
||||
msg.getBytes(StandardCharsets.UTF_8)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user