Files
WebServer/src/main/java/org/openautonomousconnection/webserver/api/WebPage.java

63 lines
1.9 KiB
Java
Raw Normal View History

2025-12-12 21:16:13 +01:00
package org.openautonomousconnection.webserver.api;
2026-02-22 17:26:22 +01:00
import org.openautonomousconnection.protocol.packets.v1_0_1.beta.web.impl.resource.WebResourceResponsePacket;
import org.openautonomousconnection.webserver.Main;
2026-03-02 17:12:49 +01:00
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.html.HTMLBodyElement;
import org.w3c.dom.html.HTMLDocument;
import org.w3c.dom.html.HTMLHeadElement;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
2025-12-12 21:16:13 +01:00
/**
2026-02-22 17:26:22 +01:00
* Server-side Java page (v1.0.1-BETA).
*
2026-03-02 17:12:49 +01:00
* <p>Every .java page must extend this class.</p>
2025-12-12 21:16:13 +01:00
*/
2026-03-02 17:12:49 +01:00
public abstract class WebPage {
private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
private static final DocumentBuilder documentBuilder;
static {
DocumentBuilder temp = null;
2026-03-02 17:12:49 +01:00
try {
temp = factory.newDocumentBuilder();
2026-03-02 17:12:49 +01:00
} catch (ParserConfigurationException e) {
Main.getValues().logger.exception("Failed to create document builder", e);
temp = null;
2026-03-02 17:12:49 +01:00
}
documentBuilder = temp;
}
2026-03-02 17:12:49 +01:00
protected final Document document;
protected final HTMLHeadElement head;
protected final HTMLBodyElement body;
2026-03-02 17:12:49 +01:00
/**
* Default constructor that creates a new Document instance
*/
public WebPage() {
this.document = documentBuilder.newDocument();
this.head = (HTMLHeadElement) this.document.createElement("head");
this.body = (HTMLBodyElement) this.document.createElement("body");
this.document.appendChild(this.head);
this.document.appendChild(this.body);
2026-03-02 17:12:49 +01:00
}
2025-12-12 21:16:13 +01:00
/**
* Handles a web request.
*
* @param ctx context (client, request, session)
2026-02-22 17:26:22 +01:00
* @return resource response packet
* @throws Exception on unexpected failures
2025-12-12 21:16:13 +01:00
*/
2026-03-02 17:12:49 +01:00
public abstract WebResourceResponsePacket handle(WebPageContext ctx) throws Exception;
2026-02-22 17:26:22 +01:00
}