mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-12 15:56:00 +08:00
Outsource the Web APIs into their own separate classes
This commit is contained in:
parent
67152ea5cc
commit
971007b589
@ -40,10 +40,12 @@ import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
|
||||
import main.java.com.djrapitops.plan.locale.Locale;
|
||||
import main.java.com.djrapitops.plan.locale.Msg;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.WebServer;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.api.bukkit.*;
|
||||
import main.java.com.djrapitops.plan.utilities.Benchmark;
|
||||
import main.java.com.djrapitops.plan.utilities.Check;
|
||||
import main.java.com.djrapitops.plan.utilities.MiscUtils;
|
||||
import main.java.com.djrapitops.plan.utilities.metrics.BStats;
|
||||
import main.java.com.djrapitops.plan.utilities.webserver.api.WebAPIManager;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
@ -174,6 +176,7 @@ public class Plan extends BukkitPlugin<Plan> {
|
||||
if (analysisRefreshTaskIsEnabled) {
|
||||
startAnalysisRefreshTask(analysisRefreshMinutes);
|
||||
}
|
||||
|
||||
Benchmark.stop("Enable", "Analysis refresh task registration");
|
||||
|
||||
Benchmark.start("WebServer Initialization");
|
||||
@ -185,6 +188,7 @@ public class Plan extends BukkitPlugin<Plan> {
|
||||
|
||||
uiServer = new WebServer(this);
|
||||
if (webserverIsEnabled) {
|
||||
registerWebAPIs();
|
||||
uiServer.initServer();
|
||||
|
||||
if (!uiServer.isEnabled()) {
|
||||
@ -198,6 +202,7 @@ public class Plan extends BukkitPlugin<Plan> {
|
||||
if (!usingAlternativeIP && serverVariableHolder.getIp().isEmpty()) {
|
||||
Log.infoColor(Locale.get(Msg.ENABLE_NOTIFY_EMPTY_IP).toString());
|
||||
}
|
||||
|
||||
Benchmark.stop("Enable", "WebServer Initialization");
|
||||
|
||||
registerCommand(new PlanCommand(this));
|
||||
@ -293,6 +298,14 @@ public class Plan extends BukkitPlugin<Plan> {
|
||||
Benchmark.stop("Enable", "Register Listeners");
|
||||
}
|
||||
|
||||
private void registerWebAPIs() {
|
||||
WebAPIManager.registerNewAPI("analytics", new AnalyticsWebAPI());
|
||||
WebAPIManager.registerNewAPI("analyze", new AnalyzeWebAPI());
|
||||
WebAPIManager.registerNewAPI("configure", new ConfigureWebAPI());
|
||||
WebAPIManager.registerNewAPI("inspection", new InspectionWebAPI());
|
||||
WebAPIManager.registerNewAPI("inspect", new InspectWebAPI());
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the database according to settings in the config.
|
||||
* <p>
|
||||
@ -300,7 +313,7 @@ public class Plan extends BukkitPlugin<Plan> {
|
||||
*
|
||||
* @return true if init was successful, false if not.
|
||||
*/
|
||||
public boolean initDatabase() {
|
||||
private boolean initDatabase() {
|
||||
databases = new HashSet<>();
|
||||
databases.add(new MySQLDB(this));
|
||||
databases.add(new SQLiteDB(this));
|
||||
|
@ -5,7 +5,6 @@ import com.sun.net.httpserver.*;
|
||||
import main.java.com.djrapitops.plan.Log;
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.Settings;
|
||||
import main.java.com.djrapitops.plan.data.UserData;
|
||||
import main.java.com.djrapitops.plan.data.WebUser;
|
||||
import main.java.com.djrapitops.plan.data.cache.PageCacheHandler;
|
||||
import main.java.com.djrapitops.plan.database.tables.SecurityTable;
|
||||
@ -15,13 +14,13 @@ import main.java.com.djrapitops.plan.ui.html.DataRequestHandler;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.*;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.api.BadRequestResponse;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.api.JsonResponse;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.api.SuccessResponse;
|
||||
import main.java.com.djrapitops.plan.utilities.Benchmark;
|
||||
import main.java.com.djrapitops.plan.utilities.HtmlUtils;
|
||||
import main.java.com.djrapitops.plan.utilities.PassEncryptUtil;
|
||||
import main.java.com.djrapitops.plan.utilities.uuid.UUIDUtility;
|
||||
import main.java.com.djrapitops.plan.utilities.webserver.api.WebAPI;
|
||||
import main.java.com.djrapitops.plan.utilities.webserver.api.WebAPIManager;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import javax.net.ssl.*;
|
||||
import java.io.*;
|
||||
@ -294,92 +293,14 @@ public class WebServer {
|
||||
|
||||
Plan plan = Plan.getInstance();
|
||||
|
||||
String playerString;
|
||||
UUID uuid;
|
||||
String identifier;
|
||||
WebAPI api = WebAPIManager.getAPI(method);
|
||||
|
||||
switch (method) {
|
||||
//TODO Add Bungee APIs
|
||||
case "analyze":
|
||||
plan.getAnalysisCache().updateCache();
|
||||
return PageCacheHandler.loadPage("success", SuccessResponse::new);
|
||||
case "inspect":
|
||||
playerString = variables.get("player");
|
||||
|
||||
if (playerString == null) {
|
||||
String error = "Player String not included";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
uuid = UUIDUtility.getUUIDOf(playerString);
|
||||
|
||||
if (uuid == null) {
|
||||
String error = "UUID not found";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
Plan.getInstance().getInspectCache().cache(uuid);
|
||||
|
||||
return PageCacheHandler.loadPage("success", SuccessResponse::new);
|
||||
case "analysis":
|
||||
case "analytics":
|
||||
identifier = "analysisJson";
|
||||
if (!PageCacheHandler.isCached(identifier)) {
|
||||
return PageCacheHandler.loadPage("No Analysis Data", () -> new BadRequestResponse("No analysis data available"));
|
||||
}
|
||||
|
||||
return PageCacheHandler.loadPage(identifier);
|
||||
case "inspection":
|
||||
playerString = variables.get("player");
|
||||
|
||||
if (playerString == null) {
|
||||
String error = "Player String not included";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
uuid = UUIDUtility.getUUIDOf(playerString);
|
||||
|
||||
if (uuid == null) {
|
||||
String error = "UUID not found";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
UserData userData = plan.getInspectCache().getFromCache(uuid);
|
||||
|
||||
if (userData == null) {
|
||||
String error = "User not cached";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
return PageCacheHandler.loadPage("inspectionJson: " + uuid, () -> new JsonResponse(plan.getInspectCache().getFromCache(uuid)));
|
||||
case "configure":
|
||||
String key = variables.get("configKey");
|
||||
|
||||
if (key == null) {
|
||||
String error = "Config Key null";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
String value = variables.get("configValue");
|
||||
|
||||
if (value == null) {
|
||||
String error = "Config Value null";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
if (value.equals("null")) {
|
||||
value = null;
|
||||
}
|
||||
|
||||
FileConfiguration config = plan.getConfig();
|
||||
config.set(key, value);
|
||||
plan.saveConfig();
|
||||
|
||||
return PageCacheHandler.loadPage("success", SuccessResponse::new);
|
||||
default:
|
||||
String error = "API Method not found";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
if (api == null) {
|
||||
String error = "API Method not found";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
return api.onResponse(plan, variables);
|
||||
}
|
||||
|
||||
private Map<String, String> readVariables(String response) {
|
||||
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Licence is provided in the jar as license.yml also here:
|
||||
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
|
||||
*/
|
||||
package main.java.com.djrapitops.plan.ui.webserver.api.bukkit;
|
||||
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.cache.PageCacheHandler;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.Response;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.api.BadRequestResponse;
|
||||
import main.java.com.djrapitops.plan.utilities.webserver.api.WebAPI;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
public class AnalyticsWebAPI implements WebAPI {
|
||||
@Override
|
||||
public Response onResponse(Plan plan, Map<String, String> variables) {
|
||||
String identifier = "analysisJson";
|
||||
|
||||
if (!PageCacheHandler.isCached(identifier)) {
|
||||
return PageCacheHandler.loadPage("No Analysis Data", () -> new BadRequestResponse("No analysis data available"));
|
||||
}
|
||||
|
||||
return PageCacheHandler.loadPage(identifier);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Licence is provided in the jar as license.yml also here:
|
||||
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
|
||||
*/
|
||||
package main.java.com.djrapitops.plan.ui.webserver.api.bukkit;
|
||||
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.cache.PageCacheHandler;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.Response;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.api.SuccessResponse;
|
||||
import main.java.com.djrapitops.plan.utilities.webserver.api.WebAPI;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
public class AnalyzeWebAPI implements WebAPI {
|
||||
@Override
|
||||
public Response onResponse(Plan plan, Map<String, String> variables) {
|
||||
plan.getAnalysisCache().updateCache();
|
||||
return PageCacheHandler.loadPage("success", SuccessResponse::new);
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Licence is provided in the jar as license.yml also here:
|
||||
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
|
||||
*/
|
||||
package main.java.com.djrapitops.plan.ui.webserver.api.bukkit;
|
||||
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.cache.PageCacheHandler;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.Response;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.api.BadRequestResponse;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.api.SuccessResponse;
|
||||
import main.java.com.djrapitops.plan.utilities.webserver.api.WebAPI;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
public class ConfigureWebAPI implements WebAPI {
|
||||
@Override
|
||||
public Response onResponse(Plan plan, Map<String, String> variables) {
|
||||
String key = variables.get("configKey");
|
||||
|
||||
if (key == null) {
|
||||
String error = "Config Key null";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
String value = variables.get("configValue");
|
||||
|
||||
if (value == null) {
|
||||
String error = "Config Value null";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
if (value.equals("null")) {
|
||||
value = null;
|
||||
}
|
||||
|
||||
FileConfiguration config = plan.getConfig();
|
||||
config.set(key, value);
|
||||
plan.saveConfig();
|
||||
|
||||
return PageCacheHandler.loadPage("success", SuccessResponse::new);
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Licence is provided in the jar as license.yml also here:
|
||||
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
|
||||
*/
|
||||
package main.java.com.djrapitops.plan.ui.webserver.api.bukkit;
|
||||
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.cache.PageCacheHandler;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.Response;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.api.BadRequestResponse;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.api.SuccessResponse;
|
||||
import main.java.com.djrapitops.plan.utilities.uuid.UUIDUtility;
|
||||
import main.java.com.djrapitops.plan.utilities.webserver.api.WebAPI;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
public class InspectWebAPI implements WebAPI {
|
||||
@Override
|
||||
public Response onResponse(Plan plan, Map<String, String> variables) {
|
||||
String playerString = variables.get("player");
|
||||
|
||||
if (playerString == null) {
|
||||
String error = "Player String not included";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
UUID uuid = UUIDUtility.getUUIDOf(playerString);
|
||||
|
||||
if (uuid == null) {
|
||||
String error = "UUID not found";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
Plan.getInstance().getInspectCache().cache(uuid);
|
||||
|
||||
return PageCacheHandler.loadPage("success", SuccessResponse::new);
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Licence is provided in the jar as license.yml also here:
|
||||
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
|
||||
*/
|
||||
package main.java.com.djrapitops.plan.ui.webserver.api.bukkit;
|
||||
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.data.UserData;
|
||||
import main.java.com.djrapitops.plan.data.cache.PageCacheHandler;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.Response;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.api.BadRequestResponse;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.api.JsonResponse;
|
||||
import main.java.com.djrapitops.plan.utilities.uuid.UUIDUtility;
|
||||
import main.java.com.djrapitops.plan.utilities.webserver.api.WebAPI;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
public class InspectionWebAPI implements WebAPI {
|
||||
@Override
|
||||
public Response onResponse(Plan plan, Map<String, String> variables) {
|
||||
String playerString = variables.get("player");
|
||||
|
||||
if (playerString == null) {
|
||||
String error = "Player String not included";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
UUID uuid = UUIDUtility.getUUIDOf(playerString);
|
||||
|
||||
if (uuid == null) {
|
||||
String error = "UUID not found";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
UserData userData = plan.getInspectCache().getFromCache(uuid);
|
||||
|
||||
if (userData == null) {
|
||||
String error = "User not cached";
|
||||
return PageCacheHandler.loadPage(error, () -> new BadRequestResponse(error));
|
||||
}
|
||||
|
||||
return PageCacheHandler.loadPage("inspectionJson: " + uuid, () -> new JsonResponse(plan.getInspectCache().getFromCache(uuid)));
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Licence is provided in the jar as license.yml also here:
|
||||
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
|
||||
*/
|
||||
package main.java.com.djrapitops.plan.utilities.webserver.api;
|
||||
|
||||
import main.java.com.djrapitops.plan.Plan;
|
||||
import main.java.com.djrapitops.plan.ui.webserver.response.Response;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
public interface WebAPI {
|
||||
|
||||
Response onResponse(Plan plan, Map<String, String> variables);
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Licence is provided in the jar as license.yml also here:
|
||||
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
|
||||
*/
|
||||
package main.java.com.djrapitops.plan.utilities.webserver.api;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Fuzzlemann
|
||||
*/
|
||||
public class WebAPIManager {
|
||||
private static Map<String, WebAPI> registry = new HashMap<>();
|
||||
|
||||
public static void registerNewAPI(String method, WebAPI api) {
|
||||
registry.put(method.toLowerCase(), api);
|
||||
}
|
||||
|
||||
public static WebAPI getAPI(String method) {
|
||||
return registry.get(method.toLowerCase());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user