From 4b4cc8ae5405416ebb281e6a3e59dde031e4ecda Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 8 Apr 2024 06:48:21 +0100 Subject: [PATCH] use better responses --- .../controller/PlayerController.java | 9 +++-- .../exception/ExceptionControllerAdvice.java | 4 +- .../java/cc/fascinated/model/player/Skin.java | 2 + .../model/response/ErrorResponse.java | 40 ------------------- .../fascinated/model/response/Response.java | 31 ++++++++++++++ .../response/impl/PlayerNotFoundResponse.java | 11 +++++ .../fascinated/service/MojangAPIService.java | 4 +- .../java/cc/fascinated/util/PlayerUtils.java | 9 ++--- .../java/cc/fascinated/util/WebRequest.java | 2 +- 9 files changed, 57 insertions(+), 55 deletions(-) delete mode 100644 src/main/java/cc/fascinated/model/response/ErrorResponse.java create mode 100644 src/main/java/cc/fascinated/model/response/Response.java create mode 100644 src/main/java/cc/fascinated/model/response/impl/PlayerNotFoundResponse.java diff --git a/src/main/java/cc/fascinated/controller/PlayerController.java b/src/main/java/cc/fascinated/controller/PlayerController.java index 4628401..e531aff 100644 --- a/src/main/java/cc/fascinated/controller/PlayerController.java +++ b/src/main/java/cc/fascinated/controller/PlayerController.java @@ -2,6 +2,7 @@ package cc.fascinated.controller; import cc.fascinated.model.player.Player; import cc.fascinated.model.player.Skin; +import cc.fascinated.model.response.impl.PlayerNotFoundResponse; import cc.fascinated.service.PlayerService; import cc.fascinated.util.PlayerUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -29,9 +30,10 @@ public class PlayerController { @GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity getPlayer(@PathVariable String id) { Player player = playerManagerService.getPlayer(id); - if (player == null) { - return new ResponseEntity<>(Map.of("error", "Player not found"), HttpStatus.NOT_FOUND); + if (player == null) { // No player with that id was found + return new PlayerNotFoundResponse().toResponseEntity(); } + // Return the player return ResponseEntity.ok() .cacheControl(cacheControl) .body(player); @@ -49,11 +51,10 @@ public class PlayerController { Skin.Parts skinPart = Skin.Parts.fromName(part); partBytes = PlayerUtils.getSkinPartBytes(skin, skinPart, size); } - if (partBytes == null) { // Fallback to the default head partBytes = PlayerUtils.getSkinPartBytes(Skin.DEFAULT_SKIN, Skin.Parts.HEAD, size); } - + // Return the part image return ResponseEntity.ok() .cacheControl(cacheControl) .contentType(MediaType.IMAGE_PNG) diff --git a/src/main/java/cc/fascinated/exception/ExceptionControllerAdvice.java b/src/main/java/cc/fascinated/exception/ExceptionControllerAdvice.java index 49e1427..423c43b 100644 --- a/src/main/java/cc/fascinated/exception/ExceptionControllerAdvice.java +++ b/src/main/java/cc/fascinated/exception/ExceptionControllerAdvice.java @@ -1,6 +1,6 @@ package cc.fascinated.exception; -import cc.fascinated.model.response.ErrorResponse; +import cc.fascinated.model.response.Response; import io.micrometer.common.lang.NonNull; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -28,6 +28,6 @@ public final class ExceptionControllerAdvice { message = "An internal error has occurred."; } ex.printStackTrace(); // Print the stack trace - return new ResponseEntity<>(new ErrorResponse(status, message), status); + return new Response(status, message).toResponseEntity(); // Return the error response } } \ No newline at end of file diff --git a/src/main/java/cc/fascinated/model/player/Skin.java b/src/main/java/cc/fascinated/model/player/Skin.java index e80c326..18e54f9 100644 --- a/src/main/java/cc/fascinated/model/player/Skin.java +++ b/src/main/java/cc/fascinated/model/player/Skin.java @@ -2,6 +2,7 @@ package cc.fascinated.model.player; import cc.fascinated.config.Config; import cc.fascinated.util.PlayerUtils; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.gson.JsonObject; import lombok.AllArgsConstructor; @@ -33,6 +34,7 @@ public class Skin { /** * The skin image for the skin */ + @JsonIgnore private final BufferedImage skinImage; /** diff --git a/src/main/java/cc/fascinated/model/response/ErrorResponse.java b/src/main/java/cc/fascinated/model/response/ErrorResponse.java deleted file mode 100644 index 1d7aaba..0000000 --- a/src/main/java/cc/fascinated/model/response/ErrorResponse.java +++ /dev/null @@ -1,40 +0,0 @@ -package cc.fascinated.model.response; - -import com.fasterxml.jackson.annotation.JsonFormat; -import io.micrometer.common.lang.NonNull; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -import lombok.ToString; -import org.springframework.http.HttpStatus; - -import java.util.Date; - -@NoArgsConstructor -@Setter -@Getter -@ToString -public final class ErrorResponse { - /** - * The status code of this error. - */ - @NonNull - private HttpStatus status; - - /** - * The message of this error. - */ - @NonNull private String message; - - /** - * The timestamp this error occurred. - */ - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss") - private Date timestamp; - - public ErrorResponse(@NonNull HttpStatus status, @NonNull String message) { - this.status = status; - this.message = message; - timestamp = new Date(); - } -} \ No newline at end of file diff --git a/src/main/java/cc/fascinated/model/response/Response.java b/src/main/java/cc/fascinated/model/response/Response.java new file mode 100644 index 0000000..49101b2 --- /dev/null +++ b/src/main/java/cc/fascinated/model/response/Response.java @@ -0,0 +1,31 @@ +package cc.fascinated.model.response; + +import io.micrometer.common.lang.NonNull; +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.servlet.function.EntityResponse; + +@Getter @AllArgsConstructor +public class Response { + + /** + * The status code of this error. + */ + private HttpStatus status; + + /** + * The message of this error. + */ + private String message; + + /** + * Gets this response as a {@link ResponseEntity}. + * + * @return the response entity + */ + public ResponseEntity toResponseEntity() { + return new ResponseEntity<>(this, status); + } +} diff --git a/src/main/java/cc/fascinated/model/response/impl/PlayerNotFoundResponse.java b/src/main/java/cc/fascinated/model/response/impl/PlayerNotFoundResponse.java new file mode 100644 index 0000000..aee048c --- /dev/null +++ b/src/main/java/cc/fascinated/model/response/impl/PlayerNotFoundResponse.java @@ -0,0 +1,11 @@ +package cc.fascinated.model.response.impl; + +import cc.fascinated.model.response.Response; +import org.springframework.http.HttpStatus; + +public class PlayerNotFoundResponse extends Response { + + public PlayerNotFoundResponse() { + super(HttpStatus.NOT_FOUND, "Player not found."); + } +} diff --git a/src/main/java/cc/fascinated/service/MojangAPIService.java b/src/main/java/cc/fascinated/service/MojangAPIService.java index 4fe8cfa..a9a6417 100644 --- a/src/main/java/cc/fascinated/service/MojangAPIService.java +++ b/src/main/java/cc/fascinated/service/MojangAPIService.java @@ -24,7 +24,7 @@ public class MojangAPIService { * @return the profile */ public MojangProfile getProfile(String id) { - return WebRequest.get(mojangSessionServerUrl + "/session/minecraft/profile/" + id, MojangProfile.class); + return WebRequest.getAsEntity(mojangSessionServerUrl + "/session/minecraft/profile/" + id, MojangProfile.class); } /** @@ -35,6 +35,6 @@ public class MojangAPIService { * @return the profile */ public MojangUsernameToUuid getUuidFromUsername(String id) { - return WebRequest.get(mojangApiUrl + "/users/profiles/minecraft/" + id, MojangUsernameToUuid.class); + return WebRequest.getAsEntity(mojangApiUrl + "/users/profiles/minecraft/" + id, MojangUsernameToUuid.class); } } diff --git a/src/main/java/cc/fascinated/util/PlayerUtils.java b/src/main/java/cc/fascinated/util/PlayerUtils.java index ab32914..aa127c7 100644 --- a/src/main/java/cc/fascinated/util/PlayerUtils.java +++ b/src/main/java/cc/fascinated/util/PlayerUtils.java @@ -27,12 +27,9 @@ public class PlayerUtils { @SneakyThrows @JsonIgnore public static BufferedImage getSkinImage(String url) { - HttpRequest request = HttpRequest.newBuilder() - .uri(new URI(url)) - .GET() - .build(); - - byte[] body = Main.HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofByteArray()).body(); + HttpResponse response = Main.HTTP_CLIENT.send(HttpRequest.newBuilder(URI.create(url)).build(), + HttpResponse.BodyHandlers.ofByteArray()); + byte[] body = response.body(); if (body == null) { return null; } diff --git a/src/main/java/cc/fascinated/util/WebRequest.java b/src/main/java/cc/fascinated/util/WebRequest.java index 5d3151c..2e71ecc 100644 --- a/src/main/java/cc/fascinated/util/WebRequest.java +++ b/src/main/java/cc/fascinated/util/WebRequest.java @@ -23,7 +23,7 @@ public class WebRequest { * @return the response * @param the type of the response */ - public static T get(String url, Class clazz) { + public static T getAsEntity(String url, Class clazz) { try { ResponseEntity profile = CLIENT.get() .uri(url)