From 116675f8a66c7623f07fcdd0639f1ed1684808b3 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Sat, 16 Sep 2017 18:57:50 +0300 Subject: [PATCH] Plugins tab caching to bungee --- .../info/BukkitInformationManager.java | 34 ++++++--- .../info/BungeeInformationManager.java | 10 +++ .../RequestInspectPluginsTabBukkitWebAPI.java | 54 +++++++++++++ .../bungee/PostInspectPluginsTabWebAPI.java | 54 +++++++++++++ .../bungee/RequestPluginsTabWebAPI.java | 75 +++++++++++++++++++ 5 files changed, 218 insertions(+), 9 deletions(-) create mode 100644 Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/RequestInspectPluginsTabBukkitWebAPI.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/PostInspectPluginsTabWebAPI.java create mode 100644 Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/RequestPluginsTabWebAPI.java diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java b/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java index 8127f8b50..0cefb85c3 100644 --- a/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java +++ b/Plan/src/main/java/com/djrapitops/plan/systems/info/BukkitInformationManager.java @@ -23,8 +23,11 @@ import main.java.com.djrapitops.plan.systems.webserver.response.InspectPageRespo import main.java.com.djrapitops.plan.systems.webserver.response.Response; import main.java.com.djrapitops.plan.systems.webserver.theme.Theme; import main.java.com.djrapitops.plan.systems.webserver.webapi.WebAPIManager; +import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.RequestInspectPluginsTabBukkitWebAPI; import main.java.com.djrapitops.plan.systems.webserver.webapi.bungee.IsCachedWebAPI; import main.java.com.djrapitops.plan.systems.webserver.webapi.bungee.PostHtmlWebAPI; +import main.java.com.djrapitops.plan.systems.webserver.webapi.bungee.PostInspectPluginsTabWebAPI; +import main.java.com.djrapitops.plan.systems.webserver.webapi.bungee.RequestPluginsTabWebAPI; import main.java.com.djrapitops.plan.systems.webserver.webapi.universal.PingWebAPI; import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.utilities.analysis.Analysis; @@ -92,8 +95,16 @@ public class BukkitInformationManager extends InformationManager { } public void cacheInspectPluginsTab(UUID uuid) { - if (usingBungeeWebServer) { - // TODO plugin tab request on bungee + cacheInspectPluginsTab(uuid, this.getClass()); + } + + public void cacheInspectPluginsTab(UUID uuid, Class origin) { + if (usingBungeeWebServer && !origin.equals(RequestInspectPluginsTabBukkitWebAPI.class)) { + try { + getWebAPI().getAPI(RequestPluginsTabWebAPI.class).sendRequest(webServerAddress, uuid); + } catch (WebAPIException e) { + Log.toLog(this.getClass().getName(), e); + } } else { String serverName = plugin.getServerInfoManager().getServerName(); HookHandler hookHandler = plugin.getHookHandler(); @@ -105,10 +116,18 @@ public class BukkitInformationManager extends InformationManager { } public void cacheInspectPluginsTab(UUID uuid, String contents) { - pluginsTabContents.put(uuid, contents); - Response inspectResponse = PageCache.loadPage("inspectPage: " + uuid); - if (inspectResponse != null) { - ((InspectPageResponse) inspectResponse).setInspectPagePluginsTab(contents); + if (usingBungeeWebServer) { + try { + getWebAPI().getAPI(PostInspectPluginsTabWebAPI.class).sendPluginsTab(webServerAddress, uuid, contents); + } catch (WebAPIException e) { + Log.toLog(this.getClass().getName(), e); + } + } else { + pluginsTabContents.put(uuid, contents); + Response inspectResponse = PageCache.loadPage("inspectPage: " + uuid); + if (inspectResponse != null) { + ((InspectPageResponse) inspectResponse).setInspectPagePluginsTab(contents); + } } } @@ -158,9 +177,6 @@ public class BukkitInformationManager extends InformationManager { @Override public String getPlayerHtml(UUID uuid) { - if (usingBungeeWebServer) { - // TODO Bungee request - } try { return Theme.replaceColors(new InspectPageParser(uuid, plugin).parse()); } catch (ParseException e) { 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 747556f7c..78c0e59a3 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 @@ -15,6 +15,7 @@ import main.java.com.djrapitops.plan.systems.webserver.response.InspectPageRespo import main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.AnalyzeWebAPI; import java.sql.SQLException; +import java.util.HashMap; import java.util.Map; import java.util.UUID; import java.util.function.Function; @@ -30,8 +31,11 @@ public class BungeeInformationManager extends InformationManager { private PlanBungee plugin; private Map bukkitServers; + private Map> pluginsTabContent; + public BungeeInformationManager(PlanBungee plugin) throws SQLException { usingBungeeWebServer = true; + pluginsTabContent = new HashMap<>(); this.plugin = plugin; refreshBukkitServerMap(); } @@ -104,4 +108,10 @@ public class BungeeInformationManager extends InformationManager { public String getPluginsTabContent(UUID uuid) { return null; } + + public void cachePluginsTabContent(UUID serverUUID, UUID uuid, String html) { + Map perServerPluginsTab = pluginsTabContent.getOrDefault(uuid, new HashMap<>()); + perServerPluginsTab.put(serverUUID, html); + pluginsTabContent.put(uuid, perServerPluginsTab); + } } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/RequestInspectPluginsTabBukkitWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/RequestInspectPluginsTabBukkitWebAPI.java new file mode 100644 index 000000000..89bdceb2e --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bukkit/RequestInspectPluginsTabBukkitWebAPI.java @@ -0,0 +1,54 @@ +/* + * 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 com.djrapitops.plugin.utilities.Compatibility; +import main.java.com.djrapitops.plan.api.IPlan; +import main.java.com.djrapitops.plan.api.exceptions.WebAPIException; +import main.java.com.djrapitops.plan.systems.info.BukkitInformationManager; +import main.java.com.djrapitops.plan.systems.webserver.PageCache; +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; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class RequestInspectPluginsTabBukkitWebAPI extends WebAPI { + + @Override + public Response onResponse(IPlan plugin, Map variables) { + if (!Compatibility.isBukkitAvailable()) { + String error = "Called a Bungee Server"; + return PageCache.loadPage(error, () -> new BadRequestResponse(error)); + } + + String uuidS = variables.get("uuid"); + if (uuidS == null) { + String error = "UUID not included"; + return PageCache.loadPage(error, () -> new BadRequestResponse(error)); + } + UUID uuid = UUID.fromString(uuidS); + + ((BukkitInformationManager) plugin.getInfoManager()).cacheInspectPluginsTab(uuid, this.getClass()); + return PageCache.loadPage("success", SuccessResponse::new); + } + + @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); + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/PostInspectPluginsTabWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/PostInspectPluginsTabWebAPI.java new file mode 100644 index 000000000..146da35a7 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/PostInspectPluginsTabWebAPI.java @@ -0,0 +1,54 @@ +/* + * 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 com.djrapitops.plugin.utilities.Verify; +import main.java.com.djrapitops.plan.api.IPlan; +import main.java.com.djrapitops.plan.api.exceptions.WebAPIException; +import main.java.com.djrapitops.plan.systems.info.BungeeInformationManager; +import main.java.com.djrapitops.plan.systems.webserver.PageCache; +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; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class PostInspectPluginsTabWebAPI extends WebAPI { + @Override + public Response onResponse(IPlan plugin, Map variables) { + String uuidS = variables.get("uuid"); + String sender = variables.get("sender"); + if (Verify.notNull(uuidS, sender)) { + String error = "uuid or sender not included"; + return PageCache.loadPage(error, () -> new BadRequestResponse(error)); + } + + UUID uuid = UUID.fromString(uuidS); + UUID serverUUID = UUID.fromString(sender); + String html = variables.get("html"); + + ((BungeeInformationManager) plugin.getInfoManager()).cachePluginsTabContent(serverUUID, uuid, html); + + return PageCache.loadPage("success", SuccessResponse::new); + } + + @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 sendPluginsTab(String address, UUID uuid, String html) throws WebAPIException { + addVariable("uuid", uuid.toString()); + addVariable("html", html); + super.sendRequest(address); + } +} \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/RequestPluginsTabWebAPI.java b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/RequestPluginsTabWebAPI.java new file mode 100644 index 000000000..8d15f33e0 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/systems/webserver/webapi/bungee/RequestPluginsTabWebAPI.java @@ -0,0 +1,75 @@ +/* + * 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 com.djrapitops.plugin.utilities.Compatibility; +import main.java.com.djrapitops.plan.Log; +import main.java.com.djrapitops.plan.api.IPlan; +import main.java.com.djrapitops.plan.api.exceptions.WebAPIException; +import main.java.com.djrapitops.plan.systems.info.server.ServerInfo; +import main.java.com.djrapitops.plan.systems.processing.Processor; +import main.java.com.djrapitops.plan.systems.webserver.PageCache; +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 main.java.com.djrapitops.plan.systems.webserver.webapi.bukkit.RequestInspectPluginsTabBukkitWebAPI; + +import java.sql.SQLException; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +/** + * //TODO Class Javadoc Comment + * + * @author Rsl1122 + */ +public class RequestPluginsTabWebAPI extends WebAPI { + @Override + public Response onResponse(IPlan plugin, Map variables) { + if (!Compatibility.isBungeeAvailable()) { + String error = "Called a Bukkit Server"; + return PageCache.loadPage(error, () -> new BadRequestResponse(error)); + } + + String uuidS = variables.get("uuid"); + if (uuidS == null) { + String error = "UUID not included"; + return PageCache.loadPage(error, () -> new BadRequestResponse(error)); + } + UUID uuid = UUID.fromString(uuidS); + + plugin.addToProcessQueue(new Processor(uuid) { + @Override + public void process() { + try { + List bukkitServers = plugin.getDB().getServerTable().getBukkitServers(); + for (ServerInfo server : bukkitServers) { + String webAddress = server.getWebAddress(); + try { + plugin.getWebServer().getWebAPI().getAPI(RequestInspectPluginsTabBukkitWebAPI.class).sendRequest(webAddress, uuid); + } catch (WebAPIException ignore) { + /* ignored */ + } + } + } catch (SQLException e) { + Log.toLog(this.getClass().getName(), e); + } + } + }); + return PageCache.loadPage("success", SuccessResponse::new); + } + + @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); + } +} \ No newline at end of file