59 lines
1.8 KiB
Java
59 lines
1.8 KiB
Java
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).
|
|
*
|
|
* <p>Every .java page must extend this class.</p>
|
|
*/
|
|
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");
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
} |