From d0cfd03ad9b99659a22dc7e8a7a7b860eda92725 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 19 Apr 2024 20:46:30 +0100 Subject: [PATCH] impl etags --- .../mcutils/backend/controller/MojangController.java | 5 ++++- .../mcutils/backend/controller/PlayerController.java | 11 +++++++++-- .../mcutils/backend/controller/ServerController.java | 12 ++++++++++-- .../backend/model/cache/CachedEndpointStatus.java | 7 ++----- .../backend/model/cache/CachedMinecraftServer.java | 3 +-- .../mcutils/backend/model/cache/CachedPlayer.java | 7 ++----- .../backend/model/cache/CachedPlayerName.java | 7 +++---- .../backend/model/cache/CachedPlayerSkinPart.java | 8 ++------ .../xyz/mcutils/backend/model/dns/DNSRecord.java | 7 ++----- .../mcutils/backend/model/mojang/EndpointStatus.java | 7 ++----- .../java/xyz/mcutils/backend/model/player/Cape.java | 4 +++- .../xyz/mcutils/backend/model/player/Player.java | 4 +++- .../backend/model/response/ErrorResponse.java | 4 ++-- .../backend/model/server/JavaMinecraftServer.java | 2 +- .../backend/model/server/MinecraftServer.java | 7 ++----- .../java/xyz/mcutils/backend/model/skin/Skin.java | 3 ++- 16 files changed, 50 insertions(+), 48 deletions(-) diff --git a/src/main/java/xyz/mcutils/backend/controller/MojangController.java b/src/main/java/xyz/mcutils/backend/controller/MojangController.java index 085e0a0..ab170ab 100644 --- a/src/main/java/xyz/mcutils/backend/controller/MojangController.java +++ b/src/main/java/xyz/mcutils/backend/controller/MojangController.java @@ -25,8 +25,11 @@ public class MojangController { @ResponseBody @GetMapping(value = "/status") public ResponseEntity getStatus() { + CachedEndpointStatus status = mojangService.getMojangApiStatus(); + return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(1, TimeUnit.MINUTES)) - .body(mojangService.getMojangApiStatus()); + .eTag(String.valueOf(status.hashCode())) + .body(status); } } diff --git a/src/main/java/xyz/mcutils/backend/controller/PlayerController.java b/src/main/java/xyz/mcutils/backend/controller/PlayerController.java index e0cc56a..ac891aa 100644 --- a/src/main/java/xyz/mcutils/backend/controller/PlayerController.java +++ b/src/main/java/xyz/mcutils/backend/controller/PlayerController.java @@ -31,18 +31,24 @@ public class PlayerController { @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getPlayer( @Parameter(description = "The UUID or Username of the player", example = "ImFascinated") @PathVariable String id) { + CachedPlayer player = playerService.getPlayer(id); + return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS)) - .body(playerService.getPlayer(id)); + .eTag(String.valueOf(player.hashCode())) + .body(player); } @ResponseBody @GetMapping(value = "/uuid/{id}", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity getPlayerUuid( @Parameter(description = "The UUID or Username of the player", example = "ImFascinated") @PathVariable String id) { + CachedPlayerName player = playerService.usernameToUuid(id); + return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(6, TimeUnit.HOURS)) - .body(playerService.usernameToUuid(id)); + .eTag(String.valueOf(player.hashCode())) + .body(player); } @GetMapping(value = "/{part}/{id}") @@ -61,6 +67,7 @@ public class PlayerController { .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS)) .contentType(MediaType.IMAGE_PNG) .header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(player.getUsername())) + .eTag(String.valueOf(player.hashCode())) .body(playerService.getSkinPart(player, part, overlays, size).getBytes()); } } diff --git a/src/main/java/xyz/mcutils/backend/controller/ServerController.java b/src/main/java/xyz/mcutils/backend/controller/ServerController.java index 3a171dc..6fbb264 100644 --- a/src/main/java/xyz/mcutils/backend/controller/ServerController.java +++ b/src/main/java/xyz/mcutils/backend/controller/ServerController.java @@ -12,6 +12,7 @@ import xyz.mcutils.backend.model.cache.CachedMinecraftServer; import xyz.mcutils.backend.service.MojangService; import xyz.mcutils.backend.service.ServerService; +import java.util.Arrays; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -34,9 +35,12 @@ public class ServerController { public ResponseEntity getServer( @Parameter(description = "The platform of the server", example = "java") @PathVariable String platform, @Parameter(description = "The hostname and port of the server", example = "aetheria.cc") @PathVariable String hostname) { + CachedMinecraftServer server = serverService.getServer(platform, hostname); + return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(5, TimeUnit.MINUTES)) - .body(serverService.getServer(platform, hostname)); + .eTag(String.valueOf(server.hashCode())) + .body(server); } @ResponseBody @@ -46,11 +50,14 @@ public class ServerController { @Parameter(description = "Whether to download the image") @RequestParam(required = false, defaultValue = "false") boolean download) { String dispositionHeader = download ? "attachment; filename=%s.png" : "inline; filename=%s.png"; + byte[] favicon = serverService.getServerFavicon(hostname); + return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS)) .contentType(MediaType.IMAGE_PNG) .header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(hostname)) - .body(serverService.getServerFavicon(hostname)); + .eTag(String.valueOf(Arrays.hashCode(favicon))) + .body(favicon); } @ResponseBody @@ -59,6 +66,7 @@ public class ServerController { @Parameter(description = "The hostname of the server", example = "aetheria.cc") @PathVariable String hostname) { return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS)) + .eTag(String.valueOf(hostname.hashCode())) .body(Map.of( "blocked", mojangService.isServerBlocked(hostname) )); diff --git a/src/main/java/xyz/mcutils/backend/model/cache/CachedEndpointStatus.java b/src/main/java/xyz/mcutils/backend/model/cache/CachedEndpointStatus.java index ead5ba6..d1ea39e 100644 --- a/src/main/java/xyz/mcutils/backend/model/cache/CachedEndpointStatus.java +++ b/src/main/java/xyz/mcutils/backend/model/cache/CachedEndpointStatus.java @@ -2,10 +2,7 @@ package xyz.mcutils.backend.model.cache; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonUnwrapped; -import lombok.Getter; -import lombok.NonNull; -import lombok.Setter; -import lombok.ToString; +import lombok.*; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; import xyz.mcutils.backend.common.CachedResponse; @@ -14,7 +11,7 @@ import xyz.mcutils.backend.model.mojang.EndpointStatus; import java.io.Serializable; import java.util.List; -@Setter @Getter @ToString +@Setter @Getter @EqualsAndHashCode(callSuper = false) @RedisHash(value = "mojangEndpointStatus", timeToLive = 60L) // 1 minute (in seconds) public class CachedEndpointStatus extends CachedResponse implements Serializable { diff --git a/src/main/java/xyz/mcutils/backend/model/cache/CachedMinecraftServer.java b/src/main/java/xyz/mcutils/backend/model/cache/CachedMinecraftServer.java index bfddbf2..4fc4eb9 100644 --- a/src/main/java/xyz/mcutils/backend/model/cache/CachedMinecraftServer.java +++ b/src/main/java/xyz/mcutils/backend/model/cache/CachedMinecraftServer.java @@ -13,8 +13,7 @@ import java.io.Serializable; /** * @author Braydon */ -@Setter @Getter @ToString -@NoArgsConstructor +@Setter @Getter @EqualsAndHashCode(callSuper = false) @RedisHash(value = "server", timeToLive = 60L) // 1 minute (in seconds) public class CachedMinecraftServer extends CachedResponse implements Serializable { /** diff --git a/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayer.java b/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayer.java index 9f2a861..e93d8cb 100644 --- a/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayer.java +++ b/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayer.java @@ -2,9 +2,7 @@ package xyz.mcutils.backend.model.cache; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonUnwrapped; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.*; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; import xyz.mcutils.backend.common.CachedResponse; @@ -18,8 +16,7 @@ import java.util.UUID; * * @author Braydon */ -@Setter @Getter -@NoArgsConstructor +@Setter @Getter @EqualsAndHashCode(callSuper = false) @RedisHash(value = "player", timeToLive = 60L * 60L) // 1 hour (in seconds) public class CachedPlayer extends CachedResponse implements Serializable { /** diff --git a/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayerName.java b/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayerName.java index 344d68e..4debfbf 100644 --- a/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayerName.java +++ b/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayerName.java @@ -1,8 +1,7 @@ package xyz.mcutils.backend.model.cache; import com.fasterxml.jackson.annotation.JsonIgnore; -import lombok.Getter; -import lombok.ToString; +import lombok.*; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; import xyz.mcutils.backend.common.CachedResponse; @@ -12,8 +11,8 @@ import java.util.UUID; /** * @author Braydon */ -@Getter -@ToString +@Setter +@Getter @EqualsAndHashCode(callSuper = false) @RedisHash(value = "playerName", timeToLive = 60L * 60L * 6) // 6 hours (in seconds) public class CachedPlayerName extends CachedResponse { /** diff --git a/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayerSkinPart.java b/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayerSkinPart.java index dbc507d..a7cd011 100644 --- a/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayerSkinPart.java +++ b/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayerSkinPart.java @@ -1,15 +1,11 @@ package xyz.mcutils.backend.model.cache; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NonNull; -import lombok.Setter; +import lombok.*; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; -@Setter -@Getter @AllArgsConstructor +@Setter @Getter @EqualsAndHashCode @RedisHash(value = "playerSkinPart", timeToLive = 60L * 60L) // 1 hour (in seconds) public class CachedPlayerSkinPart { diff --git a/src/main/java/xyz/mcutils/backend/model/dns/DNSRecord.java b/src/main/java/xyz/mcutils/backend/model/dns/DNSRecord.java index 00c2feb..4903d3b 100644 --- a/src/main/java/xyz/mcutils/backend/model/dns/DNSRecord.java +++ b/src/main/java/xyz/mcutils/backend/model/dns/DNSRecord.java @@ -1,13 +1,10 @@ package xyz.mcutils.backend.model.dns; import io.micrometer.common.lang.NonNull; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.*; -@Setter @Getter @NoArgsConstructor @AllArgsConstructor +@Setter @Getter @EqualsAndHashCode public abstract class DNSRecord { /** * The type of this record. diff --git a/src/main/java/xyz/mcutils/backend/model/mojang/EndpointStatus.java b/src/main/java/xyz/mcutils/backend/model/mojang/EndpointStatus.java index a2d6e7a..6c843f8 100644 --- a/src/main/java/xyz/mcutils/backend/model/mojang/EndpointStatus.java +++ b/src/main/java/xyz/mcutils/backend/model/mojang/EndpointStatus.java @@ -1,14 +1,11 @@ package xyz.mcutils.backend.model.mojang; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import lombok.Setter; +import lombok.*; import java.util.Map; @RequiredArgsConstructor -@Getter @Setter +@Getter @Setter @EqualsAndHashCode public class EndpointStatus { /** diff --git a/src/main/java/xyz/mcutils/backend/model/player/Cape.java b/src/main/java/xyz/mcutils/backend/model/player/Cape.java index b4dd64a..dd21898 100644 --- a/src/main/java/xyz/mcutils/backend/model/player/Cape.java +++ b/src/main/java/xyz/mcutils/backend/model/player/Cape.java @@ -2,9 +2,11 @@ package xyz.mcutils.backend.model.player; import com.google.gson.JsonObject; import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; import lombok.Getter; -@Getter @AllArgsConstructor +@AllArgsConstructor +@Getter @EqualsAndHashCode public class Cape { /** diff --git a/src/main/java/xyz/mcutils/backend/model/player/Player.java b/src/main/java/xyz/mcutils/backend/model/player/Player.java index 226fce4..e893752 100644 --- a/src/main/java/xyz/mcutils/backend/model/player/Player.java +++ b/src/main/java/xyz/mcutils/backend/model/player/Player.java @@ -1,6 +1,7 @@ package xyz.mcutils.backend.model.player; import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import xyz.mcutils.backend.common.Tuple; @@ -10,7 +11,8 @@ import xyz.mcutils.backend.model.token.MojangProfileToken; import java.util.UUID; -@Getter @AllArgsConstructor @NoArgsConstructor +@AllArgsConstructor @NoArgsConstructor +@Getter @EqualsAndHashCode public class Player { /** diff --git a/src/main/java/xyz/mcutils/backend/model/response/ErrorResponse.java b/src/main/java/xyz/mcutils/backend/model/response/ErrorResponse.java index ef84471..4adf32a 100644 --- a/src/main/java/xyz/mcutils/backend/model/response/ErrorResponse.java +++ b/src/main/java/xyz/mcutils/backend/model/response/ErrorResponse.java @@ -1,14 +1,14 @@ package xyz.mcutils.backend.model.response; import io.micrometer.common.lang.NonNull; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.ToString; import org.springframework.http.HttpStatus; import java.util.Date; -@Getter -@ToString +@Getter @ToString @EqualsAndHashCode public class ErrorResponse { /** * The status code of this error. diff --git a/src/main/java/xyz/mcutils/backend/model/server/JavaMinecraftServer.java b/src/main/java/xyz/mcutils/backend/model/server/JavaMinecraftServer.java index 6e28e8d..3e8a34b 100644 --- a/src/main/java/xyz/mcutils/backend/model/server/JavaMinecraftServer.java +++ b/src/main/java/xyz/mcutils/backend/model/server/JavaMinecraftServer.java @@ -14,7 +14,7 @@ import xyz.mcutils.backend.model.token.JavaServerStatusToken; /** * @author Braydon */ -@Setter @Getter +@Setter @Getter @EqualsAndHashCode(callSuper = false) public final class JavaMinecraftServer extends MinecraftServer { /** diff --git a/src/main/java/xyz/mcutils/backend/model/server/MinecraftServer.java b/src/main/java/xyz/mcutils/backend/model/server/MinecraftServer.java index f48c069..2e24aaa 100644 --- a/src/main/java/xyz/mcutils/backend/model/server/MinecraftServer.java +++ b/src/main/java/xyz/mcutils/backend/model/server/MinecraftServer.java @@ -1,10 +1,7 @@ package xyz.mcutils.backend.model.server; import io.micrometer.common.lang.NonNull; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; +import lombok.*; import xyz.mcutils.backend.common.ColorUtils; import xyz.mcutils.backend.model.dns.DNSRecord; import xyz.mcutils.backend.service.pinger.MinecraftServerPinger; @@ -18,7 +15,7 @@ import java.util.UUID; * @author Braydon */ @AllArgsConstructor -@Getter @Setter +@Getter @Setter @EqualsAndHashCode public class MinecraftServer { /** diff --git a/src/main/java/xyz/mcutils/backend/model/skin/Skin.java b/src/main/java/xyz/mcutils/backend/model/skin/Skin.java index 2726c36..70a50fe 100644 --- a/src/main/java/xyz/mcutils/backend/model/skin/Skin.java +++ b/src/main/java/xyz/mcutils/backend/model/skin/Skin.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.gson.JsonObject; import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.extern.log4j.Log4j2; @@ -18,7 +19,7 @@ import java.util.HashMap; import java.util.Map; @AllArgsConstructor @NoArgsConstructor -@Getter @Log4j2 +@Getter @Log4j2 @EqualsAndHashCode public class Skin { /** * The URL for the skin