From 60c75ab98ba7f8dbbd99a6cc0c5e74a5c34e6096 Mon Sep 17 00:00:00 2001 From: Miere Liniel Teixeira Date: Sun, 6 Mar 2022 22:26:22 +1100 Subject: [PATCH] Ensure buffer is not null It helps to identify whether the request payload was not sent by the client or not. --- kos-core/source/kos/api/Serializer.java | 2 +- kos-core/source/kos/core/Request.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/kos-core/source/kos/api/Serializer.java b/kos-core/source/kos/api/Serializer.java index 47bd8a6..dc6ae8b 100644 --- a/kos-core/source/kos/api/Serializer.java +++ b/kos-core/source/kos/api/Serializer.java @@ -48,7 +48,7 @@ public Buffer serialize(Object target) { } @Override - public T deserialize(Buffer buffer, Class type) { + public T deserialize(@NonNull Buffer buffer, @NonNull Class type) { return Json.decodeValue( buffer, type ); } } diff --git a/kos-core/source/kos/core/Request.java b/kos-core/source/kos/core/Request.java index 40a2595..228fadd 100644 --- a/kos-core/source/kos/core/Request.java +++ b/kos-core/source/kos/core/Request.java @@ -19,6 +19,7 @@ import io.vertx.core.buffer.*; import io.vertx.ext.web.*; import kos.api.KosContext; +import kos.core.exception.KosException; import lombok.*; import lombok.experimental.*; @@ -44,6 +45,8 @@ public T readHeader(KosContext kosContext, RoutingContext context, String na public T readBody(KosContext kosContext, RoutingContext context, String name, Class type) { val buffer = context.getBody(); + if (buffer == null) + throw new BadRequestException("Cannot read body content: is empty."); if (Buffer.class.equals(type)) return (T) buffer; val serializer = kosContext.getPayloadSerializationStrategy().serializerFor(context.request()); @@ -53,4 +56,11 @@ public T readBody(KosContext kosContext, RoutingContext context, String name public T readContext(KosContext kosContext, RoutingContext context, String name, Class type) { return (T) context.get(name); } + + public static class BadRequestException extends KosException { + + private BadRequestException(String message) { + super(message); + } + } }