From 1cfcce48065d5259042e6a4459c94f0fd414b5a6 Mon Sep 17 00:00:00 2001 From: Liam Date: Fri, 19 Apr 2024 21:00:08 +0100 Subject: [PATCH] etags attempt #2 --- src/main/java/xyz/mcutils/backend/Main.java | 2 -- src/main/java/xyz/mcutils/backend/config/Config.java | 10 ++++++++++ .../mcutils/backend/controller/MojangController.java | 1 - .../mcutils/backend/controller/PlayerController.java | 3 --- .../mcutils/backend/controller/ServerController.java | 4 ---- .../backend/model/cache/CachedEndpointStatus.java | 6 ++++-- .../backend/model/cache/CachedMinecraftServer.java | 5 ++++- .../xyz/mcutils/backend/model/cache/CachedPlayer.java | 4 +++- .../mcutils/backend/model/cache/CachedPlayerName.java | 4 +++- .../mcutils/backend/model/mojang/EndpointStatus.java | 7 ++++--- .../xyz/mcutils/backend/service/MojangService.java | 6 +++--- 11 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/main/java/xyz/mcutils/backend/Main.java b/src/main/java/xyz/mcutils/backend/Main.java index c543423..bf5102c 100644 --- a/src/main/java/xyz/mcutils/backend/Main.java +++ b/src/main/java/xyz/mcutils/backend/Main.java @@ -12,8 +12,6 @@ import java.net.http.HttpClient; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.util.Objects; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; @Log4j2 @SpringBootApplication diff --git a/src/main/java/xyz/mcutils/backend/config/Config.java b/src/main/java/xyz/mcutils/backend/config/Config.java index 663d012..5860983 100644 --- a/src/main/java/xyz/mcutils/backend/config/Config.java +++ b/src/main/java/xyz/mcutils/backend/config/Config.java @@ -6,9 +6,11 @@ import lombok.NonNull; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; +import org.springframework.web.filter.ShallowEtagHeaderFilter; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @@ -28,6 +30,14 @@ public class Config { INSTANCE = this; } + @Bean + public FilterRegistrationBean shallowEtagHeaderFilter() { + FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>(new ShallowEtagHeaderFilter()); + filterRegistrationBean.addUrlPatterns("/*"); + filterRegistrationBean.setName("etagFilter"); + return filterRegistrationBean; + } + @Bean public WebMvcConfigurer configureCors() { return new WebMvcConfigurer() { diff --git a/src/main/java/xyz/mcutils/backend/controller/MojangController.java b/src/main/java/xyz/mcutils/backend/controller/MojangController.java index bdd546f..e0cfd9f 100644 --- a/src/main/java/xyz/mcutils/backend/controller/MojangController.java +++ b/src/main/java/xyz/mcutils/backend/controller/MojangController.java @@ -29,7 +29,6 @@ public class MojangController { return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(1, TimeUnit.MINUTES).cachePublic()) - .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 8c2a1ef..7279200 100644 --- a/src/main/java/xyz/mcutils/backend/controller/PlayerController.java +++ b/src/main/java/xyz/mcutils/backend/controller/PlayerController.java @@ -35,7 +35,6 @@ public class PlayerController { return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic()) - .eTag(String.valueOf(player.hashCode())) .body(player); } @@ -47,7 +46,6 @@ public class PlayerController { return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(6, TimeUnit.HOURS).cachePublic()) - .eTag(String.valueOf(player.hashCode())) .body(player); } @@ -67,7 +65,6 @@ public class PlayerController { .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic()) .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 1818620..9fb1127 100644 --- a/src/main/java/xyz/mcutils/backend/controller/ServerController.java +++ b/src/main/java/xyz/mcutils/backend/controller/ServerController.java @@ -12,7 +12,6 @@ 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; @@ -39,7 +38,6 @@ public class ServerController { return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(5, TimeUnit.MINUTES).cachePublic()) - .eTag(String.valueOf(server.hashCode())) .body(server); } @@ -55,7 +53,6 @@ public class ServerController { .cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic()) .contentType(MediaType.IMAGE_PNG) .header(HttpHeaders.CONTENT_DISPOSITION, dispositionHeader.formatted(hostname)) - .eTag(String.valueOf(Arrays.hashCode(favicon))) .body(favicon); } @@ -65,7 +62,6 @@ 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).cachePublic()) - .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 d1ea39e..1631055 100644 --- a/src/main/java/xyz/mcutils/backend/model/cache/CachedEndpointStatus.java +++ b/src/main/java/xyz/mcutils/backend/model/cache/CachedEndpointStatus.java @@ -1,8 +1,10 @@ package xyz.mcutils.backend.model.cache; import com.fasterxml.jackson.annotation.JsonIgnore; -import com.fasterxml.jackson.annotation.JsonUnwrapped; -import lombok.*; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NonNull; +import lombok.Setter; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; import xyz.mcutils.backend.common.CachedResponse; 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 4fc4eb9..2f7c58e 100644 --- a/src/main/java/xyz/mcutils/backend/model/cache/CachedMinecraftServer.java +++ b/src/main/java/xyz/mcutils/backend/model/cache/CachedMinecraftServer.java @@ -2,7 +2,10 @@ package xyz.mcutils.backend.model.cache; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonUnwrapped; -import lombok.*; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NonNull; +import lombok.Setter; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; import xyz.mcutils.backend.common.CachedResponse; 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 e93d8cb..83fc327 100644 --- a/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayer.java +++ b/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayer.java @@ -2,7 +2,9 @@ package xyz.mcutils.backend.model.cache; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonUnwrapped; -import lombok.*; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; import xyz.mcutils.backend.common.CachedResponse; 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 4debfbf..e0eb27b 100644 --- a/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayerName.java +++ b/src/main/java/xyz/mcutils/backend/model/cache/CachedPlayerName.java @@ -1,7 +1,9 @@ package xyz.mcutils.backend.model.cache; import com.fasterxml.jackson.annotation.JsonIgnore; -import lombok.*; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; import xyz.mcutils.backend.common.CachedResponse; 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 6c843f8..4f8241e 100644 --- a/src/main/java/xyz/mcutils/backend/model/mojang/EndpointStatus.java +++ b/src/main/java/xyz/mcutils/backend/model/mojang/EndpointStatus.java @@ -1,8 +1,9 @@ package xyz.mcutils.backend.model.mojang; -import lombok.*; - -import java.util.Map; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.Setter; @RequiredArgsConstructor @Getter @Setter @EqualsAndHashCode diff --git a/src/main/java/xyz/mcutils/backend/service/MojangService.java b/src/main/java/xyz/mcutils/backend/service/MojangService.java index 0800fa9..2b8b828 100644 --- a/src/main/java/xyz/mcutils/backend/service/MojangService.java +++ b/src/main/java/xyz/mcutils/backend/service/MojangService.java @@ -11,7 +11,9 @@ import lombok.extern.log4j.Log4j2; import net.jodah.expiringmap.ExpirationPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import xyz.mcutils.backend.common.*; +import xyz.mcutils.backend.common.EnvironmentUtils; +import xyz.mcutils.backend.common.ExpiringSet; +import xyz.mcutils.backend.common.WebRequest; import xyz.mcutils.backend.model.cache.CachedEndpointStatus; import xyz.mcutils.backend.model.mojang.EndpointStatus; import xyz.mcutils.backend.model.token.MojangProfileToken; @@ -24,8 +26,6 @@ import java.net.InetAddress; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.*; -import java.util.Timer; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @Service @Log4j2 @Getter