Added not exact page support (/bridge/id)

This commit is contained in:
Norbi Peti 2018-01-02 14:05:15 +01:00
parent ad6126f460
commit 6f3b102a32
2 changed files with 60 additions and 46 deletions

View file

@ -89,4 +89,9 @@ public class BridgePage extends Page {
} }
connections.values().remove(socket); connections.values().remove(socket);
} }
@Override
public boolean exactPage() {
return false;
}
} }

View file

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