From 06eb66047b5cff325d11228d5356f4e6954a6450 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Mon, 18 Sep 2017 13:13:14 +0300 Subject: [PATCH] IsOnlineWebAPI and Removed all JsonResponse APIs (response body not used) --- .../info/BungeeInformationManager.java | 21 +++++++-- .../plan/systems/webserver/WebServer.java | 3 +- .../webapi/bukkit/GetMaxPlayersWebAPI.java | 22 --------- .../webapi/bukkit/GetOnlinePlayersWebAPI.java | 23 --------- .../webapi/bukkit/IsOnlineWebAPI.java | 47 +++++++++++++++++++ 5 files changed, 66 insertions(+), 50 deletions(-) delete mode 100644 Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/GetMaxPlayersWebAPI.java delete mode 100644 Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/GetOnlinePlayersWebAPI.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/IsOnlineWebAPI.java diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/BungeeInformationManager.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/BungeeInformationManager.java index 4361b230c..bc6d52e46 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/info/BungeeInformationManager.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/BungeeInformationManager.java @@ -8,18 +8,18 @@ import main.java.com.djrapitops.plan.Log; import main.java.com.djrapitops.plan.PlanBungee; import main.java.com.djrapitops.plan.api.exceptions.WebAPIConnectionFailException; import main.java.com.djrapitops.plan.api.exceptions.WebAPIException; +import main.java.com.djrapitops.plan.api.exceptions.WebAPINotFoundException; import main.java.com.djrapitops.plan.systems.cache.DataCache; import main.java.com.djrapitops.plan.systems.info.server.ServerInfo; import main.java.com.djrapitops.plan.systems.webserver.PageCache; import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPIManager; import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.AnalyzeWebAPI; +import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.IsOnlineWebAPI; import main.java.com.djrapitops.plan.systems.webserver.webapi.bungee.RequestPluginsTabWebAPI; import main.java.com.djrapitops.plan.utilities.html.HtmlStructure; import java.sql.SQLException; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; +import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @@ -82,6 +82,21 @@ public class BungeeInformationManager extends InformationManager { getWebAPI().getAPI(RequestPluginsTabWebAPI.class).sendRequestsToBukkitServers(plugin, uuid); } + public ServerInfo getInspectRequestProcessorServer(UUID uuid) throws SQLException { + List bukkitServers = plugin.getDB().getServerTable().getBukkitServers(); + for (ServerInfo server : bukkitServers) { + try { + getWebAPI().getAPI(IsOnlineWebAPI.class).sendRequest(server.getWebAddress()); + return server; + } catch (WebAPINotFoundException e) { + /*continue*/ + } catch (WebAPIException e) { + Log.toLog(this.getClass().getName(), e); + } + } + return bukkitServers.get(new Random().nextInt(bukkitServers.size())); + } + @Override public DataCache getDataCache() { return null; 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 4bd0452ff..d603e4105 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 @@ -60,8 +60,7 @@ public class WebServer { webAPI.registerNewAPI(new AnalyzeWebAPI()); webAPI.registerNewAPI(new ConfigurationWebAPI()); webAPI.registerNewAPI(new InspectWebAPI()); - webAPI.registerNewAPI(new GetOnlinePlayersWebAPI()); - webAPI.registerNewAPI(new GetMaxPlayersWebAPI()); + webAPI.registerNewAPI(new IsOnlineWebAPI()); webAPI.registerNewAPI(new PingWebAPI()); webAPI.registerNewAPI(new RequestInspectPluginsTabBukkitWebAPI()); diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/GetMaxPlayersWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/GetMaxPlayersWebAPI.java deleted file mode 100644 index 1fa9806e7..000000000 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/GetMaxPlayersWebAPI.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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 GetMaxPlayersWebAPI extends WebAPI { - @Override - public Response onRequest(IPlan plugin, Map variables) { - return new JsonResponse(plugin.getVariable().getMaxPlayers()); - } -} diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/GetOnlinePlayersWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/GetOnlinePlayersWebAPI.java deleted file mode 100644 index 4a5623e9d..000000000 --- a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/GetOnlinePlayersWebAPI.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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.Plan; -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 Fuzzlemann - */ -public class GetOnlinePlayersWebAPI extends WebAPI { - @Override - public Response onRequest(IPlan plugin, Map variables) { - return new JsonResponse(((Plan) plugin).getServer().getOnlinePlayers()); - } -} diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/IsOnlineWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/IsOnlineWebAPI.java new file mode 100644 index 000000000..0c58e5b54 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/IsOnlineWebAPI.java @@ -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.systems.webserver.webapi.bukkit; + +import main.java.com.djrapitops.plan.Plan; +import main.java.com.djrapitops.plan.api.IPlan; +import main.java.com.djrapitops.plan.api.exceptions.WebAPIException; +import main.java.com.djrapitops.plan.systems.webserver.response.Response; +import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPI; +import org.bukkit.entity.Player; + +import java.util.Map; +import java.util.UUID; + +/** + * @author Fuzzlemann + */ +public class IsOnlineWebAPI extends WebAPI { + @Override + public Response onRequest(IPlan plugin, Map variables) { + String uuidS = variables.get("uuid"); + if (uuidS == null) { + return badRequest("UUID not included"); + } + UUID uuid = UUID.fromString(uuidS); + + Player player = Plan.getInstance().getServer().getPlayer(uuid); + + if (player != null && player.isOnline()) { + return success(); + } else { + return fail("Not Online"); + } + } + + @Override + public void sendRequest(String address) throws WebAPIException { + throw new IllegalStateException("Wrong method call for this WebAPI, call sendRequest(String, UUID, UUID) instead."); + } + + public void sendRequest(String address, UUID uuid) throws WebAPIException { + addVariable("uuid", uuid.toString()); + super.sendRequest(address); + } +}