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);
}
@Override
public boolean exactPage() {
return false;
}
}

View file

@ -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("<h1>500 Internal Server Error</h1><pre>");
e.printStackTrace(str);
str.print("</pre>");
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("<h1>500 Internal Server Error</h1><pre>");
e.printStackTrace(str);
str.print("</pre>");
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;
}
}