Set Content-Type header to application/json for JSON endpoints (closes #6)

This commit is contained in:
Allink 2022-05-30 03:55:03 +01:00
parent 41a2475e3d
commit 5a0ae8746c
No known key found for this signature in database
GPG Key ID: 7F1F1B98F0FAAD13
4 changed files with 8 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.request.AbstractServlet; import dev.plex.request.AbstractServlet;
import dev.plex.request.GetMapping; import dev.plex.request.GetMapping;
import dev.plex.request.MappingHeaders;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.adapter.ZonedDateTimeSerializer; import dev.plex.util.adapter.ZonedDateTimeSerializer;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
@ -24,6 +25,7 @@ public class AdminsEndpoint extends AbstractServlet
private static final String TITLE = "Admins - Plex HTTPD"; private static final String TITLE = "Admins - Plex HTTPD";
@GetMapping(endpoint = "/api/admins/") @GetMapping(endpoint = "/api/admins/")
@MappingHeaders(headers = "content-type;application/json")
public String getAdmins(HttpServletRequest request, HttpServletResponse response) public String getAdmins(HttpServletRequest request, HttpServletResponse response)
{ {
String ipAddress = request.getRemoteAddr(); String ipAddress = request.getRemoteAddr();

View File

@ -48,6 +48,8 @@ public class IndefBansEndpoint extends AbstractServlet
return indefbansHTML("Not enough permissions to view this page."); 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()); return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getPunishmentManager().getIndefiniteBans().stream().toList());
} }

View File

@ -3,6 +3,7 @@ package dev.plex.request.impl;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import dev.plex.request.AbstractServlet; import dev.plex.request.AbstractServlet;
import dev.plex.request.GetMapping; import dev.plex.request.GetMapping;
import dev.plex.request.MappingHeaders;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -14,6 +15,7 @@ import org.bukkit.entity.Player;
public class ListEndpoint extends AbstractServlet public class ListEndpoint extends AbstractServlet
{ {
@GetMapping(endpoint = "/api/list/") @GetMapping(endpoint = "/api/list/")
@MappingHeaders(headers = "content-type;application/json")
public String getOnlinePlayers(HttpServletRequest request, HttpServletResponse response) public String getOnlinePlayers(HttpServletRequest request, HttpServletResponse response)
{ {
List<String> players = new ArrayList<>(); List<String> players = new ArrayList<>();

View File

@ -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()); 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()); return new GsonBuilder().registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeSerializer()).setPrettyPrinting().create().toJson(punishedPlayer.getPunishments().stream().toList());
} }