package org.openautonomousconnection.webserver.api; import org.openautonomousconnection.protocol.packets.v1_0_1.beta.web.impl.resource.WebResourceResponsePacket; import org.openautonomousconnection.webserver.Main; 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; /** * Server-side Java page (v1.0.1-BETA). * *

Every .java page must extend this class.

*/ public abstract class WebPage { private static final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); private static final DocumentBuilder documentBuilder; static { DocumentBuilder temp = null; try { temp = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { Main.getValues().logger.exception("Failed to create document builder", e); temp = null; } documentBuilder = temp; } protected final Document document; protected final HTMLHeadElement head; protected final HTMLBodyElement body; /** * 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); } /** * Handles a web request. * * @param ctx context (client, request, session) * @return resource response packet * @throws Exception on unexpected failures */ public abstract WebResourceResponsePacket handle(WebPageContext ctx) throws Exception; }