Skip to content

Commit

Permalink
Merge pull request #15 from gnmyt/fixes/response-handler
Browse files Browse the repository at this point in the history
🐛 Bug-Fix in the ResponseHandler
  • Loading branch information
gnmyt authored Jun 28, 2023
2 parents 1db7477 + 4f0a936 commit ca1e08e
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 175 deletions.
10 changes: 10 additions & 0 deletions src/main/java/de/gnmyt/mcdash/MinecraftDashboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class MinecraftDashboard extends JavaPlugin {

private static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
private static ConfigurationManager config;
private static BackupController backupController;
private static AccountManager accountManager;
Expand Down Expand Up @@ -140,4 +142,12 @@ public static AccountManager getAccountManager() {
public static BackupController getBackupController() {
return backupController;
}

/**
* Gets the executor
* @return the executor
*/
public static ScheduledExecutorService getExecutor() {
return executor;
}
}
159 changes: 62 additions & 97 deletions src/main/java/de/gnmyt/mcdash/api/handler/DefaultHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public abstract class DefaultHandler implements HttpHandler {

private Request request = null;
private ResponseController controller = null;

public ConfigurationManager manager = MinecraftDashboard.getDashboardConfig();
public AccountManager accountManager = MinecraftDashboard.getAccountManager();

Expand All @@ -40,39 +35,37 @@ public String path() {
*/
@Override
public void handle(HttpExchange exchange) {
MinecraftDashboard.getExecutor().execute(() -> {
Request request = prepareRequest(exchange, true);
ResponseController controller = new ResponseController(exchange);

List<String> authHeader = request.getHeaders().get("Authorization");
if (authHeader == null) {
controller.code(400).message("You need to provide your credentials");
return;
}

request = prepareRequest(exchange, true);

controller = new ResponseController(exchange);

List<String> authHeader = request.getHeaders().get("Authorization");

if (authHeader == null) {
controller.code(400).message("You need to provide your credentials");
return;
}

String[] authCredentials;

try {
authCredentials = new String(Base64.getDecoder().decode(authHeader.get(0)
.replace("Basic ", ""))).split(":");
} catch (Exception e) {
controller.code(400).message("You need to provide your credentials");
return;
}
String[] authCredentials;
try {
authCredentials = new String(Base64.getDecoder().decode(authHeader.get(0)
.replace("Basic ", ""))).split(":");
} catch (Exception e) {
controller.code(400).message("You need to provide your credentials");
return;
}

if (authCredentials.length != 2) {
controller.code(400).message("You need to provide your credentials");
return;
}
if (authCredentials.length != 2) {
controller.code(400).message("You need to provide your credentials");
return;
}

if (!accountManager.isValidPassword(authCredentials[0], authCredentials[1])) {
controller.code(401).message("The provided credentials are invalid");
return;
}
if (!accountManager.isValidPassword(authCredentials[0], authCredentials[1])) {
controller.code(401).message("The provided credentials are invalid");
return;
}

CompletableFuture.runAsync(() -> execute(request, controller));
execute(request, controller);
});
}

/**
Expand Down Expand Up @@ -166,7 +159,6 @@ public void register() {
* @return The prepared request
*/
protected Request prepareRequest(HttpExchange exchange, boolean writeBody) {

StringWriter writer = new StringWriter();

if (writeBody) {
Expand Down Expand Up @@ -204,21 +196,23 @@ public void runSync(Runnable runnable) {
}

/**
* Gets an string from the body
* Gets a string from the body
* @param request The request object from the HttpExchange
* @param name The name of the value you want to get
* @return the value (string)
*/
public String getStringFromBody(String name) {
public String getStringFromBody(Request request, String name) {
return request.getBody().get(name);
}

/**
* Gets an integer from the body
* @param request The request object from the HttpExchange
* @param name The name of the value you want to get
* @return the value (integer)
*/
public Integer getIntegerFromBody(String name) {
String value = getStringFromBody(name);
public Integer getIntegerFromBody(Request request, String name) {
String value = getStringFromBody(request, name);
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
Expand All @@ -227,53 +221,49 @@ public Integer getIntegerFromBody(String name) {
}

/**
* Gets an boolean from the body
* Gets a boolean from the body
* @param request The request object from the HttpExchange
* @param name The name of the value you want to get
* @return the value (boolean)
*/
public Boolean getBooleanFromBody(String name) {
return Boolean.parseBoolean(getStringFromBody(name));
public Boolean getBooleanFromBody(Request request, String name) {
return Boolean.parseBoolean(getStringFromBody(request, name));
}

/**
* Gets an string from the query
* Gets a string from the query
* @param request The request object from the HttpExchange
* @param name The name of the value you want to get
* @return the value (string)
*/
public String getStringFromQuery(String name) {
public String getStringFromQuery(Request request, String name) {
return request.getQuery().get(name);
}

/**
* Gets an integer from the query
* @param request The request object from the HttpExchange
* @param name The name of the value you want to get
* @return the value (integer)
*/
public Integer getIntegerFromQuery(String name) {
String value = getStringFromQuery(name);
public Integer getIntegerFromQuery(Request request, String name) {
String value = getStringFromQuery(request, name);
try {
return Integer.parseInt(value);
} catch (Exception e) {
return null;
}
}

/**
* Gets a boolean from the query
* @param name The name of the value you want to get
* @return the value (boolean)
*/
public Boolean getBooleanFromQuery(String name) {
return Boolean.parseBoolean(getStringFromQuery(name));
}

/**
* Checks if a string is in the body
* @param request The request object from the HttpExchange
* @param controller The response controller from the HttpExchange
* @param name The name of the value you want to check
* @return <code>true</code> if the string is in the body, otherwise <code>false</code>
*/
public boolean isStringInBody(String name) {
String value = getStringFromBody(name);
public boolean isStringInBody(Request request, ResponseController controller, String name) {
String value = getStringFromBody(request, name);
if (value == null || value.isEmpty()) {
controller.code(400).messageFormat("You need to provide %s in your request body", name);
return false;
Expand All @@ -282,13 +272,15 @@ public boolean isStringInBody(String name) {
}

/**
* Checks if a integer is in the body
* Checks if an integer is in the body
* @param request The request object from the HttpExchange
* @param controller The response controller from the HttpExchange
* @param name The name of the value you want to check
* @return <code>true</code> if the integer is in the body, otherwise <code>false</code>
*/
public boolean isIntegerInBody(String name) {
if (!isStringInBody(name)) return false;
Integer value = getIntegerFromBody(name);
public boolean isIntegerInBody(Request request, ResponseController controller, String name) {
if (!isStringInBody(request, controller, name)) return false;
Integer value = getIntegerFromBody(request, name);
if (value == null) {
controller.code(400).messageFormat("%s must be an integer", name);
}
Expand All @@ -297,12 +289,14 @@ public boolean isIntegerInBody(String name) {

/**
* Checks if a boolean is in the body
* @param request The request object from the HttpExchange
* @param controller The response controller from the HttpExchange
* @param name The name of the value you want to check
* @return <code>true</code> if the boolean is in the body, otherwise <code>false</code>
*/
public boolean isBooleanInBody(String name) {
if (!isStringInBody(name)) return false;
String value = getStringFromBody(name);
public boolean isBooleanInBody(Request request, ResponseController controller, String name) {
if (!isStringInBody(request, controller, name)) return false;
String value = getStringFromBody(request, name);
try {
if (value.equals("true") || value.equals("false")) return true;
throw new Exception();
Expand All @@ -314,47 +308,18 @@ public boolean isBooleanInBody(String name) {

/**
* Checks if a string is in the query
* @param request The request object from the HttpExchange
* @param controller The response controller from the HttpExchange
* @param name The name of the value you want to check
* @return <code>true</code> if the string is in the query, otherwise <code>false</code>
*/
public boolean isStringInQuery(String name) {
String value = getStringFromQuery(name);
public boolean isStringInQuery(Request request, ResponseController controller, String name) {
String value = getStringFromQuery(request, name);
if (value == null || value.isEmpty()) {
controller.code(400).messageFormat("You need to provide %s in your request query", name);
return false;
}
return true;
}

/**
* Checks if a integer is in the query
* @param name The name of the value you want to check
* @return <code>true</code> if the integer is in the query, otherwise <code>false</code>
*/
public boolean isIntegerInQuery(String name) {
if (!isStringInQuery(name)) return false;
Integer value = getIntegerFromQuery(name);
if (value == null) {
controller.code(400).messageFormat("%s must be an integer", name);
}
return value != null;
}

/**
* Checks if a boolean is in the query
* @param name The name of the value you want to check
* @return <code>true</code> if the boolean is in the query, otherwise <code>false</code>
*/
public Boolean isBooleanInQuery(String name) {
if (!isStringInQuery(name)) return false;
String value = getStringFromQuery(name);
try {
if (value.equals("true") || value.equals("false")) return true;
throw new Exception();
} catch (Exception e) {
controller.code(400).messageFormat("%s must be an boolean", name);
return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void send() {
os.write(bs);
}
os.close();
} catch (IOException e) {
} catch (IOException ignored) {
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/de/gnmyt/mcdash/panel/routes/ConsoleRoute.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public String path() {
@Override
public void get(Request request, ResponseController response) throws Exception {

int startLine = getIntegerFromQuery("startLine") != null ? getIntegerFromQuery("startLine") : 1;
int limit = getIntegerFromQuery("limit") != null ? getIntegerFromQuery("limit") : 500;
int startLine = getIntegerFromQuery(request, "startLine") != null ? getIntegerFromQuery(request, "startLine") : 1;
int limit = getIntegerFromQuery(request, "limit") != null ? getIntegerFromQuery(request, "limit") : 500;

Object[] lines = Files.lines(Paths.get("logs//latest.log"))
.skip(startLine)
Expand All @@ -47,11 +47,11 @@ public void get(Request request, ResponseController response) throws Exception {
*/
@Override
public void post(Request request, ResponseController response) {
if (!isStringInBody("command")) return;
if (!isStringInBody(request, response, "command")) return;

Bukkit.getLogger().warning("Executing command \"" + getStringFromBody("command") + "\"..");
Bukkit.getLogger().warning("Executing command \"" + getStringFromBody(request, "command") + "\"..");

runSync(() -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), getStringFromBody("command")));
runSync(() -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), getStringFromBody(request, "command")));

response.message("Action executed.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public void get(Request request, ResponseController response) throws Exception {
*/
@Override
public void patch(Request request, ResponseController response) throws Exception {
if (!isBooleanInBody("status")) return;
if (!isBooleanInBody(request, response, "status")) return;

runSync(() -> {
Bukkit.setWhitelist(getBooleanFromBody("status"));
response.message("Whitelist successfully " + (getBooleanFromBody("status") ? "enabled" : "disabled"));
Bukkit.setWhitelist(getBooleanFromBody(request, "status"));
response.message("Whitelist successfully " + (getBooleanFromBody(request, "status") ? "enabled" : "disabled"));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public String path() {
*/
@Override
public void get(Request request, ResponseController response) throws Exception {
if (!isStringInQuery("backup_id")) return;
if (!isStringInQuery(request, response, "backup_id")) return;

String backupId = getStringFromQuery("backup_id");
String backupId = getStringFromQuery(request, "backup_id");

if (!controller.backupExists(backupId)) {
response.code(404).message("Backup not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public String path() {
*/
@Override
public void post(Request request, ResponseController response) throws Exception {
if (!isStringInBody("backup_id")) return;
if (!isBooleanInBody("halt")) return;
if (!isStringInBody(request, response, "backup_id")) return;
if (!isBooleanInBody(request, response, "halt")) return;

String backupId = getStringFromBody("backup_id");
boolean restart = getBooleanFromBody("halt");
String backupId = getStringFromBody(request, "backup_id");
boolean restart = getBooleanFromBody(request, "halt");

if (!controller.backupExists(backupId)) {
response.code(404).message("Backup not found");
Expand Down
Loading

0 comments on commit ca1e08e

Please sign in to comment.