|
24 | 24 | import io.micronaut.core.execution.ExecutionFlow;
|
25 | 25 | import io.micronaut.core.io.buffer.ByteBuffer;
|
26 | 26 | import io.micronaut.core.type.Argument;
|
| 27 | +import io.micronaut.core.util.ArrayUtils; |
27 | 28 | import io.micronaut.core.util.CollectionUtils;
|
28 | 29 | import io.micronaut.core.util.StringUtils;
|
29 | 30 | import io.micronaut.core.util.SupplierUtil;
|
|
34 | 35 | import io.micronaut.http.MutableHttpHeaders;
|
35 | 36 | import io.micronaut.http.MutableHttpParameters;
|
36 | 37 | import io.micronaut.http.MutableHttpRequest;
|
| 38 | +import io.micronaut.http.ServerHttpRequest; |
| 39 | +import io.micronaut.http.body.ByteBody; |
37 | 40 | import io.micronaut.http.cookie.Cookie;
|
38 | 41 | import io.micronaut.http.cookie.Cookies;
|
39 | 42 | import io.micronaut.http.uri.UriBuilder;
|
|
43 | 46 | import io.micronaut.servlet.http.ServletHttpRequest;
|
44 | 47 | import io.micronaut.servlet.http.ParsedBodyHolder;
|
45 | 48 | import io.micronaut.servlet.http.ByteArrayByteBuffer;
|
| 49 | +import io.micronaut.servlet.http.body.AvailableByteArrayBody; |
46 | 50 | import org.slf4j.Logger;
|
47 | 51 |
|
48 | 52 | import java.io.BufferedReader;
|
|
71 | 75 | */
|
72 | 76 | @Internal
|
73 | 77 | @SuppressWarnings("java:S119") // More descriptive generics are better here
|
74 |
| -public abstract class ApiGatewayServletRequest<T, REQ, RES> implements MutableServletHttpRequest<REQ, T>, ServletExchange<REQ, RES>, FullHttpRequest<T>, ParsedBodyHolder<T> { |
| 78 | +public abstract class ApiGatewayServletRequest<T, REQ, RES> implements MutableServletHttpRequest<REQ, T>, ServletExchange<REQ, RES>, FullHttpRequest<T>, ParsedBodyHolder<T>, ServerHttpRequest<T> { |
75 | 79 |
|
76 | 80 | private static final Set<Class<?>> RAW_BODY_TYPES = CollectionUtils.setOf(String.class, byte[].class, ByteBuffer.class, InputStream.class);
|
77 | 81 | private static final String SLASH = "/";
|
@@ -108,7 +112,16 @@ protected ApiGatewayServletRequest(
|
108 | 112 | });
|
109 | 113 | }
|
110 | 114 |
|
111 |
| - public abstract byte[] getBodyBytes() throws IOException; |
| 115 | + @Override |
| 116 | + public @NonNull ByteBody byteBody() { |
| 117 | + try { |
| 118 | + return new AvailableByteArrayBody(getBodyBytes()); |
| 119 | + } catch (EmptyBodyException e) { |
| 120 | + return new AvailableByteArrayBody(ArrayUtils.EMPTY_BYTE_ARRAY); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public abstract byte[] getBodyBytes() throws EmptyBodyException; |
112 | 125 |
|
113 | 126 | /**
|
114 | 127 | * Given a path and the query params from the event, build a URI.
|
@@ -300,10 +313,10 @@ public void setParsedBody(T body) {
|
300 | 313 | * @return body bytes
|
301 | 314 | * @throws IOException if the body is empty
|
302 | 315 | */
|
303 |
| - protected byte[] getBodyBytes(@NonNull Supplier<String> bodySupplier, @NonNull BooleanSupplier base64EncodedSupplier) throws IOException { |
| 316 | + protected byte[] getBodyBytes(@NonNull Supplier<String> bodySupplier, @NonNull BooleanSupplier base64EncodedSupplier) throws EmptyBodyException { |
304 | 317 | String requestBody = bodySupplier.get();
|
305 | 318 | if (StringUtils.isEmpty(requestBody)) {
|
306 |
| - throw new IOException("Empty Body"); |
| 319 | + throw new EmptyBodyException(); |
307 | 320 | }
|
308 | 321 | return base64EncodedSupplier.getAsBoolean() ?
|
309 | 322 | Base64.getDecoder().decode(requestBody) : requestBody.getBytes(getCharacterEncoding());
|
@@ -358,4 +371,10 @@ protected MutableHttpParameters getParameters(@NonNull Supplier<Map<String, Stri
|
358 | 371 | protected MutableHttpHeaders getHeaders(@NonNull Supplier<Map<String, String>> singleHeaders, @NonNull Supplier<Map<String, List<String>>> multiValueHeaders) {
|
359 | 372 | return new CaseInsensitiveMutableHttpHeaders(MapCollapseUtils.collapse(multiValueHeaders.get(), singleHeaders.get()), conversionService);
|
360 | 373 | }
|
| 374 | + |
| 375 | + public static final class EmptyBodyException extends IOException { |
| 376 | + public EmptyBodyException() { |
| 377 | + super("Empty body"); |
| 378 | + } |
| 379 | + } |
361 | 380 | }
|
0 commit comments