From 5a0ae8746c1431c9609310f7b6a2324c0d2abaf9 Mon Sep 17 00:00:00 2001 From: Allink Date: Mon, 30 May 2022 03:55:03 +0100 Subject: [PATCH] Set Content-Type header to application/json for JSON endpoints (closes #6) --- src/main/java/dev/plex/request/impl/AdminsEndpoint.java | 2 ++ src/main/java/dev/plex/request/impl/IndefBansEndpoint.java | 2 ++ src/main/java/dev/plex/request/impl/ListEndpoint.java | 2 ++ src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java | 2 ++ 4 files changed, 8 insertions(+) diff --git a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java index 0715044..37c4254 100644 --- a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java +++ b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java @@ -9,6 +9,7 @@ import dev.plex.player.PlexPlayer; import dev.plex.rank.enums.Rank; import dev.plex.request.AbstractServlet; import dev.plex.request.GetMapping; +import dev.plex.request.MappingHeaders; import dev.plex.util.PlexLog; import dev.plex.util.adapter.ZonedDateTimeSerializer; import jakarta.servlet.http.HttpServletRequest; @@ -24,6 +25,7 @@ public class AdminsEndpoint extends AbstractServlet private static final String TITLE = "Admins - Plex HTTPD"; @GetMapping(endpoint = "/api/admins/") + @MappingHeaders(headers = "content-type;application/json") public String getAdmins(HttpServletRequest request, HttpServletResponse response) { String ipAddress = request.getRemoteAddr(); diff --git a/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java b/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java index 4721e6a..bd307bf 100644 --- a/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java +++ b/src/main/java/dev/plex/request/impl/IndefBansEndpoint.java @@ -48,6 +48,8 @@ public class IndefBansEndpoint extends AbstractServlet return indefbansHTML("Not enough permissions to view this page."); } } + + response.setHeader("content-type", "application/json"); return 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 567f257..c34c307 100644 --- a/src/main/java/dev/plex/request/impl/ListEndpoint.java +++ b/src/main/java/dev/plex/request/impl/ListEndpoint.java @@ -3,6 +3,7 @@ package dev.plex.request.impl; import com.google.gson.GsonBuilder; import dev.plex.request.AbstractServlet; import dev.plex.request.GetMapping; +import dev.plex.request.MappingHeaders; import jakarta.servlet.http.HttpServletRequest; import java.util.ArrayList; import java.util.List; @@ -14,6 +15,7 @@ import org.bukkit.entity.Player; public class ListEndpoint extends AbstractServlet { @GetMapping(endpoint = "/api/list/") + @MappingHeaders(headers = "content-type;application/json") public String getOnlinePlayers(HttpServletRequest request, HttpServletResponse response) { List players = new ArrayList<>(); diff --git a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java index 3d168d7..ca4b0fc 100644 --- a/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java +++ b/src/main/java/dev/plex/request/impl/PunishmentsEndpoint.java @@ -80,6 +80,8 @@ public class PunishmentsEndpoint extends AbstractServlet return new GsonBuilder().registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().peek(punishment -> punishment.setIp("")).toList()); } } + + response.setHeader("content-type", "application/json"); return new GsonBuilder().registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList()); }