From fb717387d220933cb90ad0e5ad321348d67e98c3 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sat, 16 Sep 2017 10:53:13 +0300 Subject: [PATCH] Fixed WebAPI, DevCommand, Improved some WebAPI, New WebAPI. --- .../java/com/djrapitops/plan/Settings.java | 1 + .../djrapitops/plan/command/PlanCommand.java | 5 ++ .../plan/command/commands/DevCommand.java | 71 +++++++++++++++++++ .../plan/systems/webserver/WebServer.java | 2 +- .../response/AnalysisPageResponse.java | 5 ++ .../response/InspectPageResponse.java | 6 ++ .../plan/systems/webserver/webapi/WebAPI.java | 10 ++- .../webapi/bukkit/InspectWebAPI.java | 7 +- .../webapi/bukkit/MaxPlayersWebAPI.java | 22 ++++++ .../webapi/bukkit/OnlinePlayersWebAPI.java | 5 +- .../webapi/bungee/PostHtmlWebAPI.java | 55 ++++++++++++++ 11 files changed, 180 insertions(+), 9 deletions(-) create mode 100644 Plan/src/main/java/com/djrapitops/plan/command/commands/DevCommand.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/MaxPlayersWebAPI.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/PostHtmlWebAPI.java diff --git a/Plan/src/main/java/com/djrapitops/plan/Settings.java b/Plan/src/main/java/com/djrapitops/plan/Settings.java index 1193dc253..7c004d0f2 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Settings.java +++ b/Plan/src/main/java/com/djrapitops/plan/Settings.java @@ -24,6 +24,7 @@ public enum Settings { LOG_UNKNOWN_COMMANDS("Data.Commands.LogUnknownCommands"), COMBINE_COMMAND_ALIASES("Data.Commands.CombineCommandAliases"), WRITE_NEW_LOCALE("Plugin.WriteNewLocaleFileOnStart"), + DEV_MODE("Plugin.Dev"), // Integer WEBSERVER_PORT("WebServer.Port"), diff --git a/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java index f2f825db1..814e4828d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/PlanCommand.java @@ -5,6 +5,7 @@ import com.djrapitops.plugin.command.TreeCommand; import com.djrapitops.plugin.command.defaultcmds.StatusCommand; import main.java.com.djrapitops.plan.Permissions; import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.Settings; import main.java.com.djrapitops.plan.command.commands.*; import main.java.com.djrapitops.plan.locale.Locale; import main.java.com.djrapitops.plan.locale.Msg; @@ -52,5 +53,9 @@ public class PlanCommand extends TreeCommand { commands.add(registerCommand); commands.add(new WebUserCommand(plugin, registerCommand)); } + + if (Settings.DEV_MODE.isTrue()) { + commands.add(new DevCommand(plugin)); + } } } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/DevCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/DevCommand.java new file mode 100644 index 000000000..d6bfdd647 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/DevCommand.java @@ -0,0 +1,71 @@ +/* + * 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.command.commands; + +import com.djrapitops.plugin.command.CommandType; +import com.djrapitops.plugin.command.ISender; +import com.djrapitops.plugin.command.SubCommand; +import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.api.exceptions.WebAPIException; +import main.java.com.djrapitops.plan.locale.Locale; +import main.java.com.djrapitops.plan.locale.Msg; +import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI; +import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.InspectWebAPI; +import main.java.com.djrapitops.plan.utilities.Check; + +import java.util.UUID; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class DevCommand extends SubCommand { + + private final Plan plugin; + + public DevCommand(Plan plugin) { + super("dev", CommandType.CONSOLE_WITH_ARGUMENTS, "plan.*", "Test Plugin functions not testable with unit tests.", ""); + this.plugin = plugin; + } + + @Override + public boolean onCommand(ISender sender, String cmd, String[] args) { + if (!Check.isTrue(args.length >= 1, Locale.get(Msg.CMD_FAIL_REQ_ONE_ARG).toString(), sender)) { + return true; + } + String feature = args[0]; + switch (feature) { + case "webapi": + if (!Check.isTrue(args.length >= 2, Locale.get(Msg.CMD_FAIL_REQ_ONE_ARG).toString(), sender)) { + return true; + } + if (!webapi(args[1])) { + sender.sendMessage("[Plan] No such API / Exception occurred."); + } + default: + break; + } + return true; + } + + private boolean webapi(String method) { + WebAPI api = plugin.getWebServer().getWebAPI().getAPI(method); + if (api == null) { + return false; + } + try { + if (api instanceof InspectWebAPI) { + ((InspectWebAPI) api).sendRequest(plugin.getWebServer().getAccessAddress(), Plan.getServerUUID(), UUID.randomUUID()); + } else { + api.sendRequest(plugin.getWebServer().getAccessAddress(), Plan.getServerUUID()); + } + return true; + } catch (WebAPIException e) { + e.printStackTrace(); + } + return false; + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/WebServer.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/WebServer.java index ee589741f..f21d074e1 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/WebServer.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/WebServer.java @@ -190,7 +190,7 @@ public class WebServer { } public String getAccessAddress() { - return getProtocol() + ":/" + HtmlUtils.getIP(); + return getProtocol() + "://" + HtmlUtils.getIP(); } public WebAPIManager getWebAPI() { diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/AnalysisPageResponse.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/AnalysisPageResponse.java index 49d59987d..47286ef4e 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/AnalysisPageResponse.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/AnalysisPageResponse.java @@ -13,4 +13,9 @@ public class AnalysisPageResponse extends Response { super.setHeader("HTTP/1.1 200 OK"); super.setContent(Theme.replaceColors(informationManager.getAnalysisHtml())); } + + public AnalysisPageResponse(String html) { + super.setHeader("HTTP/1.1 200 OK"); + super.setContent(Theme.replaceColors(html)); + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/InspectPageResponse.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/InspectPageResponse.java index 5c38afcd1..62b6b2e77 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/InspectPageResponse.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/response/InspectPageResponse.java @@ -19,6 +19,12 @@ public class InspectPageResponse extends Response { setInspectPagePluginsTab(infoManager.getPluginsTabContent(uuid)); } + public InspectPageResponse(InformationManager infoManager, UUID uuid, String html) { + super.setHeader("HTTP/1.1 200 OK"); + super.setContent(Theme.replaceColors(html)); + setInspectPagePluginsTab(infoManager.getPluginsTabContent(uuid)); + } + public void setInspectPagePluginsTab(String inspectPagePluginsTab) { if (this.inspectPagePluginsTab != null) { setContent(getContent().replace(this.inspectPagePluginsTab, inspectPagePluginsTab)); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java index 1bdea205a..12debd51a 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/WebAPI.java @@ -15,6 +15,7 @@ import java.io.DataOutputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URL; +import java.util.HashMap; import java.util.Map; import java.util.UUID; @@ -25,6 +26,10 @@ public abstract class WebAPI { private Map variables; + public WebAPI() { + this.variables = new HashMap<>(); + } + public abstract Response onResponse(IPlan plugin, Map variables); public void sendRequest(String address, UUID receiverUUID) throws WebAPIException { @@ -59,16 +64,15 @@ public abstract class WebAPI { int responseCode = connection.getResponseCode(); switch (responseCode) { case 200: - break; + return; case 400: throw new WebAPIException("Bad Request: " + url.toString() + "|" + parameters); default: throw new WebAPIException(url.toString() + "| Wrong response code " + responseCode); } } catch (IOException e) { - throw new WebAPIConnectionFailException("API connection failed.", e); + throw new WebAPIConnectionFailException("API connection failed. address: " + address, e); } - throw new WebAPIException("Response entity was null"); } protected void addVariable(String key, String value) { diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/InspectWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/InspectWebAPI.java index 1ba3392b3..22a1ad7da 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/InspectWebAPI.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/InspectWebAPI.java @@ -16,7 +16,7 @@ import java.util.Map; import java.util.UUID; /** - * @author Fuzzlemann + * @author Rsl1122 */ public class InspectWebAPI extends WebAPI { @Override @@ -33,6 +33,11 @@ public class InspectWebAPI extends WebAPI { return PageCache.loadPage("success", SuccessResponse::new); } + @Override + public void sendRequest(String address, UUID receiverUUID) throws WebAPIException { + throw new IllegalStateException("Wrong method call for this WebAPI, call sendRequest(String, UUID, UUID) instead."); + } + public void sendRequest(String address, UUID receiverUUID, UUID uuid) throws WebAPIException { addVariable("uuid", uuid.toString()); super.sendRequest(address, receiverUUID); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/MaxPlayersWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/MaxPlayersWebAPI.java new file mode 100644 index 000000000..44863841e --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/MaxPlayersWebAPI.java @@ -0,0 +1,22 @@ +/* + * 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.systems.webserver.webapi.bukkit; + +import main.java.com.djrapitops.plan.api.IPlan; +import main.java.com.djrapitops.plan.systems.webserver.response.Response; +import main.java.com.djrapitops.plan.systems.webserver.response.api.JsonResponse; +import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI; + +import java.util.Map; + +/** + * @author Rsl1122 + */ +public class MaxPlayersWebAPI extends WebAPI { + @Override + public Response onResponse(IPlan plugin, Map variables) { + return new JsonResponse(plugin.getVariable().getMaxPlayers()); + } +} diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/OnlinePlayersWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/OnlinePlayersWebAPI.java index 6a5bd1aca..d26ab1002 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/OnlinePlayersWebAPI.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/OnlinePlayersWebAPI.java @@ -9,7 +9,6 @@ import main.java.com.djrapitops.plan.api.IPlan; import main.java.com.djrapitops.plan.systems.webserver.response.Response; import main.java.com.djrapitops.plan.systems.webserver.response.api.JsonResponse; import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI; -import org.bukkit.Server; import java.util.Map; @@ -19,8 +18,6 @@ import java.util.Map; public class OnlinePlayersWebAPI extends WebAPI { @Override public Response onResponse(IPlan plugin, Map variables) { - Server server = ((Plan) plugin).getServer(); - - return new JsonResponse(server.getOnlinePlayers() + "/" + server.getMaxPlayers()); + return new JsonResponse(((Plan) plugin).getServer().getOnlinePlayers()); } } diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/PostHtmlWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/PostHtmlWebAPI.java new file mode 100644 index 000000000..658a68e93 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/PostHtmlWebAPI.java @@ -0,0 +1,55 @@ +/* + * 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.systems.webserver.webapi.bungee; + +import main.java.com.djrapitops.plan.api.IPlan; +import main.java.com.djrapitops.plan.systems.info.InformationManager; +import main.java.com.djrapitops.plan.systems.webserver.PageCache; +import main.java.com.djrapitops.plan.systems.webserver.response.AnalysisPageResponse; +import main.java.com.djrapitops.plan.systems.webserver.response.InspectPageResponse; +import main.java.com.djrapitops.plan.systems.webserver.response.Response; +import main.java.com.djrapitops.plan.systems.webserver.response.api.BadRequestResponse; +import main.java.com.djrapitops.plan.systems.webserver.response.api.SuccessResponse; +import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI; + +import java.util.Map; +import java.util.UUID; + +/** + * WebAPI for posting Html pages such as Inspect, players or server pages. + * + * @author Rsl1122 + */ +public class PostHtmlWebAPI extends WebAPI { + + private final IPlan plugin; + + public PostHtmlWebAPI(IPlan plugin) { + this.plugin = plugin; + } + + @Override + public Response onResponse(IPlan plugin, Map variables) { + try { + String html = variables.get("html"); + String target = variables.get("target"); + InformationManager infoManager = plugin.getInfoManager(); + switch (target) { + case "inspectPage": + String uuid = variables.get("uuid"); + PageCache.loadPage("inspectPage:" + uuid, () -> new InspectPageResponse(infoManager, UUID.fromString(uuid), html)); + break; + case "analysisPage": + PageCache.loadPage("analysisPage:" + variables.get("sender"), () -> new AnalysisPageResponse(html)); + default: + String error = "Faulty Target"; + return PageCache.loadPage(error, () -> new BadRequestResponse(error)); + } + return PageCache.loadPage("success", SuccessResponse::new); + } catch (NullPointerException e) { + return PageCache.loadPage(e.toString(), () -> new BadRequestResponse(e.toString())); + } + } +} \ No newline at end of file