From 7550db5fb00bb6f5be1d5603f37b9af703951c6a Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Fri, 15 Apr 2022 13:55:40 -0500 Subject: [PATCH] work in progress --- .../java/dev/plex/request/AbstractServlet.java | 10 ++++++++++ .../dev/plex/request/impl/AdminsEndpoint.java | 14 +++++++------- .../dev/plex/request/impl/IndefBansEndpoint.java | 13 +++++++------ .../java/dev/plex/request/impl/ListEndpoint.java | 2 +- .../plex/request/impl/PunishmentsEndpoint.java | 16 +++++++++------- 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/main/java/dev/plex/request/AbstractServlet.java b/src/main/java/dev/plex/request/AbstractServlet.java index 69378d8..2e531f6 100644 --- a/src/main/java/dev/plex/request/AbstractServlet.java +++ b/src/main/java/dev/plex/request/AbstractServlet.java @@ -79,6 +79,16 @@ public class AbstractServlet extends HttpServlet }); } + public String createBasicHTML(String title, String body) + { + return "" + title + "" + body + ""; + } + + public String createJSONHTML(String title, String json) + { + return "" + title + "
" + json + "
"; + } + @Data public static class Mapping { diff --git a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java index c7f8719..fc39e87 100644 --- a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java +++ b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java @@ -12,28 +12,28 @@ import dev.plex.request.GetMapping; import dev.plex.util.PlexLog; import dev.plex.util.adapter.LocalDateTimeSerializer; import jakarta.servlet.http.HttpServletRequest; - import java.time.LocalDateTime; -import java.util.UUID; import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; public class AdminsEndpoint extends AbstractServlet { + private static final String TITLE = "Admins - Plex HTTPD"; + @GetMapping(endpoint = "/api/admins/") public String getAdmins(HttpServletRequest request) { String ipAddress = request.getRemoteAddr(); if (ipAddress == null) { - return "An IP address could not be detected. Please ensure you are connecting using IPv4."; + return createBasicHTML(TITLE, "An IP address could not be detected. Please ensure you are connecting using IPv4."); } final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); if (player == null) { // This likely means they've never joined the server before. That's okay. We can just not return IPs. - return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())); + return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList()))); } if (Plex.get().getSystem().equalsIgnoreCase("ranks")) { @@ -41,7 +41,7 @@ public class AdminsEndpoint extends AbstractServlet if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) { // Don't return IPs either if the person is not an Admin or above. - return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())); + return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList()))); } } else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) @@ -51,9 +51,9 @@ public class AdminsEndpoint extends AbstractServlet if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.admins.access")) { // If the person doesn't have permission, don't return IPs - return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())); + return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList()))); } } - return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers()); + return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers())); } } diff --git a/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java b/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java index 2a6417c..0db3a24 100644 --- a/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java +++ b/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java @@ -10,31 +10,32 @@ import dev.plex.request.AbstractServlet; import dev.plex.request.GetMapping; import dev.plex.util.PlexLog; import jakarta.servlet.http.HttpServletRequest; -import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; public class IndefBansEndpoint extends AbstractServlet { + private static final String TITLE = "Indefinite Bans - Plex HTTPD"; + @GetMapping(endpoint = "/api/indefbans/") public String getBans(HttpServletRequest request) { String ipAddress = request.getRemoteAddr(); if (ipAddress == null) { - return "An IP address could not be detected. Please ensure you are connecting using IPv4."; + return createBasicHTML(TITLE, "An IP address could not be detected. Please ensure you are connecting using IPv4."); } final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); if (player == null) { - return "Couldn't load your IP Address: " + ipAddress + ". Have you joined the server before?"; + return createBasicHTML(TITLE, "Couldn't load your IP Address: " + ipAddress + ". Have you joined the server before?"); } if (Plex.get().getSystem().equalsIgnoreCase("ranks")) { PlexLog.debug("Plex-HTTPD using ranks check"); if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) { - return "Not a high enough rank to view this page."; + return createBasicHTML(TITLE, "Not a high enough rank to view this page."); } } else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) @@ -43,9 +44,9 @@ public class IndefBansEndpoint extends AbstractServlet final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(player.getUuid()); if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.indefbans.access")) { - return "Not enough permissions to view this page."; + return createBasicHTML(TITLE, "Not enough permissions to view this page."); } } - return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getPunishmentManager().getIndefiniteBans().stream().toList()); + return createJSONHTML(TITLE, new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getPunishmentManager().getIndefiniteBans().stream().toList())); } } diff --git a/src/main/java/dev/plex/request/impl/ListEndpoint.java b/src/main/java/dev/plex/request/impl/ListEndpoint.java index 8b6fcf5..3c33a82 100644 --- a/src/main/java/dev/plex/request/impl/ListEndpoint.java +++ b/src/main/java/dev/plex/request/impl/ListEndpoint.java @@ -20,6 +20,6 @@ public class ListEndpoint extends AbstractServlet { players.add(player.getName()); } - return new GsonBuilder().setPrettyPrinting().create().toJson(players.stream().toList()); + return createJSONHTML("List - Plex HTTPD", new GsonBuilder().setPrettyPrinting().create().toJson(players.stream().toList())); } } diff --git a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java index 8903a1e..535c63b 100644 --- a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java +++ b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java @@ -22,13 +22,15 @@ import org.bukkit.OfflinePlayer; public class PunishmentsEndpoint extends AbstractServlet { + private static final String TITLE = "Punishments - Plex HTTPD"; + @GetMapping(endpoint = "/api/punishments/") public String getPunishments(HttpServletRequest request) { String ipAddress = request.getRemoteAddr(); if (ipAddress == null) { - return "An IP address could not be detected. Please ensure you are connecting using IPv4."; + return createBasicHTML(TITLE, "An IP address could not be detected. Please ensure you are connecting using IPv4."); } if (request.getPathInfo() == null) { @@ -65,16 +67,16 @@ public class PunishmentsEndpoint extends AbstractServlet final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); if (punishedPlayer == null) { - return "This player has never joined the server before."; + return createBasicHTML(TITLE, "This player has never joined the server before."); } if (punishedPlayer.getPunishments().isEmpty()) { - return "This player has been a good boy. They have no punishments!"; + return createBasicHTML(TITLE, "This player has been a good boy. They have no punishments!"); } if (player == null) { // If the player is null, give it to them without the IPs - return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList()); + return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList())); } if (Plex.get().getSystem().equalsIgnoreCase("ranks")) { @@ -82,7 +84,7 @@ public class PunishmentsEndpoint extends AbstractServlet if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) { // Don't return IPs either if the person is not an Admin or above. - return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList()); + return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList())); } } else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) @@ -92,9 +94,9 @@ public class PunishmentsEndpoint extends AbstractServlet if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.punishments.access")) { // If the person doesn't have permission, don't return IPs - return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList()); + return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList())); } } - return new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList()); + return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList())); } }