diff --git a/src/buttondevteam/website/page/BridgePage.java b/src/buttondevteam/website/page/BridgePage.java index 2d2b272..cc47c9a 100644 --- a/src/buttondevteam/website/page/BridgePage.java +++ b/src/buttondevteam/website/page/BridgePage.java @@ -89,4 +89,9 @@ public class BridgePage extends Page { } connections.values().remove(socket); } + + @Override + public boolean exactPage() { + return false; + } } diff --git a/src/buttondevteam/website/page/Page.java b/src/buttondevteam/website/page/Page.java index 0df2d80..a669067 100644 --- a/src/buttondevteam/website/page/Page.java +++ b/src/buttondevteam/website/page/Page.java @@ -1,46 +1,55 @@ -package buttondevteam.website.page; - -import java.io.PrintStream; -import org.apache.commons.io.output.ByteArrayOutputStream; - -import com.sun.net.httpserver.*; - -import buttondevteam.lib.TBMCCoreAPI; -import buttondevteam.website.io.IOHelper; -import buttondevteam.website.io.Response; - -/** - * Add to {@link Main}.Pages - */ -public abstract class Page implements HttpHandler { - public abstract String GetName(); - - @Override - public void handle(HttpExchange exchange) { - try { - exchange.getResponseHeaders().add("Access-Control-Allow-Origin", "https://tbmcplugins.github.io"); - if (exchange.getRequestURI().getPath().equals("/" + GetName())) - IOHelper.SendResponse(handlePage(exchange)); - else { - IOHelper.SendResponse(404, "404 Not found: " + exchange.getRequestURI().getPath(), exchange); - } - } catch (Exception e) { - TBMCCoreAPI.SendException("Internal Server Error in ButtonWebsiteModule!", e); - try { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream str = new PrintStream(baos); - str.print("

500 Internal Server Error

");
-				e.printStackTrace(str);
-				str.print("
"); - IOHelper.SendResponse(500, baos.toString("UTF-8"), exchange); - } catch (Exception e1) { - TBMCCoreAPI.SendException("Exception while sending Internal Server Error in ButtonWebsiteModule!", e1); - } - } - } - - /** - * The main logic of the endpoint. Use IOHelper to retrieve the message sent and other things. - */ - public abstract Response handlePage(HttpExchange exchange); -} +package buttondevteam.website.page; + +import java.io.PrintStream; +import org.apache.commons.io.output.ByteArrayOutputStream; + +import com.sun.net.httpserver.*; + +import buttondevteam.lib.TBMCCoreAPI; +import buttondevteam.website.io.IOHelper; +import buttondevteam.website.io.Response; + +/** + * Add to {@link Main}.Pages + */ +public abstract class Page implements HttpHandler { + public abstract String GetName(); + + @Override + public final void handle(HttpExchange exchange) { + try { + exchange.getResponseHeaders().add("Access-Control-Allow-Origin", "https://tbmcplugins.github.io"); + if (exactPage() ? exchange.getRequestURI().getPath().equals("/" + GetName()) : true) + IOHelper.SendResponse(handlePage(exchange)); + else { + IOHelper.SendResponse(404, "404 Not found: " + exchange.getRequestURI().getPath(), exchange); + } + } catch (Exception e) { + TBMCCoreAPI.SendException("Internal Server Error in ButtonWebsiteModule!", e); + try { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream str = new PrintStream(baos); + str.print("

500 Internal Server Error

");
+				e.printStackTrace(str);
+				str.print("
"); + IOHelper.SendResponse(500, baos.toString("UTF-8"), exchange); + } catch (Exception e1) { + TBMCCoreAPI.SendException("Exception while sending Internal Server Error in ButtonWebsiteModule!", e1); + } + } + } + + /** + * The main logic of the endpoint. Use IOHelper to retrieve the message sent and other things. + */ + public abstract Response handlePage(HttpExchange exchange); + + /** + * Whether to return 404 when the URL doesn't match the exact path + * + * @return + */ + public boolean exactPage() { + return true; + } +}