From 6fa3f298c70291f7393e07a260991d445e5b520e Mon Sep 17 00:00:00 2001 From: Nicolas PETERS Date: Wed, 20 Nov 2024 23:38:31 +0100 Subject: [PATCH] #1084: remove duplicated code with populateContentAndContentType --- .../spring/AwsSpringHttpProcessingUtils.java | 41 ++++++++++++++----- ...DelegatingLambdaContainerHandlerTests.java | 1 - 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/aws-serverless-java-container-springboot3/src/main/java/com/amazonaws/serverless/proxy/spring/AwsSpringHttpProcessingUtils.java b/aws-serverless-java-container-springboot3/src/main/java/com/amazonaws/serverless/proxy/spring/AwsSpringHttpProcessingUtils.java index 9ce8f1dc..f7d1e000 100644 --- a/aws-serverless-java-container-springboot3/src/main/java/com/amazonaws/serverless/proxy/spring/AwsSpringHttpProcessingUtils.java +++ b/aws-serverless-java-container-springboot3/src/main/java/com/amazonaws/serverless/proxy/spring/AwsSpringHttpProcessingUtils.java @@ -122,17 +122,12 @@ private static HttpServletRequest generateRequest1(String request, Context lambd MultiValueMapAdapter headers = new MultiValueMapAdapter(v1Request.getMultiValueHeaders()); httpRequest.setHeaders(headers); } - if (StringUtils.hasText(v1Request.getBody())) { - if (v1Request.getHeaders().get(HttpHeaders.CONTENT_TYPE)==null) { - httpRequest.setContentType("application/json"); - } - if (v1Request.isBase64Encoded()) { - httpRequest.setContent(Base64.getMimeDecoder().decode(v1Request.getBody())); - } else { - Charset charseEncoding = parseCharacterEncoding(v1Request.getHeaders().get(HttpHeaders.CONTENT_TYPE)); - httpRequest.setContent(v1Request.getBody().getBytes(charseEncoding)); - } - } + populateContentAndContentType( + v1Request.getBody(), + v1Request.getHeaders().get(HttpHeaders.CONTENT_TYPE), + v1Request.isBase64Encoded(), + httpRequest + ); if (v1Request.getRequestContext() != null) { httpRequest.setAttribute(RequestReader.API_GATEWAY_CONTEXT_PROPERTY, v1Request.getRequestContext()); httpRequest.setAttribute(RequestReader.ALB_CONTEXT_PROPERTY, v1Request.getRequestContext().getElb()); @@ -159,6 +154,12 @@ private static HttpServletRequest generateRequest2(String request, Context lambd v2Request.getHeaders().forEach(httpRequest::setHeader); + populateContentAndContentType( + v2Request.getBody(), + v2Request.getHeaders().get(HttpHeaders.CONTENT_TYPE), + v2Request.isBase64Encoded(), + httpRequest + ); if (StringUtils.hasText(v2Request.getBody())) { if (v2Request.getHeaders().get(HttpHeaders.CONTENT_TYPE)==null) { @@ -197,6 +198,24 @@ private static T readValue(String json, Class clazz, ObjectMapper mapper) } } + private static void populateContentAndContentType( + String body, + String contentType, + boolean base64Encoded, + ServerlessHttpServletRequest httpRequest) { + if (StringUtils.hasText(body)) { + if (contentType == null) { + httpRequest.setContentType("application/json"); + } + if (base64Encoded) { + httpRequest.setContent(Base64.getMimeDecoder().decode(body)); + } else { + Charset charseEncoding = parseCharacterEncoding(contentType); + httpRequest.setContent(body.getBytes(charseEncoding)); + } + } + } + static final String HEADER_KEY_VALUE_SEPARATOR = "="; static final String HEADER_VALUE_SEPARATOR = ";"; static final String ENCODING_VALUE_KEY = "charset"; diff --git a/aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/SpringDelegatingLambdaContainerHandlerTests.java b/aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/SpringDelegatingLambdaContainerHandlerTests.java index f9f60e84..61957fe2 100644 --- a/aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/SpringDelegatingLambdaContainerHandlerTests.java +++ b/aws-serverless-java-container-springboot3/src/test/java/com/amazonaws/serverless/proxy/spring/SpringDelegatingLambdaContainerHandlerTests.java @@ -345,7 +345,6 @@ private byte[] generateHttpRequest2(Map requestMap, String method, String path, if (!CollectionUtils.isEmpty(headers)) { map.put("headers", headers); } - System.out.println(map); return mapper.writeValueAsBytes(map); } }